stm32l4xx_hal_smartcard_ex.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. @verbatim
  13. =============================================================================
  14. ##### SMARTCARD peripheral extended features #####
  15. =============================================================================
  16. [..]
  17. The Extended SMARTCARD HAL driver can be used as follows:
  18. (#) After having configured the SMARTCARD basic features with HAL_SMARTCARD_Init(),
  19. then program SMARTCARD advanced features if required (TX/RX pins swap, TimeOut,
  20. auto-retry counter,...) in the hsmartcard AdvancedInit structure.
  21. @endverbatim
  22. ******************************************************************************
  23. * @attention
  24. *
  25. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  26. *
  27. * Redistribution and use in source and binary forms, with or without modification,
  28. * are permitted provided that the following conditions are met:
  29. * 1. Redistributions of source code must retain the above copyright notice,
  30. * this list of conditions and the following disclaimer.
  31. * 2. Redistributions in binary form must reproduce the above copyright notice,
  32. * this list of conditions and the following disclaimer in the documentation
  33. * and/or other materials provided with the distribution.
  34. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  35. * may be used to endorse or promote products derived from this software
  36. * without specific prior written permission.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  39. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  42. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  43. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  44. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  45. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  46. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  47. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48. *
  49. ******************************************************************************
  50. */
  51. /* Includes ------------------------------------------------------------------*/
  52. #include "stm32l4xx_hal.h"
  53. /** @addtogroup STM32L4xx_HAL_Driver
  54. * @{
  55. */
  56. /** @defgroup SMARTCARDEx SMARTCARDEx
  57. * @brief SMARTCARD Extended HAL module driver
  58. * @{
  59. */
  60. #ifdef HAL_SMARTCARD_MODULE_ENABLED
  61. /* Private typedef -----------------------------------------------------------*/
  62. /* Private define ------------------------------------------------------------*/
  63. /* Private macros ------------------------------------------------------------*/
  64. /* Private variables ---------------------------------------------------------*/
  65. /* Private function prototypes -----------------------------------------------*/
  66. #if defined(USART_CR1_FIFOEN)
  67. static void SMARTCARDEx_SetNbDataToProcess(SMARTCARD_HandleTypeDef *hsmartcard);
  68. #endif
  69. /* Exported functions --------------------------------------------------------*/
  70. /** @defgroup SMARTCARDEx_Exported_Functions SMARTCARD Extended Exported Functions
  71. * @{
  72. */
  73. /** @defgroup SMARTCARDEx_Exported_Functions_Group2 IO operation functions
  74. * @brief Extended SMARTCARD Transmit/Receive functions
  75. *
  76. @verbatim
  77. ===============================================================================
  78. ##### IO operation functions #####
  79. ===============================================================================
  80. This subsection provides a set of FIFO mode related callback functions.
  81. (#) TX/RX Fifos Callbacks:
  82. (+) HAL_SMARTCARDEx_RxFifoFullCallback()
  83. (+) HAL_SMARTCARDEx_TxFifoEmptyCallback()
  84. @endverbatim
  85. * @{
  86. */
  87. #if defined(USART_CR1_FIFOEN)
  88. /**
  89. * @brief SMARTCARD RX Fifo full callback.
  90. * @param hsmartcard SMARTCARD handle.
  91. * @retval None
  92. */
  93. __weak void HAL_SMARTCARDEx_RxFifoFullCallback (SMARTCARD_HandleTypeDef *hsmartcard)
  94. {
  95. /* Prevent unused argument(s) compilation warning */
  96. UNUSED(hsmartcard);
  97. /* NOTE : This function should not be modified, when the callback is needed,
  98. the HAL_SMARTCARDEx_RxFifoFullCallback can be implemented in the user file.
  99. */
  100. }
  101. /**
  102. * @brief SMARTCARD TX Fifo empty callback.
  103. * @param hsmartcard SMARTCARD handle.
  104. * @retval None
  105. */
  106. __weak void HAL_SMARTCARDEx_TxFifoEmptyCallback (SMARTCARD_HandleTypeDef *hsmartcard)
  107. {
  108. /* Prevent unused argument(s) compilation warning */
  109. UNUSED(hsmartcard);
  110. /* NOTE : This function should not be modified, when the callback is needed,
  111. the HAL_SMARTCARDEx_TxFifoEmptyCallback can be implemented in the user file.
  112. */
  113. }
  114. #endif
  115. /**
  116. * @}
  117. */
  118. /** @defgroup SMARTCARDEx_Exported_Functions_Group3 Extended Peripheral Control functions
  119. * @brief Extended control functions
  120. *
  121. @verbatim
  122. ===============================================================================
  123. ##### Peripheral Control functions #####
  124. ===============================================================================
  125. [..]
  126. This subsection provides a set of functions allowing to initialize the SMARTCARD.
  127. (+) HAL_SMARTCARDEx_BlockLength_Config() API allows to configure the Block Length on the fly
  128. (+) HAL_SMARTCARDEx_TimeOut_Config() API allows to configure the receiver timeout value on the fly
  129. (+) HAL_SMARTCARDEx_EnableReceiverTimeOut() API enables the receiver timeout feature
  130. (+) HAL_SMARTCARDEx_DisableReceiverTimeOut() API disables the receiver timeout feature
  131. (+) HAL_SMARTCARDEx_EnableFifoMode() API enables the FIFO mode
  132. (+) HAL_SMARTCARDEx_DisableFifoMode() API disables the FIFO mode
  133. (+) HAL_SMARTCARDEx_SetTxFifoThreshold() API sets the TX FIFO threshold
  134. (+) HAL_SMARTCARDEx_SetRxFifoThreshold() API sets the RX FIFO threshold
  135. @endverbatim
  136. * @{
  137. */
  138. /**
  139. * @brief Update on the fly the SMARTCARD block length in RTOR register.
  140. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  141. * the configuration information for the specified SMARTCARD module.
  142. * @param BlockLength SMARTCARD block length (8-bit long at most)
  143. * @retval None
  144. */
  145. void HAL_SMARTCARDEx_BlockLength_Config(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t BlockLength)
  146. {
  147. MODIFY_REG(hsmartcard->Instance->RTOR, USART_RTOR_BLEN, ((uint32_t)BlockLength << USART_RTOR_BLEN_Pos));
  148. }
  149. /**
  150. * @brief Update on the fly the receiver timeout value in RTOR register.
  151. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  152. * the configuration information for the specified SMARTCARD module.
  153. * @param TimeOutValue receiver timeout value in number of baud blocks. The timeout
  154. * value must be less or equal to 0x0FFFFFFFF.
  155. * @retval None
  156. */
  157. void HAL_SMARTCARDEx_TimeOut_Config(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t TimeOutValue)
  158. {
  159. assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue));
  160. MODIFY_REG(hsmartcard->Instance->RTOR, USART_RTOR_RTO, TimeOutValue);
  161. }
  162. /**
  163. * @brief Enable the SMARTCARD receiver timeout feature.
  164. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  165. * the configuration information for the specified SMARTCARD module.
  166. * @retval HAL status
  167. */
  168. HAL_StatusTypeDef HAL_SMARTCARDEx_EnableReceiverTimeOut(SMARTCARD_HandleTypeDef *hsmartcard)
  169. {
  170. if(hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  171. {
  172. /* Process Locked */
  173. __HAL_LOCK(hsmartcard);
  174. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  175. /* Set the USART RTOEN bit */
  176. SET_BIT(hsmartcard->Instance->CR2, USART_CR2_RTOEN);
  177. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  178. /* Process Unlocked */
  179. __HAL_UNLOCK(hsmartcard);
  180. return HAL_OK;
  181. }
  182. else
  183. {
  184. return HAL_BUSY;
  185. }
  186. }
  187. /**
  188. * @brief Disable the SMARTCARD receiver timeout feature.
  189. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  190. * the configuration information for the specified SMARTCARD module.
  191. * @retval HAL status
  192. */
  193. HAL_StatusTypeDef HAL_SMARTCARDEx_DisableReceiverTimeOut(SMARTCARD_HandleTypeDef *hsmartcard)
  194. {
  195. if(hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  196. {
  197. /* Process Locked */
  198. __HAL_LOCK(hsmartcard);
  199. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  200. /* Clear the USART RTOEN bit */
  201. CLEAR_BIT(hsmartcard->Instance->CR2, USART_CR2_RTOEN);
  202. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  203. /* Process Unlocked */
  204. __HAL_UNLOCK(hsmartcard);
  205. return HAL_OK;
  206. }
  207. else
  208. {
  209. return HAL_BUSY;
  210. }
  211. }
  212. #if defined(USART_CR1_FIFOEN)
  213. /**
  214. * @brief Enable the FIFO mode.
  215. * @param hsmartcard SMARTCARD handle.
  216. * @retval HAL status
  217. */
  218. HAL_StatusTypeDef HAL_SMARTCARDEx_EnableFifoMode(SMARTCARD_HandleTypeDef *hsmartcard)
  219. {
  220. uint32_t tmpcr1 = 0;
  221. /* Check parameters */
  222. assert_param(IS_UART_FIFO_INSTANCE(hsmartcard->Instance));
  223. /* Process Locked */
  224. __HAL_LOCK(hsmartcard);
  225. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  226. /* Save actual SMARTCARD configuration */
  227. tmpcr1 = READ_REG(hsmartcard->Instance->CR1);
  228. /* Disable SMARTCARD */
  229. __HAL_SMARTCARD_DISABLE(hsmartcard);
  230. /* Enable FIFO mode */
  231. SET_BIT(tmpcr1, USART_CR1_FIFOEN);
  232. hsmartcard->FifoMode = SMARTCARD_FIFOMODE_ENABLE;
  233. /* Restore SMARTCARD configuration */
  234. WRITE_REG(hsmartcard->Instance->CR1, tmpcr1);
  235. /* Determine the number of data to process during RX/TX ISR execution */
  236. SMARTCARDEx_SetNbDataToProcess(hsmartcard);
  237. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  238. /* Process Unlocked */
  239. __HAL_UNLOCK(hsmartcard);
  240. return HAL_OK;
  241. }
  242. /**
  243. * @brief Disable the FIFO mode.
  244. * @param hsmartcard SMARTCARD handle.
  245. * @retval HAL status
  246. */
  247. HAL_StatusTypeDef HAL_SMARTCARDEx_DisableFifoMode(SMARTCARD_HandleTypeDef *hsmartcard)
  248. {
  249. uint32_t tmpcr1 = 0;
  250. /* Check parameters */
  251. assert_param(IS_UART_FIFO_INSTANCE(hsmartcard->Instance));
  252. /* Process Locked */
  253. __HAL_LOCK(hsmartcard);
  254. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  255. /* Save actual SMARTCARD configuration */
  256. tmpcr1 = READ_REG(hsmartcard->Instance->CR1);
  257. /* Disable SMARTCARD */
  258. __HAL_SMARTCARD_DISABLE(hsmartcard);
  259. /* Enable FIFO mode */
  260. CLEAR_BIT(tmpcr1, USART_CR1_FIFOEN);
  261. hsmartcard->FifoMode = SMARTCARD_FIFOMODE_DISABLE;
  262. /* Restore SMARTCARD configuration */
  263. WRITE_REG(hsmartcard->Instance->CR1, tmpcr1);
  264. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  265. /* Process Unlocked */
  266. __HAL_UNLOCK(hsmartcard);
  267. return HAL_OK;
  268. }
  269. /**
  270. * @brief Set the TXFIFO threshold.
  271. * @param hsmartcard SMARTCARD handle.
  272. * @param Threshold TX FIFO threshold value
  273. * This parameter can be one of the following values:
  274. * @arg @ref SMARTCARD_TXFIFO_THRESHOLD_1_8
  275. * @arg @ref SMARTCARD_TXFIFO_THRESHOLD_1_4
  276. * @arg @ref SMARTCARD_TXFIFO_THRESHOLD_1_2
  277. * @arg @ref SMARTCARD_TXFIFO_THRESHOLD_3_4
  278. * @arg @ref SMARTCARD_TXFIFO_THRESHOLD_7_8
  279. * @arg @ref SMARTCARD_TXFIFO_THRESHOLD_8_8
  280. * @retval HAL status
  281. */
  282. HAL_StatusTypeDef HAL_SMARTCARDEx_SetTxFifoThreshold(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Threshold)
  283. {
  284. uint32_t tmpcr1 = 0;
  285. /* Check parameters */
  286. assert_param(IS_UART_FIFO_INSTANCE(hsmartcard->Instance));
  287. assert_param(IS_SMARTCARD_TXFIFO_THRESHOLD(Threshold));
  288. /* Process Locked */
  289. __HAL_LOCK(hsmartcard);
  290. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  291. /* Save actual SMARTCARD configuration */
  292. tmpcr1 = READ_REG(hsmartcard->Instance->CR1);
  293. /* Disable SMARTCARD */
  294. __HAL_SMARTCARD_DISABLE(hsmartcard);
  295. /* Update TX threshold configuration */
  296. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_TXFTCFG, Threshold);
  297. /* Determine the number of data to process during RX/TX ISR execution */
  298. SMARTCARDEx_SetNbDataToProcess(hsmartcard);
  299. /* Restore SMARTCARD configuration */
  300. MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_UE, tmpcr1);
  301. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  302. /* Process Unlocked */
  303. __HAL_UNLOCK(hsmartcard);
  304. return HAL_OK;
  305. }
  306. /**
  307. * @brief Set the RXFIFO threshold.
  308. * @param hsmartcard SMARTCARD handle.
  309. * @param Threshold RX FIFO threshold value
  310. * This parameter can be one of the following values:
  311. * @arg @ref SMARTCARD_RXFIFO_THRESHOLD_1_8
  312. * @arg @ref SMARTCARD_RXFIFO_THRESHOLD_1_4
  313. * @arg @ref SMARTCARD_RXFIFO_THRESHOLD_1_2
  314. * @arg @ref SMARTCARD_RXFIFO_THRESHOLD_3_4
  315. * @arg @ref SMARTCARD_RXFIFO_THRESHOLD_7_8
  316. * @arg @ref SMARTCARD_RXFIFO_THRESHOLD_8_8
  317. * @retval HAL status
  318. */
  319. HAL_StatusTypeDef HAL_SMARTCARDEx_SetRxFifoThreshold(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Threshold)
  320. {
  321. uint32_t tmpcr1 = 0;
  322. /* Check parameters */
  323. assert_param(IS_UART_FIFO_INSTANCE(hsmartcard->Instance));
  324. assert_param(IS_SMARTCARD_RXFIFO_THRESHOLD(Threshold));
  325. /* Process Locked */
  326. __HAL_LOCK(hsmartcard);
  327. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  328. /* Save actual SMARTCARD configuration */
  329. tmpcr1 = READ_REG(hsmartcard->Instance->CR1);
  330. /* Disable SMARTCARD */
  331. __HAL_SMARTCARD_DISABLE(hsmartcard);
  332. /* Update RX threshold configuration */
  333. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_RXFTCFG, Threshold);
  334. /* Determine the number of data to process during RX/TX ISR execution */
  335. SMARTCARDEx_SetNbDataToProcess(hsmartcard);
  336. /* Restore SMARTCARD configuration */
  337. MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_UE, tmpcr1);
  338. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  339. /* Process Unlocked */
  340. __HAL_UNLOCK(hsmartcard);
  341. return HAL_OK;
  342. }
  343. #endif
  344. /**
  345. * @}
  346. */
  347. /**
  348. * @}
  349. */
  350. /** @defgroup SMARTCARDEx_Private_Functions SMARTCARD Extended private Functions
  351. * @{
  352. */
  353. #if defined(USART_CR1_FIFOEN)
  354. /**
  355. * @brief Calculate the number of data to process in RX/TX ISR.
  356. * @note The RX FIFO depth and the TX FIFO depth is extracted from
  357. * the USART configuration registers.
  358. * @param hsmartcard SMARTCARD handle.
  359. * @retval None
  360. */
  361. static void SMARTCARDEx_SetNbDataToProcess(SMARTCARD_HandleTypeDef *hsmartcard)
  362. {
  363. uint8_t rx_fifo_depth;
  364. uint8_t tx_fifo_depth;
  365. uint8_t rx_fifo_threshold;
  366. uint8_t tx_fifo_threshold;
  367. uint8_t numerator[] = {1, 1, 1, 3, 7, 1};
  368. uint8_t denominator[] = {8, 4, 2, 4, 8, 1};
  369. if (hsmartcard->FifoMode == SMARTCARD_FIFOMODE_DISABLE)
  370. {
  371. hsmartcard->NbTxDataToProcess = 1;
  372. hsmartcard->NbRxDataToProcess = 1;
  373. }
  374. else
  375. {
  376. rx_fifo_depth = 8; /* RX Fifo size */
  377. tx_fifo_depth = 8; /* TX Fifo size */
  378. rx_fifo_threshold = (uint8_t)(READ_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTCFG) >> USART_CR3_RXFTCFG_Pos);
  379. tx_fifo_threshold = (uint8_t)(READ_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTCFG) >> USART_CR3_TXFTCFG_Pos);
  380. hsmartcard->NbTxDataToProcess = (uint8_t)(tx_fifo_depth * numerator[tx_fifo_threshold])/denominator[tx_fifo_threshold];
  381. hsmartcard->NbRxDataToProcess = (uint8_t)(rx_fifo_depth * numerator[rx_fifo_threshold])/denominator[rx_fifo_threshold];
  382. }
  383. }
  384. #endif
  385. /**
  386. * @}
  387. */
  388. #endif /* HAL_SMARTCARD_MODULE_ENABLED */
  389. /**
  390. * @}
  391. */
  392. /**
  393. * @}
  394. */
  395. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/