cancel
Showing results for 
Search instead for 
Did you mean: 

Stm32L151RCxxx USART Hang issue, Interrupt Based TX/RX simultaneously

ishmeetsinghis
Associate II
Posted on January 13, 2014 at 13:32

Hi,

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. Hang 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;
}

#stm32 #uart #usart #receive
4 REPLIES 4
troy1818
Senior
Posted on January 13, 2014 at 14:15

Does it really hang in the function

USART_ITConfig or when calling it?

If not, where exactly does you program hang? .

We can see some suitable locations in the code itself:

while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);

while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);

Also in stm32 lib there are a lot of asserts (

assert_param) that might fall under the definition of ''hanging'' since many times just a while loop is there doing nothing.

Also an interrupt to exception vector in the code will do the same (while(1);) if not implemented in a good way.

Regards,

rygelxvi

Posted on January 13, 2014 at 17:36

A casual analysis would suggest you're probably stuck in a storm of unserviced interrupts.

There is a pointless spin on TXE, and spinning on TC also seems to be something to do outside of an interrupt service routine, if at all.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
ishmeetsinghis
Associate II
Posted on January 14, 2014 at 16:11

I have removed the following lines from the IRQ handler, But I still face hang issue.

while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
USART_ClearFlag(USART3, USART_FLAG_TC);

I also checked inside ''assert_param'' and interrupt to exception vectors in code, But the code doesn't reach there, I have checked this by prints on another UART. The hang issue is coming when we return from the IRQ Handler after disabling the TXE Interrupt.
Posted on January 14, 2014 at 16:21

Awesome, but what happens if CTS is asserted, and you don't bother to service the pending TXE interrupt?

That it ''hangs'' really doesn't convey sufficient information to the remote observer, it might make sense to you, but you have access to the system/debugger, and nobody else does. You'll have to be specific about where it hangs, and in which code or code loops it is in.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..