I used to use the SPI (without DMA) functions from ST library directly to control my radio, but now I'd like to move to ChibiOS to do the same thing. The problem I'm facing now is that I could not get the correct part information. Basically in my code, I send a command to ask for part information, and then another command to read the response. The data field will arrive after CTS has been received. Could someone point out if I use the SPI+DMA function wrong? Thanks,
PS. I removed the serial over usb codes just for clarity.
Shan
Code: Select all
static uint8_t txbuf[64] = {0};
static uint8_t rxbuf[64] = {0};
static const SPIConfig spicfg = {
NULL,
GPIOB,
12,
SPI_CR1_MSTR | SPI_CR1_BR_1 | SPI_CR1_SSM | SPI_CR1_DFF /* 0b0000101000010100 */
};
void checkCTS() {
while(TRUE) {
rxbuf[0] = 0x00;
spiReceive(&SPID2, 1, rxbuf);
if (rxbuf[0] != 0xff) {
} else { /* CTS received, break */
break;
}
}
}
int main(void) {
unsigned i;
halInit();
chSysInit();
sduObjectInit(&SDU1);
sduStart(&SDU1, &serusbcfg);
palSetPadMode(GPIOB, 12, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); /* NSS. */
palSetPadMode(GPIOB, 13, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST); /* SCK. */
palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(5)); /* MISO. */
palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST); /* MOSI. */
palSetPad(GPIOB, 12); /* NSS pin pull high in the start */
while (TRUE) {
spiAcquireBus(&SPID2);
spiStart(&SPID2, &spicfg);
spiSelect(&SPID2);
txbuf[0] = PART_INFO;
spiSend(&SPID2, 1, txbuf);
spiUnselect(&SPID2);
spiSelect(&SPID2);
txbuf[0] = READ_CMD_BUFF;
spiSend(&SPID2, 1, txbuf);
checkCTS();
spiReceive(&SPID2, 8, rxbuf);
spiUnselect(&SPID2);
for (i = 0; i < 8; i++) {
chprintf((BaseSequentialStream *)&SDU1, "0x%02x\r\n", rxbuf[i]);
}
spiReleaseBus(&SPID2);
chprintf((BaseSequentialStream *)&SDU1, "------------------------------\r\n");