AnsweredAssumed Answered

STM32F4 USART2 Trapped in RXNE RESET

Question asked by roofie01 on Jun 25, 2017
Latest reply on Jun 26, 2017 by Clive One

Hi,

I have been working on this for 2 days trying to figure out why my application just stops. After looking at posts on this forum, and other places, and reading the STM32F4 reference manual, I see no problems with my code, but seeing odd behaviour from USART2. Here's my code.

 

void USART_Response( char *s, uint16_t len )

{

    while( len-- )
    {
        while( USART_GetFlagStatus(USART2, USART_FLAG_TXE ) == RESET);
        USART_SendData( USART2, *s++ );
    }        

   //so I can see what this is doing

    printf("%i: SR = %x\n", srCtr++, USART2->SR);   // PRINTS 0xD0 == 11010000 = TXE, TC, IDLE are SET
            
    while( 1 ) // poll response
    {
        while( USART_GetFlagStatus(USART2, USART_FLAG_RXNE ) == RESET);     // ALWAYS STALLS HERE     
        rxCode = USART2->DR;
     
        if( rxCode == 0x3E ) //a regular '>' has been received
        {
            PROMPT_SET = TRUE;

            printf("%x, ", rxCode);
        }   
        else if( rxCode = 0xD )
        {
           PROMPT_SET = FALSE;

            printf("%x\n", rxCode);
           break;
        }
    }

}

 

The printf are there for debugging. It prints these 2 lines over, until it stalls on RXNE:

3e, d

0xd0

 

The strings sent out are to a screen. The target device first sends back a 0x3E, then a carriage return (0xD) That's it.It will sail along for 15 mins, sometimes an hour, then suddenly choke. With the SR at 0xD0, I cannot understand why this is getting stalled here. This happens with/without the  debugger running as well.

 

I could understand it if the ORE was set, or IDLE was not set, but what can I do to fix this? Any advice would be greatly appreciated, thanks. 

 

ADDED AFTER: USART SETUP CODE

void USART2_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;   

 

    //enable USART peripheral clock
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

 

    //enable peripheral A clock
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

 

    GPIO_InitStructure.GPIO_Pin = SCRN_UART_TX | SCRN_UART_RX;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;             
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;        
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;            
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;            
    GPIO_Init(GPIOA, &GPIO_InitStructure);                    

 

    //set to alternate function 1 for UART operation
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
    
    /* USARTx configured as:
        - SCRN_UART_BAUD= 115200 baud
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
    */
    USART_InitStructure.USART_BaudRate = SCRN_UART_BAUD;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
 
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
    USART_Init(USART2, &USART_InitStructure);
    USART_Cmd(USART2, ENABLE);
}

Outcomes