stm32l4xx_hal_smartcard_ex.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_smartcard_ex.c
  4. * @author MCD Application Team
  5. * @brief SMARTCARD HAL module driver.
  6. * This file provides extended firmware functions to manage the following
  7. * functionalities of the SmartCard.
  8. * + Initialization and de-initialization functions
  9. * + Peripheral Control functions
  10. *
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * Copyright (c) 2017 STMicroelectronics.
  15. * All rights reserved.
  16. *
  17. * This software is licensed under terms that can be found in the LICENSE file
  18. * in the root directory of this software component.
  19. * If no LICENSE file comes with this software, it is provided AS-IS.
  20. *
  21. ******************************************************************************
  22. @verbatim
  23. =============================================================================
  24. ##### SMARTCARD peripheral extended features #####
  25. =============================================================================
  26. [..]
  27. The Extended SMARTCARD HAL driver can be used as follows:
  28. (#) After having configured the SMARTCARD basic features with HAL_SMARTCARD_Init(),
  29. then program SMARTCARD advanced features if required (TX/RX pins swap, TimeOut,
  30. auto-retry counter,...) in the hsmartcard AdvancedInit structure.
  31. (#) FIFO mode enabling/disabling and RX/TX FIFO threshold programming.
  32. -@- When SMARTCARD operates in FIFO mode, FIFO mode must be enabled prior
  33. starting RX/TX transfers. Also RX/TX FIFO thresholds must be
  34. configured prior starting RX/TX transfers.
  35. @endverbatim
  36. ******************************************************************************
  37. */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "stm32l4xx_hal.h"
  40. /** @addtogroup STM32L4xx_HAL_Driver
  41. * @{
  42. */
  43. /** @defgroup SMARTCARDEx SMARTCARDEx
  44. * @brief SMARTCARD Extended HAL module driver
  45. * @{
  46. */
  47. #ifdef HAL_SMARTCARD_MODULE_ENABLED
  48. /* Private typedef -----------------------------------------------------------*/
  49. /* Private define ------------------------------------------------------------*/
  50. /** @defgroup SMARTCARDEx_Private_Constants SMARTCARD Extended Private Constants
  51. * @{
  52. */
  53. /* UART RX FIFO depth */
  54. #define RX_FIFO_DEPTH 8U
  55. /* UART TX FIFO depth */
  56. #define TX_FIFO_DEPTH 8U
  57. /**
  58. * @}
  59. */
  60. /* Private macros ------------------------------------------------------------*/
  61. /* Private variables ---------------------------------------------------------*/
  62. /* Private function prototypes -----------------------------------------------*/
  63. #if defined(USART_CR1_FIFOEN)
  64. static void SMARTCARDEx_SetNbDataToProcess(SMARTCARD_HandleTypeDef *hsmartcard);
  65. #endif /* USART_CR1_FIFOEN */
  66. /* Exported functions --------------------------------------------------------*/
  67. /** @defgroup SMARTCARDEx_Exported_Functions SMARTCARD Extended Exported Functions
  68. * @{
  69. */
  70. /** @defgroup SMARTCARDEx_Exported_Functions_Group1 Extended Peripheral Control functions
  71. * @brief Extended control functions
  72. *
  73. @verbatim
  74. ===============================================================================
  75. ##### Peripheral Control functions #####
  76. ===============================================================================
  77. [..]
  78. This subsection provides a set of functions allowing to initialize the SMARTCARD.
  79. (+) HAL_SMARTCARDEx_BlockLength_Config() API allows to configure the Block Length on the fly
  80. (+) HAL_SMARTCARDEx_TimeOut_Config() API allows to configure the receiver timeout value on the fly
  81. (+) HAL_SMARTCARDEx_EnableReceiverTimeOut() API enables the receiver timeout feature
  82. (+) HAL_SMARTCARDEx_DisableReceiverTimeOut() API disables the receiver timeout feature
  83. @endverbatim
  84. * @{
  85. */
  86. /** @brief Update on the fly the SMARTCARD block length in RTOR register.
  87. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  88. * the configuration information for the specified SMARTCARD module.
  89. * @param BlockLength SMARTCARD block length (8-bit long at most)
  90. * @retval None
  91. */
  92. void HAL_SMARTCARDEx_BlockLength_Config(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t BlockLength)
  93. {
  94. MODIFY_REG(hsmartcard->Instance->RTOR, USART_RTOR_BLEN, ((uint32_t)BlockLength << USART_RTOR_BLEN_Pos));
  95. }
  96. /** @brief Update on the fly the receiver timeout value in RTOR register.
  97. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  98. * the configuration information for the specified SMARTCARD module.
  99. * @param TimeOutValue receiver timeout value in number of baud blocks. The timeout
  100. * value must be less or equal to 0x0FFFFFFFF.
  101. * @retval None
  102. */
  103. void HAL_SMARTCARDEx_TimeOut_Config(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t TimeOutValue)
  104. {
  105. assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue));
  106. MODIFY_REG(hsmartcard->Instance->RTOR, USART_RTOR_RTO, TimeOutValue);
  107. }
  108. /** @brief Enable the SMARTCARD receiver timeout feature.
  109. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  110. * the configuration information for the specified SMARTCARD module.
  111. * @retval HAL status
  112. */
  113. HAL_StatusTypeDef HAL_SMARTCARDEx_EnableReceiverTimeOut(SMARTCARD_HandleTypeDef *hsmartcard)
  114. {
  115. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  116. {
  117. /* Process Locked */
  118. __HAL_LOCK(hsmartcard);
  119. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  120. /* Set the USART RTOEN bit */
  121. SET_BIT(hsmartcard->Instance->CR2, USART_CR2_RTOEN);
  122. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  123. /* Process Unlocked */
  124. __HAL_UNLOCK(hsmartcard);
  125. return HAL_OK;
  126. }
  127. else
  128. {
  129. return HAL_BUSY;
  130. }
  131. }
  132. /** @brief Disable the SMARTCARD receiver timeout feature.
  133. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  134. * the configuration information for the specified SMARTCARD module.
  135. * @retval HAL status
  136. */
  137. HAL_StatusTypeDef HAL_SMARTCARDEx_DisableReceiverTimeOut(SMARTCARD_HandleTypeDef *hsmartcard)
  138. {
  139. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  140. {
  141. /* Process Locked */
  142. __HAL_LOCK(hsmartcard);
  143. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  144. /* Clear the USART RTOEN bit */
  145. CLEAR_BIT(hsmartcard->Instance->CR2, USART_CR2_RTOEN);
  146. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  147. /* Process Unlocked */
  148. __HAL_UNLOCK(hsmartcard);
  149. return HAL_OK;
  150. }
  151. else
  152. {
  153. return HAL_BUSY;
  154. }
  155. }
  156. /**
  157. * @}
  158. */
  159. /** @defgroup SMARTCARDEx_Exported_Functions_Group2 Extended Peripheral IO operation functions
  160. * @brief SMARTCARD Transmit and Receive functions
  161. *
  162. @verbatim
  163. ===============================================================================
  164. ##### IO operation functions #####
  165. ===============================================================================
  166. [..]
  167. This subsection provides a set of FIFO mode related callback functions.
  168. (#) TX/RX Fifos Callbacks:
  169. (++) HAL_SMARTCARDEx_RxFifoFullCallback()
  170. (++) HAL_SMARTCARDEx_TxFifoEmptyCallback()
  171. @endverbatim
  172. * @{
  173. */
  174. #if defined(USART_CR1_FIFOEN)
  175. /**
  176. * @brief SMARTCARD RX Fifo full callback.
  177. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  178. * the configuration information for the specified SMARTCARD module.
  179. * @retval None
  180. */
  181. __weak void HAL_SMARTCARDEx_RxFifoFullCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  182. {
  183. /* Prevent unused argument(s) compilation warning */
  184. UNUSED(hsmartcard);
  185. /* NOTE : This function should not be modified, when the callback is needed,
  186. the HAL_SMARTCARDEx_RxFifoFullCallback can be implemented in the user file.
  187. */
  188. }
  189. /**
  190. * @brief SMARTCARD TX Fifo empty callback.
  191. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  192. * the configuration information for the specified SMARTCARD module.
  193. * @retval None
  194. */
  195. __weak void HAL_SMARTCARDEx_TxFifoEmptyCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  196. {
  197. /* Prevent unused argument(s) compilation warning */
  198. UNUSED(hsmartcard);
  199. /* NOTE : This function should not be modified, when the callback is needed,
  200. the HAL_SMARTCARDEx_TxFifoEmptyCallback can be implemented in the user file.
  201. */
  202. }
  203. #endif /* USART_CR1_FIFOEN */
  204. /**
  205. * @}
  206. */
  207. /** @defgroup SMARTCARDEx_Exported_Functions_Group3 Extended Peripheral FIFO Control functions
  208. * @brief SMARTCARD control functions
  209. *
  210. @verbatim
  211. ===============================================================================
  212. ##### Peripheral FIFO Control functions #####
  213. ===============================================================================
  214. [..]
  215. This subsection provides a set of functions allowing to control the SMARTCARD
  216. FIFO feature.
  217. (+) HAL_SMARTCARDEx_EnableFifoMode() API enables the FIFO mode
  218. (+) HAL_SMARTCARDEx_DisableFifoMode() API disables the FIFO mode
  219. (+) HAL_SMARTCARDEx_SetTxFifoThreshold() API sets the TX FIFO threshold
  220. (+) HAL_SMARTCARDEx_SetRxFifoThreshold() API sets the RX FIFO threshold
  221. @endverbatim
  222. * @{
  223. */
  224. #if defined(USART_CR1_FIFOEN)
  225. /**
  226. * @brief Enable the FIFO mode.
  227. * @param hsmartcard SMARTCARD handle.
  228. * @retval HAL status
  229. */
  230. HAL_StatusTypeDef HAL_SMARTCARDEx_EnableFifoMode(SMARTCARD_HandleTypeDef *hsmartcard)
  231. {
  232. uint32_t tmpcr1;
  233. /* Check parameters */
  234. assert_param(IS_UART_FIFO_INSTANCE(hsmartcard->Instance));
  235. /* Process Locked */
  236. __HAL_LOCK(hsmartcard);
  237. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  238. /* Save actual SMARTCARD configuration */
  239. tmpcr1 = READ_REG(hsmartcard->Instance->CR1);
  240. /* Disable SMARTCARD */
  241. __HAL_SMARTCARD_DISABLE(hsmartcard);
  242. /* Enable FIFO mode */
  243. SET_BIT(tmpcr1, USART_CR1_FIFOEN);
  244. hsmartcard->FifoMode = SMARTCARD_FIFOMODE_ENABLE;
  245. /* Restore SMARTCARD configuration */
  246. WRITE_REG(hsmartcard->Instance->CR1, tmpcr1);
  247. /* Determine the number of data to process during RX/TX ISR execution */
  248. SMARTCARDEx_SetNbDataToProcess(hsmartcard);
  249. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  250. /* Process Unlocked */
  251. __HAL_UNLOCK(hsmartcard);
  252. return HAL_OK;
  253. }
  254. /**
  255. * @brief Disable the FIFO mode.
  256. * @param hsmartcard SMARTCARD handle.
  257. * @retval HAL status
  258. */
  259. HAL_StatusTypeDef HAL_SMARTCARDEx_DisableFifoMode(SMARTCARD_HandleTypeDef *hsmartcard)
  260. {
  261. uint32_t tmpcr1;
  262. /* Check parameters */
  263. assert_param(IS_UART_FIFO_INSTANCE(hsmartcard->Instance));
  264. /* Process Locked */
  265. __HAL_LOCK(hsmartcard);
  266. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  267. /* Save actual SMARTCARD configuration */
  268. tmpcr1 = READ_REG(hsmartcard->Instance->CR1);
  269. /* Disable SMARTCARD */
  270. __HAL_SMARTCARD_DISABLE(hsmartcard);
  271. /* Enable FIFO mode */
  272. CLEAR_BIT(tmpcr1, USART_CR1_FIFOEN);
  273. hsmartcard->FifoMode = SMARTCARD_FIFOMODE_DISABLE;
  274. /* Restore SMARTCARD configuration */
  275. WRITE_REG(hsmartcard->Instance->CR1, tmpcr1);
  276. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  277. /* Process Unlocked */
  278. __HAL_UNLOCK(hsmartcard);
  279. return HAL_OK;
  280. }
  281. /**
  282. * @brief Set the TXFIFO threshold.
  283. * @param hsmartcard SMARTCARD handle.
  284. * @param Threshold TX FIFO threshold value
  285. * This parameter can be one of the following values:
  286. * @arg @ref SMARTCARD_TXFIFO_THRESHOLD_1_8
  287. * @arg @ref SMARTCARD_TXFIFO_THRESHOLD_1_4
  288. * @arg @ref SMARTCARD_TXFIFO_THRESHOLD_1_2
  289. * @arg @ref SMARTCARD_TXFIFO_THRESHOLD_3_4
  290. * @arg @ref SMARTCARD_TXFIFO_THRESHOLD_7_8
  291. * @arg @ref SMARTCARD_TXFIFO_THRESHOLD_8_8
  292. * @retval HAL status
  293. */
  294. HAL_StatusTypeDef HAL_SMARTCARDEx_SetTxFifoThreshold(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Threshold)
  295. {
  296. uint32_t tmpcr1;
  297. /* Check parameters */
  298. assert_param(IS_UART_FIFO_INSTANCE(hsmartcard->Instance));
  299. assert_param(IS_SMARTCARD_TXFIFO_THRESHOLD(Threshold));
  300. /* Process Locked */
  301. __HAL_LOCK(hsmartcard);
  302. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  303. /* Save actual SMARTCARD configuration */
  304. tmpcr1 = READ_REG(hsmartcard->Instance->CR1);
  305. /* Disable SMARTCARD */
  306. __HAL_SMARTCARD_DISABLE(hsmartcard);
  307. /* Update TX threshold configuration */
  308. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_TXFTCFG, Threshold);
  309. /* Determine the number of data to process during RX/TX ISR execution */
  310. SMARTCARDEx_SetNbDataToProcess(hsmartcard);
  311. /* Restore SMARTCARD configuration */
  312. MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_UE, tmpcr1);
  313. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  314. /* Process Unlocked */
  315. __HAL_UNLOCK(hsmartcard);
  316. return HAL_OK;
  317. }
  318. /**
  319. * @brief Set the RXFIFO threshold.
  320. * @param hsmartcard SMARTCARD handle.
  321. * @param Threshold RX FIFO threshold value
  322. * This parameter can be one of the following values:
  323. * @arg @ref SMARTCARD_RXFIFO_THRESHOLD_1_8
  324. * @arg @ref SMARTCARD_RXFIFO_THRESHOLD_1_4
  325. * @arg @ref SMARTCARD_RXFIFO_THRESHOLD_1_2
  326. * @arg @ref SMARTCARD_RXFIFO_THRESHOLD_3_4
  327. * @arg @ref SMARTCARD_RXFIFO_THRESHOLD_7_8
  328. * @arg @ref SMARTCARD_RXFIFO_THRESHOLD_8_8
  329. * @retval HAL status
  330. */
  331. HAL_StatusTypeDef HAL_SMARTCARDEx_SetRxFifoThreshold(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Threshold)
  332. {
  333. uint32_t tmpcr1;
  334. /* Check parameters */
  335. assert_param(IS_UART_FIFO_INSTANCE(hsmartcard->Instance));
  336. assert_param(IS_SMARTCARD_RXFIFO_THRESHOLD(Threshold));
  337. /* Process Locked */
  338. __HAL_LOCK(hsmartcard);
  339. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  340. /* Save actual SMARTCARD configuration */
  341. tmpcr1 = READ_REG(hsmartcard->Instance->CR1);
  342. /* Disable SMARTCARD */
  343. __HAL_SMARTCARD_DISABLE(hsmartcard);
  344. /* Update RX threshold configuration */
  345. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_RXFTCFG, Threshold);
  346. /* Determine the number of data to process during RX/TX ISR execution */
  347. SMARTCARDEx_SetNbDataToProcess(hsmartcard);
  348. /* Restore SMARTCARD configuration */
  349. MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_UE, tmpcr1);
  350. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  351. /* Process Unlocked */
  352. __HAL_UNLOCK(hsmartcard);
  353. return HAL_OK;
  354. }
  355. #endif /* USART_CR1_FIFOEN */
  356. /**
  357. * @}
  358. */
  359. /**
  360. * @}
  361. */
  362. /** @defgroup SMARTCARDEx_Private_Functions SMARTCARD Extended Private Functions
  363. * @{
  364. */
  365. #if defined(USART_CR1_FIFOEN)
  366. /**
  367. * @brief Calculate the number of data to process in RX/TX ISR.
  368. * @note The RX FIFO depth and the TX FIFO depth is extracted from
  369. * the USART configuration registers.
  370. * @param hsmartcard SMARTCARD handle.
  371. * @retval None
  372. */
  373. static void SMARTCARDEx_SetNbDataToProcess(SMARTCARD_HandleTypeDef *hsmartcard)
  374. {
  375. uint8_t rx_fifo_depth;
  376. uint8_t tx_fifo_depth;
  377. uint8_t rx_fifo_threshold;
  378. uint8_t tx_fifo_threshold;
  379. /* 2 0U/1U added for MISRAC2012-Rule-18.1_b and MISRAC2012-Rule-18.1_d */
  380. static const uint8_t numerator[] = {1U, 1U, 1U, 3U, 7U, 1U, 0U, 0U};
  381. static const uint8_t denominator[] = {8U, 4U, 2U, 4U, 8U, 1U, 1U, 1U};
  382. if (hsmartcard->FifoMode == SMARTCARD_FIFOMODE_DISABLE)
  383. {
  384. hsmartcard->NbTxDataToProcess = 1U;
  385. hsmartcard->NbRxDataToProcess = 1U;
  386. }
  387. else
  388. {
  389. rx_fifo_depth = RX_FIFO_DEPTH;
  390. tx_fifo_depth = TX_FIFO_DEPTH;
  391. rx_fifo_threshold = (uint8_t)(READ_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTCFG) >> USART_CR3_RXFTCFG_Pos);
  392. tx_fifo_threshold = (uint8_t)(READ_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTCFG) >> USART_CR3_TXFTCFG_Pos);
  393. hsmartcard->NbTxDataToProcess = ((uint16_t)tx_fifo_depth * numerator[tx_fifo_threshold]) / \
  394. (uint16_t)denominator[tx_fifo_threshold];
  395. hsmartcard->NbRxDataToProcess = ((uint16_t)rx_fifo_depth * numerator[rx_fifo_threshold]) / \
  396. (uint16_t)denominator[rx_fifo_threshold];
  397. }
  398. }
  399. #endif /* USART_CR1_FIFOEN */
  400. /**
  401. * @}
  402. */
  403. #endif /* HAL_SMARTCARD_MODULE_ENABLED */
  404. /**
  405. * @}
  406. */
  407. /**
  408. * @}
  409. */