野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 404|回复: 1

HAL库在初始化uart时,HAL_UART_Receive_IT函数会直接跳到HAL-BUSY

[复制链接]
发表于 2019-8-20 18:42:03 | 显示全部楼层 |阅读模式
本帖最后由 野论坛真是好 于 2019-8-20 18:43 编辑

[mw_shl_code=c,true]
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
    //GPIO端口设置
        GPIO_InitTypeDef GPIO_InitStruct;        
        if(huart->Instance==USART2)
        {
                USART2_RX_GPIO_CLK_ENABLE();                        //使能GPIOA时钟
                USART2_TX_GPIO_CLK_ENABLE();
               
                USART2_CLK_ENABLE();                        //使能USART2时钟               
               
                GPIO_InitStruct.Alternate = USART2_TX_AF;
                GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
                GPIO_InitStruct.Pin = USART2_TX_PIN;
                GPIO_InitStruct.Pull = GPIO_PULLUP;
                GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
                HAL_GPIO_Init(USART2_TX_PORT, &GPIO_InitStruct);
               
                GPIO_InitStruct.Alternate = USART2_RX_AF;
                GPIO_InitStruct.Pin = USART2_RX_PIN;
                HAL_GPIO_Init(USART2_RX_PORT, &GPIO_InitStruct);               
               
                HAL_NVIC_SetPriority(USART2_IRQn,0,0);//第二个数字,对于Core M0无用
                HAL_NVIC_EnableIRQ(USART2_IRQn);
               
//                __HAL_UART_ENABLE_IT(&UART2_Handler,UART_IT_RXNE);                  
                HAL_UART_Receive_IT(&UART2_Handler, (uint8_t *)&aRxBuffer, 1);        //每接收到一个字节,就会进入一次回调函数,1为一个字节就进入        ,已经开启RXNE中断
               
//                aRxBuffer = USART2->ISR;               
        }
}




/**
  * @brief Receive an amount of data in interrupt mode.
  * @param huart UART handle.
  * @param pData pointer to data buffer.
  * @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.
  * @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 */
  if(huart->RxState == HAL_UART_STATE_READY)    //这里不能进入,会直接跳过
  {
    if((pData == NULL ) || (Size == 0U))
    {
      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. */
    if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
    {
      if((((uint32_t)pData)&1U) != 0U)
      {
        return  HAL_ERROR;
      }
    }

    /* Process Locked */
    __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->ErrorCode = HAL_UART_ERROR_NONE;
    huart->RxState = HAL_UART_STATE_BUSY_RX;

    /* Process Unlocked */
    __HAL_UNLOCK(huart);

    /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
    SET_BIT(huart->Instance->CR3, USART_CR3_EIE);

    /* Enable the UART Parity Error and Data Register not empty Interrupts */
    SET_BIT(huart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);

    return HAL_OK;
  }
  else
  {
    return HAL_BUSY;
  }
}[/mw_shl_code]

这个问题怎么解决,求助
回复

使用道具 举报

发表于 2019-8-21 11:26:11 | 显示全部楼层
帮顶
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|站点地图|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号-2 ) 大学生ARM嵌入式2群

GMT+8, 2020-8-3 23:59 , Processed in 0.148855 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表