STM32H735xx HAL User Manual
stm32h7xx_hal_smartcard_ex.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32h7xx_hal_smartcard_ex.c
00004   * @author  MCD Application Team
00005   * @brief   SMARTCARD HAL module driver.
00006   *          This file provides extended firmware functions to manage the following
00007   *          functionalities of the SmartCard.
00008   *           + Initialization and de-initialization functions
00009   *           + Peripheral Control functions
00010   *
00011   ******************************************************************************
00012   * @attention
00013   *
00014   * Copyright (c) 2017 STMicroelectronics.
00015   * All rights reserved.
00016   *
00017   * This software is licensed under terms that can be found in the LICENSE file
00018   * in the root directory of this software component.
00019   * If no LICENSE file comes with this software, it is provided AS-IS.
00020   *
00021   ******************************************************************************
00022   @verbatim
00023   =============================================================================
00024                ##### SMARTCARD peripheral extended features  #####
00025   =============================================================================
00026   [..]
00027   The Extended SMARTCARD HAL driver can be used as follows:
00028 
00029     (#) After having configured the SMARTCARD basic features with HAL_SMARTCARD_Init(),
00030         then program SMARTCARD advanced features if required (TX/RX pins swap, TimeOut,
00031         auto-retry counter,...) in the hsmartcard AdvancedInit structure.
00032 
00033     (#) FIFO mode enabling/disabling and RX/TX FIFO threshold programming.
00034 
00035         -@- When SMARTCARD operates in FIFO mode, FIFO mode must be enabled prior
00036             starting RX/TX transfers. Also RX/TX FIFO thresholds must be
00037             configured prior starting RX/TX transfers.
00038 
00039   @endverbatim
00040   ******************************************************************************
00041   */
00042 
00043 /* Includes ------------------------------------------------------------------*/
00044 #include "stm32h7xx_hal.h"
00045 
00046 /** @addtogroup STM32H7xx_HAL_Driver
00047   * @{
00048   */
00049 
00050 /** @defgroup SMARTCARDEx SMARTCARDEx
00051   * @brief SMARTCARD Extended HAL module driver
00052   * @{
00053   */
00054 #ifdef HAL_SMARTCARD_MODULE_ENABLED
00055 
00056 /* Private typedef -----------------------------------------------------------*/
00057 /* Private define ------------------------------------------------------------*/
00058 /** @defgroup SMARTCARDEx_Private_Constants SMARTCARD Extended Private Constants
00059   * @{
00060   */
00061 /* UART RX FIFO depth */
00062 #define RX_FIFO_DEPTH 16U
00063 
00064 /* UART TX FIFO depth */
00065 #define TX_FIFO_DEPTH 16U
00066 /**
00067   * @}
00068   */
00069 
00070 /* Private macros ------------------------------------------------------------*/
00071 /* Private variables ---------------------------------------------------------*/
00072 /* Private function prototypes -----------------------------------------------*/
00073 static void SMARTCARDEx_SetNbDataToProcess(SMARTCARD_HandleTypeDef *hsmartcard);
00074 
00075 /* Exported functions --------------------------------------------------------*/
00076 /** @defgroup SMARTCARDEx_Exported_Functions  SMARTCARD Extended Exported Functions
00077   * @{
00078   */
00079 
00080 /** @defgroup SMARTCARDEx_Exported_Functions_Group1 Extended Peripheral Control functions
00081   * @brief    Extended control functions
00082   *
00083 @verbatim
00084   ===============================================================================
00085                       ##### Peripheral Control functions #####
00086   ===============================================================================
00087   [..]
00088   This subsection provides a set of functions allowing to initialize the SMARTCARD.
00089      (+) HAL_SMARTCARDEx_BlockLength_Config() API allows to configure the Block Length on the fly
00090      (+) HAL_SMARTCARDEx_TimeOut_Config() API allows to configure the receiver timeout value on the fly
00091      (+) HAL_SMARTCARDEx_EnableReceiverTimeOut() API enables the receiver timeout feature
00092      (+) HAL_SMARTCARDEx_DisableReceiverTimeOut() API disables the receiver timeout feature
00093 
00094 @endverbatim
00095   * @{
00096   */
00097 
00098 /** @brief Update on the fly the SMARTCARD block length in RTOR register.
00099   * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
00100   *                    the configuration information for the specified SMARTCARD module.
00101   * @param BlockLength SMARTCARD block length (8-bit long at most)
00102   * @retval None
00103   */
00104 void HAL_SMARTCARDEx_BlockLength_Config(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t BlockLength)
00105 {
00106   MODIFY_REG(hsmartcard->Instance->RTOR, USART_RTOR_BLEN, ((uint32_t)BlockLength << USART_RTOR_BLEN_Pos));
00107 }
00108 
00109 /** @brief Update on the fly the receiver timeout value in RTOR register.
00110   * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
00111   *                    the configuration information for the specified SMARTCARD module.
00112   * @param TimeOutValue receiver timeout value in number of baud blocks. The timeout
00113   *                     value must be less or equal to 0x0FFFFFFFF.
00114   * @retval None
00115   */
00116 void HAL_SMARTCARDEx_TimeOut_Config(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t TimeOutValue)
00117 {
00118   assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue));
00119   MODIFY_REG(hsmartcard->Instance->RTOR, USART_RTOR_RTO, TimeOutValue);
00120 }
00121 
00122 /** @brief Enable the SMARTCARD receiver timeout feature.
00123   * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
00124   *                    the configuration information for the specified SMARTCARD module.
00125   * @retval HAL status
00126   */
00127 HAL_StatusTypeDef HAL_SMARTCARDEx_EnableReceiverTimeOut(SMARTCARD_HandleTypeDef *hsmartcard)
00128 {
00129   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
00130   {
00131     /* Process Locked */
00132     __HAL_LOCK(hsmartcard);
00133 
00134     hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
00135 
00136     /* Set the USART RTOEN bit */
00137     SET_BIT(hsmartcard->Instance->CR2, USART_CR2_RTOEN);
00138 
00139     hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
00140 
00141     /* Process Unlocked */
00142     __HAL_UNLOCK(hsmartcard);
00143 
00144     return HAL_OK;
00145   }
00146   else
00147   {
00148     return HAL_BUSY;
00149   }
00150 }
00151 
00152 /** @brief Disable the SMARTCARD receiver timeout feature.
00153   * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
00154   *                    the configuration information for the specified SMARTCARD module.
00155   * @retval HAL status
00156   */
00157 HAL_StatusTypeDef HAL_SMARTCARDEx_DisableReceiverTimeOut(SMARTCARD_HandleTypeDef *hsmartcard)
00158 {
00159   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
00160   {
00161     /* Process Locked */
00162     __HAL_LOCK(hsmartcard);
00163 
00164     hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
00165 
00166     /* Clear the USART RTOEN bit */
00167     CLEAR_BIT(hsmartcard->Instance->CR2, USART_CR2_RTOEN);
00168 
00169     hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
00170 
00171     /* Process Unlocked */
00172     __HAL_UNLOCK(hsmartcard);
00173 
00174     return HAL_OK;
00175   }
00176   else
00177   {
00178     return HAL_BUSY;
00179   }
00180 }
00181 
00182 /**
00183   * @}
00184   */
00185 
00186 /** @defgroup SMARTCARDEx_Exported_Functions_Group2 Extended Peripheral IO operation functions
00187   * @brief   SMARTCARD Transmit and Receive functions
00188   *
00189 @verbatim
00190  ===============================================================================
00191                       ##### IO operation functions #####
00192  ===============================================================================
00193     [..]
00194     This subsection provides a set of FIFO mode related callback functions.
00195 
00196     (#) TX/RX Fifos Callbacks:
00197         (++) HAL_SMARTCARDEx_RxFifoFullCallback()
00198         (++) HAL_SMARTCARDEx_TxFifoEmptyCallback()
00199 
00200 @endverbatim
00201   * @{
00202   */
00203 
00204 /**
00205   * @brief  SMARTCARD RX Fifo full callback.
00206   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
00207   *                   the configuration information for the specified SMARTCARD module.
00208   * @retval None
00209   */
00210 __weak void HAL_SMARTCARDEx_RxFifoFullCallback(SMARTCARD_HandleTypeDef *hsmartcard)
00211 {
00212   /* Prevent unused argument(s) compilation warning */
00213   UNUSED(hsmartcard);
00214 
00215   /* NOTE : This function should not be modified, when the callback is needed,
00216             the HAL_SMARTCARDEx_RxFifoFullCallback can be implemented in the user file.
00217    */
00218 }
00219 
00220 /**
00221   * @brief  SMARTCARD TX Fifo empty callback.
00222   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
00223   *                   the configuration information for the specified SMARTCARD module.
00224   * @retval None
00225   */
00226 __weak void HAL_SMARTCARDEx_TxFifoEmptyCallback(SMARTCARD_HandleTypeDef *hsmartcard)
00227 {
00228   /* Prevent unused argument(s) compilation warning */
00229   UNUSED(hsmartcard);
00230 
00231   /* NOTE : This function should not be modified, when the callback is needed,
00232             the HAL_SMARTCARDEx_TxFifoEmptyCallback can be implemented in the user file.
00233    */
00234 }
00235 
00236 /**
00237   * @}
00238   */
00239 
00240 /** @defgroup SMARTCARDEx_Exported_Functions_Group3 Extended Peripheral FIFO Control functions
00241   *  @brief   SMARTCARD control functions
00242   *
00243 @verbatim
00244  ===============================================================================
00245                   ##### Peripheral FIFO Control functions #####
00246  ===============================================================================
00247     [..]
00248     This subsection provides a set of functions allowing to control the SMARTCARD
00249     FIFO feature.
00250      (+) HAL_SMARTCARDEx_EnableFifoMode() API enables the FIFO mode
00251      (+) HAL_SMARTCARDEx_DisableFifoMode() API disables the FIFO mode
00252      (+) HAL_SMARTCARDEx_SetTxFifoThreshold() API sets the TX FIFO threshold
00253      (+) HAL_SMARTCARDEx_SetRxFifoThreshold() API sets the RX FIFO threshold
00254 @endverbatim
00255   * @{
00256   */
00257 
00258 /**
00259   * @brief  Enable the FIFO mode.
00260   * @param hsmartcard SMARTCARD handle.
00261   * @retval HAL status
00262   */
00263 HAL_StatusTypeDef HAL_SMARTCARDEx_EnableFifoMode(SMARTCARD_HandleTypeDef *hsmartcard)
00264 {
00265   uint32_t tmpcr1;
00266 
00267   /* Check parameters */
00268   assert_param(IS_UART_FIFO_INSTANCE(hsmartcard->Instance));
00269 
00270   /* Process Locked */
00271   __HAL_LOCK(hsmartcard);
00272 
00273   hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
00274 
00275   /* Save actual SMARTCARD configuration */
00276   tmpcr1 = READ_REG(hsmartcard->Instance->CR1);
00277 
00278   /* Disable SMARTCARD */
00279   __HAL_SMARTCARD_DISABLE(hsmartcard);
00280 
00281   /* Enable FIFO mode */
00282   SET_BIT(tmpcr1, USART_CR1_FIFOEN);
00283   hsmartcard->FifoMode = SMARTCARD_FIFOMODE_ENABLE;
00284 
00285   /* Restore SMARTCARD configuration */
00286   WRITE_REG(hsmartcard->Instance->CR1, tmpcr1);
00287 
00288   /* Determine the number of data to process during RX/TX ISR execution */
00289   SMARTCARDEx_SetNbDataToProcess(hsmartcard);
00290 
00291   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
00292 
00293   /* Process Unlocked */
00294   __HAL_UNLOCK(hsmartcard);
00295 
00296   return HAL_OK;
00297 }
00298 
00299 /**
00300   * @brief  Disable the FIFO mode.
00301   * @param hsmartcard SMARTCARD handle.
00302   * @retval HAL status
00303   */
00304 HAL_StatusTypeDef HAL_SMARTCARDEx_DisableFifoMode(SMARTCARD_HandleTypeDef *hsmartcard)
00305 {
00306   uint32_t tmpcr1;
00307 
00308   /* Check parameters */
00309   assert_param(IS_UART_FIFO_INSTANCE(hsmartcard->Instance));
00310 
00311   /* Process Locked */
00312   __HAL_LOCK(hsmartcard);
00313 
00314   hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
00315 
00316   /* Save actual SMARTCARD configuration */
00317   tmpcr1 = READ_REG(hsmartcard->Instance->CR1);
00318 
00319   /* Disable SMARTCARD */
00320   __HAL_SMARTCARD_DISABLE(hsmartcard);
00321 
00322   /* Enable FIFO mode */
00323   CLEAR_BIT(tmpcr1, USART_CR1_FIFOEN);
00324   hsmartcard->FifoMode = SMARTCARD_FIFOMODE_DISABLE;
00325 
00326   /* Restore SMARTCARD configuration */
00327   WRITE_REG(hsmartcard->Instance->CR1, tmpcr1);
00328 
00329   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
00330 
00331   /* Process Unlocked */
00332   __HAL_UNLOCK(hsmartcard);
00333 
00334   return HAL_OK;
00335 }
00336 
00337 /**
00338   * @brief  Set the TXFIFO threshold.
00339   * @param hsmartcard      SMARTCARD handle.
00340   * @param Threshold  TX FIFO threshold value
00341   *          This parameter can be one of the following values:
00342   *            @arg @ref SMARTCARD_TXFIFO_THRESHOLD_1_8
00343   *            @arg @ref SMARTCARD_TXFIFO_THRESHOLD_1_4
00344   *            @arg @ref SMARTCARD_TXFIFO_THRESHOLD_1_2
00345   *            @arg @ref SMARTCARD_TXFIFO_THRESHOLD_3_4
00346   *            @arg @ref SMARTCARD_TXFIFO_THRESHOLD_7_8
00347   *            @arg @ref SMARTCARD_TXFIFO_THRESHOLD_8_8
00348   * @retval HAL status
00349   */
00350 HAL_StatusTypeDef HAL_SMARTCARDEx_SetTxFifoThreshold(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Threshold)
00351 {
00352   uint32_t tmpcr1;
00353 
00354   /* Check parameters */
00355   assert_param(IS_UART_FIFO_INSTANCE(hsmartcard->Instance));
00356   assert_param(IS_SMARTCARD_TXFIFO_THRESHOLD(Threshold));
00357 
00358   /* Process Locked */
00359   __HAL_LOCK(hsmartcard);
00360 
00361   hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
00362 
00363   /* Save actual SMARTCARD configuration */
00364   tmpcr1 = READ_REG(hsmartcard->Instance->CR1);
00365 
00366   /* Disable SMARTCARD */
00367   __HAL_SMARTCARD_DISABLE(hsmartcard);
00368 
00369   /* Update TX threshold configuration */
00370   MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_TXFTCFG, Threshold);
00371 
00372   /* Determine the number of data to process during RX/TX ISR execution */
00373   SMARTCARDEx_SetNbDataToProcess(hsmartcard);
00374 
00375   /* Restore SMARTCARD configuration */
00376   MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_UE, tmpcr1);
00377 
00378   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
00379 
00380   /* Process Unlocked */
00381   __HAL_UNLOCK(hsmartcard);
00382 
00383   return HAL_OK;
00384 }
00385 
00386 /**
00387   * @brief  Set the RXFIFO threshold.
00388   * @param hsmartcard      SMARTCARD handle.
00389   * @param Threshold  RX FIFO threshold value
00390   *          This parameter can be one of the following values:
00391   *            @arg @ref SMARTCARD_RXFIFO_THRESHOLD_1_8
00392   *            @arg @ref SMARTCARD_RXFIFO_THRESHOLD_1_4
00393   *            @arg @ref SMARTCARD_RXFIFO_THRESHOLD_1_2
00394   *            @arg @ref SMARTCARD_RXFIFO_THRESHOLD_3_4
00395   *            @arg @ref SMARTCARD_RXFIFO_THRESHOLD_7_8
00396   *            @arg @ref SMARTCARD_RXFIFO_THRESHOLD_8_8
00397   * @retval HAL status
00398   */
00399 HAL_StatusTypeDef HAL_SMARTCARDEx_SetRxFifoThreshold(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Threshold)
00400 {
00401   uint32_t tmpcr1;
00402 
00403   /* Check parameters */
00404   assert_param(IS_UART_FIFO_INSTANCE(hsmartcard->Instance));
00405   assert_param(IS_SMARTCARD_RXFIFO_THRESHOLD(Threshold));
00406 
00407   /* Process Locked */
00408   __HAL_LOCK(hsmartcard);
00409 
00410   hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
00411 
00412   /* Save actual SMARTCARD configuration */
00413   tmpcr1 = READ_REG(hsmartcard->Instance->CR1);
00414 
00415   /* Disable SMARTCARD */
00416   __HAL_SMARTCARD_DISABLE(hsmartcard);
00417 
00418   /* Update RX threshold configuration */
00419   MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_RXFTCFG, Threshold);
00420 
00421   /* Determine the number of data to process during RX/TX ISR execution */
00422   SMARTCARDEx_SetNbDataToProcess(hsmartcard);
00423 
00424   /* Restore SMARTCARD configuration */
00425   MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_UE, tmpcr1);
00426 
00427   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
00428 
00429   /* Process Unlocked */
00430   __HAL_UNLOCK(hsmartcard);
00431 
00432   return HAL_OK;
00433 }
00434 
00435 /**
00436   * @}
00437   */
00438 
00439 /**
00440   * @}
00441   */
00442 
00443 /** @defgroup SMARTCARDEx_Private_Functions  SMARTCARD Extended Private Functions
00444   * @{
00445   */
00446 
00447 /**
00448   * @brief Calculate the number of data to process in RX/TX ISR.
00449   * @note The RX FIFO depth and the TX FIFO depth is extracted from
00450   *       the USART configuration registers.
00451   * @param hsmartcard SMARTCARD handle.
00452   * @retval None
00453   */
00454 static void SMARTCARDEx_SetNbDataToProcess(SMARTCARD_HandleTypeDef *hsmartcard)
00455 {
00456   uint8_t rx_fifo_depth;
00457   uint8_t tx_fifo_depth;
00458   uint8_t rx_fifo_threshold;
00459   uint8_t tx_fifo_threshold;
00460   /* 2 0U/1U added for MISRAC2012-Rule-18.1_b and MISRAC2012-Rule-18.1_d */
00461   static const uint8_t numerator[]   = {1U, 1U, 1U, 3U, 7U, 1U, 0U, 0U};
00462   static const uint8_t denominator[] = {8U, 4U, 2U, 4U, 8U, 1U, 1U, 1U};
00463 
00464   if (hsmartcard->FifoMode == SMARTCARD_FIFOMODE_DISABLE)
00465   {
00466     hsmartcard->NbTxDataToProcess = 1U;
00467     hsmartcard->NbRxDataToProcess = 1U;
00468   }
00469   else
00470   {
00471     rx_fifo_depth = RX_FIFO_DEPTH;
00472     tx_fifo_depth = TX_FIFO_DEPTH;
00473     rx_fifo_threshold = (uint8_t)(READ_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTCFG) >> USART_CR3_RXFTCFG_Pos);
00474     tx_fifo_threshold = (uint8_t)(READ_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTCFG) >> USART_CR3_TXFTCFG_Pos);
00475     hsmartcard->NbTxDataToProcess = ((uint16_t)tx_fifo_depth * numerator[tx_fifo_threshold]) / \
00476                                     (uint16_t)denominator[tx_fifo_threshold];
00477     hsmartcard->NbRxDataToProcess = ((uint16_t)rx_fifo_depth * numerator[rx_fifo_threshold]) / \
00478                                     (uint16_t)denominator[rx_fifo_threshold];
00479   }
00480 }
00481 
00482 /**
00483   * @}
00484   */
00485 
00486 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
00487 
00488 /**
00489   * @}
00490   */
00491 
00492 /**
00493   * @}
00494   */
00495