Showing results for 
Search instead for 
Did you mean: 

UART on STM32F091 stops receiving data even if they're sent

Associate II

So, I'm having strange behavior regarding the serial number of my board with an STM32F091.

I have a Python software that sends a packet on the serial port every 100 ms. I have initialized the serial port on the ucontroller with the same settings as are set in the Python software to transmit.



/* USART2 init function */
static void MX_USART2_UART_Init(void)

huart2.Instance = USART2;
huart2.Init.BaudRate = 38400;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
_Error_Handler(__FILE__, __LINE__);

HAL_UART_Receive_IT(&huart2, &cdr2, 1);



I'm using the RX callback to manage the RX on the serial port



void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
/* Set transmission flag: trasfer complete*/
if (UartHandle->Instance == USART2)
rx2[puwrrx2] = cdr2;

if( puwrrx2 < ( RX2_LEN - 1 ) )
puwrrx2 = 0;

HAL_UART_Receive_IT(&huart2, &cdr2, 1);



and the TX callback to put back the serial in RX



void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
/* Set transmission flag: trasfer complete*/
if (UartHandle->Instance == USART2)






#define DE_RS485_HIGH HAL_GPIO_WritePin(RS485_En2_uC_GPIO_Port, RS485_En2_uC_Pin, GPIO_PIN_SET)
#define DE_RS485_LOW HAL_GPIO_WritePin(RS485_En2_uC_GPIO_Port, RS485_En2_uC_Pin, GPIO_PIN_RESET)



I trasmit every 100 ms with a function that calls



HAL_UART_Transmit_IT(&huart2, tx2, packet_lenght);



For some reason, I still have to understand why, at a certain point, the serial stops receiving data or, at least, the rx2[] buffer stays empty even if I'm sending data.

Is there a way to check if the UART goes in error and, at least, reset it to make it work again?

After some investigation, I found out that the main issue is that RxXferCount goes to zero for some reason. This put the RxState to HUAL_UART_STATE_READY instead of putting the state back to BUSY RX.

The CR1 register has also the bit 2 RE: Receiver enable set to 0 and the ISR register shows both FE: Framing error and NF: START bit Noise detection flag.

The main issue is that when everything I've just described happens, the `HAL_UART_ErrorCallback` is not called

Staying in debug I've noticed that the only callback my code enters in is the `USART3_8_IRQhandler`

From what I understand is that with `RxXferCount=0` the micro do not automatically clear the errors and goes back to the RX mode, but stays stuck in the situation I've just described

Pavel A.

UART stops receiving "of no reason" => check and clear the receiver overflow.

Is there a way to check if the UART goes in error and, at least, reset it to make it work again?

Of course. Both "HAL" and "LL" libraries have functions to check and clear the overflow and other errors. Enable the UART error callback, see if it is called.