2019-07-20 06:10发布
爱因福尔 发表于 2016-11-25 10:50 HAL_UART_IRQHandler(&UART1_Handler); //μ÷óÃHAL¿aÖD¶Ï′|àí1«ó ...
最多设置5个标签!
timeout=0;
while (HAL_UART_GetState(&UART1_Handler) != HAL_UART_STATE_READY)//μè′y¾íD÷
{
timeout++;////3¬ê±′|àí
if(timeout>HAL_MAX_DELAY) break;
}
timeout=0;
while(HAL_UART_Receive_IT(&UART1_Handler, (u8 *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)//ò»′Î′|àííê3éÖ®oó£¬ÖØD¿aÆôÖD¶Ï2¢éèÖÃRxXferCountÎa1
{
timeout++; //3¬ê±′|àí
if(timeout>HAL_MAX_DELAY) break;
}
原子在程序上用了两个等待,这里不太明白,中断处理完成后,HAL_UART_IRQHandler(&UART1_Handler); 这个函数结束之后,huart.state的状态不就已经确定了吗。等待过程中会改变吗
寄存器版本有没有这个bug?
具体原因不明,但是应该是,HAL库的bug,通信速率高低都会有这个问题,所以应该和stm32的中断处理时间无关。未知原因导致(HAL_UART_Receive_IT(&huart1,RxdBuff1, 1) != HAL_OK)复位错误。HAL_UART_Receive_IT 官方原函数
HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
{
/* Check that a Rx process is not already ongoing */
if(huart->RxState == HAL_UART_STATE_READY) //怀疑是此处造成的错误,但是具体原因不知道,复位之前强制huart->RxState = HAL_UART_STATE_READY,并且清除USART1->SR;。USART1->SR;
//就可以使用了
{
if((pData == NULL ) || (Size == 0U))
{
return HAL_ERROR;
}
/* Process Locked */
__HAL_LOCK(huart);
huart->pRxBuffPtr = pData;
huart->RxXferSize = Size;
huart->RxXferCount = Size;
huart->ErrorCode = HAL_UART_ERROR_NONE;
huart->RxState = HAL_UART_STATE_BUSY_RX;
/* Enable the UART Parity Error Interrupt */
__HAL_UART_ENABLE_IT(huart, UART_IT_PE);
/* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
__HAL_UART_ENABLE_IT(huart, UART_IT_ERR);
/* Process Unlocked */
__HAL_UNLOCK(huart);
/* Enable the UART Data Register not empty Interrupt */
__HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
return HAL_OK;
}
else
{
return HAL_BUSY;
}
}
可以做一个双缓冲,用于 接收中断中存放接收字节的数组 rx_fifo可以设置为1。然后自己再根据实际需求,定义一个缓冲数组 fifo[];实测用PC发送一组数据到单片机,发送完成后触发超时,标志位F_timeout=1, 然后单片机再发给PC数据。
代码如下
#include "app.h"
/*============================================================================================
* 系统定时器中断
*============================================================================================*/
static uint16 count1 = 0;
static uint16 count2 = 0;
void HAL_SYSTICK_Callback(void)
{
sys.F_1ms = 1;
count1++;
if(count1>=10)
{
count1 = 0;
sys.F_10ms = 1;
}
count2++;
if(count2>=100)
{
count2 = 0;
sys.F_100ms = 1;
}
}
/*============================================================================================
* 串口接收中断
*============================================================================================*/
uint8 fifo[5000]; //二层接收缓冲
uint16 p1=0; //接收数量
uint8 rx_fifo[1]; //一层接收缓冲,用于官方库函数
extern UART_HandleTypeDef huart1;
/*============================================================================================
* 超时检测
*============================================================================================*/
uint8 E_timeout=0;
uint8 F_timeout=0;
uint8 C_timeout=0;
void func_timeout()
{
if(E_timeout)
{
C_timeout++;
if(C_timeout>=100)
{
C_timeout = 0;
F_timeout = 1;
}
}
else
{
C_timeout = 0;
F_timeout = 0;
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
uint8 i=0;
UNUSED(huart1);
HAL_UART_Receive_IT(&huart1,rx_fifo,sizeof(rx_fifo)); //接收函数
fifo[p1] = rx_fifo[0];
p1++;
if(p1>=5000)
{
p1 = 0;
}
/*接收超时检测:接收到数据-->开启超时检测并reset计数值*/
E_timeout = 1;
C_timeout = 0;
}
然后主程序中
if(sys.F_1ms)
{
sys.F_1ms = 0;
func_timeout();
if(F_timeout)
{
F_timeout = 0;
E_timeout = 0;
HAL_UART_Transmit_IT(&huart1,fifo,p1);
p1 = 0;
}
}
实测截图:同一段文字复制了很多次,一次发送总计4000多byte
file:///D:/%E5%B8%B8%E7%94%A8%E8%BD%AF%E4%BB%B6/YoudaoNote/%E7%AC%94%E8%AE%B0/qq127390A56C09F11E62A2848E550B9AE2/937870868bab47bf83f8b02436721836/%7Eqhb_d2d_gfa.png
一周热门 更多>