STM32HAL库中断函数原理

STM32HAL库中断函数原理

  1. 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)//检查串口是否准备接收数据 { if ((pData =https://www.it610.com/article/= NULL) || (Size == 0U))//如果数据缓冲区为空,或者需要接收的数据个数为0个则返回错误标志 { 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; //将接收状态设置为忙/* Process Unlocked */ __HAL_UNLOCK(huart); //解锁/* 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); //使能错误中断/* Enable the UART Data Register not empty Interrupt */ __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); //使能数据寄存器非空中断,数据寄存器中有值就中断。即使能接收中断return HAL_OK; //返回OK状态 } else { return HAL_BUSY; //返回忙状态 } }

  1. 发送函数同理。
  2. 其他问题可以留言,也可以访问我的个人博客谢谢!

    推荐阅读