Skip to main content
kyouma001
Associate II
May 12, 2023
Solved

STM32F407 Discovery Board USART2 Read String

  • May 12, 2023
  • 4 replies
  • 1487 views

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.

This topic has been closed for replies.
Best answer by Bob S

Don't printf() from interrupt handlers.

What does your code do when the 2nd byte is sent? Do you even see the 2nd interrupt?

4 replies

Bob S
Bob SBest answer
Super User
May 12, 2023

Don't printf() from interrupt handlers.

What does your code do when the 2nd byte is sent? Do you even see the 2nd interrupt?

Tesla DeLorean
Guru
May 12, 2023

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.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
waclawek.jan
Super User
May 12, 2023

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

kyouma001
kyouma001Author
Associate II
May 15, 2023

Sorry for late reply, I commented all printfs in interrupt function. Now it is working. Thank you all :beaming_face_with_smiling_eyes: