2024-08-30 06:55 AM
Hello,
I'm using two STM32F4 nucleo boards and my goal is to transmit data unidirectionnaly from one board to another using SPI in a loop.
The first board is configured as a Master and it only transmits data. And the second one is configured as a Slave and it only receives data.
The data is set to "ABCD" and it does not change during the test.
The data is transmited without any problem. I checked it using an oscilloscope.
However, the problem is that I receive the correct data only during a certain amount of time and the data is alterd after some itteration. Here is some output of the data on my PuTTy console.
Data captured from PuTTy
And this is the code I used to transmit the data on the Master Board
uint8_t TX_Buffer [4] = "ABCD";
while (1){
HAL_SPI_Transmit(&hspi1, TX_Buffer, 4, 100);
HAL_Delay(100);
}
And this is the code I used to receive the data
/*---------------------*/
//main.C
/*---------------------*/
#define SIZE_BUF 4
#define SIZE_UART_BUF 6
volatile uint8_t spi_rx_complete = 0;
uint8_t uart_buf[SIZE_UART_BUF];
int uart_buf_len;
uint8_t RX_Buffer[SIZE_BUF]; // DATA to receive
int main(void){
while (1){
if (1 == spi_rx_complete){ //Process the message
spi_rx_complete = 0;
uart_buf_len = sprintf((char *)uart_buf, "%s", RX_Buffer);
HAL_UART_Transmit(&huart3, uart_buf, uart_buf_len, 500);
} else if (0 == spi_rx_complete){
spi_rx_complete = -1;
memset(&RX_Buffer, '\0', SIZE_BUF);
HAL_SPI_Receive_DMA(&hspi1, RX_Buffer, SIZE_BUF);
}
HAL_Delay(10);
}
}
/*---------------------*/
stm32f4xx_it.c
/*---------------------*/
extern volatile int spi_rx_complete;
extern SPI_HandleTypeDef hspi1;
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) {
if(hspi->Instance == hspi1.Instance && -1 == spi_rx_complete) {
spi_rx_complete = 1;
}
}
About the configuration of the two boards:
The boad rate of the Master is set to 468.75 Kbits/second
And the other parameters is left to the default values
Thank you
2024-08-30 07:20 AM
> uart_buf_len = sprintf((char *)uart_buf, "%s", RX_Buffer);
RX_Buffer is not null terminated. This leads to a potential overflow and undefined/bad behavior.
Perhaps copy over the 4 characters and send exactly those.
Otherwise code seems okay.
2024-08-30 08:28 AM
Thank you for your response
I changed the code with the following one, but I still got the same problem
if (1 == msgFromBouee){ //Process the message
msgFromBouee = 0;
HAL_UART_Transmit(&huart3, RX_Buffer, SIZE_BUF, 500);
} else if (0 == msgFromBouee){
msgFromBouee = -1;
memset(RX_Buffer, '\0', SIZE_BUF);
HAL_SPI_Receive_DMA(&hspi1, RX_Buffer, SIZE_BUF);
}