stm32f103 freertos issue - usart2 false setting of RXNE flag.

engineershrenik wrote on Wednesday, January 08, 2014:

Hi, I am using freertos demo for stm32f103x and using stm32f103vct6. Usart 1 is working fine but when i configured usart2 transmit function works fine but usart for receive function RXNE flags is setting falsely hence i am getting garbage data. This is not happening when i am programming two usarts without freertos in keil. Is there any configuration problem? Please help me to solve this issue.
I am posting my code too.

void USART2_Init(void)
GPIO_InitTypeDef GPIO_InitStructure;

     USART_InitTypeDef USART_InitStructure;

     //enable bus clocks

     RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

     //Set USART2 Tx (PA2) as AF push-pull

     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

     GPIO_Init(GPIOA, &GPIO_InitStructure);

     //Set USART2 Rx (PA3) as input floating

     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

     GPIO_Init(GPIOA, &GPIO_InitStructure);

     USART_InitStructure.USART_BaudRate = 115200;

     USART_InitStructure.USART_WordLength = USART_WordLength_8b;

     USART_InitStructure.USART_StopBits = USART_StopBits_1;

     USART_InitStructure.USART_Parity = USART_Parity_No ;

     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

    USART_Init(USART2, &USART_InitStructure);

    //Enable USART2



/* Usart functions --------------------------------- ------------------------/

  • Function Name : SerialPutChar
  • Description : Print a character on the HyperTerminal
  • Input : - c: The character to be printed
  • Output : None
  • Return : None
    void SerialPutChar(u8 c)
    USART_SendData(USART2, c);
    while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);

int GetChar (void)
while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
return USART_ReceiveData(USART2);

  • Function Name : Serial_PutString
  • Description : Print a string on the HyperTerminal
  • Input : - s: The string to be printed
  • Output : None
  • Return : None
    void Serial_PutString(u8 *s)
    while (*s != ‘\0’)
    s ++;

static portTASK_FUNCTION(vBTTask,pvParameters)

int data;

printf("Usart2 is initialized Press Key \r\n");
Serial_PutString("test usart string \r\n");

	 data = GetChar();


here data variable is getting continuously garbage data using GetChar function.

Please let me know. i guess its freertos related issue only.

rtel wrote on Wednesday, January 08, 2014:

i guess its freertos related issue only.

I cannot see anything related to FreeRTOS in the code you have posted. Is your FreeRTOS application running ok in every way other than your use of the ST peripheral drivers? If so, then are you using the latest FreeRTOS version with configASSERT() defined, because there were some asserts put in specifically for users of the ST peripheral drivers as those particular drivers require the Cortex-M to be set up in a certain way in order to function correctly with FreeRTOS. However, this related to interrupt priorities, and your code does not look like it is doing anything other than polling registers - so your problem could equally be due to the fact that your polling code is starving all the other tasks of processing time (polling is generally not used in RTOS applications, not without some kind of fast timeout anyway).


engineershrenik wrote on Wednesday, January 08, 2014:

I even tried interrupt for serial but still its going in IRQ again and again even if there is no data on Rx Pin. Even if i disabled complete freertos i get same problem hence i thought it would be problem regarding some rtos configuration.