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