Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void spi_test(BaseSequentialStream *chp, int argc, char **argv)
- {
- //select LED15 to signify entry
- palSetPadMode(GPIOD, 15, PAL_MODE_OUTPUT_PUSHPULL);
- palSetPad(GPIOD, 15);
- //start the SPI driver
- const SPIConfig cfg =
- {
- NULL,
- GPIOE, // port
- GPIOE_CS_SPI, // /SS
- SPI_CR1_BR_0 | SPI_CR1_BR_1 |
- SPI_CR1_CPOL | SPI_CR1_CPHA
- };
- uint8_t tx[2], rx[2];
- int32_t x, y, z;
- spiStart(&SPID1, &cfg);
- spiSelect(&SPID1);
- //X, Y, Z are in 0x29, 0x2B and 0x2D
- //Control bytes for init are 0x20, 0x21, 0x22
- tx[0] = 0x20;
- tx[1] = 0x73;
- spiSend(&SPID1, 2, tx);
- spiUnselect(&SPID1);
- spiSelect(&SPID1);
- tx[0] = 0x21;
- tx[1] = 0x00;
- spiSend(&SPID1, 2, tx);
- spiUnselect(&SPID1);
- spiSelect(&SPID1);
- tx[0] = 0x22;
- tx[1] = 0x00;
- spiSend(&SPID1, 2, tx);
- spiUnselect(&SPID1);
- //init done, now read out values
- tx[0] = 0x80 | 0x29;
- tx[1] = 0xff;
- spiSelect(&SPID1);
- spiExchange(&SPID1, 2, tx, rx);
- spiUnselect(&SPID1);
- x = (int8_t) rx[1];
- tx[0] = 0x80 | 0x2B;
- tx[1] = 0xff;
- spiSelect(&SPID1);
- spiExchange(&SPID1, 2, tx, rx);
- spiUnselect(&SPID1);
- y = (int8_t) rx[1];
- tx[0] = 0x80 | 0x2D;
- tx[1] = 0xff;
- spiSelect(&SPID1);
- spiExchange(&SPID1, 2, tx, rx);
- spiUnselect(&SPID1);
- //__asm__("bkpt");
- z = (int8_t) rx[1];
- chThdSleepMilliseconds(50);
- chprintf(chp, "Accelerometer: (%d,%d,%d)\r\n", x, y, z);
- spiUnselect(&SPID2);
- palClearPad(GPIOD, 15);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement