HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)注释

/**
  * @brief Receive an amount of data in interrupt mode.(中断方式接收指定数量的数据)
  * @param huart UART handle.(UART handle,这个handle数据结构包含了很多参数)
  * @param pData pointer to data buffer.(pData是接收缓存的指针)
  * @param Size amount of data to be received.
  * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  *         address of user data buffer for storing data to be received, should be aligned on a half word frontier (16 bits)
  *         (as received data will be handled using u16 pointer cast). Depending on compilation chain,
  *         use of specific alignment compilation directives or pragmas might be required to ensure proper alignment for pData.
  		(如果设置的数据长度是9bit,并且是无校验,则数据缓存的指针必须是双字节对齐的,就是说指针的最低有效位LSB,bit0必须是0)
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
{
  /* Check that a Rx process is not already ongoing *///判断huart的状态,如果是Reday状态,才对其进行配置,如果不是,则什么都不做
  if(huart->RxState == HAL_UART_STATE_READY)
  {
    if((pData == NULL ) || (Size == 0U))//如果指针为空,或者接收数据长度等于0,则返回
    {
      return HAL_ERROR;
    }

    /* In case of 9bits/No Parity transfer, pData buffer provided as input paramter 
       should be aligned on a u16 frontier, as data to be received from RDR will be 
       handled through a u16 cast. *///如果数据长度是9bit,则指针必须双字节对齐,否则返回HAL_ERROR
    if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
    {
      if((((uint32_t)pData)&1U) != 0U)
      {
        return  HAL_ERROR;
      }
    }

    /* Process Locked *///配置前锁定 huart,可能是放置被意外修改吧
    __HAL_LOCK(huart);
	
    huart->pRxBuffPtr = pData;//设置缓存指针,
    huart->RxXferSize = Size;//设置接收数据数量
    huart->RxXferCount = Size;//设置接收计数器

    /* Computation of UART mask to apply to RDR register */
    UART_MASK_COMPUTATION(huart);//计算huart的mask参数,详见UART_MASK_COMPUTATION()函数,是根据数据位长度是8bit还是9bit,是否校验,来计算mask的

    huart->ErrorCode = HAL_UART_ERROR_NONE;//ErrorCode设置为无错误
    huart->RxState = HAL_UART_STATE_BUSY_RX;//接收状态设置为接收忙碌

    /* Process Unlocked */
    __HAL_UNLOCK(huart);//huart开锁

    /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
    SET_BIT(huart->Instance->CR3, USART_CR3_EIE);//使能UART出错中断(帧错误,噪声错误,过载错误)

    /* Enable the UART Parity Error and Data Register not empty Interrupts */
    SET_BIT(huart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);//使能UART奇偶校验错误中断和数据寄存器非空中断

    return HAL_OK;//配置顺利,返回HAL_OK;
  }
  else
  {
    return HAL_BUSY;//huart不是REDAY状态,返回HAL_BUSY;
  }
}

 

你可能感兴趣的:(HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)注释)