STM32G474xx HAL 用户手册
stm32g4xx_hal_smartcard.c
跳转到此文件的文档。
00001 /**
00002   ******************************************************************************
00003   * @file    stm32g4xx_hal_smartcard.c
00004   * @author  MCD Application Team
00005   * @brief   SMARTCARD HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the SMARTCARD peripheral:
00008   *           + Initialization and de-initialization functions
00009   *           + IO operation functions
00010   *           + Peripheral Control functions
00011   *           + Peripheral State and Error functions
00012   *
00013   ******************************************************************************
00014   * @attention
00015   *
00016   * Copyright (c) 2019 STMicroelectronics.
00017   * All rights reserved.
00018   *
00019   * This software is licensed under terms that can be found in the LICENSE file
00020   * in the root directory of this software component.
00021   * If no LICENSE file comes with this software, it is provided AS-IS.
00022   *
00023   ******************************************************************************
00024   @verbatim
00025   ==============================================================================
00026                         ##### How to use this driver #####
00027   ==============================================================================
00028   [..]
00029     The SMARTCARD HAL driver can be used as follows:
00030 
00031     (#) Declare a SMARTCARD_HandleTypeDef handle structure (eg. SMARTCARD_HandleTypeDef hsmartcard).
00032     (#) Associate a USART to the SMARTCARD handle hsmartcard.
00033     (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
00034         (++) Enable the USARTx interface clock.
00035         (++) USART pins configuration:
00036              (+++) Enable the clock for the USART GPIOs.
00037              (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input).
00038         (++) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
00039              and HAL_SMARTCARD_Receive_IT() APIs):
00040              (+++) Configure the USARTx interrupt priority.
00041              (+++) Enable the NVIC USART IRQ handle.
00042         (++) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
00043              and HAL_SMARTCARD_Receive_DMA() APIs):
00044              (+++) Declare a DMA handle structure for the Tx/Rx channel.
00045              (+++) Enable the DMAx interface clock.
00046              (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
00047              (+++) Configure the DMA Tx/Rx channel.
00048              (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
00049              (+++) Configure the priority and enable the NVIC for the transfer complete
00050                    interrupt on the DMA Tx/Rx channel.
00051 
00052     (#) Program the Baud Rate, Parity, Mode(Receiver/Transmitter), clock enabling/disabling and accordingly,
00053         the clock parameters (parity, phase, last bit), prescaler value, guard time and NACK on transmission
00054         error enabling or disabling in the hsmartcard handle Init structure.
00055 
00056     (#) If required, program SMARTCARD advanced features (TX/RX pins swap, TimeOut, auto-retry counter,...)
00057         in the hsmartcard handle AdvancedInit structure.
00058 
00059     (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
00060         (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
00061              by calling the customized HAL_SMARTCARD_MspInit() API.
00062         [..]
00063         (@) The specific SMARTCARD interrupts (Transmission complete interrupt,
00064              RXNE interrupt and Error Interrupts) will be managed using the macros
00065              __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
00066 
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_SMARTCARD_Transmit()
00074        (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
00075 
00076      *** Interrupt mode IO operation ***
00077      ===================================
00078      [..]
00079        (+) Send an amount of data in non-blocking mode using HAL_SMARTCARD_Transmit_IT()
00080        (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can
00081             add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback()
00082        (+) Receive an amount of data in non-blocking mode using HAL_SMARTCARD_Receive_IT()
00083        (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can
00084             add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback()
00085        (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
00086             add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback()
00087 
00088      *** DMA mode IO operation ***
00089      ==============================
00090      [..]
00091        (+) Send an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
00092        (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can
00093             add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback()
00094        (+) Receive an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
00095        (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can
00096             add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback()
00097        (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
00098             add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback()
00099 
00100      *** SMARTCARD HAL driver macros list ***
00101      ========================================
00102      [..]
00103        Below the list of most used macros in SMARTCARD HAL driver.
00104 
00105        (+) __HAL_SMARTCARD_GET_FLAG : Check whether or not the specified SMARTCARD flag is set
00106        (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
00107        (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
00108        (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
00109        (+) __HAL_SMARTCARD_GET_IT_SOURCE: Check whether or not the specified SMARTCARD interrupt is enabled
00110 
00111      [..]
00112        (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
00113 
00114     ##### Callback registration #####
00115     ==================================
00116 
00117     [..]
00118     The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS when set to 1
00119     allows the user to configure dynamically the driver callbacks.
00120 
00121     [..]
00122     Use Function HAL_SMARTCARD_RegisterCallback() to register a user callback.
00123     Function HAL_SMARTCARD_RegisterCallback() allows to register following callbacks:
00124     (+) TxCpltCallback            : Tx Complete Callback.
00125     (+) RxCpltCallback            : Rx Complete Callback.
00126     (+) ErrorCallback             : Error Callback.
00127     (+) AbortCpltCallback         : Abort Complete Callback.
00128     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
00129     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
00130     (+) RxFifoFullCallback        : Rx Fifo Full Callback.
00131     (+) TxFifoEmptyCallback       : Tx Fifo Empty Callback.
00132     (+) MspInitCallback           : SMARTCARD MspInit.
00133     (+) MspDeInitCallback         : SMARTCARD MspDeInit.
00134     This function takes as parameters the HAL peripheral handle, the Callback ID
00135     and a pointer to the user callback function.
00136 
00137     [..]
00138     Use function HAL_SMARTCARD_UnRegisterCallback() to reset a callback to the default
00139     weak function.
00140     HAL_SMARTCARD_UnRegisterCallback() takes as parameters the HAL peripheral handle,
00141     and the Callback ID.
00142     This function allows to reset following callbacks:
00143     (+) TxCpltCallback            : Tx Complete Callback.
00144     (+) RxCpltCallback            : Rx Complete Callback.
00145     (+) ErrorCallback             : Error Callback.
00146     (+) AbortCpltCallback         : Abort Complete Callback.
00147     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
00148     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
00149     (+) RxFifoFullCallback        : Rx Fifo Full Callback.
00150     (+) TxFifoEmptyCallback       : Tx Fifo Empty Callback.
00151     (+) MspInitCallback           : SMARTCARD MspInit.
00152     (+) MspDeInitCallback         : SMARTCARD MspDeInit.
00153 
00154     [..]
00155     By default, after the HAL_SMARTCARD_Init() and when the state is HAL_SMARTCARD_STATE_RESET
00156     all callbacks are set to the corresponding weak functions:
00157     examples HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback().
00158     Exception done for MspInit and MspDeInit functions that are respectively
00159     reset to the legacy weak functions in the HAL_SMARTCARD_Init()
00160     and HAL_SMARTCARD_DeInit() only when these callbacks are null (not registered beforehand).
00161     If not, MspInit or MspDeInit are not null, the HAL_SMARTCARD_Init() and HAL_SMARTCARD_DeInit()
00162     keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
00163 
00164     [..]
00165     Callbacks can be registered/unregistered in HAL_SMARTCARD_STATE_READY state only.
00166     Exception done MspInit/MspDeInit that can be registered/unregistered
00167     in HAL_SMARTCARD_STATE_READY or HAL_SMARTCARD_STATE_RESET state, thus registered (user)
00168     MspInit/DeInit callbacks can be used during the Init/DeInit.
00169     In that case first register the MspInit/MspDeInit user callbacks
00170     using HAL_SMARTCARD_RegisterCallback() before calling HAL_SMARTCARD_DeInit()
00171     or HAL_SMARTCARD_Init() function.
00172 
00173     [..]
00174     When The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS is set to 0 or
00175     not defined, the callback registration feature is not available
00176     and weak callbacks are used.
00177 
00178 
00179   @endverbatim
00180   ******************************************************************************
00181   */
00182 
00183 /* Includes ------------------------------------------------------------------*/
00184 #include "stm32g4xx_hal.h"
00185 
00186 /** @addtogroup STM32G4xx_HAL_Driver
00187   * @{
00188   */
00189 
00190 /** @defgroup SMARTCARD SMARTCARD
00191   * @brief HAL SMARTCARD module driver
00192   * @{
00193   */
00194 
00195 #ifdef HAL_SMARTCARD_MODULE_ENABLED
00196 
00197 /* Private typedef -----------------------------------------------------------*/
00198 /* Private define ------------------------------------------------------------*/
00199 /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants
00200   * @{
00201   */
00202 #define SMARTCARD_TEACK_REACK_TIMEOUT  1000U       /*!< SMARTCARD TX or RX enable acknowledge time-out value */
00203 
00204 #define USART_CR1_FIELDS  ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
00205                                       USART_CR1_RE | USART_CR1