
stm32f4xx_hal_uart.c 中的HAL_UART_IRQHandler 函数,增加如下代码 /* UART Idle interrupt occurred --------------------------------------------*/ if (((isrflags & USART_SR_IDLE) != RESET) && ((cr1its & USART_CR1_IDLEIE) != RESET))4 n7 f' E% K m { __HAL_UART_CLEAR_IDLEFLAG(huart);9 d3 O% U( U* Y: ^" F$ b __HAL_UART_DISABLE_IT(huart, UART_IT_IDLE); HAL_UART_RxIdleCallback(huart); return;6 A1 n- a7 K( z/ e3 j0 Q- h }) v1 `8 I7 z# L6 ], C6 e ! b: j0 W& k8 Y q3 F 以下__weak 函数也在stm32f4xx_hal_uart.c 中定义: e4 m# d1 r/ K4 a /** * @brief Rx Idle callbacks. * @param huart: Pointer to a UART_HandleTypeDef structure that contains9 y, U7 `4 z; _' U * the configuration information for the specified UART module. * @retval None */ __weak void HAL_UART_RxIdleCallback(UART_HandleTypeDef *huart)" e" t1 D! u8 n& v {, j! k6 w2 Y) {# r /* Prevent unused argument(s) compilation warning */ UNUSED(huart);/ j Y, `, z+ { /* NOTE: This function should not be modified, when the callback is needed, the HAL_UART_RxIdleCallback can be implemented in the user file */ } 同时,stm32f4xx_hal_uart.h中增加如下函数声明,算是给最新的HAL库打补丁了。 void HAL_UART_RxIdleCallback(UART_HandleTypeDef *huart); 最后,在生成的代码中,增加如下实际函数。( {* E& I1 K. [ /* USER CODE BEGIN 1 */: p8 l& ?0 E" F3 {* K0 l /**7 f+ f! i4 m+ o+ b* R+ y+ S4 A9 O * @brief Rx Transfer completed callbacks. * @param huart Pointer to a UART_HandleTypeDef structure that contains0 q! j6 p l. z! Z. z# r' j% U$ p: @0 g * the configuration information for the specified UART module.- H& `2 N9 u0 U1 ` * @retval None */3 u5 e! c+ }' E+ a/ w+ F void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if (huart->Instance == USART1) { USART1_Rx_Buf[USART1_Rx_Count] = USART1_Rx_temp; HAL_UART_Receive_IT(huart, (uint8_t *)&USART1_Rx_temp, 1);, e( h; {$ y6 _ if (++USART1_Rx_Count >= USART1_Rx_Buf_Size) { USART1_Rx_Count = 0; 3 M- t! f& j* x; c: q } __HAL_UART_CLEAR_IDLEFLAG(&huart1); __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);* D$ i& _* g6 A' C+ D } } 8 a$ S+ ~! X( T$ \: K1 u /** * @brief Rx Idle callbacks. * @param huart: Pointer to a UART_HandleTypeDef structure that contains1 k; x! p* q4 { * the configuration information for the specified UART module. * @retval None */ void HAL_UART_RxIdleCallback(UART_HandleTypeDef *huart) {) @. K8 Y! f. d W% \ if (huart->Instance == USART1) { __HAL_UART_CLEAR_IDLEFLAG(huart);! ?8 h, \, W6 q% ~ __HAL_UART_ENABLE_IT(huart, UART_IT_IDLE); if(USART1_Rx_Buf[1] == 0x55){ __NOP();; E/ S& f, q9 e# x+ O( `& K+ s __NOP(); __NOP(); } else if(USART1_Rx_Buf[1] == 0xA5){3 D4 c- f# f* p2 B __NOP();3 h/ w3 P7 C" x) G/ V __NOP(); __NOP();& c0 Z. o% t. o4 y. ~ w }" V: O" A% E7 @ s& ~& { memset((uint8_t *)USART1_Rx_Buf, 0, 1024); USART1_Rx_Count = 0;0 }6 F' ^' ~# \7 q4 N7 W } } 3 W' z* y3 l, s7 Z3 d8 |! Z /* USER CODE END 1 */ " o" z8 j# c: k; v/ m" M 经测试,不管连发多少个字符,都会进空闲中断回调函数,这样应用程序就非常好处理了。- L! p: e, h5 \& M6 U( [# r 2019/05/10 wjandsq@163.com 6 \4 J' ?# C e6 }$ Q! p |
1 @, }7 o+ E" F
很好用,多谢分享