美文网首页工作生活
STM32 中 UART 和 USART 的区别

STM32 中 UART 和 USART 的区别

作者: dexfire | 来源:发表于2019-07-02 00:56 被阅读0次

    STM32 中 UART 和 USART 的区别

    概述

    UART:Universal asynchronous receiver-transmitter
    通用异步收发器

    USART:Universal synchronous and asynchronous receiver-transmitter
    通用同步/异步收发器

    HAL库 stm32f1xx_hal_uart.c 中对 UART 初始化描述
    /**
      ******************************************************************************
      * @file    stm32f1xx_hal_uart.c
      * @author  MCD Application Team
      * @brief   UART HAL module driver.
      *          This file provides firmware functions to manage the following
      *          functionalities of the Universal Asynchronous Receiver Transmitter (UART) peripheral:
      *           + Initialization and de-initialization functions
      *           + IO operation functions
      *           + Peripheral Control functions
      *           + Peripheral State and Errors functions
      @verbatim
      ==============================================================================
                            ##### How to use this driver #####
      ==============================================================================
      [..]
        The UART HAL driver can be used as follows:
    
        (#) Declare a UART_HandleTypeDef handle structure.
        (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
            (##) Enable the USARTx interface clock.
            (##) UART pins configuration:
                (+++) Enable the clock for the UART GPIOs.
                (+++) Configure the UART pins (TX as alternate function pull-up, RX as alternate function Input).
            (##) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
                 and HAL_UART_Receive_IT() APIs):
                (+++) Configure the USARTx interrupt priority.
                (+++) Enable the NVIC USART IRQ handle.
            (##) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
                 and HAL_UART_Receive_DMA() APIs):
                (+++) Declare a DMA handle structure for the Tx/Rx channel.
                (+++) Enable the DMAx interface clock.
                (+++) Configure the declared DMA handle structure with the required 
                      Tx/Rx parameters.                
                (+++) Configure the DMA Tx/Rx channel.
                (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
                (+++) Configure the priority and enable the NVIC for the transfer complete 
                      interrupt on the DMA Tx/Rx channel.
                (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
                      (used for last byte sending completion detection in DMA non circular mode)
    
        (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware 
            flow control and Mode(Receiver/Transmitter) in the huart Init structure.
    
        (#) For the UART asynchronous mode, initialize the UART registers by calling
            the HAL_UART_Init() API.
    
        (#) For the UART Half duplex mode, initialize the UART registers by calling 
            the HAL_HalfDuplex_Init() API.
    
        (#) For the LIN mode, initialize the UART registers by calling the HAL_LIN_Init() API.
    
        (#) For the Multi-Processor mode, initialize the UART registers by calling 
            the HAL_MultiProcessor_Init() API.
    
         [..] 
           (@) The specific UART interrupts (Transmission complete interrupt, 
                RXNE interrupt and Error Interrupts) will be managed using the macros
                __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the transmit 
                and receive process.
    
         [..] 
           (@) These APIs (HAL_UART_Init() and HAL_HalfDuplex_Init()) configure also the 
                low level Hardware GPIO, CLOCK, CORTEX...etc) by calling the customized
                HAL_UART_MspInit() API.
    
         [..]
            Three operation modes are available within this driver:
    
         *** Polling mode IO operation ***
         =================================
         [..]
           (+) Send an amount of data in blocking mode using HAL_UART_Transmit()
           (+) Receive an amount of data in blocking mode using HAL_UART_Receive()
    
         *** Interrupt mode IO operation ***
         ===================================
         [..]
           (+) Send an amount of data in non blocking mode using HAL_UART_Transmit_IT() 
           (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can 
                add his own code by customization of function pointer HAL_UART_TxCpltCallback
           (+) Receive an amount of data in non blocking mode using HAL_UART_Receive_IT() 
           (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can 
                add his own code by customization of function pointer HAL_UART_RxCpltCallback
           (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can 
                add his own code by customization of function pointer HAL_UART_ErrorCallback
    
         *** DMA mode IO operation ***
         ==============================
         [..] 
           (+) Send an amount of data in non blocking mode (DMA) using HAL_UART_Transmit_DMA() 
           (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can 
                add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback 
           (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can 
                add his own code by customization of function pointer HAL_UART_TxCpltCallback
           (+) Receive an amount of data in non blocking mode (DMA) using HAL_UART_Receive_DMA() 
           (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can 
                add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback 
           (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can 
                add his own code by customization of function pointer HAL_UART_RxCpltCallback
           (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can 
                add his own code by customization of function pointer HAL_UART_ErrorCallback
           (+) Pause the DMA Transfer using HAL_UART_DMAPause()
           (+) Resume the DMA Transfer using HAL_UART_DMAResume()
           (+) Stop the DMA Transfer using HAL_UART_DMAStop()
    
         *** UART HAL driver macros list ***
         =============================================
         [..]
           Below the list of most used macros in UART HAL driver.
    
          (+) __HAL_UART_ENABLE: Enable the UART peripheral 
          (+) __HAL_UART_DISABLE: Disable the UART peripheral
          (+) __HAL_UART_GET_FLAG : Check whether the specified UART flag is set or not
          (+) __HAL_UART_CLEAR_FLAG : Clear the specified UART pending flag
          (+) __HAL_UART_ENABLE_IT: Enable the specified UART interrupt
          (+) __HAL_UART_DISABLE_IT: Disable the specified UART interrupt
          (+) __HAL_UART_GET_IT_SOURCE: Check whether the specified UART interrupt has occurred or not
    
         [..]
           (@) You can refer to the UART HAL driver header file for more useful macros 
      @endverbatim
         [..]
           (@) Additionnal remark: If the parity is enabled, then the MSB bit of the data written
               in the data register is transmitted but is changed by the parity bit.
               Depending on the frame length defined by the M bit (8-bits or 9-bits),
               the possible UART frame formats are as listed in the following table:
        +-------------------------------------------------------------+
        |   M bit |  PCE bit  |            UART frame                 |
        |---------------------|---------------------------------------|
        |    0    |    0      |    | SB | 8 bit data | STB |          |
        |---------|-----------|---------------------------------------|
        |    0    |    1      |    | SB | 7 bit data | PB | STB |     |
        |---------|-----------|---------------------------------------|
        |    1    |    0      |    | SB | 9 bit data | STB |          |
        |---------|-----------|---------------------------------------|
        |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
        +-------------------------------------------------------------+
    */
    
    HAL库中 stm32f1xx_hal_usart.c 中对 USART 的描述
    /**
      ******************************************************************************
      * @file    stm32f1xx_hal_usart.c
      * @author  MCD Application Team
      * @brief   USART HAL module driver.
      *          This file provides firmware functions to manage the following
      *          functionalities of the Universal Synchronous Asynchronous Receiver Transmitter (USART) peripheral:
      *           + Initialization and de-initialization functions
      *           + IO operation functions
      *           + Peripheral Control functions
      @verbatim
      ==============================================================================
                            ##### How to use this driver #####
      ==============================================================================
      [..]
        The USART HAL driver can be used as follows:
    
        (#) Declare a USART_HandleTypeDef handle structure.
        (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit () API:
            (##) Enable the USARTx interface clock.
            (##) USART pins configuration:
                 (+++) Enable the clock for the USART GPIOs.
                 (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input).
            (##) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
                 HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
                 (+++) Configure the USARTx interrupt priority.
                 (+++) Enable the NVIC USART IRQ handle.
            (##) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA()
                 HAL_USART_Receive_DMA() and HAL_USART_TransmitReceive_DMA() APIs):
                 (+++) Declare a DMA handle structure for the Tx/Rx channel.
                 (+++) Enable the DMAx interface clock.
                 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
                 (+++) Configure the DMA Tx/Rx channel.
                 (+++) Associate the initialized DMA handle to the USART DMA Tx/Rx handle.
                 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
                (+++) Configure the priority and enable the NVIC for the transfer complete 
                      interrupt on the DMA Tx/Rx channel.
                (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
                      (used for last byte sending completion detection in DMA non circular mode)
    
        (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
            flow control and Mode(Receiver/Transmitter) in the husart Init structure.
    
        (#) Initialize the USART registers by calling the HAL_USART_Init() API:
            (++) These APIs configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
                 by calling the customized HAL_USART_MspInit(&husart) API.
    
            -@@- The specific USART interrupts (Transmission complete interrupt,
                 RXNE interrupt and Error Interrupts) will be managed using the macros
                 __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
    
        (#) Three operation modes are available within this driver :
    
         *** Polling mode IO operation ***
         =================================
         [..]
           (+) Send an amount of data in blocking mode using HAL_USART_Transmit()
           (+) Receive an amount of data in blocking mode using HAL_USART_Receive()
    
         *** Interrupt mode IO operation ***
         ===================================
         [..]
           (+) Send an amount of data in non blocking mode using HAL_USART_Transmit_IT()
           (+) At transmission end of transfer HAL_USART_TxHalfCpltCallback is executed and user can
                add his own code by customization of function pointer HAL_USART_TxCpltCallback
           (+) Receive an amount of data in non blocking mode using HAL_USART_Receive_IT()
           (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can
                add his own code by customization of function pointer HAL_USART_RxCpltCallback
           (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can
                add his own code by customization of function pointer HAL_USART_ErrorCallback
    
         *** DMA mode IO operation ***
         ==============================
         [..]
           (+) Send an amount of data in non blocking mode (DMA) using HAL_USART_Transmit_DMA()
           (+) At transmission end of half transfer HAL_USART_TxHalfCpltCallback is executed and user can
                add his own code by customization of function pointer HAL_USART_TxHalfCpltCallback
           (+) At transmission end of transfer HAL_USART_TxCpltCallback is executed and user can
                add his own code by customization of function pointer HAL_USART_TxCpltCallback
           (+) Receive an amount of data in non blocking mode (DMA) using HAL_USART_Receive_DMA()
           (+) At reception end of half transfer HAL_USART_RxHalfCpltCallback is executed and user can
                add his own code by customization of function pointer HAL_USART_RxHalfCpltCallback
           (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can
                add his own code by customization of function pointer HAL_USART_RxCpltCallback
           (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can
                add his own code by customization of function pointer HAL_USART_ErrorCallback
           (+) Pause the DMA Transfer using HAL_USART_DMAPause()
           (+) Resume the DMA Transfer using HAL_USART_DMAResume()
           (+) Stop the DMA Transfer using HAL_USART_DMAStop()
    
         *** USART HAL driver macros list ***
         =============================================
         [..]
           Below the list of most used macros in USART HAL driver.
    
           (+) __HAL_USART_ENABLE: Enable the USART peripheral
           (+) __HAL_USART_DISABLE: Disable the USART peripheral
           (+) __HAL_USART_GET_FLAG : Check whether the specified USART flag is set or not
           (+) __HAL_USART_CLEAR_FLAG : Clear the specified USART pending flag
           (+) __HAL_USART_ENABLE_IT: Enable the specified USART interrupt
           (+) __HAL_USART_DISABLE_IT: Disable the specified USART interrupt
    
         [..]
           (@) You can refer to the USART HAL driver header file for more useful macros
    
      @endverbatim
         [..]
           (@) Additionnal remark: If the parity is enabled, then the MSB bit of the data written
               in the data register is transmitted but is changed by the parity bit.
               Depending on the frame length defined by the M bit (8-bits or 9-bits),
               the possible USART frame formats are as listed in the following table:
        +-------------------------------------------------------------+
        |   M bit |  PCE bit  |            USART frame                 |
        |---------------------|---------------------------------------|
        |    0    |    0      |    | SB | 8 bit data | STB |          |
        |---------|-----------|---------------------------------------|
        |    0    |    1      |    | SB | 7 bit data | PB | STB |     |
        |---------|-----------|---------------------------------------|
        |    1    |    0      |    | SB | 9 bit data | STB |          |
        |---------|-----------|---------------------------------------|
        |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
        +-------------------------------------------------------------+
    */
    
    STM32F103RB_Nucleo 例程中的 UART 收发示例代码
    int main(void)
    {
      /* STM32F103xB HAL library initialization:
           - Configure the Flash prefetch
           - Systick timer is configured by default as source of time base, but user 
             can eventually implement his proper time base source (a general purpose 
             timer for example or other time source), keeping in mind that Time base 
             duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
             handled in milliseconds basis.
           - Set NVIC Group Priority to 4
           - Low Level Initialization
         */
      HAL_Init();
    
      /* Configure the system clock to 64 MHz */
      SystemClock_Config();
      
      /* Configure LED2 */
      BSP_LED_Init(LED2);
    
      /*##-1- Configure the UART peripheral ######################################*/
      /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
      /* UART configured as follows:
          - Word Length = 8 Bits
          - Stop Bit = One Stop bit
          - Parity = None
          - BaudRate = 9600 baud
          - Hardware flow control disabled (RTS and CTS signals) */
      UartHandle.Instance        = USARTx;
    
      UartHandle.Init.BaudRate     = 9600;
      UartHandle.Init.WordLength   = UART_WORDLENGTH_8B;
      UartHandle.Init.StopBits     = UART_STOPBITS_1;
      UartHandle.Init.Parity       = UART_PARITY_NONE;
      UartHandle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;
      UartHandle.Init.Mode         = UART_MODE_TX_RX;
      if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
      {
        Error_Handler();
      }  
      if(HAL_UART_Init(&UartHandle) != HAL_OK)
      {
        Error_Handler();
      }
      
    #ifdef TRANSMITTER_BOARD
    
      /* Configure User push-button in Interrupt mode */
      BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
      
      /* Wait for User push-button press before starting the Communication.
         In the meantime, LED2 is blinking */
      while(UserButtonStatus == 0)
      {
          /* Toggle LED2*/
          BSP_LED_Toggle(LED2); 
          HAL_Delay(100);
      }
      
      BSP_LED_Off(LED2); 
      
      
      /* The board sends the message and expects to receive it back */
      
      /*##-2- Start the transmission process #####################################*/  
      /* While the UART in reception process, user can transmit data through 
         "aTxBuffer" buffer */
      if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 5000)!= HAL_OK)
      {
        Error_Handler();   
      }
      
      
      /*##-3- Put UART peripheral in reception process ###########################*/  
      if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 5000) != HAL_OK)
      {
        Error_Handler();  
      }
       
     
    #else
      
      /* The board receives the message and sends it back */
    
      /*##-2- Put UART peripheral in reception process ###########################*/
      if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 0x1FFFFFF) != HAL_OK)
      {
        Error_Handler();
      }
     
      
      /*##-3- Start the transmission process #####################################*/  
      /* While the UART in reception process, user can transmit data through 
         "aTxBuffer" buffer */
      if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 5000)!= HAL_OK)
      {
        Error_Handler();
      }
      
      
    #endif /* TRANSMITTER_BOARD */
      
      /*##-4- Compare the sent and received buffers ##############################*/
      if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE))
      {
        Error_Handler();
      }
       
      /* Turn on LED2 if test passes then enter infinite loop */
      BSP_LED_On(LED2); 
      /* Infinite loop */
      while (1)
      {
      }
    }
    

    相关文章

      网友评论

        本文标题:STM32 中 UART 和 USART 的区别

        本文链接:https://www.haomeiwen.com/subject/lujncctx.html