AnsweredAssumed Answered

UART problem betwen stm32f407 and fpga

Question asked by anton.bogdan on Apr 30, 2017
Latest reply on Apr 30, 2017 by KIC8462852 EPIC204278916



i have a design where a fpga sends a packet of 10bytes at a baud of 57970 to USART2 of the stm32f407VE device.


The idea was to use USART2  where RX pin is receiving data from the fpga,  and the TX pin sends the data to a PC.


The problem is that when i try to store the incoming data into a string i store wrong chars.... 


The data packet has the following format   Byte1,byte2,byte3....byte9, 0A   - the last byte is '\n' indicating the end of the packet


What i am doing wrong?  Is the fpga to fast for the uart of the stm32?



I am using the RX interrupt feature, see the code bellow

#define MAX_STRLEN 9 // this is the maximum string length of our string in characters
volatile char received_string[MAX_STRLEN+1]; // this will hold the recieved string

The uart init routine



RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 ; // Pins 5 (TX) and 6 (RX) are used
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; 
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; 
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; 
GPIO_Init(GPIOD, &GPIO_InitStruct);


GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2); //

USART_InitStruct.USART_BaudRate = 57970; 
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1; // we want 1 stop bit (standard)
USART_InitStruct.USART_Parity = USART_Parity_No; // we don't want a parity bit (standard)
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART2, &USART_InitStruct);



NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 



And the interrupt handler



void USART2_IRQHandler(void)

if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)//enter interrupt when STM32 receice data.
static uint8_t cnt = 0;// string lenght
char t = USART2->DR; // the character from the USART2 data register is saved in t

      if( (t != '\n') && (cnt < MAX_STRLEN ) ){
                                                                     received_string[cnt] = t;
      else{ // otherwise reset the character counter and print the received string
           cnt = 0;
           USART_puts(USART2, received_string);


GPIO_ToggleBits(GPIOD, GPIO_Pin_12);


Bellow is a logic capture on the RX (fpga side) and TX(to the pc side) of the uart2