问题:记录一次STM32长时间工作后,串口不能正常接收数据,其他功能正常。
场景简单描述:在某一个产品上STM32作为前面板的控制,有多个中断开启,大部分的中断级别为0,串口的中断级别为1,串口中断接收,普通方式发送。出现长时间工作之后串口不能接收,但能正常发送数据。
问题分析:
- 应该是串口接收的时候出现异常(溢出,因为串口中断接收的级别比较低,可能被其他的高优先级中断打断),STM32 HAL库的异常处理是没有有影响,晚上的例子是会不断进入中断
- 串口出现接收出现忙碌
针对第一个问题,在错误处理中断中加入溢出处理
HAL_StatusTypeDef g_RxStatus = 0;
void HAL_UART_RxCpltCallback( UART_HandleTypeDef* huart ) //¶¨Òå USART1 ½ÓÊÕÍê³É»Øµ÷º¯Êý¹¦ÄÜ
{
/* Prevent unused argument(s) compilation warning */
// UNUSED(huart);
/* NOTE: This function should not be modified, when the callback is needed,
the HAL_UART_RxCpltCallback could be implemented in the user file
*/
// Check if there's space. Put received char in buffer.
if( ringBufferPut( &g_UartRxBuffer, m_RecvChar ) == FALSE )
{
// no space in buffer, increment overflow counter
m_UartRxOverflow++;
ringBufferReset( &g_UartRxBuffer );
}
g_RxStatus = HAL_UART_Receive_IT( &huart1, &m_RecvChar, 1 );
PDEBUGE( "uart rx status = %d\n", status );
}
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
/* Prevent unused argument(s) compilation warning */
// UNUSED(huart);
/* NOTE: This function should not be modified, when the callback is needed,
the HAL_UART_ErrorCallback could be implemented in the user file
*/
if( huart->ErrorCode & HAL_UART_ERROR_ORE )//Overflow error
{
uint32_t temp = huart->Instance->SR;
temp = huart->Instance->DR;
}
}
针对串口忙碌,增加返回值判断,如果忙碌定时10ms重新检查
void CTRLCOM_CheckRxBusy()
{
if( g_RxStatus == HAL_BUSY )
{
g_RxStatus = HAL_UART_Receive_IT( &huart1, &m_RecvChar, 1 );
}
}