STM32G474xx HAL用户手册
stm32g4xx_hal_irda.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32g4xx_hal_irda.c
00004   * @author  MCD Application Team
00005   * @brief   IRDA HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the IrDA (Infrared Data Association) Peripheral
00008   *          (IRDA)
00009   *           + Initialization and de-initialization functions
00010   *           + IO operation functions
00011   *           + Peripheral State and Errors functions
00012   *           + Peripheral Control functions
00013   *
00014   ******************************************************************************
00015   * @attention
00016   *
00017   * Copyright (c) 2019 STMicroelectronics.
00018   * All rights reserved.
00019   *
00020   * This software is licensed under terms that can be found in the LICENSE file
00021   * in the root directory of this software component.
00022   * If no LICENSE file comes with this software, it is provided AS-IS.
00023   *
00024   ******************************************************************************
00025   @verbatim
00026   ==============================================================================
00027                         ##### How to use this driver #####
00028   ==============================================================================
00029   [..]
00030     The IRDA HAL driver can be used as follows:
00031 
00032     (#) Declare a IRDA_HandleTypeDef handle structure (eg. IRDA_HandleTypeDef hirda).
00033     (#) Initialize the IRDA low level resources by implementing the HAL_IRDA_MspInit() API
00034         in setting the associated USART or UART in IRDA mode:
00035         (++) Enable the USARTx/UARTx interface clock.
00036         (++) USARTx/UARTx pins configuration:
00037             (+++) Enable the clock for the USARTx/UARTx GPIOs.
00038             (+++) Configure these USARTx/UARTx pins (TX as alternate function pull-up, RX as alternate function Input).
00039         (++) NVIC configuration if you need to use interrupt process (HAL_IRDA_Transmit_IT()
00040              and HAL_IRDA_Receive_IT() APIs):
00041             (+++) Configure the USARTx/UARTx interrupt priority.
00042             (+++) Enable the NVIC USARTx/UARTx IRQ handle.
00043             (+++) The specific IRDA interrupts (Transmission complete interrupt,
00044                   RXNE interrupt and Error Interrupts) will be managed using the macros
00045                   __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
00046 
00047         (++) DMA Configuration if you need to use DMA process (HAL_IRDA_Transmit_DMA()
00048              and HAL_IRDA_Receive_DMA() APIs):
00049             (+++) Declare a DMA handle structure for the Tx/Rx channel.
00050             (+++) Enable the DMAx interface clock.
00051             (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
00052             (+++) Configure the DMA Tx/Rx channel.
00053             (+++) Associate the initialized DMA handle to the IRDA DMA Tx/Rx handle.
00054             (+++) Configure the priority and enable the NVIC for the transfer
00055                   complete interrupt on the DMA Tx/Rx channel.
00056 
00057     (#) Program the Baud Rate, Word Length and Parity and Mode(Receiver/Transmitter),
00058         the normal or low power mode and the clock prescaler in the hirda handle Init structure.
00059 
00060     (#) Initialize the IRDA registers by calling the HAL_IRDA_Init() API:
00061         (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
00062              by calling the customized HAL_IRDA_MspInit() API.
00063 
00064          -@@- The specific IRDA interrupts (Transmission complete interrupt,
00065              RXNE interrupt and Error Interrupts) will be managed using the macros
00066              __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
00067 
00068     (#) Three operation modes are available within this driver :
00069 
00070      *** Polling mode IO operation ***
00071      =================================
00072      [..]
00073        (+) Send an amount of data in blocking mode using HAL_IRDA_Transmit()
00074        (+) Receive an amount of data in blocking mode using HAL_IRDA_Receive()
00075 
00076      *** Interrupt mode IO operation ***
00077      ===================================
00078      [..]
00079        (+) Send an amount of data in non-blocking mode using HAL_IRDA_Transmit_IT()
00080        (+) At transmission end of transfer HAL_IRDA_TxCpltCallback() is executed and user can
00081             add his own code by customization of function pointer HAL_IRDA_TxCpltCallback()
00082        (+) Receive an amount of data in non-blocking mode using HAL_IRDA_Receive_IT()
00083        (+) At reception end of transfer HAL_IRDA_RxCpltCallback() is executed and user can
00084             add his own code by customization of function pointer HAL_IRDA_RxCpltCallback()
00085        (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
00086             add his own code by customization of function pointer HAL_IRDA_ErrorCallback()
00087 
00088      *** DMA mode IO operation ***
00089      ==============================
00090      [..]
00091        (+) Send an amount of data in non-blocking mode (DMA) using HAL_IRDA_Transmit_DMA()
00092        (+) At transmission half of transfer HAL_IRDA_TxHalfCpltCallback() is executed and user can
00093             add his own code by customization of function pointer HAL_IRDA_TxHalfCpltCallback()
00094        (+) At transmission end of transfer HAL_IRDA_TxCpltCallback() is executed and user can
00095             add his own code by customization of function pointer HAL_IRDA_TxCpltCallback()
00096        (+) Receive an amount of data in non-blocking mode (DMA) using HAL_IRDA_Receive_DMA()
00097        (+) At reception half of transfer HAL_IRDA_RxHalfCpltCallback() is executed and user can
00098             add his own code by customization of function pointer HAL_IRDA_RxHalfCpltCallback()
00099        (+) At reception end of transfer HAL_IRDA_RxCpltCallback() is executed and user can
00100             add his own code by customization of function pointer HAL_IRDA_RxCpltCallback()
00101        (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
00102             add his own code by customization of function pointer HAL_IRDA_ErrorCallback()
00103 
00104      *** IRDA HAL driver macros list ***
00105      ====================================
00106      [..]
00107        Below the list of most used macros in IRDA HAL driver.
00108 
00109        (+) __HAL_IRDA_ENABLE: Enable the IRDA peripheral
00110        (+) __HAL_IRDA_DISABLE: Disable the IRDA peripheral
00111        (+) __HAL_IRDA_GET_FLAG : Check whether the specified IRDA flag is set or not
00112        (+) __HAL_IRDA_CLEAR_FLAG : Clear the specified IRDA pending flag
00113        (+) __HAL_IRDA_ENABLE_IT: Enable the specified IRDA interrupt
00114        (+) __HAL_IRDA_DISABLE_IT: Disable the specified IRDA interrupt
00115        (+) __HAL_IRDA_GET_IT_SOURCE: Check whether or not the specified IRDA interrupt is enabled
00116 
00117      [..]
00118        (@) You can refer to the IRDA HAL driver header file for more useful macros
00119 
00120     ##### Callback registration #####
00121     ==================================
00122 
00123     [..]
00124     The compilation define USE_HAL_IRDA_REGISTER_CALLBACKS when set to 1
00125     allows the user to configure dynamically the driver callbacks.
00126 
00127     [..]
00128     Use Function HAL_IRDA_RegisterCallback() to register a user callback.
00129     Function HAL_IRDA_RegisterCallback() allows to register following callbacks:
00130     (+) TxHalfCpltCallback        : Tx Half Complete Callback.
00131     (+) TxCpltCallback            : Tx Complete Callback.
00132     (+) RxHalfCpltCallback        : Rx Half Complete Callback.
00133     (+) RxCpltCallback            : Rx Complete Callback.
00134     (+) ErrorCallback             : Error Callback.
00135     (+) AbortCpltCallback         : Abort Complete Callback.
00136     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
00137     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
00138     (+) MspInitCallback           : IRDA MspInit.
00139     (+) MspDeInitCallback         : IRDA MspDeInit.
00140     This function takes as parameters the HAL peripheral handle, the Callback ID
00141     and a pointer to the user callback function.
00142 
00143     [..]
00144     Use function HAL_IRDA_UnRegisterCallback() to reset a callback to the default
00145     weak function.
00146     HAL_IRDA_UnRegisterCallback() takes as parameters the HAL peripheral handle,
00147     and the Callback ID.
00148     This function allows to reset following callbacks:
00149     (+) TxHalfCpltCallback        : Tx Half Complete Callback.
00150     (+) TxCpltCallback            : Tx Complete Callback.
00151     (+) RxHalfCpltCallback        : Rx Half Complete Callback.
00152     (+) RxCpltCallback            : Rx Complete Callback.
00153     (+) ErrorCallback             : Error Callback.
00154     (+) AbortCpltCallback         : Abort Complete Callback.
00155     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
00156     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
00157     (+) MspInitCallback           : IRDA MspInit.
00158     (+) MspDeInitCallback         : IRDA MspDeInit.
00159 
00160     [..]
00161     By default, after the HAL_IRDA_Init() and when the state is HAL_IRDA_STATE_RESET
00162     all callbacks are set to the corresponding weak functions:
00163     examples HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxHalfCpltCallback().
00164     Exception done for MspInit and MspDeInit functions that are respectively
00165     reset to the legacy weak functions in the HAL_IRDA_Init()
00166     and HAL_IRDA_DeInit() only when these callbacks are null (not registered beforehand).
00167     If not, MspInit or MspDeInit are not null, the HAL_IRDA_Init() and HAL_IRDA_DeInit()
00168     keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
00169 
00170     [..]
00171     Callbacks can be registered/unregistered in HAL_IRDA_STATE_READY state only.
00172     Exception done MspInit/MspDeInit that can be registered/unregistered
00173     in HAL_IRDA_STATE_READY or HAL_IRDA_STATE_RESET state, thus registered (user)
00174     MspInit/DeInit callbacks can be used during the Init/DeInit.
00175     In that case first register the MspInit/MspDeInit user callbacks
00176     using HAL_IRDA_RegisterCallback() before calling HAL_IRDA_DeInit()
00177     or HAL_IRDA_Init() function.
00178 
00179     [..]
00180     When The compilation define USE_HAL_IRDA_REGISTER_CALLBACKS is set to 0 or
00181     not defined, the callback registration feature is not available
00182     and weak callbacks are used.
00183 
00184   @endverbatim
00185   ******************************************************************************
00186   */
00187 
00188 /* Includes ------------------------------------------------------------------*/
00189 #include "stm32g4xx_hal.h"
00190 
00191 /** @addtogroup STM32G4xx_HAL_Driver
00192   * @{
00193   */
00194 
00195 /** @defgroup IRDA IRDA
00196   * @brief HAL IRDA module driver
00197   * @{
00198   */
00199 
00200 #ifdef HAL_IRDA_MODULE_ENABLED
00201 
00202 /* Private typedef -----------------------------------------------------------*/
00203 /* Private define ------------------------------------------------------------*/
00204 /** @defgroup IRDA_Private_Constants IRDA Private Constants
00205   * @{
00206   */
00207 #define IRDA_TEACK_REACK_TIMEOUT            1000U                                   /*!< IRDA TX or RX enable acknowledge time-out value  */
00208 
00209 #define IRDA_CR1_FIELDS  ((uint32_t)(USART_CR1_M | USART_CR1_PCE \
00210                                      | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE))  /*!< UART or USART CR1 fields of parameters set by IRDA_SetConfig API */
00211 
00212 #define USART_BRR_MIN    0x10U        /*!< USART BRR minimum authorized value */
00213 
00214 #define USART_BRR_MAX    0x0000FFFFU  /*!< USART BRR maximum authorized value */
00215 /**
00216   * @}
00217   */
00218 
00219 /* Private macros ------------------------------------------------------------*/
00220 /** @defgroup IRDA_Private_Macros IRDA Private Macros
00221   * @{
00222   */
00223 /** @brief