|
stm32f4xx_hal_uart.c 中的HAL_UART_IRQHandler 函数,增加如下代码 /* UART Idle interrupt occurred --------------------------------------------*/ if (((isrflags & USART_SR_IDLE) != RESET) && ((cr1its & USART_CR1_IDLEIE) != RESET)) {/ I# i W9 O( d6 S) [$ T9 r X __HAL_UART_CLEAR_IDLEFLAG(huart); __HAL_UART_DISABLE_IT(huart, UART_IT_IDLE); HAL_UART_RxIdleCallback(huart); s9 p0 `- Y. s Z1 W9 c; R! ^/ x return;$ u# s6 @3 W$ k2 o3 q4 Z } 以下__weak 函数也在stm32f4xx_hal_uart.c 中定义 /** * @brief Rx Idle callbacks. * @param huart: Pointer to a UART_HandleTypeDef structure that contains+ E8 E) r8 E/ X6 d K * the configuration information for the specified UART module. * @retval None */; L5 l) w. h) G8 ?) E __weak void HAL_UART_RxIdleCallback(UART_HandleTypeDef *huart)$ N0 j6 y- }7 z {% S4 m2 ^$ P' g& e /* Prevent unused argument(s) compilation warning */ UNUSED(huart);, p" f* `+ D# ` k7 h6 G: G- i) H /* NOTE: This function should not be modified, when the callback is needed,. s3 y' I, _3 X+ ] @ the HAL_UART_RxIdleCallback can be implemented in the user file */% t/ b8 z4 w: h, [6 F. n" F6 o& E S( b/ ] } 同时,stm32f4xx_hal_uart.h中增加如下函数声明,算是给最新的HAL库打补丁了。6 R% n' Y% U* q. Y void HAL_UART_RxIdleCallback(UART_HandleTypeDef *huart); : y& O: g/ R; [' O 最后,在生成的代码中,增加如下实际函数。 /* USER CODE BEGIN 1 */3 q4 {+ j7 Q S9 r /** * @brief Rx Transfer completed callbacks. * @param huart Pointer to a UART_HandleTypeDef structure that contains * the configuration information for the specified UART module. * @retval None */ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)# d' m4 W' `3 }% s& Y { if (huart->Instance == USART1) {3 l- V9 i( j$ @$ W2 W& o USART1_Rx_Buf[USART1_Rx_Count] = USART1_Rx_temp; HAL_UART_Receive_IT(huart, (uint8_t *)&USART1_Rx_temp, 1); if (++USART1_Rx_Count >= USART1_Rx_Buf_Size) {% G) _) d* S3 o, U+ Q% e USART1_Rx_Count = 0; 4 E5 K& J( O; f% i+ } } __HAL_UART_CLEAR_IDLEFLAG(&huart1);( M$ Z0 q6 s9 m% }4 G" @ __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);. ~6 {, p' I" @/ e( M }" a' J" k* E3 Y% ~ } /** * @brief Rx Idle callbacks. * @param huart: Pointer to a UART_HandleTypeDef structure that contains' `$ z4 U3 U2 k( | * the configuration information for the specified UART module. * @retval None */ void HAL_UART_RxIdleCallback(UART_HandleTypeDef *huart) {' X3 J+ I2 B& ^# u if (huart->Instance == USART1) { __HAL_UART_CLEAR_IDLEFLAG(huart); __HAL_UART_ENABLE_IT(huart, UART_IT_IDLE);; G* n$ P1 z. C8 F, A9 L if(USART1_Rx_Buf[1] == 0x55){/ s9 B0 U$ h* F, u) K& b& Q& p __NOP();; ]% {) V" g2 d- f0 R! P __NOP(); __NOP();2 P$ `& h$ r6 I s: W } else if(USART1_Rx_Buf[1] == 0xA5){ __NOP(); __NOP();/ ?* z' L) |5 y. F __NOP();6 Q. x8 M" g. V8 \# w } memset((uint8_t *)USART1_Rx_Buf, 0, 1024); USART1_Rx_Count = 0; }- H# S* m8 J) V2 E2 R$ K } /* USER CODE END 1 */ / J, v0 @! [* I' U4 z) A8 e 经测试,不管连发多少个字符,都会进空闲中断回调函数,这样应用程序就非常好处理了。" C. w. Z) D( F8 s$ ]3 M n/ g ! f! {( w! ~$ Y0 J, s/ {5 n3 v 2019/05/10 wjandsq@163.com : S. k2 A" P# J5 e$ F |
微信公众号
手机版
很好用,多谢分享