[C] 纯文本查看 复制代码
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;
}
}
这个问题怎么解决,求助 |