UART 接收中断在成功接收数小时后停止触发 [英] UART receive interrupt stops triggering after several hours of successful receive

查看:16
本文介绍了UART 接收中断在成功接收数小时后停止触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用与 xbee 连接的 STM32f4 发现板来接收来自远程温度传感器的温度数据.使用的代码是 CMIS UART 示例代码.我将接收数据包数据,一次 1 个字节.换句话说,只要接收到每个字节,就会调用 UART 接收中断.一旦我得到完整的数据包,我将复制温度数据.我的 UART 回调函数可以正常工作.但是几个小时后,UART接收中断停止工作,UART无法接收任何东西.但是 UART 传输仍然有效.我正在使用波特率 115200 的 UART1.我已将 UART 中断优先级设置为 0,并且没有其他中断共享此优先级.所有其他中断优先级均低于 UART.谁能告诉我为什么UART中断停止触发?

I am using STM32f4 discovery board connected with xbee to receive temperature data from remote temperature sensor. Code used is CMIS UART example code. I will receive packet data, 1 byte at a time. In other words UART receive interrupt will be called whenever each byte receives. Once I gets the complete packet I will copy the temperature data. My UART callback function works without any issue. But after several hours, UART receive interrupt stops working and UART cannot receive anything. However UART transmission still works. I am using UART1 with baud rate 115200. I have set UART interrupt priority as 0 and no other interrupt shares this priority. All other interrupt priority are lower than UART. Can any one please tell me why UART interrupt stops triggering?

#define PACKET_DELIMETER 0x7E

uint8_t g_frame_ok=0; //flag to indicate complete packet received
uint8_t g_index_of_aoBuf=0; //Index of receive buffer
uint8_t g_aoBuf_of_xbee[100]={0};//Receive Buffer
uint8_t r_byte=0; //Receiving byte 

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *allUartHandle)
{
 __HAL_UART_FLUSH_DRREGISTER(allUartHandle); 

 if(HAL_UART_Receive_IT(allUartHandle, (uint8_t *)&r_byte, 1) == HAL_OK) //Interrupt occurs when each byte arrives
 {
    if(r_byte==PACKET_DELIMETER)
    {
        //start receiving packet
    }
    if( g_index_of_aoBuf>=g_aoBuf_of_xbee[2]+4)
    {
        g_frame_ok=1;
        BSP_LED_On(LED4);
    }           
   }
 }

推荐答案

我没用过你提到的API,所以我可能是错的,但是以下是我看了之后注意到的一些事情:

I never used the API you mention, so I may be wrong, but here are a few things I noticed after taking a look :

HAL_UART_RxCpltCallback 不是 UART 中断.它是来自 HAL 子系统的回调,当您发出的接收请求完成时将调用该回调.这意味着它只会在您发出接收请求后的一段时间内被调用.你无权访问 UART 中断,如果你使用 HAL 层,你不应该尝试弄乱它.

HAL_UART_RxCpltCallback is not an UART interrupt. It's a callback from the HAL subsystem that will be called when a receive request you issued is completed. This means it will only be called some time after you issue a receive request. You don't have access to the UART interrupt, and you shouldn't try to mess with it if you use the HAL layer.

关于这个,HAL_UART_Receive_IT其实是一个会发出接收请求的函数.它总是会立即返回,并且永远不会收到任何东西.这意味着调用后接收缓冲区中的数据无效.发出请求后,HAL_UART_RxCpltCallback 将在接收完成后随时调用.只有此时缓冲区中的数据才有效.要检索数据,您可以使用 HAL_UART_Receive_IT 中的相同变量,但数据缓冲区也可通过回调参数中的 (UART_HandleTypeDef*)->pRxBuffPtr 获得.

About this, HAL_UART_Receive_IT is actually a function that will issue a receive request. It will always return immediately, and it will never receive anything. This means the data in the receive buffer just after the call is not valid. Once you issued the request, HAL_UART_RxCpltCallback will be called anytime after, when the receive is complete. Only at this point the data in the buffer will be valid. To retrieve the data you can use the same variable from the HAL_UART_Receive_IT, but the data buffer will also be available through (UART_HandleTypeDef*)->pRxBuffPtr from the callback parameter.

我认为在回调中再次调用 HAL_UART_Receive_IT 是可以的,但最好还是在最后调用.

I think it's OK to call HAL_UART_Receive_IT again in the callback, but it's probably better to do it at the end.

另外,__HAL_UART_FLUSH_DRREGISTER 是做什么用的?对我来说,它看起来弊大于利.

Also, what is __HAL_UART_FLUSH_DRREGISTER used for ? To me it looks like it will do more harm than good.

这篇关于UART 接收中断在成功接收数小时后停止触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆