芯片:stm32f042
库:hal
1,
//main函数的while(1)上面写下面两句话 __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE); HAL_UART_Receive_DMA(&huart1, (uint8_t*)receive_buff, BUFFER_SIZE); //main.c底部,这是通过cube配置自动生成的。 static void MX_NVIC_Init(void) { /* USART1_IRQn interrupt configuration */ HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART1_IRQn); }
2,
//uart.c里面 #include <string.h> extern uint8_t receive_buff[BUFFER_SIZE]; #ifdef __GNUC__ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif PUTCHAR_PROTOTYPE { HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF); return ch; }
3,
//uart.c里面 void MY_UART_IDLECallback(UART_HandleTypeDef *huart) { HAL_UART_DMAStop(&huart1); uint8_t data_length=BUFFER_SIZE-__HAL_DMA_GET_COUNTER(&hdma_usart1_rx); printf("Receive接收 Data_length = %d : ",data_length); HAL_UART_Transmit(&huart1,receive_buff,data_length,0x200); mijia_cmd_chuli(receive_buff,data_length); printf("\r\n"); memset(receive_buff,0,data_length); data_length = 0; HAL_UART_Receive_DMA(&huart1, (uint8_t*)receive_buff, BUFFER_SIZE); } void MY_UART_IRQHandler(UART_HandleTypeDef *huart) { if(USART1==huart1.Instance) { if(RESET != __HAL_UART_GET_FLAG(&huart1,UART_FLAG_IDLE)) { __HAL_UART_CLEAR_IDLEFLAG(&huart1); printf("\r\nUART1 Idle IQR Detected\r\n"); MY_UART_IDLECallback(huart); } } }
4,
//uart.h extern UART_HandleTypeDef huart1; #define BUFFER_SIZE (255) void MX_USART1_UART_Init(void);
5,
//stm32f0xx_it.h extern void MY_UART_IRQHandler(UART_HandleTypeDef *huart); void DMA1_Channel2_3_IRQHandler(void) { /* USER CODE BEGIN DMA1_Channel2_3_IRQn 0 */ /* USER CODE END DMA1_Channel2_3_IRQn 0 */ HAL_DMA_IRQHandler(&hdma_usart1_rx); /* USER CODE BEGIN DMA1_Channel2_3_IRQn 1 */ /* USER CODE END DMA1_Channel2_3_IRQn 1 */ } void USART1_IRQHandler(void) { /* USER CODE BEGIN USART1_IRQn 0 */ /* USER CODE END USART1_IRQn 0 */ HAL_UART_IRQHandler(&huart1); /* USER CODE BEGIN USART1_IRQn 1 */ MY_UART_IRQHandler(&huart1); /* USER CODE END USART1_IRQn 1 */ }
声明:
本文采用
BY-NC-SA
协议进行授权,如无注明均为原创,转载请注明转自
走着的小站
本文地址: STM32使用串口DMA+空闲中断实现不定长接收数据
本文地址: STM32使用串口DMA+空闲中断实现不定长接收数据