cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f103vct6 usart2 false RXNE flag setting and reception using keil

engineershrenik
Associate
Posted on January 06, 2014 at 13:14

Hi,

I would need 3 USARTs for a project (I am using stm32f103vct6 microcontroller). One USART is already functional properly which is USART1. I am trying same with USART2. Transmit is working fine but but RXNE flag is falsely setting up here and hence its giving garbage Rx character. I tried same code using arm-none-linux-gcc compiler in which its working fine (no false RXNE flag setting), hence i thought it could be compiler related issue. I am posting my code for reference.

void USART2_Init(void)

{

                        GPIO_InitTypeDef GPIO_InitStructure;

                        USART_InitTypeDef USART_InitStructure;

                        //enable bus clocks

                        RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

                        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,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_Cmd(USART2, ENABLE);

}

/* 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')

        {

        SerialPutChar(*s);

        s ++;

        }

}

void main()

{

  int data;

          USART1_Init();

        Serial_PutString(''This is test put string function on open source compiler\r\n'');

                                  //printf(''This is containing the printf debug command\r\n'');

                  while(1)

                  {

                          data = GetChar();

                          SerialPutChar(data);

                  }

}

#stm32f103-usart
2 REPLIES 2
Posted on January 06, 2014 at 16:39

I'd definitely front test for TXE before blind writing the data register.

Don't park a register view over the USART peripheral in the debugger. At first glance the code doesn't look bad.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
engineershrenik
Associate
Posted on January 07, 2014 at 08:47

In fact i am using FreeRTOS in this. without RTOS usart2 working fine in keil. But with RTOS i guess there is some problem. I am customizing FreeRTOS demo for stm32f103 for project.