2025-02-26 3:17 AM - edited 2025-02-26 3:36 AM
I'm trying to receive data through USART1 using STM32F407. I decided to not use HAL api's to achieve that, so I'm receiving character by character and storing them in an array. Every character I send is received correctly but apart from the actual data I'm receiving an extra character in the first position of the buffer. It sometimes is a 'p' , sometimes a '\n' . I'm attaching some debug screenshots and a code snippet.
I enable the RX DR not empty interrupt:
__HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);
My interrupt handler:
uint8_t group_input_str[250];
volatile uint32_t idx = 0;
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
if(__HAL_UART_GET_IT_SOURCE(&huart1,UART_IT_RXNE))
{
group_input_str[idx++] = huart1.Instance->DR;
}
if(idx>=250) // prevent overflow
idx=249;
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
/* USER CODE END USART1_IRQn 1 */
}
I sent a buffer containing help from the very begining, from another board(STM32F103),and the logic analiser caught :
But on the debugger I got:
UART configurations in both boards: 115200,8,N,1
I lowered down the baud rate, switched from HSE(8 MHz) to HSI, lowered down SYSCLK from 100MHz to 16MHz, but no success. What could be wrong?
2025-02-26 11:56 PM
Seeing his buffer, i remember having this kind of issues where the sender is just putting information non-stop in the buffer.
What i suggest is to create a "protocol" in top of UART, so when you get the IT, you see if the character you get is an 'h' and then you continue receiving it all.
Hope it helps.
2025-02-27 1:47 AM
@urbito wrote:Seeing his buffer, i remember having this kind of issues where the sender is just putting information non-stop in the buffer..
Yes - exactly!
@BentoCopi this is what I'm talking about with synchronisation.
2025-02-27 2:14 AM - edited 2025-02-27 2:27 AM
There several ways to achieve synchronization:
It is also recommended to use some type of crc to check the integrity of the packet.
2025-02-27 2:30 AM
@unsigned_char_array wrote:There several ways to achieve synchronization:
Indeed.
@BentoCopi For a simple test case, it can just be done manually - as I've described - but you must have some way to ensure that your receiver & transmitter are "in-sync".
Otherwise, you will find that your receiver will start picking up at some random point within the message.