STM32
直播中

山中老虎

8年用户 921经验值
擅长:制造/封装
私信 关注
[问答]

Debug程序发送2个或2个以上字节到串口出现溢出错误怎么办?

Debug程序发送2个或2个以上字节到串口出现溢出错误怎么办?

回帖(1)

李涛

2021-12-9 14:07:08

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;
......
}
举报

更多回帖

发帖
×
20
完善资料,
赚取积分