This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

STM32F103VCT USART2 False Reception issue

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);
                  }
}

Please let me know what can be done to solve this. What i am missing in above code.

Parents
  • These are well known issues when using SPL, nobody knows why, but it happens ;] Lose the extremely inefficient and mangled SPL code and write some SFR-based initialization code, like

    /*
    Configure USART2 for DMA based TRX with hw flow control
    */
    static void uartinit(void)
    {
    //USART2 APB1 30MHz
    USART2->CR3=0x3C0;
    USART2->CR2=0x0;
    USART2->BRR=(16<<4)|4; //mantissa, fraction(/16)
    USART2->SR&=~0xC0; //kill TC,RXNE
    USART2->CR1=0x200C;
    }
    


    This is possibly not very user friendly, but it works flawlessly. I never got any issues with U(S)ARTs on this uCs, so the problem is in your code, not in the silicon. Some months ago I developed a device utilizing three U(S)ARTs (with and without DMA) at the same time on the F103RB, and I never encountered any strange behavior of U(S)ARTs...

Reply
  • These are well known issues when using SPL, nobody knows why, but it happens ;] Lose the extremely inefficient and mangled SPL code and write some SFR-based initialization code, like

    /*
    Configure USART2 for DMA based TRX with hw flow control
    */
    static void uartinit(void)
    {
    //USART2 APB1 30MHz
    USART2->CR3=0x3C0;
    USART2->CR2=0x0;
    USART2->BRR=(16<<4)|4; //mantissa, fraction(/16)
    USART2->SR&=~0xC0; //kill TC,RXNE
    USART2->CR1=0x200C;
    }
    


    This is possibly not very user friendly, but it works flawlessly. I never got any issues with U(S)ARTs on this uCs, so the problem is in your code, not in the silicon. Some months ago I developed a device utilizing three U(S)ARTs (with and without DMA) at the same time on the F103RB, and I never encountered any strange behavior of U(S)ARTs...

Children