You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

495 lines
16 KiB

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