cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407 Discovery Board USART2 Read String

kyouma001
Associate II

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Bob S
Principal

Don't printf() from interrupt handlers.

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

View solution in original post

4 REPLIES 4
Bob S
Principal

Don't printf() from interrupt handlers.

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

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
Up vote any posts that you find helpful, it shows what's working..

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
Associate II

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