Question
STM32H743XI Multi SPI Circular DMA with GPIO manipulation
Hi, I want to achieve the title's behaviour to communicate with multiple SPI sensors in the 5 SPI buses at the highest possible throughput and minimal CPU load. I first tried the following with 1 bus.
void cpltCB(void);
uint8_t RXBUFF[8];
void main(void) {
...
HAL_SPI_RegisterCallback(&hspi1, HAL_SPI_RX_COMPLETE_CB_ID, cpltCB);
HAL_SPI_Receive_DMA(&hspi1, RXBUFF, 1); // SPI and DMA set up to transfer by WORD in a circular manner
...
}
void cpltCB(void) {
static uint32_t sensorID;
switch (sensorID) {
case 0:
HAL_GPIO_Write(SENSOR0_CS_PORT, SENSOR0_CS_PIN, 1);
// do something with the WORD
HAL_GPIO_Write(SENSOR1_CS_PORT, SENSOR1_CS_PIN, 0);
sensorID = 1;
break;
case 1:
HAL_GPIO_Write(SENSOR1_CS_PORT, SENSOR1_CS_PIN, 1);
// do something with the WORD
HAL_GPIO_Write(SENSOR0_CS_PORT, SENSOR0_CS_PIN, 0);
sensorID = 0;
break;
default:
sensorID = 0;
break;
}
}I noticed that a few CLK cycles have passed before the cpltCB interrupt was triggered, resulting in the array of bytes being shifted aft randomly depending on when the interrupt was fired. I then tried the following:
void halfCpltCB(void);
void cpltCB(void) {;} // never called in this implementation
uint8_t RXBUFF[8];
void main(void) {
...
HAL_SPI_RegisterCallback(&hspi1, HAL_SPI_RX_HALF_COMPLETE_CB_ID, halfCpltCB);
HAL_SPI_RegisterCallback(&hspi1, HAL_SPI_RX_COMPLETE_CB_ID, cpltCB);
HAL_SPI_Receive_DMA(&hspi1, RXBUFF, 2); // 2 WORDS this time
...
}
void halfCpltCB(void) {
static uint32_t sensorID;
__HAL_DMA_DISABLE(hspi1.hdmarx); // disables DMA
switch (sensorID) {
case 0:
HAL_GPIO_Write(SENSOR0_CS_PORT, SENSOR0_CS_PIN, 1); // pulls up CS when 1 WORD is received
// do something with the WORD
HAL_GPIO_Write(SENSOR1_CS_PORT, SENSOR1_CS_PIN, 0); // select the other slave
sensorID = 1;
break;
case 1:
HAL_GPIO_Write(SENSOR1_CS_PORT, SENSOR1_CS_PIN, 1); // pulls up CS when 1 WORD is received
// do something with the WORD
HAL_GPIO_Write(SENSOR0_CS_PORT, SENSOR0_CS_PIN, 0); // select the other slave
sensorID = 0;
break;
default:
sensorID = 0;
break;
}
__HAL_DMA_ENABLE(hspi1.hdmarx); // reenables DMA
}The WORD is still shifted in the buffer.
Any hints on how I can achieve what I want?
Thanks in advance.
