STM32 串口固件庫中定義的幾個中斷標誌什麼意思?

STM32 串口固件庫中定義的幾個中斷標誌位什麼意思?

#define USART_IT_PE                       ((uint16_t)0x0028)
#define USART_IT_TXE                      ((uint16_t)0x0727)
#define USART_IT_TC                       ((uint16_t)0x0626)
#define USART_IT_RXNE                     ((uint16_t)0x0525)
#define USART_IT_IDLE                     ((uint16_t)0x0424)
#define USART_IT_LBD                      ((uint16_t)0x0846)
#define USART_IT_CTS                      ((uint16_t)0x096A)
#define USART_IT_ERR                      ((uint16_t)0x0060)
#define USART_IT_ORE                      ((uint16_t)0x0360)
#define USART_IT_NE                       ((uint16_t)0x0260)
#define USART_IT_FE                       ((uint16_t)0x0160)

可見,在stm32f10x_usart.h中宏定義的以上幾個宏,很沒有規律,咋一看還真不知道爲什麼會這麼定義,其實通過代碼就很容易明白:
D7~D5:代表中斷標誌位對應的中斷使能位在 CR1、CR2還是CR3寄存器中
D4~D0:代表中斷標誌位對應的中斷使能位在CRx寄存器的哪一位
D15~D8:代表中斷標誌位在SR寄存器中的哪一位

上面我們僅僅是給出了這個結論,其實僅僅是看上面的幾行宏定義是看不出來各位代表什麼意思的,只有從代碼中推理出來,下面給出stm32f10x_usart.c中USART_GetITStatus函數的實現就可以推理出上面爲什麼將上面的那幾個宏定義成那種形式了!
/**
  * @brief Checks whether the specified USART interrupt has occurred or not.
  * @param USARTx: Select the USART or the UART peripheral.
  * This parameter can be one of the following values:
  * USART1, USART2, USART3, UART4 or UART5.
  * @param USART_IT: specifies the USART interrupt source to check.
  * This parameter can be one of the following values:
  * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
  * @arg USART_IT_LBD: LIN Break detection interrupt
  * @arg USART_IT_TXE: Tansmit Data Register empty interrupt
  * @arg USART_IT_TC: Transmission complete interrupt
  * @arg USART_IT_RXNE: Receive Data register not empty interrupt
  * @arg USART_IT_IDLE: Idle line detection interrupt
  * @arg USART_IT_ORE: OverRun Error interrupt
  * @arg USART_IT_NE: Noise Error interrupt
  * @arg USART_IT_FE: Framing Error interrupt
  * @arg USART_IT_PE: Parity Error interrupt
  * @retval The new state of USART_IT (SET or RESET).
  */
ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
{
  uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
  ITStatus bitstatus = RESET;
  /* Check the parameters */
  assert_param(IS_USART_ALL_PERIPH(USARTx));
  assert_param(IS_USART_GET_IT(USART_IT));
  /* The CTS interrupt is not available for UART4 and UART5 */
  if (USART_IT == USART_IT_CTS)
  {
    assert_param(IS_USART_123_PERIPH(USARTx));
  }
 
  /* Get the USART register index */
  usartreg = (((uint8_t)USART_IT) >> 0x05);     //由此可見D7~D5:代表中斷標誌位對應的中斷使能位在 CR1、CR2還是CR3寄存器中
  /* Get the interrupt position */
  itmask = USART_IT & IT_Mask;
  itmask = (uint32_t)0x01 << itmask;    //由此可見D4~D0:代表中斷標誌位對應的中斷使能位在CRx寄存器的哪一位
 
  if (usartreg == 0x01) /* The IT is in CR1 register */
  {
    itmask &= USARTx->CR1;
  }
  else if (usartreg == 0x02) /* The IT is in CR2 register */
  {
    itmask &= USARTx->CR2;
  }
  else /* The IT is in CR3 register */
  {
    itmask &= USARTx->CR3;
  }
 
  bitpos = USART_IT >> 0x08;    //由此可見D15~D8:代表中斷標誌位在SR寄存器中的哪一位
  bitpos = (uint32_t)0x01 << bitpos;
  bitpos &= USARTx->SR;
  if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }
 
  return bitstatus;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章