2023-05-12 06:49 AM
Hi I am using STM32F407 discovery board. Trying to receive string with USART2 interrupt in this video.
Here is my usart2 receive and interrupt codes:
uint8_t receiveUart(void)
{
uint8_t rxData = 0;
// IF THERE IS DATA IN THE RECEIVE DATA REGISTER
// USART_ISR_RXNE EXPANDS TO (1 << 5)
if(USART2->SR & USART_SR_RXNE)
{
// READ DATA FROM THE REGISTER
rxData = USART2->DR;
}
return rxData;
}
void USART2_IRQHandler(void)
{
USART_2_msg[uart_2_mgr[0]] = receiveUart();
printf("mgr0: %d\n", uart_2_mgr[0]);
printf("char: %c\n", USART_2_msg[uart_2_mgr[0]]);
if(uart_2_mgr[3])
{
if(USART_2_msg[uart_2_mgr[0]] == uart_2_mgr[4])
{
uart_2_mgr[0] = 0;
uart_2_mgr[1] = 1;
}
else
{
uart_2_mgr[0]++;
}
}
else
{
// Timer strategy
uart_2_mgr[0]++;
uart_2_mgr[6] = uart_2_mgr[5];
//systick_int_start();
}
}
printf("message: %s\n", USART_2_msg);
}
When I send a character by serial terminal, It is able to receive 1 character and store it to the buffer. If I send a string, it only stores the first character. I want to write the code without HAL libs. How can I receive the whole string?
Thanks in advance.
Solved! Go to Solution.
2023-05-12 09:30 AM
Don't printf() from interrupt handlers.
What does your code do when the 2nd byte is sent? Do you even see the 2nd interrupt?
2023-05-12 09:30 AM
Don't printf() from interrupt handlers.
What does your code do when the 2nd byte is sent? Do you even see the 2nd interrupt?
2023-05-12 09:48 AM
How exactly does the code resynchronize itself?
Would suggest making something simpler, just accumulating characters.
Check error/status for received data.
NUL terminate data if you plan on using string functions on it.
2023-05-12 11:19 AM
As BobS said above, don't printf() from interrupt handlers.
It takes too long and as it's in the interrupt handler itself, there's nothing which would read out the following characters, so the next two characters overflow the UART - and as you don't check for overflow, the whole code then just loops the interrupt infinitely (I guess uart_2_mgr[3] is zero, the pointer increment after "timer strategy" eventually overflows USART_2_msg[] and ultimately overflows the whole RAM - or something similar).
JW
2023-05-15 03:11 AM
Sorry for late reply, I commented all printfs in interrupt function. Now it is working. Thank you all :beaming_face_with_smiling_eyes: