7

I am running USART3, on 921600 BaudRate, using RTS CTS, I am always facing system hang when I try to do RX and TX simultaneously. I have pasted the main and IRQ code. IRQ is designed to Transmit a char 'A' while dropping all received data. Hangs happens when we disable USART_ITConfig(USART3, USART_IT_TXE, DISABLE);

Uart_Configuration()...

USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART3, &USART_ClockInitStructure);

USART_InitStructure.USART_Mode = (USART_Mode_Tx|USART_Mode_Rx);
USART_InitStructure.USART_BaudRate = u32BaudRate;
USART_OverSampling8Cmd(USART3, DISABLE);
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS; 
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;

USART_Init(USART3, &USART_InitStructure);  
USART_ITConfig(USART3,USART_IT_TXE, DISABLE);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_Cmd(USART3, ENABLE);

Main.c ...

uint8_t UART_TransmitData(void)
{
   if(u8IntTxFlag==1)
   {
       u8IntTxFlag=0;
       USART_ITConfig(USART3, USART_IT_TXE, ENABLE);      
       return TRUE;
   }
   return FALSE;
}


void USART3_IRQHandler(void)
{
   /* Implemented full duplex serial communication */
   /*  UART RX  */
   if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
   {
     USART_ReceiveData(USART3);
   }

   /*    UART TX    */
   if(USART_GetITStatus(USART3, USART_IT_TXE) != RESET)
   {
     if(USART_GetFlagStatus(USART3, USART_FLAG_CTS) == RESET)
     {
         while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
         USART_SendData(USART3, 'A');
         while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
         USART_ClearFlag(USART3, USART_FLAG_TC);
         USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
         u8IntTxFlag=1;
     }
     else
     {
        USART_ClearFlag(USART3, USART_FLAG_CTS);
     }
   }
}


int main(void)
{
  RCC_ClocksTypeDef RCC_Clocks;

  RCC_Configuration();
  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.HCLK_Frequency / 2000);

  NVIC_Configuration();

  Init_GPIOs();

  SerialUARTConfig(921600, 0, 1, 8, 1);

                while(1)
                {
                                UART_TransmitData();
                                f_SleepMs(5);

                }
                return 0;
}
Ishmeet
  • 1,540
  • 4
  • 17
  • 34
  • 2
    You likely have a deadlock in your logic. Hook up an SWD debugger (any of the STM32 discovery boards will do) and figure out where it is hanging, or if it is not hanging but crashing and ending up in a fault handler. Also, you might want to consider the possibility of UART error flags getting set (line noise, or at your baud rate, buffer overflow) and clear those. – Chris Stratton Jan 15 '14 at 16:01
  • Should you be disabling the USART clock? `USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;` – Fiddling Bits Jan 20 '14 at 23:33
  • Have you tried using a gpio pin for debug and setting/resetting it in different locations? That way you can trace which part of the code is in an infinite loop. – Shahbaz Jan 21 '14 at 15:27
  • Try http://www.coocox.org/show_exam/USART/391.html – Clarus Jan 21 '14 at 21:56

1 Answers1

4

Maybe the USART_IT_TXE interrupt happens before you disable it? So the "Tx empty" interrupt pending flag is already set and there is no procedure inside your IRQHandler to clear that flag.

kirill
  • 366
  • 1
  • 8