/* UART4 interrupt Init */
HAL_NVIC_SetPriority(UART4_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(UART4_IRQn);
/* USER CODE BEGIN UART4_MspInit 1 */
/* USER CODE END UART4_MspInit 1 */
}
}
/**
* @brief This function handles UART4 global interrupt.
*/
/**********串口4中断入口***********/
void UART4_IRQHandler(void)
{
/* USER CODE BEGIN UART4_IRQn 0 */
/* USER CODE END UART4_IRQn 0 */
HAL_UART_IRQHandler(&huart4);
/* USER CODE BEGIN UART4_IRQn 1 */
if(__HAL_UART_GET_FLAG(&huart4,UART_FLAG_IDLE)!=RESET)//判断是否是空闲中断
{
__HAL_UART_CLEAR_IDLEFLAG(&huart4);
uart_rx_idle_callback(&huart4);
}
/* USER CODE END UART4_IRQn 1 */
}
这是我在H7上面用的空闲中断+DMA,给你参考一下:
/**********初始化串口4***********/
void MX_UART4_Init(void)
{
huart4.Instance = UART4;
huart4.Init.BaudRate = 115200;
huart4.Init.WordLength=UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.Init.Prescaler = UART_PRESCALER_DIV1;
huart4.Init.FIFOMode = UART_FIFOMODE_DISABLE;
huart4.Init.TXFIFOThreshold = UART_TXFIFO_THRESHOLD_1_8;
huart4.Init.RXFIFOThreshold = UART_RXFIFO_THRESHOLD_1_8;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart4) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
__HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);//使能空闲中断
HAL_UART_Receive_DMA(&huart4, (uint8_t*)uart4Rx,UART_RECEIVE_MAX);//启动DMA接收65535个字节
}
/************DMA配置*************/
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(uartHandle->Instance==UART4)
{
/* USER CODE BEGIN UART4_MspInit 0 */
/* USER CODE END UART4_MspInit 0 */
/* UART4 clock enable */
__HAL_RCC_UART4_CLK_ENABLE();
/**UART4 GPIO Configuration
PC10 ------> UART4_TX
PC11 ------> UART4_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* UART4 DMA Init */
/* UART4_RX Init */
hdma_uart4_rx.Instance = DMA1_Stream4;
hdma_uart4_rx.Init.Request = DMA_REQUEST_UART4_RX;
hdma_uart4_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_uart4_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_uart4_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_uart4_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_uart4_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_uart4_rx.Init.Mode = DMA_CIRCULAR;//配置成normal也可以,随便
hdma_uart4_rx.Init.Priority = DMA_PRIORITY_LOW;
hdma_uart4_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_uart4_rx) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
__HAL_LINKDMA(uartHandle,hdmarx,hdma_uart4_rx);
/* UART4_TX Init */
hdma_uart4_tx.Instance = DMA1_Stream5;
hdma_uart4_tx.Init.Request = DMA_REQUEST_UART4_TX;
hdma_uart4_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_uart4_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_uart4_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_uart4_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_uart4_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_uart4_tx.Init.Mode = DMA_NORMAL;
hdma_uart4_tx.Init.Priority = DMA_PRIORITY_LOW;
hdma_uart4_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_uart4_tx) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
__HAL_LINKDMA(uartHandle,hdmatx,hdma_uart4_tx);
/* UART4 interrupt Init */
HAL_NVIC_SetPriority(UART4_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(UART4_IRQn);
/* USER CODE BEGIN UART4_MspInit 1 */
/* USER CODE END UART4_MspInit 1 */
}
}
/**
* @brief This function handles UART4 global interrupt.
*/
/**********串口4中断入口***********/
void UART4_IRQHandler(void)
{
/* USER CODE BEGIN UART4_IRQn 0 */
/* USER CODE END UART4_IRQn 0 */
HAL_UART_IRQHandler(&huart4);
/* USER CODE BEGIN UART4_IRQn 1 */
if(__HAL_UART_GET_FLAG(&huart4,UART_FLAG_IDLE)!=RESET)//判断是否是空闲中断
{
__HAL_UART_CLEAR_IDLEFLAG(&huart4);
uart_rx_idle_callback(&huart4);
}
/* USER CODE END UART4_IRQn 1 */
}
/**************空闲中断处理***************/
void uart_rx_idle_callback(UART_HandleTypeDef *huart)
{
uint32_t temp;
if(huart->Instance==USART6)
{
uint32_t len;
HAL_UART_DMAStop(&huart6);
temp=((DMA_Stream_TypeDef *)hdma_usart6_rx.Instance)->NDTR;
//printf("%d ",UART_RECEIVE_MAX-temp);
uart6RxLen=UART_RECEIVE_MAX-temp;
HAL_UART_Receive_DMA(&huart6, (uint8_t*)uart6Rx,UART_RECEIVE_MAX);
}
else if(huart->Instance==UART4)
{
HAL_UART_DMAStop(&huart4);
temp=((DMA_Stream_TypeDef *)hdma_uart4_rx.Instance)->NDTR;
//printf("%d ",UART_RECEIVE_MAX-temp);
uart4RxLen=UART_RECEIVE_MAX-temp;//实际接收长度,必须小于65535
/**********/
HAL_UART_Receive_DMA(&huart4, (uint8_t*)uart4Rx,UART_RECEIVE_MAX);//再次启动DMA接收
}
}
一周热门 更多>