Debug程序使其运行在一处断点处,停止程序运行。发送2个或2个以上字节到串口,串口会出现溢出错误。
void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
{
//执行 UART_EndRxTransfer(huart);
UART_EndRxTransfer(huart);
......
HAL_UART_ErrorCallback(huart);
......
}
static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
{
/* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
/* At end of Rx process, restore huart->RxState to Ready */
huart->RxState = HAL_UART_STATE_READY;
/* Reset RxIsr function pointer */
huart->RxISR = NULL;
}
重写HAL_UART_ErrorCallback代码
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
if(huart == &huart1)
{
//执行HAL_UART_ErrorCallback时,还处于lock,需先unlock,
//因为HAL_UART_Receive_IT执行时需判断如果是lock则直接返回BUSY
__HAL_UNLOCK(huart);
HAL_UART_Receive_IT(&huart1,Tx_M_Buff_Temp[0],1);
}
}
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)
{
if ((pData == NULL) || (Size == 0U))
{
return HAL_ERROR;
}
//需判断如果是lock则直接返回BUSY
__HAL_LOCK(huart);
huart->pRxBuffPtr = pData;
huart->RxXferSize = Size;
huart->RxXferCount = Size;
huart->RxISR = NULL;
......
}
Debug程序使其运行在一处断点处,停止程序运行。发送2个或2个以上字节到串口,串口会出现溢出错误。
void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
{
//执行 UART_EndRxTransfer(huart);
UART_EndRxTransfer(huart);
......
HAL_UART_ErrorCallback(huart);
......
}
static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
{
/* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
/* At end of Rx process, restore huart->RxState to Ready */
huart->RxState = HAL_UART_STATE_READY;
/* Reset RxIsr function pointer */
huart->RxISR = NULL;
}
重写HAL_UART_ErrorCallback代码
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
if(huart == &huart1)
{
//执行HAL_UART_ErrorCallback时,还处于lock,需先unlock,
//因为HAL_UART_Receive_IT执行时需判断如果是lock则直接返回BUSY
__HAL_UNLOCK(huart);
HAL_UART_Receive_IT(&huart1,Tx_M_Buff_Temp[0],1);
}
}
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)
{
if ((pData == NULL) || (Size == 0U))
{
return HAL_ERROR;
}
//需判断如果是lock则直接返回BUSY
__HAL_LOCK(huart);
huart->pRxBuffPtr = pData;
huart->RxXferSize = Size;
huart->RxXferCount = Size;
huart->RxISR = NULL;
......
}
举报