简述
实际开发过程中,DMA通道全部占用,余下串口无DMA使用,改用非DMA方式接收数据。
配置过程
MCU使用STM32F427,串口配置用图说明(偷个懒)。
在主函数初始化中,除开cube自动生成的串口初始化外,还需额外调用指明接收buffer[size]和使能空闲中断接收。
HAL_UART_Receive_IT(&huart8,recv_buffer, USART_RX_SIZE);//指明接收buffer和size
__HAL_UART_ENABLE_IT(&huart8, UART_IT_IDLE);//开启空闲中断
/* 中断接收数据处理过程 */
void recv_handle(UART_HandleTypeDef *huart)
{
uint16_t length = 0;
if ( __HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE))
{
__HAL_UART_CLEAR_IDLEFLAG(huart);
if (huart->RxState == HAL_UART_STATE_BUSY_RX)
{
/* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
/* At end of Rx process, restore huart->RxState to Ready */
huart->RxState = HAL_UART_STATE_READY;
}
length = USART_RX_SIZE - huart->RxXferCount;
//数据处理,写入缓存区
for (uint8_t i; i < length; i++)
{
printf("%d", recv_buffer
);
}
recv_flag = true;
HAL_UART_Receive_IT(huart, recv_buffer, USART_RX_SIZE);
}
}
简述
实际开发过程中,DMA通道全部占用,余下串口无DMA使用,改用非DMA方式接收数据。
配置过程
MCU使用STM32F427,串口配置用图说明(偷个懒)。
在主函数初始化中,除开cube自动生成的串口初始化外,还需额外调用指明接收buffer[size]和使能空闲中断接收。
HAL_UART_Receive_IT(&huart8,recv_buffer, USART_RX_SIZE);//指明接收buffer和size
__HAL_UART_ENABLE_IT(&huart8, UART_IT_IDLE);//开启空闲中断
/* 中断接收数据处理过程 */
void recv_handle(UART_HandleTypeDef *huart)
{
uint16_t length = 0;
if ( __HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE))
{
__HAL_UART_CLEAR_IDLEFLAG(huart);
if (huart->RxState == HAL_UART_STATE_BUSY_RX)
{
/* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
/* At end of Rx process, restore huart->RxState to Ready */
huart->RxState = HAL_UART_STATE_READY;
}
length = USART_RX_SIZE - huart->RxXferCount;
//数据处理,写入缓存区
for (uint8_t i; i < length; i++)
{
printf("%d", recv_buffer
);
}
recv_flag = true;
HAL_UART_Receive_IT(huart, recv_buffer, USART_RX_SIZE);
}
}
举报