前言
STM32固件庫中提供了串口收發的標志位函數,包括USART_GetFlagStatus(…,…);和USART_GetITStatus(…,…);,兩者容易混淆,重點區別就在於:前者返回值是中斷標志位狀態(讀SR寄存器),后者返回值是中斷發生與否的判斷(讀CR寄存器),以下主要對這兩個函數進行分析。
一、USART_GETFlagStatus(…,…)
20 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG) 21 { 22 FlagStatus bitstatus = RESET; 23 /* Check the parameters */ 24 assert_param(IS_USART_ALL_PERIPH(USARTx)); 25 assert_param(IS_USART_FLAG(USART_FLAG)); 26 /* The CTS flag is not available for UART4 and UART5 */ 27 if (USART_FLAG == USART_FLAG_CTS) 28 { 29 assert_param(IS_USART_123_PERIPH(USARTx)); 30 } 31 32 if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET) 33 { 34 bitstatus = SET; 35 } 36 else 37 { 38 bitstatus = RESET; 39 } 40 return bitstatus; 41 }
1.代碼解析
該函數用於檢測串口中斷標志位的狀態。
其中,24、25、29三行用於檢測所用參數是否符合該函數的范圍。該函數的第一個形參只能是USART1,USART2,USART3,UART4,UART5,第二個形參只能是以下內容:
從32行開始,檢測SR寄存器的狀態,在SET和RESET中進行比較。
2.函數使用
函數返回值為SET或RESET,即可直接用於判斷
在沒有使能相應的中斷函數時,通常使用該函數來判斷標志位是否置位。
但串口觸發中斷后,需要清除標志位,由文章后部分描述
二、USART_GetITStatus(…,…)
20 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT) 21 { 22 uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00; 23 ITStatus bitstatus = RESET; 24 /* Check the parameters */ 25 assert_param(IS_USART_ALL_PERIPH(USARTx)); 26 assert_param(IS_USART_GET_IT(USART_IT)); 27 /* The CTS interrupt is not available for UART4 and UART5 */ 28 if (USART_IT == USART_IT_CTS) 29 { 30 assert_param(IS_USART_123_PERIPH(USARTx)); 31 } 32 33 /* Get the USART register index */ 34 usartreg = (((uint8_t)USART_IT) >> 0x05); 35 /* Get the interrupt position */ 36 itmask = USART_IT & IT_Mask; 37 itmask = (uint32_t)0x01 << itmask; 38 39 if (usartreg == 0x01) /* The IT is in CR1 register */ 40 { 41 itmask &= USARTx->CR1; 42 } 43 else if (usartreg == 0x02) /* The IT is in CR2 register */ 44 { 45 itmask &= USARTx->CR2; 46 } 47 else /* The IT is in CR3 register */ 48 { 49 itmask &= USARTx->CR3; 50 } 51 52 bitpos = USART_IT >> 0x08; 53 bitpos = (uint32_t)0x01 << bitpos; 54 bitpos &= USARTx->SR; 55 if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET)) 56 { 57 bitstatus = SET; 58 } 59 else 60 { 61 bitstatus = RESET; 62 } 63 64 return bitstatus; 65 }
1.代碼解析
代碼中分別讀取串口控制寄存器CR1,CR2,CR3的狀態,獲取中斷發生的動作,返回SET或RESET。
2.函數使用
函數返回值為SET或RESET,即可直接用於判斷。
除了可以判斷中斷標志位外,還能判斷是否發生了中斷。
但串口觸發中斷后,需要清除標志位,由文章后部分描述
三、 USART_ClearFlag(…,…)
27 void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG) 28 { 29 /* Check the parameters */ 30 assert_param(IS_USART_ALL_PERIPH(USARTx)); 31 assert_param(IS_USART_CLEAR_FLAG(USART_FLAG)); 32 /* The CTS flag is not available for UART4 and UART5 */ 33 if ((USART_FLAG & USART_FLAG_CTS) == USART_FLAG_CTS) 34 { 35 assert_param(IS_USART_123_PERIPH(USARTx)); 36 } 37 38 USARTx->SR = (uint16_t)~USART_FLAG; 39 }
1.代碼解析
該函數用於軟件清除標志位。
例如,常用的參數為USART_FLAG_RXNE,庫中定義的參數為0x0020,取反后為0xFFDF,恰好可以使SR寄存器的RXNE位置零(根據參考手冊)。同時根據函數note,USART_FLAG_RXNE也可以通過讀DR寄存器進行清位,即調用函數USART_ReceiveData();。
2.函數使用
可以用在中斷處理函數中對標志位進行清除操作。
四、USART_ClearITPendingBit(…,…)
28 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT) 29 { 30 uint16_t bitpos = 0x00, itmask = 0x00; 31 /* Check the parameters */ 32 assert_param(IS_USART_ALL_PERIPH(USARTx)); 33 assert_param(IS_USART_CLEAR_IT(USART_IT)); 34 /* The CTS interrupt is not available for UART4 and UART5 */ 35 if (USART_IT == USART_IT_CTS) 36 { 37 assert_param(IS_USART_123_PERIPH(USARTx)); 38 } 39 40 bitpos = USART_IT >> 0x08; 41 itmask = ((uint16_t)0x01 << (uint16_t)bitpos); 42 USARTx->SR = (uint16_t)~itmask; 43 }
該函數與USART_ClearFlag(…,…);功能相同,都是對SR寄存器某位進行清除操作,只是概念不一樣。
例如,常用的參數為USART_IT_RXNE,庫中定義的參數為0x0525,進入函數中itmask的值為0x20,取反為0xDF,恰好可以使SR寄存器的RXNE位置零(根據參考手冊)。同時根據函數note,USART_FLAG_RXNE也可以通過讀DR寄存器進行清位,即調用函數USART_ReceiveData();。
備注:
USART_ClearFlag(…,…);和USART_ClearITPendingBit(…,…);的函數說明里面都有各個參數的清位方式說明。