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.
 
 
 

3158 lines
120 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_hal_smartcard.c
  4. * @author MCD Application Team
  5. * @brief SMARTCARD HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the SMARTCARD peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State and Error functions
  12. *
  13. @verbatim
  14. ==============================================================================
  15. ##### How to use this driver #####
  16. ==============================================================================
  17. [..]
  18. The SMARTCARD HAL driver can be used as follows:
  19. (#) Declare a SMARTCARD_HandleTypeDef handle structure (eg. SMARTCARD_HandleTypeDef hsmartcard).
  20. (#) Associate a USART to the SMARTCARD handle hsmartcard.
  21. (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
  22. (++) Enable the USARTx interface clock.
  23. (++) USART pins configuration:
  24. (+++) Enable the clock for the USART GPIOs.
  25. (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input).
  26. (++) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
  27. and HAL_SMARTCARD_Receive_IT() APIs):
  28. (+++) Configure the USARTx interrupt priority.
  29. (+++) Enable the NVIC USART IRQ handle.
  30. (++) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
  31. and HAL_SMARTCARD_Receive_DMA() APIs):
  32. (+++) Declare a DMA handle structure for the Tx/Rx channel.
  33. (+++) Enable the DMAx interface clock.
  34. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  35. (+++) Configure the DMA Tx/Rx channel.
  36. (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
  37. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
  38. (#) Program the Baud Rate, Parity, Mode(Receiver/Transmitter), clock enabling/disabling and accordingly,
  39. the clock parameters (parity, phase, last bit), prescaler value, guard time and NACK on transmission
  40. error enabling or disabling in the hsmartcard handle Init structure.
  41. (#) If required, program SMARTCARD advanced features (TX/RX pins swap, TimeOut, auto-retry counter,...)
  42. in the hsmartcard handle AdvancedInit structure.
  43. (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
  44. (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  45. by calling the customized HAL_SMARTCARD_MspInit() API.
  46. [..]
  47. (@) The specific SMARTCARD interrupts (Transmission complete interrupt,
  48. RXNE interrupt and Error Interrupts) will be managed using the macros
  49. __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
  50. [..]
  51. [..] Three operation modes are available within this driver :
  52. *** Polling mode IO operation ***
  53. =================================
  54. [..]
  55. (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
  56. (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
  57. *** Interrupt mode IO operation ***
  58. ===================================
  59. [..]
  60. (+) Send an amount of data in non-blocking mode using HAL_SMARTCARD_Transmit_IT()
  61. (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can
  62. add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback()
  63. (+) Receive an amount of data in non-blocking mode using HAL_SMARTCARD_Receive_IT()
  64. (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can
  65. add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback()
  66. (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  67. add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback()
  68. *** DMA mode IO operation ***
  69. ==============================
  70. [..]
  71. (+) Send an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
  72. (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can
  73. add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback()
  74. (+) Receive an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
  75. (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can
  76. add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback()
  77. (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  78. add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback()
  79. *** SMARTCARD HAL driver macros list ***
  80. ========================================
  81. [..]
  82. Below the list of most used macros in SMARTCARD HAL driver.
  83. (+) __HAL_SMARTCARD_GET_FLAG : Check whether or not the specified SMARTCARD flag is set
  84. (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
  85. (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
  86. (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
  87. (+) __HAL_SMARTCARD_GET_IT_SOURCE: Check whether or not the specified SMARTCARD interrupt is enabled
  88. [..]
  89. (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
  90. ##### Callback registration #####
  91. ==================================
  92. [..]
  93. The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS when set to 1
  94. allows the user to configure dynamically the driver callbacks.
  95. [..]
  96. Use Function HAL_SMARTCARD_RegisterCallback() to register a user callback.
  97. Function HAL_SMARTCARD_RegisterCallback() allows to register following callbacks:
  98. (+) TxCpltCallback : Tx Complete Callback.
  99. (+) RxCpltCallback : Rx Complete Callback.
  100. (+) ErrorCallback : Error Callback.
  101. (+) AbortCpltCallback : Abort Complete Callback.
  102. (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  103. (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
  104. (+) RxFifoFullCallback : Rx Fifo Full Callback.
  105. (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
  106. (+) MspInitCallback : SMARTCARD MspInit.
  107. (+) MspDeInitCallback : SMARTCARD MspDeInit.
  108. This function takes as parameters the HAL peripheral handle, the Callback ID
  109. and a pointer to the user callback function.
  110. [..]
  111. Use function @ref HAL_SMARTCARD_UnRegisterCallback() to reset a callback to the default
  112. weak (surcharged) function.
  113. @ref HAL_SMARTCARD_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  114. and the Callback ID.
  115. This function allows to reset following callbacks:
  116. (+) TxCpltCallback : Tx Complete Callback.
  117. (+) RxCpltCallback : Rx Complete Callback.
  118. (+) ErrorCallback : Error Callback.
  119. (+) AbortCpltCallback : Abort Complete Callback.
  120. (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  121. (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
  122. (+) RxFifoFullCallback : Rx Fifo Full Callback.
  123. (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
  124. (+) MspInitCallback : SMARTCARD MspInit.
  125. (+) MspDeInitCallback : SMARTCARD MspDeInit.
  126. [..]
  127. By default, after the HAL_SMARTCARD_Init() and when the state is HAL_SMARTCARD_STATE_RESET
  128. all callbacks are set to the corresponding weak (surcharged) functions:
  129. examples HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback().
  130. Exception done for MspInit and MspDeInit functions that are respectively
  131. reset to the legacy weak (surcharged) functions in the HAL_SMARTCARD_Init()
  132. and HAL_SMARTCARD_DeInit() only when these callbacks are null (not registered beforehand).
  133. If not, MspInit or MspDeInit are not null, the HAL_SMARTCARD_Init() and HAL_SMARTCARD_DeInit()
  134. keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  135. [..]
  136. Callbacks can be registered/unregistered in HAL_SMARTCARD_STATE_READY state only.
  137. Exception done MspInit/MspDeInit that can be registered/unregistered
  138. in HAL_SMARTCARD_STATE_READY or HAL_SMARTCARD_STATE_RESET state, thus registered (user)
  139. MspInit/DeInit callbacks can be used during the Init/DeInit.
  140. In that case first register the MspInit/MspDeInit user callbacks
  141. using HAL_SMARTCARD_RegisterCallback() before calling HAL_SMARTCARD_DeInit()
  142. or HAL_SMARTCARD_Init() function.
  143. [..]
  144. When The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS is set to 0 or
  145. not defined, the callback registration feature is not available
  146. and weak (surcharged) callbacks are used.
  147. @endverbatim
  148. ******************************************************************************
  149. * @attention
  150. *
  151. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  152. * All rights reserved.</center></h2>
  153. *
  154. * This software component is licensed by ST under BSD 3-Clause license,
  155. * the "License"; You may not use this file except in compliance with the
  156. * License. You may obtain a copy of the License at:
  157. * opensource.org/licenses/BSD-3-Clause
  158. *
  159. ******************************************************************************
  160. */
  161. /* Includes ------------------------------------------------------------------*/
  162. #include "stm32wbxx_hal.h"
  163. /** @addtogroup STM32WBxx_HAL_Driver
  164. * @{
  165. */
  166. /** @defgroup SMARTCARD SMARTCARD
  167. * @brief HAL SMARTCARD module driver
  168. * @{
  169. */
  170. #ifdef HAL_SMARTCARD_MODULE_ENABLED
  171. /* Private typedef -----------------------------------------------------------*/
  172. /* Private define ------------------------------------------------------------*/
  173. /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants
  174. * @{
  175. */
  176. #define SMARTCARD_TEACK_REACK_TIMEOUT 1000U /*!< SMARTCARD TX or RX enable acknowledge time-out value */
  177. #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  178. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8| \
  179. USART_CR1_FIFOEN )) /*!< USART CR1 fields of parameters set by SMARTCARD_SetConfig API */
  180. #define USART_CR2_CLK_FIELDS ((uint32_t)(USART_CR2_CLKEN | USART_CR2_CPOL | USART_CR2_CPHA | \
  181. USART_CR2_LBCL)) /*!< SMARTCARD clock-related USART CR2 fields of parameters */
  182. #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_RTOEN | USART_CR2_CLK_FIELDS | USART_CR2_STOP)) /*!< USART CR2 fields of parameters set by SMARTCARD_SetConfig API */
  183. #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_ONEBIT | USART_CR3_NACK | USART_CR3_SCARCNT | \
  184. USART_CR3_TXFTCFG | USART_CR3_RXFTCFG )) /*!< USART CR3 fields of parameters set by SMARTCARD_SetConfig API */
  185. #define USART_BRR_MIN 0x10U /*!< USART BRR minimum authorized value */
  186. #define USART_BRR_MAX 0x0000FFFFU /*!< USART BRR maximum authorized value */
  187. /**
  188. * @}
  189. */
  190. /* Private macros ------------------------------------------------------------*/
  191. /* Private variables ---------------------------------------------------------*/
  192. /* Private function prototypes -----------------------------------------------*/
  193. /** @addtogroup SMARTCARD_Private_Functions
  194. * @{
  195. */
  196. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  197. void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsmartcard);
  198. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  199. static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard);
  200. static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard);
  201. static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard);
  202. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag,
  203. FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
  204. static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard);
  205. static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard);
  206. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  207. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  208. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
  209. static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  210. static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  211. static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  212. static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  213. static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  214. static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard);
  215. static void SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard);
  216. static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
  217. static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard);
  218. static void SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard);
  219. /**
  220. * @}
  221. */
  222. /* Exported functions --------------------------------------------------------*/
  223. /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
  224. * @{
  225. */
  226. /** @defgroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions
  227. * @brief Initialization and Configuration functions
  228. *
  229. @verbatim
  230. ==============================================================================
  231. ##### Initialization and Configuration functions #####
  232. ==============================================================================
  233. [..]
  234. This subsection provides a set of functions allowing to initialize the USARTx
  235. associated to the SmartCard.
  236. (+) These parameters can be configured:
  237. (++) Baud Rate
  238. (++) Parity: parity should be enabled, frame Length is fixed to 8 bits plus parity
  239. (++) Receiver/transmitter modes
  240. (++) Synchronous mode (and if enabled, phase, polarity and last bit parameters)
  241. (++) Prescaler value
  242. (++) Guard bit time
  243. (++) NACK enabling or disabling on transmission error
  244. (+) The following advanced features can be configured as well:
  245. (++) TX and/or RX pin level inversion
  246. (++) data logical level inversion
  247. (++) RX and TX pins swap
  248. (++) RX overrun detection disabling
  249. (++) DMA disabling on RX error
  250. (++) MSB first on communication line
  251. (++) Time out enabling (and if activated, timeout value)
  252. (++) Block length
  253. (++) Auto-retry counter
  254. [..]
  255. The HAL_SMARTCARD_Init() API follows the USART synchronous configuration procedures
  256. (details for the procedures are available in reference manual).
  257. @endverbatim
  258. The USART frame format is given in the following table:
  259. Table 1. USART frame format.
  260. +---------------------------------------------------------------+
  261. | M1M0 bits | PCE bit | USART frame |
  262. |-----------------------|---------------------------------------|
  263. | 01 | 1 | | SB | 8 bit data | PB | STB | |
  264. +---------------------------------------------------------------+
  265. * @{
  266. */
  267. /**
  268. * @brief Initialize the SMARTCARD mode according to the specified
  269. * parameters in the SMARTCARD_HandleTypeDef and initialize the associated handle.
  270. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  271. * the configuration information for the specified SMARTCARD module.
  272. * @retval HAL status
  273. */
  274. HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsmartcard)
  275. {
  276. /* Check the SMARTCARD handle allocation */
  277. if (hsmartcard == NULL)
  278. {
  279. return HAL_ERROR;
  280. }
  281. /* Check the USART associated to the SMARTCARD handle */
  282. assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
  283. if (hsmartcard->gState == HAL_SMARTCARD_STATE_RESET)
  284. {
  285. /* Allocate lock resource and initialize it */
  286. hsmartcard->Lock = HAL_UNLOCKED;
  287. #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
  288. SMARTCARD_InitCallbacksToDefault(hsmartcard);
  289. if (hsmartcard->MspInitCallback == NULL)
  290. {
  291. hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit;
  292. }
  293. /* Init the low level hardware */
  294. hsmartcard->MspInitCallback(hsmartcard);
  295. #else
  296. /* Init the low level hardware : GPIO, CLOCK */
  297. HAL_SMARTCARD_MspInit(hsmartcard);
  298. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  299. }
  300. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  301. /* Disable the Peripheral to set smartcard mode */
  302. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  303. /* In SmartCard mode, the following bits must be kept cleared:
  304. - LINEN in the USART_CR2 register,
  305. - HDSEL and IREN bits in the USART_CR3 register.*/
  306. CLEAR_BIT(hsmartcard->Instance->CR2, USART_CR2_LINEN);
  307. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN));
  308. /* set the USART in SMARTCARD mode */
  309. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_SCEN);
  310. /* Set the SMARTCARD Communication parameters */
  311. if (SMARTCARD_SetConfig(hsmartcard) == HAL_ERROR)
  312. {
  313. return HAL_ERROR;
  314. }
  315. /* Set the SMARTCARD transmission completion indication */
  316. SMARTCARD_TRANSMISSION_COMPLETION_SETTING(hsmartcard);
  317. if (hsmartcard->AdvancedInit.AdvFeatureInit != SMARTCARD_ADVFEATURE_NO_INIT)
  318. {
  319. SMARTCARD_AdvFeatureConfig(hsmartcard);
  320. }
  321. /* Enable the Peripheral */
  322. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  323. /* TEACK and/or REACK to check before moving hsmartcard->gState and hsmartcard->RxState to Ready */
  324. return (SMARTCARD_CheckIdleState(hsmartcard));
  325. }
  326. /**
  327. * @brief DeInitialize the SMARTCARD peripheral.
  328. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  329. * the configuration information for the specified SMARTCARD module.
  330. * @retval HAL status
  331. */
  332. HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsmartcard)
  333. {
  334. /* Check the SMARTCARD handle allocation */
  335. if (hsmartcard == NULL)
  336. {
  337. return HAL_ERROR;
  338. }
  339. /* Check the USART/UART associated to the SMARTCARD handle */
  340. assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
  341. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  342. /* Disable the Peripheral */
  343. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  344. WRITE_REG(hsmartcard->Instance->CR1, 0x0U);
  345. WRITE_REG(hsmartcard->Instance->CR2, 0x0U);
  346. WRITE_REG(hsmartcard->Instance->CR3, 0x0U);
  347. WRITE_REG(hsmartcard->Instance->RTOR, 0x0U);
  348. WRITE_REG(hsmartcard->Instance->GTPR, 0x0U);
  349. /* DeInit the low level hardware */
  350. #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
  351. if (hsmartcard->MspDeInitCallback == NULL)
  352. {
  353. hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
  354. }
  355. /* DeInit the low level hardware */
  356. hsmartcard->MspDeInitCallback(hsmartcard);
  357. #else
  358. HAL_SMARTCARD_MspDeInit(hsmartcard);
  359. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  360. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  361. hsmartcard->gState = HAL_SMARTCARD_STATE_RESET;
  362. hsmartcard->RxState = HAL_SMARTCARD_STATE_RESET;
  363. /* Process Unlock */
  364. __HAL_UNLOCK(hsmartcard);
  365. return HAL_OK;
  366. }
  367. /**
  368. * @brief Initialize the SMARTCARD MSP.
  369. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  370. * the configuration information for the specified SMARTCARD module.
  371. * @retval None
  372. */
  373. __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsmartcard)
  374. {
  375. /* Prevent unused argument(s) compilation warning */
  376. UNUSED(hsmartcard);
  377. /* NOTE : This function should not be modified, when the callback is needed,
  378. the HAL_SMARTCARD_MspInit can be implemented in the user file
  379. */
  380. }
  381. /**
  382. * @brief DeInitialize the SMARTCARD MSP.
  383. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  384. * the configuration information for the specified SMARTCARD module.
  385. * @retval None
  386. */
  387. __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsmartcard)
  388. {
  389. /* Prevent unused argument(s) compilation warning */
  390. UNUSED(hsmartcard);
  391. /* NOTE : This function should not be modified, when the callback is needed,
  392. the HAL_SMARTCARD_MspDeInit can be implemented in the user file
  393. */
  394. }
  395. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  396. /**
  397. * @brief Register a User SMARTCARD Callback
  398. * To be used instead of the weak predefined callback
  399. * @param hsmartcard smartcard handle
  400. * @param CallbackID ID of the callback to be registered
  401. * This parameter can be one of the following values:
  402. * @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
  403. * @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
  404. * @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
  405. * @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  406. * @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  407. * @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  408. * @arg @ref HAL_SMARTCARD_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
  409. * @arg @ref HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
  410. * @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
  411. * @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
  412. * @param pCallback pointer to the Callback function
  413. * @retval HAL status
  414. */
  415. HAL_StatusTypeDef HAL_SMARTCARD_RegisterCallback(SMARTCARD_HandleTypeDef *hsmartcard,
  416. HAL_SMARTCARD_CallbackIDTypeDef CallbackID, pSMARTCARD_CallbackTypeDef pCallback)
  417. {
  418. HAL_StatusTypeDef status = HAL_OK;
  419. if (pCallback == NULL)
  420. {
  421. /* Update the error code */
  422. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  423. return HAL_ERROR;
  424. }
  425. /* Process locked */
  426. __HAL_LOCK(hsmartcard);
  427. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  428. {
  429. switch (CallbackID)
  430. {
  431. case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
  432. hsmartcard->TxCpltCallback = pCallback;
  433. break;
  434. case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
  435. hsmartcard->RxCpltCallback = pCallback;
  436. break;
  437. case HAL_SMARTCARD_ERROR_CB_ID :
  438. hsmartcard->ErrorCallback = pCallback;
  439. break;
  440. case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
  441. hsmartcard->AbortCpltCallback = pCallback;
  442. break;
  443. case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
  444. hsmartcard->AbortTransmitCpltCallback = pCallback;
  445. break;
  446. case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
  447. hsmartcard->AbortReceiveCpltCallback = pCallback;
  448. break;
  449. case HAL_SMARTCARD_RX_FIFO_FULL_CB_ID :
  450. hsmartcard->RxFifoFullCallback = pCallback;
  451. break;
  452. case HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID :
  453. hsmartcard->TxFifoEmptyCallback = pCallback;
  454. break;
  455. case HAL_SMARTCARD_MSPINIT_CB_ID :
  456. hsmartcard->MspInitCallback = pCallback;
  457. break;
  458. case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  459. hsmartcard->MspDeInitCallback = pCallback;
  460. break;
  461. default :
  462. /* Update the error code */
  463. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  464. /* Return error status */
  465. status = HAL_ERROR;
  466. break;
  467. }
  468. }
  469. else if (hsmartcard->gState == HAL_SMARTCARD_STATE_RESET)
  470. {
  471. switch (CallbackID)
  472. {
  473. case HAL_SMARTCARD_MSPINIT_CB_ID :
  474. hsmartcard->MspInitCallback = pCallback;
  475. break;
  476. case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  477. hsmartcard->MspDeInitCallback = pCallback;
  478. break;
  479. default :
  480. /* Update the error code */
  481. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  482. /* Return error status */
  483. status = HAL_ERROR;
  484. break;
  485. }
  486. }
  487. else
  488. {
  489. /* Update the error code */
  490. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  491. /* Return error status */
  492. status = HAL_ERROR;
  493. }
  494. /* Release Lock */
  495. __HAL_UNLOCK(hsmartcard);
  496. return status;
  497. }
  498. /**
  499. * @brief Unregister an SMARTCARD callback
  500. * SMARTCARD callback is redirected to the weak predefined callback
  501. * @param hsmartcard smartcard handle
  502. * @param CallbackID ID of the callback to be unregistered
  503. * This parameter can be one of the following values:
  504. * @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
  505. * @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
  506. * @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
  507. * @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  508. * @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  509. * @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  510. * @arg @ref HAL_SMARTCARD_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
  511. * @arg @ref HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
  512. * @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
  513. * @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
  514. * @retval HAL status
  515. */
  516. HAL_StatusTypeDef HAL_SMARTCARD_UnRegisterCallback(SMARTCARD_HandleTypeDef *hsmartcard,
  517. HAL_SMARTCARD_CallbackIDTypeDef CallbackID)
  518. {
  519. HAL_StatusTypeDef status = HAL_OK;
  520. /* Process locked */
  521. __HAL_LOCK(hsmartcard);
  522. if (HAL_SMARTCARD_STATE_READY == hsmartcard->gState)
  523. {
  524. switch (CallbackID)
  525. {
  526. case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
  527. hsmartcard->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */
  528. break;
  529. case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
  530. hsmartcard->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */
  531. break;
  532. case HAL_SMARTCARD_ERROR_CB_ID :
  533. hsmartcard->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */
  534. break;
  535. case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
  536. hsmartcard->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  537. break;
  538. case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
  539. hsmartcard->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
  540. break;
  541. case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
  542. hsmartcard->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
  543. break;
  544. case HAL_SMARTCARD_RX_FIFO_FULL_CB_ID :
  545. hsmartcard->RxFifoFullCallback = HAL_SMARTCARDEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */
  546. break;
  547. case HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID :
  548. hsmartcard->TxFifoEmptyCallback = HAL_SMARTCARDEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
  549. break;
  550. case HAL_SMARTCARD_MSPINIT_CB_ID :
  551. hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit; /* Legacy weak MspInitCallback */
  552. break;
  553. case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  554. hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit; /* Legacy weak MspDeInitCallback */
  555. break;
  556. default :
  557. /* Update the error code */
  558. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  559. /* Return error status */
  560. status = HAL_ERROR;
  561. break;
  562. }
  563. }
  564. else if (HAL_SMARTCARD_STATE_RESET == hsmartcard->gState)
  565. {
  566. switch (CallbackID)
  567. {
  568. case HAL_SMARTCARD_MSPINIT_CB_ID :
  569. hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit;
  570. break;
  571. case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  572. hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
  573. break;
  574. default :
  575. /* Update the error code */
  576. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  577. /* Return error status */
  578. status = HAL_ERROR;
  579. break;
  580. }
  581. }
  582. else
  583. {
  584. /* Update the error code */
  585. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  586. /* Return error status */
  587. status = HAL_ERROR;
  588. }
  589. /* Release Lock */
  590. __HAL_UNLOCK(hsmartcard);
  591. return status;
  592. }
  593. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  594. /**
  595. * @}
  596. */
  597. /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
  598. * @brief SMARTCARD Transmit and Receive functions
  599. *
  600. @verbatim
  601. ==============================================================================
  602. ##### IO operation functions #####
  603. ==============================================================================
  604. [..]
  605. This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
  606. [..]
  607. Smartcard is a single wire half duplex communication protocol.
  608. The Smartcard interface is designed to support asynchronous protocol Smartcards as
  609. defined in the ISO 7816-3 standard. The USART should be configured as:
  610. (+) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
  611. (+) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
  612. [..]
  613. (#) There are two modes of transfer:
  614. (##) Blocking mode: The communication is performed in polling mode.
  615. The HAL status of all data processing is returned by the same function
  616. after finishing transfer.
  617. (##) Non-Blocking mode: The communication is performed using Interrupts
  618. or DMA, the relevant API's return the HAL status.
  619. The end of the data processing will be indicated through the
  620. dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
  621. using DMA mode.
  622. (##) The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
  623. will be executed respectively at the end of the Transmit or Receive process
  624. The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication
  625. error is detected.
  626. (#) Blocking mode APIs are :
  627. (##) HAL_SMARTCARD_Transmit()
  628. (##) HAL_SMARTCARD_Receive()
  629. (#) Non Blocking mode APIs with Interrupt are :
  630. (##) HAL_SMARTCARD_Transmit_IT()
  631. (##) HAL_SMARTCARD_Receive_IT()
  632. (##) HAL_SMARTCARD_IRQHandler()
  633. (#) Non Blocking mode functions with DMA are :
  634. (##) HAL_SMARTCARD_Transmit_DMA()
  635. (##) HAL_SMARTCARD_Receive_DMA()
  636. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  637. (##) HAL_SMARTCARD_TxCpltCallback()
  638. (##) HAL_SMARTCARD_RxCpltCallback()
  639. (##) HAL_SMARTCARD_ErrorCallback()
  640. (#) Non-Blocking mode transfers could be aborted using Abort API's :
  641. (##) HAL_SMARTCARD_Abort()
  642. (##) HAL_SMARTCARD_AbortTransmit()
  643. (##) HAL_SMARTCARD_AbortReceive()
  644. (##) HAL_SMARTCARD_Abort_IT()
  645. (##) HAL_SMARTCARD_AbortTransmit_IT()
  646. (##) HAL_SMARTCARD_AbortReceive_IT()
  647. (#) For Abort services based on interrupts (HAL_SMARTCARD_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
  648. (##) HAL_SMARTCARD_AbortCpltCallback()
  649. (##) HAL_SMARTCARD_AbortTransmitCpltCallback()
  650. (##) HAL_SMARTCARD_AbortReceiveCpltCallback()
  651. (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  652. Errors are handled as follows :
  653. (##) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  654. to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
  655. Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
  656. and HAL_SMARTCARD_ErrorCallback() user callback is executed. Transfer is kept ongoing on SMARTCARD side.
  657. If user wants to abort it, Abort services should be called by user.
  658. (##) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  659. This concerns Frame Error in Interrupt mode tranmission, Overrun Error in Interrupt mode reception and all errors in DMA mode.
  660. Error code is set to allow user to identify error type, and HAL_SMARTCARD_ErrorCallback() user callback is executed.
  661. @endverbatim
  662. * @{
  663. */
  664. /**
  665. * @brief Send an amount of data in blocking mode.
  666. * @note When FIFO mode is enabled, writing a data in the TDR register adds one
  667. * data to the TXFIFO. Write operations to the TDR register are performed
  668. * when TXFNF flag is set. From hardware perspective, TXFNF flag and
  669. * TXE are mapped on the same bit-field.
  670. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  671. * the configuration information for the specified SMARTCARD module.
  672. * @param pData pointer to data buffer.
  673. * @param Size amount of data to be sent.
  674. * @param Timeout Timeout duration.
  675. * @retval HAL status
  676. */
  677. HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size,
  678. uint32_t Timeout)
  679. {
  680. uint32_t tickstart;
  681. uint8_t *ptmpdata = pData;
  682. /* Check that a Tx process is not already ongoing */
  683. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  684. {
  685. if ((ptmpdata == NULL) || (Size == 0U))
  686. {
  687. return HAL_ERROR;
  688. }
  689. /* Process Locked */
  690. __HAL_LOCK(hsmartcard);
  691. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  692. /* Init tickstart for timeout management */
  693. tickstart = HAL_GetTick();
  694. /* Disable the Peripheral first to update mode for TX master */
  695. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  696. /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
  697. the bidirectional line to detect a NACK signal in case of parity error.
  698. Therefore, the receiver block must be enabled as well (RE bit must be set). */
  699. if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
  700. && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  701. {
  702. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  703. }
  704. /* Enable Tx */
  705. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  706. /* Enable the Peripheral */
  707. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  708. /* Perform a TX/RX FIFO Flush */
  709. __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
  710. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  711. hsmartcard->TxXferSize = Size;
  712. hsmartcard->TxXferCount = Size;
  713. while (hsmartcard->TxXferCount > 0U)
  714. {
  715. hsmartcard->TxXferCount--;
  716. if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  717. {
  718. return HAL_TIMEOUT;
  719. }
  720. hsmartcard->Instance->TDR = (uint8_t)(*ptmpdata & 0xFFU);
  721. ptmpdata++;
  722. }
  723. if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_TRANSMISSION_COMPLETION_FLAG(hsmartcard), RESET, tickstart,
  724. Timeout) != HAL_OK)
  725. {
  726. return HAL_TIMEOUT;
  727. }
  728. /* Disable the Peripheral first to update mode */
  729. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  730. if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
  731. && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  732. {
  733. /* In case of TX only mode, if NACK is enabled, receiver block has been enabled
  734. for Transmit phase. Disable this receiver block. */
  735. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  736. }
  737. if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
  738. || (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  739. {
  740. /* Perform a TX FIFO Flush at end of Tx phase, as all sent bytes are appearing in Rx Data register */
  741. __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
  742. }
  743. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  744. /* At end of Tx process, restore hsmartcard->gState to Ready */
  745. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  746. /* Process Unlocked */
  747. __HAL_UNLOCK(hsmartcard);
  748. return HAL_OK;
  749. }
  750. else
  751. {
  752. return HAL_BUSY;
  753. }
  754. }
  755. /**
  756. * @brief Receive an amount of data in blocking mode.
  757. * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
  758. * is not empty. Read operations from the RDR register are performed when
  759. * RXFNE flag is set. From hardware perspective, RXFNE flag and
  760. * RXNE are mapped on the same bit-field.
  761. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  762. * the configuration information for the specified SMARTCARD module.
  763. * @param pData pointer to data buffer.
  764. * @param Size amount of data to be received.
  765. * @param Timeout Timeout duration.
  766. * @retval HAL status
  767. */
  768. HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size,
  769. uint32_t Timeout)
  770. {
  771. uint32_t tickstart;
  772. uint8_t *ptmpdata = pData;
  773. /* Check that a Rx process is not already ongoing */
  774. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  775. {
  776. if ((ptmpdata == NULL) || (Size == 0U))
  777. {
  778. return HAL_ERROR;
  779. }
  780. /* Process Locked */
  781. __HAL_LOCK(hsmartcard);
  782. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  783. hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  784. /* Init tickstart for timeout management */
  785. tickstart = HAL_GetTick();
  786. hsmartcard->RxXferSize = Size;
  787. hsmartcard->RxXferCount = Size;
  788. /* Check the remain data to be received */
  789. while (hsmartcard->RxXferCount > 0U)
  790. {
  791. hsmartcard->RxXferCount--;
  792. if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  793. {
  794. return HAL_TIMEOUT;
  795. }
  796. *ptmpdata = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0x00FF);
  797. ptmpdata++;
  798. }
  799. /* At end of Rx process, restore hsmartcard->RxState to Ready */
  800. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  801. /* Process Unlocked */
  802. __HAL_UNLOCK(hsmartcard);
  803. return HAL_OK;
  804. }
  805. else
  806. {
  807. return HAL_BUSY;
  808. }
  809. }
  810. /**
  811. * @brief Send an amount of data in interrupt mode.
  812. * @note When FIFO mode is disabled, USART interrupt is generated whenever
  813. * USART_TDR register is empty, i.e one interrupt per data to transmit.
  814. * @note When FIFO mode is enabled, USART interrupt is generated whenever
  815. * TXFIFO threshold reached. In that case the interrupt rate depends on
  816. * TXFIFO threshold configuration.
  817. * @note This function sets the hsmartcard->TxIsr function pointer according to
  818. * the FIFO mode (data transmission processing depends on FIFO mode).
  819. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  820. * the configuration information for the specified SMARTCARD module.
  821. * @param pData pointer to data buffer.
  822. * @param Size amount of data to be sent.
  823. * @retval HAL status
  824. */
  825. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  826. {
  827. /* Check that a Tx process is not already ongoing */
  828. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  829. {
  830. if ((pData == NULL) || (Size == 0U))
  831. {
  832. return HAL_ERROR;
  833. }
  834. /* Process Locked */
  835. __HAL_LOCK(hsmartcard);
  836. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  837. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  838. hsmartcard->pTxBuffPtr = pData;
  839. hsmartcard->TxXferSize = Size;
  840. hsmartcard->TxXferCount = Size;
  841. hsmartcard->TxISR = NULL;
  842. /* Disable the Peripheral first to update mode for TX master */
  843. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  844. /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
  845. the bidirectional line to detect a NACK signal in case of parity error.
  846. Therefore, the receiver block must be enabled as well (RE bit must be set). */
  847. if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
  848. && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  849. {
  850. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  851. }
  852. /* Enable Tx */
  853. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  854. /* Enable the Peripheral */
  855. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  856. /* Perform a TX/RX FIFO Flush */
  857. __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
  858. /* Configure Tx interrupt processing */
  859. if (hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE)
  860. {
  861. /* Set the Tx ISR function pointer */
  862. hsmartcard->TxISR = SMARTCARD_TxISR_FIFOEN;
  863. /* Process Unlocked */
  864. __HAL_UNLOCK(hsmartcard);
  865. /* Enable the SMARTCARD Error Interrupt: (Frame error) */
  866. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  867. /* Enable the TX FIFO threshold interrupt */
  868. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
  869. }
  870. else
  871. {
  872. /* Set the Tx ISR function pointer */
  873. hsmartcard->TxISR = SMARTCARD_TxISR;
  874. /* Process Unlocked */
  875. __HAL_UNLOCK(hsmartcard);
  876. /* Enable the SMARTCARD Error Interrupt: (Frame error) */
  877. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  878. /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */
  879. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  880. }
  881. return HAL_OK;
  882. }
  883. else
  884. {
  885. return HAL_BUSY;
  886. }
  887. }
  888. /**
  889. * @brief Receive an amount of data in interrupt mode.
  890. * @note When FIFO mode is disabled, USART interrupt is generated whenever
  891. * USART_RDR register can be read, i.e one interrupt per data to receive.
  892. * @note When FIFO mode is enabled, USART interrupt is generated whenever
  893. * RXFIFO threshold reached. In that case the interrupt rate depends on
  894. * RXFIFO threshold configuration.
  895. * @note This function sets the hsmartcard->RxIsr function pointer according to
  896. * the FIFO mode (data reception processing depends on FIFO mode).
  897. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  898. * the configuration information for the specified SMARTCARD module.
  899. * @param pData pointer to data buffer.
  900. * @param Size amount of data to be received.
  901. * @retval HAL status
  902. */
  903. HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  904. {
  905. /* Check that a Rx process is not already ongoing */
  906. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  907. {
  908. if ((pData == NULL) || (Size == 0U))
  909. {
  910. return HAL_ERROR;
  911. }
  912. /* Process Locked */
  913. __HAL_LOCK(hsmartcard);
  914. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  915. hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  916. hsmartcard->pRxBuffPtr = pData;
  917. hsmartcard->RxXferSize = Size;
  918. hsmartcard->RxXferCount = Size;
  919. /* Configure Rx interrupt processing */
  920. if ((hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE) && (Size >= hsmartcard->NbRxDataToProcess))
  921. {
  922. /* Set the Rx ISR function pointer */
  923. hsmartcard->RxISR = SMARTCARD_RxISR_FIFOEN;
  924. /* Process Unlocked */
  925. __HAL_UNLOCK(hsmartcard);
  926. /* Enable the SMARTCART Parity Error interrupt and RX FIFO Threshold interrupt */
  927. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  928. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE);
  929. }
  930. else
  931. {
  932. /* Set the Rx ISR function pointer */
  933. hsmartcard->RxISR = SMARTCARD_RxISR;
  934. /* Process Unlocked */
  935. __HAL_UNLOCK(hsmartcard);
  936. /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
  937. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
  938. }
  939. /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  940. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  941. return HAL_OK;
  942. }
  943. else
  944. {
  945. return HAL_BUSY;
  946. }
  947. }
  948. /**
  949. * @brief Send an amount of data in DMA mode.
  950. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  951. * the configuration information for the specified SMARTCARD module.
  952. * @param pData pointer to data buffer.
  953. * @param Size amount of data to be sent.
  954. * @retval HAL status
  955. */
  956. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  957. {
  958. /* Check that a Tx process is not already ongoing */
  959. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  960. {
  961. if ((pData == NULL) || (Size == 0U))
  962. {
  963. return HAL_ERROR;
  964. }
  965. /* Process Locked */
  966. __HAL_LOCK(hsmartcard);
  967. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  968. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  969. hsmartcard->pTxBuffPtr = pData;
  970. hsmartcard->TxXferSize = Size;
  971. hsmartcard->TxXferCount = Size;
  972. /* Disable the Peripheral first to update mode for TX master */
  973. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  974. /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
  975. the bidirectional line to detect a NACK signal in case of parity error.
  976. Therefore, the receiver block must be enabled as well (RE bit must be set). */
  977. if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
  978. && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  979. {
  980. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  981. }
  982. /* Enable Tx */
  983. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  984. /* Enable the Peripheral */
  985. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  986. /* Perform a TX/RX FIFO Flush */
  987. __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
  988. /* Set the SMARTCARD DMA transfer complete callback */
  989. hsmartcard->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
  990. /* Set the SMARTCARD error callback */
  991. hsmartcard->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
  992. /* Set the DMA abort callback */
  993. hsmartcard->hdmatx->XferAbortCallback = NULL;
  994. /* Enable the SMARTCARD transmit DMA channel */
  995. if (HAL_DMA_Start_IT(hsmartcard->hdmatx, (uint32_t)hsmartcard->pTxBuffPtr, (uint32_t)&hsmartcard->Instance->TDR,
  996. Size) == HAL_OK)
  997. {
  998. /* Clear the TC flag in the ICR register */
  999. CLEAR_BIT(hsmartcard->Instance->ICR, USART_ICR_TCCF);
  1000. /* Process Unlocked */
  1001. __HAL_UNLOCK(hsmartcard);
  1002. /* Enable the UART Error Interrupt: (Frame error) */
  1003. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1004. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  1005. in the SMARTCARD associated USART CR3 register */
  1006. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1007. return HAL_OK;
  1008. }
  1009. else
  1010. {
  1011. /* Set error code to DMA */
  1012. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1013. /* Process Unlocked */
  1014. __HAL_UNLOCK(hsmartcard);
  1015. /* Restore hsmartcard->State to ready */
  1016. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1017. return HAL_ERROR;
  1018. }
  1019. }
  1020. else
  1021. {
  1022. return HAL_BUSY;
  1023. }
  1024. }
  1025. /**
  1026. * @brief Receive an amount of data in DMA mode.
  1027. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1028. * the configuration information for the specified SMARTCARD module.
  1029. * @param pData pointer to data buffer.
  1030. * @param Size amount of data to be received.
  1031. * @note The SMARTCARD-associated USART parity is enabled (PCE = 1),
  1032. * the received data contain the parity bit (MSB position).
  1033. * @retval HAL status
  1034. */
  1035. HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  1036. {
  1037. /* Check that a Rx process is not already ongoing */
  1038. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  1039. {
  1040. if ((pData == NULL) || (Size == 0U))
  1041. {
  1042. return HAL_ERROR;
  1043. }
  1044. /* Process Locked */
  1045. __HAL_LOCK(hsmartcard);
  1046. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1047. hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  1048. hsmartcard->pRxBuffPtr = pData;
  1049. hsmartcard->RxXferSize = Size;
  1050. /* Set the SMARTCARD DMA transfer complete callback */
  1051. hsmartcard->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
  1052. /* Set the SMARTCARD DMA error callback */
  1053. hsmartcard->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
  1054. /* Set the DMA abort callback */
  1055. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1056. /* Enable the DMA channel */
  1057. if (HAL_DMA_Start_IT(hsmartcard->hdmarx, (uint32_t)&hsmartcard->Instance->RDR, (uint32_t)hsmartcard->pRxBuffPtr,
  1058. Size) == HAL_OK)
  1059. {
  1060. /* Process Unlocked */
  1061. __HAL_UNLOCK(hsmartcard);
  1062. /* Enable the SMARTCARD Parity Error Interrupt */
  1063. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  1064. /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  1065. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1066. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  1067. in the SMARTCARD associated USART CR3 register */
  1068. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1069. return HAL_OK;
  1070. }
  1071. else
  1072. {
  1073. /* Set error code to DMA */
  1074. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1075. /* Process Unlocked */
  1076. __HAL_UNLOCK(hsmartcard);
  1077. /* Restore hsmartcard->State to ready */
  1078. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1079. return HAL_ERROR;
  1080. }
  1081. }
  1082. else
  1083. {
  1084. return HAL_BUSY;
  1085. }
  1086. }
  1087. /**
  1088. * @brief Abort ongoing transfers (blocking mode).
  1089. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1090. * the configuration information for the specified SMARTCARD module.
  1091. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1092. * This procedure performs following operations :
  1093. * - Disable SMARTCARD Interrupts (Tx and Rx)
  1094. * - Disable the DMA transfer in the peripheral register (if enabled)
  1095. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1096. * - Set handle State to READY
  1097. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1098. * @retval HAL status
  1099. */
  1100. HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsmartcard)
  1101. {
  1102. /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
  1103. CLEAR_BIT(hsmartcard->Instance->CR1,
  1104. (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE |
  1105. USART_CR1_EOBIE));
  1106. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
  1107. /* Disable the SMARTCARD DMA Tx request if enabled */
  1108. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1109. {
  1110. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1111. /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  1112. if (hsmartcard->hdmatx != NULL)
  1113. {
  1114. /* Set the SMARTCARD DMA Abort callback to Null.
  1115. No call back execution at end of DMA abort procedure */
  1116. hsmartcard->hdmatx->XferAbortCallback = NULL;
  1117. if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK)
  1118. {
  1119. if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1120. {
  1121. /* Set error code to DMA */
  1122. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1123. return HAL_TIMEOUT;
  1124. }
  1125. }
  1126. }
  1127. }
  1128. /* Disable the SMARTCARD DMA Rx request if enabled */
  1129. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1130. {
  1131. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1132. /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  1133. if (hsmartcard->hdmarx != NULL)
  1134. {
  1135. /* Set the SMARTCARD DMA Abort callback to Null.
  1136. No call back execution at end of DMA abort procedure */
  1137. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1138. if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK)
  1139. {
  1140. if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1141. {
  1142. /* Set error code to DMA */
  1143. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1144. return HAL_TIMEOUT;
  1145. }
  1146. }
  1147. }
  1148. }
  1149. /* Reset Tx and Rx transfer counters */
  1150. hsmartcard->TxXferCount = 0U;
  1151. hsmartcard->RxXferCount = 0U;
  1152. /* Clear the Error flags in the ICR register */
  1153. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1154. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
  1155. SMARTCARD_CLEAR_EOBF);
  1156. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  1157. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1158. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1159. /* Reset Handle ErrorCode to No Error */
  1160. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1161. return HAL_OK;
  1162. }
  1163. /**
  1164. * @brief Abort ongoing Transmit transfer (blocking mode).
  1165. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1166. * the configuration information for the specified SMARTCARD module.
  1167. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1168. * This procedure performs following operations :
  1169. * - Disable SMARTCARD Interrupts (Tx)
  1170. * - Disable the DMA transfer in the peripheral register (if enabled)
  1171. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1172. * - Set handle State to READY
  1173. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1174. * @retval HAL status
  1175. */
  1176. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsmartcard)
  1177. {
  1178. /* Disable TCIE, TXEIE and TXFTIE interrupts */
  1179. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  1180. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
  1181. /* Check if a receive process is ongoing or not. If not disable ERR IT */
  1182. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  1183. {
  1184. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1185. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1186. }
  1187. /* Disable the SMARTCARD DMA Tx request if enabled */
  1188. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1189. {
  1190. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1191. /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  1192. if (hsmartcard->hdmatx != NULL)
  1193. {
  1194. /* Set the SMARTCARD DMA Abort callback to Null.
  1195. No call back execution at end of DMA abort procedure */
  1196. hsmartcard->hdmatx->XferAbortCallback = NULL;
  1197. if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK)
  1198. {
  1199. if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1200. {
  1201. /* Set error code to DMA */
  1202. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1203. return HAL_TIMEOUT;
  1204. }
  1205. }
  1206. }
  1207. }
  1208. /* Reset Tx transfer counter */
  1209. hsmartcard->TxXferCount = 0U;
  1210. /* Clear the Error flags in the ICR register */
  1211. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  1212. /* Restore hsmartcard->gState to Ready */
  1213. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1214. return HAL_OK;
  1215. }
  1216. /**
  1217. * @brief Abort ongoing Receive transfer (blocking mode).
  1218. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1219. * the configuration information for the specified SMARTCARD module.
  1220. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1221. * This procedure performs following operations :
  1222. * - Disable SMARTCARD Interrupts (Rx)
  1223. * - Disable the DMA transfer in the peripheral register (if enabled)
  1224. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1225. * - Set handle State to READY
  1226. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1227. * @retval HAL status
  1228. */
  1229. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsmartcard)
  1230. {
  1231. /* Disable RTOIE, EOBIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
  1232. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  1233. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
  1234. /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
  1235. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  1236. {
  1237. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1238. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1239. }
  1240. /* Disable the SMARTCARD DMA Rx request if enabled */
  1241. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1242. {
  1243. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1244. /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  1245. if (hsmartcard->hdmarx != NULL)
  1246. {
  1247. /* Set the SMARTCARD DMA Abort callback to Null.
  1248. No call back execution at end of DMA abort procedure */
  1249. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1250. if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK)
  1251. {
  1252. if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1253. {
  1254. /* Set error code to DMA */
  1255. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1256. return HAL_TIMEOUT;
  1257. }
  1258. }
  1259. }
  1260. }
  1261. /* Reset Rx transfer counter */
  1262. hsmartcard->RxXferCount = 0U;
  1263. /* Clear the Error flags in the ICR register */
  1264. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1265. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
  1266. SMARTCARD_CLEAR_EOBF);
  1267. /* Restore hsmartcard->RxState to Ready */
  1268. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1269. return HAL_OK;
  1270. }
  1271. /**
  1272. * @brief Abort ongoing transfers (Interrupt mode).
  1273. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1274. * the configuration information for the specified SMARTCARD module.
  1275. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1276. * This procedure performs following operations :
  1277. * - Disable SMARTCARD Interrupts (Tx and Rx)
  1278. * - Disable the DMA transfer in the peripheral register (if enabled)
  1279. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1280. * - Set handle State to READY
  1281. * - At abort completion, call user abort complete callback
  1282. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1283. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1284. * @retval HAL status
  1285. */
  1286. HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1287. {
  1288. uint32_t abortcplt = 1U;
  1289. /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
  1290. CLEAR_BIT(hsmartcard->Instance->CR1,
  1291. (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE |
  1292. USART_CR1_EOBIE));
  1293. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
  1294. /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
  1295. before any call to DMA Abort functions */
  1296. /* DMA Tx Handle is valid */
  1297. if (hsmartcard->hdmatx != NULL)
  1298. {
  1299. /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
  1300. Otherwise, set it to NULL */
  1301. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1302. {
  1303. hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
  1304. }
  1305. else
  1306. {
  1307. hsmartcard->hdmatx->XferAbortCallback = NULL;
  1308. }
  1309. }
  1310. /* DMA Rx Handle is valid */
  1311. if (hsmartcard->hdmarx != NULL)
  1312. {
  1313. /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
  1314. Otherwise, set it to NULL */
  1315. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1316. {
  1317. hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
  1318. }
  1319. else
  1320. {
  1321. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1322. }
  1323. }
  1324. /* Disable the SMARTCARD DMA Tx request if enabled */
  1325. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1326. {
  1327. /* Disable DMA Tx at UART level */
  1328. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1329. /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
  1330. if (hsmartcard->hdmatx != NULL)
  1331. {
  1332. /* SMARTCARD Tx DMA Abort callback has already been initialised :
  1333. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1334. /* Abort DMA TX */
  1335. if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  1336. {
  1337. hsmartcard->hdmatx->XferAbortCallback = NULL;
  1338. }
  1339. else
  1340. {
  1341. abortcplt = 0U;
  1342. }
  1343. }
  1344. }
  1345. /* Disable the SMARTCARD DMA Rx request if enabled */
  1346. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1347. {
  1348. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1349. /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
  1350. if (hsmartcard->hdmarx != NULL)
  1351. {
  1352. /* SMARTCARD Rx DMA Abort callback has already been initialised :
  1353. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1354. /* Abort DMA RX */
  1355. if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  1356. {
  1357. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1358. abortcplt = 1U;
  1359. }
  1360. else
  1361. {
  1362. abortcplt = 0U;
  1363. }
  1364. }
  1365. }
  1366. /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  1367. if (abortcplt == 1U)
  1368. {
  1369. /* Reset Tx and Rx transfer counters */
  1370. hsmartcard->TxXferCount = 0U;
  1371. hsmartcard->RxXferCount = 0U;
  1372. /* Clear ISR function pointers */
  1373. hsmartcard->RxISR = NULL;
  1374. hsmartcard->TxISR = NULL;
  1375. /* Reset errorCode */
  1376. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1377. /* Clear the Error flags in the ICR register */
  1378. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1379. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
  1380. SMARTCARD_CLEAR_EOBF);
  1381. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  1382. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1383. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1384. /* As no DMA to be aborted, call directly user Abort complete callback */
  1385. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1386. /* Call registered Abort complete callback */
  1387. hsmartcard->AbortCpltCallback(hsmartcard);
  1388. #else
  1389. /* Call legacy weak Abort complete callback */
  1390. HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  1391. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1392. }
  1393. return HAL_OK;
  1394. }
  1395. /**
  1396. * @brief Abort ongoing Transmit transfer (Interrupt mode).
  1397. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1398. * the configuration information for the specified SMARTCARD module.
  1399. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1400. * This procedure performs following operations :
  1401. * - Disable SMARTCARD Interrupts (Tx)
  1402. * - Disable the DMA transfer in the peripheral register (if enabled)
  1403. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1404. * - Set handle State to READY
  1405. * - At abort completion, call user abort complete callback
  1406. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1407. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1408. * @retval HAL status
  1409. */
  1410. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1411. {
  1412. /* Disable TCIE, TXEIE and TXFTIE interrupts */
  1413. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  1414. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
  1415. /* Check if a receive process is ongoing or not. If not disable ERR IT */
  1416. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  1417. {
  1418. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1419. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1420. }
  1421. /* Disable the SMARTCARD DMA Tx request if enabled */
  1422. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1423. {
  1424. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1425. /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
  1426. if (hsmartcard->hdmatx != NULL)
  1427. {
  1428. /* Set the SMARTCARD DMA Abort callback :
  1429. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1430. hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
  1431. /* Abort DMA TX */
  1432. if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  1433. {
  1434. /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
  1435. hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
  1436. }
  1437. }
  1438. else
  1439. {
  1440. /* Reset Tx transfer counter */
  1441. hsmartcard->TxXferCount = 0U;
  1442. /* Clear TxISR function pointers */
  1443. hsmartcard->TxISR = NULL;
  1444. /* Restore hsmartcard->gState to Ready */
  1445. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1446. /* As no DMA to be aborted, call directly user Abort complete callback */
  1447. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1448. /* Call registered Abort Transmit Complete Callback */
  1449. hsmartcard->AbortTransmitCpltCallback(hsmartcard);
  1450. #else
  1451. /* Call legacy weak Abort Transmit Complete Callback */
  1452. HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  1453. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1454. }
  1455. }
  1456. else
  1457. {
  1458. /* Reset Tx transfer counter */
  1459. hsmartcard->TxXferCount = 0U;
  1460. /* Clear TxISR function pointers */
  1461. hsmartcard->TxISR = NULL;
  1462. /* Clear the Error flags in the ICR register */
  1463. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  1464. /* Restore hsmartcard->gState to Ready */
  1465. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1466. /* As no DMA to be aborted, call directly user Abort complete callback */
  1467. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1468. /* Call registered Abort Transmit Complete Callback */
  1469. hsmartcard->AbortTransmitCpltCallback(hsmartcard);
  1470. #else
  1471. /* Call legacy weak Abort Transmit Complete Callback */
  1472. HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  1473. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1474. }
  1475. return HAL_OK;
  1476. }
  1477. /**
  1478. * @brief Abort ongoing Receive transfer (Interrupt mode).
  1479. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1480. * the configuration information for the specified SMARTCARD module.
  1481. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1482. * This procedure performs following operations :
  1483. * - Disable SMARTCARD Interrupts (Rx)
  1484. * - Disable the DMA transfer in the peripheral register (if enabled)
  1485. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1486. * - Set handle State to READY
  1487. * - At abort completion, call user abort complete callback
  1488. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1489. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1490. * @retval HAL status
  1491. */
  1492. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1493. {
  1494. /* Disable RTOIE, EOBIE, RXNE, PE, RXFT and ERR (Frame error, noise error, overrun error) interrupts */
  1495. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  1496. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
  1497. /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
  1498. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  1499. {
  1500. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1501. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1502. }
  1503. /* Disable the SMARTCARD DMA Rx request if enabled */
  1504. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1505. {
  1506. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1507. /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
  1508. if (hsmartcard->hdmarx != NULL)
  1509. {
  1510. /* Set the SMARTCARD DMA Abort callback :
  1511. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1512. hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
  1513. /* Abort DMA RX */
  1514. if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  1515. {
  1516. /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
  1517. hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
  1518. }
  1519. }
  1520. else
  1521. {
  1522. /* Reset Rx transfer counter */
  1523. hsmartcard->RxXferCount = 0U;
  1524. /* Clear RxISR function pointer */
  1525. hsmartcard->RxISR = NULL;
  1526. /* Clear the Error flags in the ICR register */
  1527. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1528. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
  1529. SMARTCARD_CLEAR_EOBF);
  1530. /* Restore hsmartcard->RxState to Ready */
  1531. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1532. /* As no DMA to be aborted, call directly user Abort complete callback */
  1533. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1534. /* Call registered Abort Receive Complete Callback */
  1535. hsmartcard->AbortReceiveCpltCallback(hsmartcard);
  1536. #else
  1537. /* Call legacy weak Abort Receive Complete Callback */
  1538. HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  1539. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1540. }
  1541. }
  1542. else
  1543. {
  1544. /* Reset Rx transfer counter */
  1545. hsmartcard->RxXferCount = 0U;
  1546. /* Clear RxISR function pointer */
  1547. hsmartcard->RxISR = NULL;
  1548. /* Clear the Error flags in the ICR register */
  1549. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1550. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
  1551. SMARTCARD_CLEAR_EOBF);
  1552. /* Restore hsmartcard->RxState to Ready */
  1553. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1554. /* As no DMA to be aborted, call directly user Abort complete callback */
  1555. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1556. /* Call registered Abort Receive Complete Callback */
  1557. hsmartcard->AbortReceiveCpltCallback(hsmartcard);
  1558. #else
  1559. /* Call legacy weak Abort Receive Complete Callback */
  1560. HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  1561. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1562. }
  1563. return HAL_OK;
  1564. }
  1565. /**
  1566. * @brief Handle SMARTCARD interrupt requests.
  1567. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1568. * the configuration information for the specified SMARTCARD module.
  1569. * @retval None
  1570. */
  1571. void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsmartcard)
  1572. {
  1573. uint32_t isrflags = READ_REG(hsmartcard->Instance->ISR);
  1574. uint32_t cr1its = READ_REG(hsmartcard->Instance->CR1);
  1575. uint32_t cr3its = READ_REG(hsmartcard->Instance->CR3);
  1576. uint32_t errorflags;
  1577. uint32_t errorcode;
  1578. /* If no error occurs */
  1579. errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
  1580. if (errorflags == 0U)
  1581. {
  1582. /* SMARTCARD in mode Receiver ---------------------------------------------------*/
  1583. if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
  1584. && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
  1585. || ((cr3its & USART_CR3_RXFTIE) != 0U)))
  1586. {
  1587. if (hsmartcard->RxISR != NULL)
  1588. {
  1589. hsmartcard->RxISR(hsmartcard);
  1590. }
  1591. return;
  1592. }
  1593. }
  1594. /* If some errors occur */
  1595. if ((errorflags != 0U)
  1596. && ((((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)
  1597. || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != 0U))))
  1598. {
  1599. /* SMARTCARD parity error interrupt occurred -------------------------------------*/
  1600. if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
  1601. {
  1602. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_PEF);
  1603. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
  1604. }
  1605. /* SMARTCARD frame error interrupt occurred --------------------------------------*/
  1606. if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  1607. {
  1608. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_FEF);
  1609. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
  1610. }
  1611. /* SMARTCARD noise error interrupt occurred --------------------------------------*/
  1612. if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  1613. {
  1614. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_NEF);
  1615. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
  1616. }
  1617. /* SMARTCARD Over-Run interrupt occurred -----------------------------------------*/
  1618. if (((isrflags & USART_ISR_ORE) != 0U)
  1619. && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
  1620. || ((cr3its & USART_CR3_RXFTIE) != 0U)
  1621. || ((cr3its & USART_CR3_EIE) != 0U)))
  1622. {
  1623. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_OREF);
  1624. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
  1625. }
  1626. /* SMARTCARD receiver timeout interrupt occurred -----------------------------------------*/
  1627. if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U))
  1628. {
  1629. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_RTOF);
  1630. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_RTO;
  1631. }
  1632. /* Call SMARTCARD Error Call back function if need be --------------------------*/
  1633. if (hsmartcard->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
  1634. {
  1635. /* SMARTCARD in mode Receiver ---------------------------------------------------*/
  1636. if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
  1637. && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
  1638. || ((cr3its & USART_CR3_RXFTIE) != 0U)))
  1639. {
  1640. if (hsmartcard->RxISR != NULL)
  1641. {
  1642. hsmartcard->RxISR(hsmartcard);
  1643. }
  1644. }
  1645. /* If Error is to be considered as blocking :
  1646. - Receiver Timeout error in Reception
  1647. - Overrun error in Reception
  1648. - any error occurs in DMA mode reception
  1649. */
  1650. errorcode = hsmartcard->ErrorCode;
  1651. if ((HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1652. || ((errorcode & (HAL_SMARTCARD_ERROR_RTO | HAL_SMARTCARD_ERROR_ORE)) != 0U))
  1653. {
  1654. /* Blocking error : transfer is aborted
  1655. Set the SMARTCARD state ready to be able to start again the process,
  1656. Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1657. SMARTCARD_EndRxTransfer(hsmartcard);
  1658. /* Disable the SMARTCARD DMA Rx request if enabled */
  1659. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1660. {
  1661. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1662. /* Abort the SMARTCARD DMA Rx channel */
  1663. if (hsmartcard->hdmarx != NULL)
  1664. {
  1665. /* Set the SMARTCARD DMA Abort callback :
  1666. will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
  1667. hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
  1668. /* Abort DMA RX */
  1669. if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  1670. {
  1671. /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
  1672. hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
  1673. }
  1674. }
  1675. else
  1676. {
  1677. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1678. /* Call registered user error callback */
  1679. hsmartcard->ErrorCallback(hsmartcard);
  1680. #else
  1681. /* Call legacy weak user error callback */
  1682. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1683. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1684. }
  1685. }
  1686. else
  1687. {
  1688. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1689. /* Call registered user error callback */
  1690. hsmartcard->ErrorCallback(hsmartcard);
  1691. #else
  1692. /* Call legacy weak user error callback */
  1693. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1694. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1695. }
  1696. }
  1697. /* other error type to be considered as blocking :
  1698. - Frame error in Transmission
  1699. */
  1700. else if ((hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  1701. && ((errorcode & HAL_SMARTCARD_ERROR_FE) != 0U))
  1702. {
  1703. /* Blocking error : transfer is aborted
  1704. Set the SMARTCARD state ready to be able to start again the process,
  1705. Disable Tx Interrupts, and disable Tx DMA request, if ongoing */
  1706. SMARTCARD_EndTxTransfer(hsmartcard);
  1707. /* Disable the SMARTCARD DMA Tx request if enabled */
  1708. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1709. {
  1710. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1711. /* Abort the SMARTCARD DMA Tx channel */
  1712. if (hsmartcard->hdmatx != NULL)
  1713. {
  1714. /* Set the SMARTCARD DMA Abort callback :
  1715. will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
  1716. hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
  1717. /* Abort DMA TX */
  1718. if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  1719. {
  1720. /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
  1721. hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
  1722. }
  1723. }
  1724. else
  1725. {
  1726. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1727. /* Call registered user error callback */
  1728. hsmartcard->ErrorCallback(hsmartcard);
  1729. #else
  1730. /* Call legacy weak user error callback */
  1731. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1732. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1733. }
  1734. }
  1735. else
  1736. {
  1737. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1738. /* Call registered user error callback */
  1739. hsmartcard->ErrorCallback(hsmartcard);
  1740. #else
  1741. /* Call legacy weak user error callback */
  1742. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1743. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1744. }
  1745. }
  1746. else
  1747. {
  1748. /* Non Blocking error : transfer could go on.
  1749. Error is notified to user through user error callback */
  1750. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1751. /* Call registered user error callback */
  1752. hsmartcard->ErrorCallback(hsmartcard);
  1753. #else
  1754. /* Call legacy weak user error callback */
  1755. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1756. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1757. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1758. }
  1759. }
  1760. return;
  1761. } /* End if some error occurs */
  1762. /* SMARTCARD in mode Receiver, end of block interruption ------------------------*/
  1763. if (((isrflags & USART_ISR_EOBF) != 0U) && ((cr1its & USART_CR1_EOBIE) != 0U))
  1764. {
  1765. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1766. __HAL_UNLOCK(hsmartcard);
  1767. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1768. /* Call registered Rx complete callback */
  1769. hsmartcard->RxCpltCallback(hsmartcard);
  1770. #else
  1771. /* Call legacy weak Rx complete callback */
  1772. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  1773. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1774. /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
  1775. to be available during HAL_SMARTCARD_RxCpltCallback() processing */
  1776. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_EOBF);
  1777. return;
  1778. }
  1779. /* SMARTCARD in mode Transmitter ------------------------------------------------*/
  1780. if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
  1781. && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
  1782. || ((cr3its & USART_CR3_TXFTIE) != 0U)))
  1783. {
  1784. if (hsmartcard->TxISR != NULL)
  1785. {
  1786. hsmartcard->TxISR(hsmartcard);
  1787. }
  1788. return;
  1789. }
  1790. /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
  1791. if (__HAL_SMARTCARD_GET_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET)
  1792. {
  1793. if (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET)
  1794. {
  1795. SMARTCARD_EndTransmit_IT(hsmartcard);
  1796. return;
  1797. }
  1798. }
  1799. /* SMARTCARD TX Fifo Empty occurred ----------------------------------------------*/
  1800. if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U))
  1801. {
  1802. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1803. /* Call registered Tx Fifo Empty Callback */
  1804. hsmartcard->TxFifoEmptyCallback(hsmartcard);
  1805. #else
  1806. /* Call legacy weak Tx Fifo Empty Callback */
  1807. HAL_SMARTCARDEx_TxFifoEmptyCallback(hsmartcard);
  1808. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1809. return;
  1810. }
  1811. /* SMARTCARD RX Fifo Full occurred ----------------------------------------------*/
  1812. if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U))
  1813. {
  1814. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1815. /* Call registered Rx Fifo Full Callback */
  1816. hsmartcard->RxFifoFullCallback(hsmartcard);
  1817. #else
  1818. /* Call legacy weak Rx Fifo Full Callback */
  1819. HAL_SMARTCARDEx_RxFifoFullCallback(hsmartcard);
  1820. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1821. return;
  1822. }
  1823. }
  1824. /**
  1825. * @brief Tx Transfer completed callback.
  1826. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1827. * the configuration information for the specified SMARTCARD module.
  1828. * @retval None
  1829. */
  1830. __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1831. {
  1832. /* Prevent unused argument(s) compilation warning */
  1833. UNUSED(hsmartcard);
  1834. /* NOTE : This function should not be modified, when the callback is needed,
  1835. the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
  1836. */
  1837. }
  1838. /**
  1839. * @brief Rx Transfer completed callback.
  1840. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1841. * the configuration information for the specified SMARTCARD module.
  1842. * @retval None
  1843. */
  1844. __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1845. {
  1846. /* Prevent unused argument(s) compilation warning */
  1847. UNUSED(hsmartcard);
  1848. /* NOTE : This function should not be modified, when the callback is needed,
  1849. the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
  1850. */
  1851. }
  1852. /**
  1853. * @brief SMARTCARD error callback.
  1854. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1855. * the configuration information for the specified SMARTCARD module.
  1856. * @retval None
  1857. */
  1858. __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1859. {
  1860. /* Prevent unused argument(s) compilation warning */
  1861. UNUSED(hsmartcard);
  1862. /* NOTE : This function should not be modified, when the callback is needed,
  1863. the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
  1864. */
  1865. }
  1866. /**
  1867. * @brief SMARTCARD Abort Complete callback.
  1868. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1869. * the configuration information for the specified SMARTCARD module.
  1870. * @retval None
  1871. */
  1872. __weak void HAL_SMARTCARD_AbortCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1873. {
  1874. /* Prevent unused argument(s) compilation warning */
  1875. UNUSED(hsmartcard);
  1876. /* NOTE : This function should not be modified, when the callback is needed,
  1877. the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
  1878. */
  1879. }
  1880. /**
  1881. * @brief SMARTCARD Abort Complete callback.
  1882. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1883. * the configuration information for the specified SMARTCARD module.
  1884. * @retval None
  1885. */
  1886. __weak void HAL_SMARTCARD_AbortTransmitCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1887. {
  1888. /* Prevent unused argument(s) compilation warning */
  1889. UNUSED(hsmartcard);
  1890. /* NOTE : This function should not be modified, when the callback is needed,
  1891. the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
  1892. */
  1893. }
  1894. /**
  1895. * @brief SMARTCARD Abort Receive Complete callback.
  1896. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1897. * the configuration information for the specified SMARTCARD module.
  1898. * @retval None
  1899. */
  1900. __weak void HAL_SMARTCARD_AbortReceiveCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1901. {
  1902. /* Prevent unused argument(s) compilation warning */
  1903. UNUSED(hsmartcard);
  1904. /* NOTE : This function should not be modified, when the callback is needed,
  1905. the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
  1906. */
  1907. }
  1908. /**
  1909. * @}
  1910. */
  1911. /** @defgroup SMARTCARD_Exported_Functions_Group4 Peripheral State and Errors functions
  1912. * @brief SMARTCARD State and Errors functions
  1913. *
  1914. @verbatim
  1915. ==============================================================================
  1916. ##### Peripheral State and Errors functions #####
  1917. ==============================================================================
  1918. [..]
  1919. This subsection provides a set of functions allowing to return the State of SmartCard
  1920. handle and also return Peripheral Errors occurred during communication process
  1921. (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
  1922. of the SMARTCARD peripheral.
  1923. (+) HAL_SMARTCARD_GetError() checks in run-time errors that could occur during
  1924. communication.
  1925. @endverbatim
  1926. * @{
  1927. */
  1928. /**
  1929. * @brief Return the SMARTCARD handle state.
  1930. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1931. * the configuration information for the specified SMARTCARD module.
  1932. * @retval SMARTCARD handle state
  1933. */
  1934. HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsmartcard)
  1935. {
  1936. /* Return SMARTCARD handle state */
  1937. uint32_t temp1;
  1938. uint32_t temp2;
  1939. temp1 = (uint32_t)hsmartcard->gState;
  1940. temp2 = (uint32_t)hsmartcard->RxState;
  1941. return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
  1942. }
  1943. /**
  1944. * @brief Return the SMARTCARD handle error code.
  1945. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1946. * the configuration information for the specified SMARTCARD module.
  1947. * @retval SMARTCARD handle Error Code
  1948. */
  1949. uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsmartcard)
  1950. {
  1951. return hsmartcard->ErrorCode;
  1952. }
  1953. /**
  1954. * @}
  1955. */
  1956. /**
  1957. * @}
  1958. */
  1959. /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
  1960. * @{
  1961. */
  1962. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1963. /**
  1964. * @brief Initialize the callbacks to their default values.
  1965. * @param hsmartcard SMARTCARD handle.
  1966. * @retval none
  1967. */
  1968. void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsmartcard)
  1969. {
  1970. /* Init the SMARTCARD Callback settings */
  1971. hsmartcard->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */
  1972. hsmartcard->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */
  1973. hsmartcard->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */
  1974. hsmartcard->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  1975. hsmartcard->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
  1976. hsmartcard->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
  1977. hsmartcard->RxFifoFullCallback = HAL_SMARTCARDEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */
  1978. hsmartcard->TxFifoEmptyCallback = HAL_SMARTCARDEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
  1979. }
  1980. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  1981. /**
  1982. * @brief Configure the SMARTCARD associated USART peripheral.
  1983. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1984. * the configuration information for the specified SMARTCARD module.
  1985. * @retval HAL status
  1986. */
  1987. static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard)
  1988. {
  1989. uint32_t tmpreg;
  1990. SMARTCARD_ClockSourceTypeDef clocksource;
  1991. HAL_StatusTypeDef ret = HAL_OK;
  1992. const uint16_t SMARTCARDPrescTable[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U};
  1993. uint32_t pclk;
  1994. /* Check the parameters */
  1995. assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
  1996. assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard->Init.BaudRate));
  1997. assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard->Init.WordLength));
  1998. assert_param(IS_SMARTCARD_STOPBITS(hsmartcard->Init.StopBits));
  1999. assert_param(IS_SMARTCARD_PARITY(hsmartcard->Init.Parity));
  2000. assert_param(IS_SMARTCARD_MODE(hsmartcard->Init.Mode));
  2001. assert_param(IS_SMARTCARD_POLARITY(hsmartcard->Init.CLKPolarity));
  2002. assert_param(IS_SMARTCARD_PHASE(hsmartcard->Init.CLKPhase));
  2003. assert_param(IS_SMARTCARD_LASTBIT(hsmartcard->Init.CLKLastBit));
  2004. assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsmartcard->Init.OneBitSampling));
  2005. assert_param(IS_SMARTCARD_NACK(hsmartcard->Init.NACKEnable));
  2006. assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard->Init.TimeOutEnable));
  2007. assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard->Init.AutoRetryCount));
  2008. assert_param(IS_SMARTCARD_CLOCKPRESCALER(hsmartcard->Init.ClockPrescaler));
  2009. /*-------------------------- USART CR1 Configuration -----------------------*/
  2010. /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
  2011. * Oversampling is forced to 16 (OVER8 = 0).
  2012. * Configure the Parity and Mode:
  2013. * set PS bit according to hsmartcard->Init.Parity value
  2014. * set TE and RE bits according to hsmartcard->Init.Mode value */
  2015. tmpreg = (uint32_t) hsmartcard->Init.Parity | hsmartcard->Init.Mode;
  2016. tmpreg |= (uint32_t) hsmartcard->Init.WordLength | hsmartcard->FifoMode;
  2017. MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_FIELDS, tmpreg);
  2018. /*-------------------------- USART CR2 Configuration -----------------------*/
  2019. tmpreg = hsmartcard->Init.StopBits;
  2020. /* Synchronous mode is activated by default */
  2021. tmpreg |= (uint32_t) USART_CR2_CLKEN | hsmartcard->Init.CLKPolarity;
  2022. tmpreg |= (uint32_t) hsmartcard->Init.CLKPhase | hsmartcard->Init.CLKLastBit;
  2023. tmpreg |= (uint32_t) hsmartcard->Init.TimeOutEnable;
  2024. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_FIELDS, tmpreg);
  2025. /*-------------------------- USART CR3 Configuration -----------------------*/
  2026. /* Configure
  2027. * - one-bit sampling method versus three samples' majority rule
  2028. * according to hsmartcard->Init.OneBitSampling
  2029. * - NACK transmission in case of parity error according
  2030. * to hsmartcard->Init.NACKEnable
  2031. * - autoretry counter according to hsmartcard->Init.AutoRetryCount */
  2032. tmpreg = (uint32_t) hsmartcard->Init.OneBitSampling | hsmartcard->Init.NACKEnable;
  2033. tmpreg |= ((uint32_t)hsmartcard->Init.AutoRetryCount << USART_CR3_SCARCNT_Pos);
  2034. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_FIELDS, tmpreg);
  2035. /*--------------------- SMARTCARD clock PRESC Configuration ----------------*/
  2036. /* Configure
  2037. * - SMARTCARD Clock Prescaler: set PRESCALER according to hsmartcard->Init.ClockPrescaler value */
  2038. MODIFY_REG(hsmartcard->Instance->PRESC, USART_PRESC_PRESCALER, hsmartcard->Init.ClockPrescaler);
  2039. /*-------------------------- USART GTPR Configuration ----------------------*/
  2040. tmpreg = (hsmartcard->Init.Prescaler | ((uint32_t)hsmartcard->Init.GuardTime << USART_GTPR_GT_Pos));
  2041. MODIFY_REG(hsmartcard->Instance->GTPR, (uint16_t)(USART_GTPR_GT | USART_GTPR_PSC), (uint16_t)tmpreg);
  2042. /*-------------------------- USART RTOR Configuration ----------------------*/
  2043. tmpreg = ((uint32_t)hsmartcard->Init.BlockLength << USART_RTOR_BLEN_Pos);
  2044. if (hsmartcard->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE)
  2045. {
  2046. assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue));
  2047. tmpreg |= (uint32_t) hsmartcard->Init.TimeOutValue;
  2048. }
  2049. MODIFY_REG(hsmartcard->Instance->RTOR, (USART_RTOR_RTO | USART_RTOR_BLEN), tmpreg);
  2050. /*-------------------------- USART BRR Configuration -----------------------*/
  2051. SMARTCARD_GETCLOCKSOURCE(hsmartcard, clocksource);
  2052. tmpreg = 0U;
  2053. switch (clocksource)
  2054. {
  2055. case SMARTCARD_CLOCKSOURCE_PCLK2:
  2056. pclk = HAL_RCC_GetPCLK2Freq();
  2057. tmpreg = (uint16_t)(((pclk / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2058. break;
  2059. case SMARTCARD_CLOCKSOURCE_HSI:
  2060. tmpreg = (uint16_t)(((HSI_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2061. break;
  2062. case SMARTCARD_CLOCKSOURCE_SYSCLK:
  2063. pclk = HAL_RCC_GetSysClockFreq();
  2064. tmpreg = (uint16_t)(((pclk / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2065. break;
  2066. case SMARTCARD_CLOCKSOURCE_LSE:
  2067. tmpreg = (uint16_t)(((uint16_t)(LSE_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2068. break;
  2069. default:
  2070. ret = HAL_ERROR;
  2071. break;
  2072. }
  2073. /* USARTDIV must be greater than or equal to 0d16 */
  2074. if ((tmpreg >= USART_BRR_MIN) && (tmpreg <= USART_BRR_MAX))
  2075. {
  2076. hsmartcard->Instance->BRR = tmpreg;
  2077. }
  2078. else
  2079. {
  2080. ret = HAL_ERROR;
  2081. }
  2082. /* Initialize the number of data to process during RX/TX ISR execution */
  2083. hsmartcard->NbTxDataToProcess = 1U;
  2084. hsmartcard->NbRxDataToProcess = 1U;
  2085. /* Clear ISR function pointers */
  2086. hsmartcard->RxISR = NULL;
  2087. hsmartcard->TxISR = NULL;
  2088. return ret;
  2089. }
  2090. /**
  2091. * @brief Configure the SMARTCARD associated USART peripheral advanced features.
  2092. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2093. * the configuration information for the specified SMARTCARD module.
  2094. * @retval None
  2095. */
  2096. static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard)
  2097. {
  2098. /* Check whether the set of advanced features to configure is properly set */
  2099. assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard->AdvancedInit.AdvFeatureInit));
  2100. /* if required, configure TX pin active level inversion */
  2101. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT))
  2102. {
  2103. assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard->AdvancedInit.TxPinLevelInvert));
  2104. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_TXINV, hsmartcard->AdvancedInit.TxPinLevelInvert);
  2105. }
  2106. /* if required, configure RX pin active level inversion */
  2107. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT))
  2108. {
  2109. assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard->AdvancedInit.RxPinLevelInvert));
  2110. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_RXINV, hsmartcard->AdvancedInit.RxPinLevelInvert);
  2111. }
  2112. /* if required, configure data inversion */
  2113. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT))
  2114. {
  2115. assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard->AdvancedInit.DataInvert));
  2116. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_DATAINV, hsmartcard->AdvancedInit.DataInvert);
  2117. }
  2118. /* if required, configure RX/TX pins swap */
  2119. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT))
  2120. {
  2121. assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard->AdvancedInit.Swap));
  2122. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_SWAP, hsmartcard->AdvancedInit.Swap);
  2123. }
  2124. /* if required, configure RX overrun detection disabling */
  2125. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT))
  2126. {
  2127. assert_param(IS_SMARTCARD_OVERRUN(hsmartcard->AdvancedInit.OverrunDisable));
  2128. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_OVRDIS, hsmartcard->AdvancedInit.OverrunDisable);
  2129. }
  2130. /* if required, configure DMA disabling on reception error */
  2131. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT))
  2132. {
  2133. assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard->AdvancedInit.DMADisableonRxError));
  2134. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_DDRE, hsmartcard->AdvancedInit.DMADisableonRxError);
  2135. }
  2136. /* if required, configure MSB first on communication line */
  2137. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT))
  2138. {
  2139. assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard->AdvancedInit.MSBFirst));
  2140. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_MSBFIRST, hsmartcard->AdvancedInit.MSBFirst);
  2141. }
  2142. }
  2143. /**
  2144. * @brief Check the SMARTCARD Idle State.
  2145. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2146. * the configuration information for the specified SMARTCARD module.
  2147. * @retval HAL status
  2148. */
  2149. static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard)
  2150. {
  2151. uint32_t tickstart;
  2152. /* Initialize the SMARTCARD ErrorCode */
  2153. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  2154. /* Init tickstart for timeout management */
  2155. tickstart = HAL_GetTick();
  2156. /* Check if the Transmitter is enabled */
  2157. if ((hsmartcard->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  2158. {
  2159. /* Wait until TEACK flag is set */
  2160. if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_TEACK, RESET, tickstart,
  2161. SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
  2162. {
  2163. /* Timeout occurred */
  2164. return HAL_TIMEOUT;
  2165. }
  2166. }
  2167. /* Check if the Receiver is enabled */
  2168. if ((hsmartcard->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  2169. {
  2170. /* Wait until REACK flag is set */
  2171. if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_REACK, RESET, tickstart,
  2172. SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
  2173. {
  2174. /* Timeout occurred */
  2175. return HAL_TIMEOUT;
  2176. }
  2177. }
  2178. /* Initialize the SMARTCARD states */
  2179. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2180. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2181. /* Process Unlocked */
  2182. __HAL_UNLOCK(hsmartcard);
  2183. return HAL_OK;
  2184. }
  2185. /**
  2186. * @brief Handle SMARTCARD Communication Timeout.
  2187. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2188. * the configuration information for the specified SMARTCARD module.
  2189. * @param Flag Specifies the SMARTCARD flag to check.
  2190. * @param Status The new Flag status (SET or RESET).
  2191. * @param Tickstart Tick start value
  2192. * @param Timeout Timeout duration.
  2193. * @retval HAL status
  2194. */
  2195. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag,
  2196. FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  2197. {
  2198. /* Wait until flag is set */
  2199. while ((__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) ? SET : RESET) == Status)
  2200. {
  2201. /* Check for the Timeout */
  2202. if (Timeout != HAL_MAX_DELAY)
  2203. {
  2204. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  2205. {
  2206. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  2207. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE));
  2208. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2209. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2210. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2211. /* Process Unlocked */
  2212. __HAL_UNLOCK(hsmartcard);
  2213. return HAL_TIMEOUT;
  2214. }
  2215. }
  2216. }
  2217. return HAL_OK;
  2218. }
  2219. /**
  2220. * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
  2221. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2222. * the configuration information for the specified SMARTCARD module.
  2223. * @retval None
  2224. */
  2225. static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
  2226. {
  2227. /* Disable TXEIE, TCIE and ERR (Frame error, noise error, overrun error) interrupts */
  2228. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  2229. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2230. /* At end of Tx process, restore hsmartcard->gState to Ready */
  2231. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2232. }
  2233. /**
  2234. * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
  2235. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2236. * the configuration information for the specified SMARTCARD module.
  2237. * @retval None
  2238. */
  2239. static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
  2240. {
  2241. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  2242. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
  2243. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2244. /* At end of Rx process, restore hsmartcard->RxState to Ready */
  2245. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2246. }
  2247. /**
  2248. * @brief DMA SMARTCARD transmit process complete callback.
  2249. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  2250. * the configuration information for the specified DMA module.
  2251. * @retval None
  2252. */
  2253. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  2254. {
  2255. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2256. hsmartcard->TxXferCount = 0U;
  2257. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  2258. in the SMARTCARD associated USART CR3 register */
  2259. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  2260. /* Enable the SMARTCARD Transmit Complete Interrupt */
  2261. __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2262. }
  2263. /**
  2264. * @brief DMA SMARTCARD receive process complete callback.
  2265. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  2266. * the configuration information for the specified DMA module.
  2267. * @retval None
  2268. */
  2269. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  2270. {
  2271. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2272. hsmartcard->RxXferCount = 0U;
  2273. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  2274. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  2275. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2276. /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
  2277. in the SMARTCARD associated USART CR3 register */
  2278. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  2279. /* At end of Rx process, restore hsmartcard->RxState to Ready */
  2280. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2281. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2282. /* Call registered Rx complete callback */
  2283. hsmartcard->RxCpltCallback(hsmartcard);
  2284. #else
  2285. /* Call legacy weak Rx complete callback */
  2286. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  2287. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2288. }
  2289. /**
  2290. * @brief DMA SMARTCARD communication error callback.
  2291. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  2292. * the configuration information for the specified DMA module.
  2293. * @retval None
  2294. */
  2295. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
  2296. {
  2297. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2298. /* Stop SMARTCARD DMA Tx request if ongoing */
  2299. if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  2300. {
  2301. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  2302. {
  2303. hsmartcard->TxXferCount = 0U;
  2304. SMARTCARD_EndTxTransfer(hsmartcard);
  2305. }
  2306. }
  2307. /* Stop SMARTCARD DMA Rx request if ongoing */
  2308. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  2309. {
  2310. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  2311. {
  2312. hsmartcard->RxXferCount = 0U;
  2313. SMARTCARD_EndRxTransfer(hsmartcard);
  2314. }
  2315. }
  2316. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
  2317. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2318. /* Call registered user error callback */
  2319. hsmartcard->ErrorCallback(hsmartcard);
  2320. #else
  2321. /* Call legacy weak user error callback */
  2322. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  2323. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2324. }
  2325. /**
  2326. * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
  2327. * (To be called at end of DMA Abort procedure following error occurrence).
  2328. * @param hdma DMA handle.
  2329. * @retval None
  2330. */
  2331. static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  2332. {
  2333. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2334. hsmartcard->RxXferCount = 0U;
  2335. hsmartcard->TxXferCount = 0U;
  2336. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2337. /* Call registered user error callback */
  2338. hsmartcard->ErrorCallback(hsmartcard);
  2339. #else
  2340. /* Call legacy weak user error callback */
  2341. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  2342. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2343. }
  2344. /**
  2345. * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user
  2346. * (To be called at end of DMA Tx Abort procedure following user abort request).
  2347. * @note When this callback is executed, User Abort complete call back is called only if no
  2348. * Abort still ongoing for Rx DMA Handle.
  2349. * @param hdma DMA handle.
  2350. * @retval None
  2351. */
  2352. static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  2353. {
  2354. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2355. hsmartcard->hdmatx->XferAbortCallback = NULL;
  2356. /* Check if an Abort process is still ongoing */
  2357. if (hsmartcard->hdmarx != NULL)
  2358. {
  2359. if (hsmartcard->hdmarx->XferAbortCallback != NULL)
  2360. {
  2361. return;
  2362. }
  2363. }
  2364. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2365. hsmartcard->TxXferCount = 0U;
  2366. hsmartcard->RxXferCount = 0U;
  2367. /* Reset errorCode */
  2368. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  2369. /* Clear the Error flags in the ICR register */
  2370. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  2371. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
  2372. SMARTCARD_CLEAR_EOBF);
  2373. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  2374. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2375. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2376. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2377. /* Call registered Abort complete callback */
  2378. hsmartcard->AbortCpltCallback(hsmartcard);
  2379. #else
  2380. /* Call legacy weak Abort complete callback */
  2381. HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  2382. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2383. }
  2384. /**
  2385. * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user
  2386. * (To be called at end of DMA Rx Abort procedure following user abort request).
  2387. * @note When this callback is executed, User Abort complete call back is called only if no
  2388. * Abort still ongoing for Tx DMA Handle.
  2389. * @param hdma DMA handle.
  2390. * @retval None
  2391. */
  2392. static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  2393. {
  2394. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2395. hsmartcard->hdmarx->XferAbortCallback = NULL;
  2396. /* Check if an Abort process is still ongoing */
  2397. if (hsmartcard->hdmatx != NULL)
  2398. {
  2399. if (hsmartcard->hdmatx->XferAbortCallback != NULL)
  2400. {
  2401. return;
  2402. }
  2403. }
  2404. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2405. hsmartcard->TxXferCount = 0U;
  2406. hsmartcard->RxXferCount = 0U;
  2407. /* Reset errorCode */
  2408. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  2409. /* Clear the Error flags in the ICR register */
  2410. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  2411. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
  2412. SMARTCARD_CLEAR_EOBF);
  2413. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  2414. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2415. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2416. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2417. /* Call registered Abort complete callback */
  2418. hsmartcard->AbortCpltCallback(hsmartcard);
  2419. #else
  2420. /* Call legacy weak Abort complete callback */
  2421. HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  2422. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2423. }
  2424. /**
  2425. * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
  2426. * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
  2427. * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
  2428. * and leads to user Tx Abort Complete callback execution).
  2429. * @param hdma DMA handle.
  2430. * @retval None
  2431. */
  2432. static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  2433. {
  2434. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2435. hsmartcard->TxXferCount = 0U;
  2436. /* Clear the Error flags in the ICR register */
  2437. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  2438. /* Restore hsmartcard->gState to Ready */
  2439. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2440. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2441. /* Call registered Abort Transmit Complete Callback */
  2442. hsmartcard->AbortTransmitCpltCallback(hsmartcard);
  2443. #else
  2444. /* Call legacy weak Abort Transmit Complete Callback */
  2445. HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  2446. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2447. }
  2448. /**
  2449. * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
  2450. * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
  2451. * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
  2452. * and leads to user Rx Abort Complete callback execution).
  2453. * @param hdma DMA handle.
  2454. * @retval None
  2455. */
  2456. static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  2457. {
  2458. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2459. hsmartcard->RxXferCount = 0U;
  2460. /* Clear the Error flags in the ICR register */
  2461. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  2462. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
  2463. SMARTCARD_CLEAR_EOBF);
  2464. /* Restore hsmartcard->RxState to Ready */
  2465. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2466. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2467. /* Call registered Abort Receive Complete Callback */
  2468. hsmartcard->AbortReceiveCpltCallback(hsmartcard);
  2469. #else
  2470. /* Call legacy weak Abort Receive Complete Callback */
  2471. HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  2472. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2473. }
  2474. /**
  2475. * @brief Send an amount of data in non-blocking mode.
  2476. * @note Function called under interruption only, once
  2477. * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
  2478. * and when the FIFO mode is disabled.
  2479. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2480. * the configuration information for the specified SMARTCARD module.
  2481. * @retval None
  2482. */
  2483. static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard)
  2484. {
  2485. /* Check that a Tx process is ongoing */
  2486. if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  2487. {
  2488. if (hsmartcard->TxXferCount == 0U)
  2489. {
  2490. /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
  2491. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  2492. /* Enable the SMARTCARD Transmit Complete Interrupt */
  2493. __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2494. }
  2495. else
  2496. {
  2497. hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
  2498. hsmartcard->pTxBuffPtr++;
  2499. hsmartcard->TxXferCount--;
  2500. }
  2501. }
  2502. }
  2503. /**
  2504. * @brief Send an amount of data in non-blocking mode.
  2505. * @note Function called under interruption only, once
  2506. * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
  2507. * and when the FIFO mode is enabled.
  2508. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2509. * the configuration information for the specified SMARTCARD module.
  2510. * @retval None
  2511. */
  2512. static void SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
  2513. {
  2514. uint16_t nb_tx_data;
  2515. /* Check that a Tx process is ongoing */
  2516. if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  2517. {
  2518. for (nb_tx_data = hsmartcard->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
  2519. {
  2520. if (hsmartcard->TxXferCount == 0U)
  2521. {
  2522. /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
  2523. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  2524. /* Enable the SMARTCARD Transmit Complete Interrupt */
  2525. __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2526. }
  2527. else if (READ_BIT(hsmartcard->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
  2528. {
  2529. hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
  2530. hsmartcard->pTxBuffPtr++;
  2531. hsmartcard->TxXferCount--;
  2532. }
  2533. else
  2534. {
  2535. /* Nothing to do */
  2536. }
  2537. }
  2538. }
  2539. }
  2540. /**
  2541. * @brief Wrap up transmission in non-blocking mode.
  2542. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2543. * the configuration information for the specified SMARTCARD module.
  2544. * @retval None
  2545. */
  2546. static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  2547. {
  2548. /* Disable the SMARTCARD Transmit Complete Interrupt */
  2549. __HAL_SMARTCARD_DISABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2550. /* Check if a receive process is ongoing or not. If not disable ERR IT */
  2551. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  2552. {
  2553. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  2554. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2555. }
  2556. /* Disable the Peripheral first to update mode */
  2557. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  2558. if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
  2559. && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  2560. {
  2561. /* In case of TX only mode, if NACK is enabled, receiver block has been enabled
  2562. for Transmit phase. Disable this receiver block. */
  2563. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  2564. }
  2565. if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
  2566. || (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  2567. {
  2568. /* Perform a TX FIFO Flush at end of Tx phase, as all sent bytes are appearing in Rx Data register */
  2569. __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
  2570. }
  2571. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  2572. /* Tx process is ended, restore hsmartcard->gState to Ready */
  2573. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2574. /* Clear TxISR function pointer */
  2575. hsmartcard->TxISR = NULL;
  2576. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2577. /* Call registered Tx complete callback */
  2578. hsmartcard->TxCpltCallback(hsmartcard);
  2579. #else
  2580. /* Call legacy weak Tx complete callback */
  2581. HAL_SMARTCARD_TxCpltCallback(hsmartcard);
  2582. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2583. }
  2584. /**
  2585. * @brief Receive an amount of data in non-blocking mode.
  2586. * @note Function called under interruption only, once
  2587. * interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
  2588. * and when the FIFO mode is disabled.
  2589. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2590. * the configuration information for the specified SMARTCARD module.
  2591. * @retval None
  2592. */
  2593. static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard)
  2594. {
  2595. /* Check that a Rx process is ongoing */
  2596. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  2597. {
  2598. *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
  2599. hsmartcard->pRxBuffPtr++;
  2600. hsmartcard->RxXferCount--;
  2601. if (hsmartcard->RxXferCount == 0U)
  2602. {
  2603. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
  2604. /* Check if a transmit process is ongoing or not. If not disable ERR IT */
  2605. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  2606. {
  2607. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  2608. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2609. }
  2610. /* Disable the SMARTCARD Parity Error Interrupt */
  2611. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  2612. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2613. /* Clear RxISR function pointer */
  2614. hsmartcard->RxISR = NULL;
  2615. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2616. /* Call registered Rx complete callback */
  2617. hsmartcard->RxCpltCallback(hsmartcard);
  2618. #else
  2619. /* Call legacy weak Rx complete callback */
  2620. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  2621. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2622. }
  2623. }
  2624. else
  2625. {
  2626. /* Clear RXNE interrupt flag */
  2627. __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
  2628. }
  2629. }
  2630. /**
  2631. * @brief Receive an amount of data in non-blocking mode.
  2632. * @note Function called under interruption only, once
  2633. * interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
  2634. * and when the FIFO mode is enabled.
  2635. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2636. * the configuration information for the specified SMARTCARD module.
  2637. * @retval None
  2638. */
  2639. static void SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
  2640. {
  2641. uint16_t nb_rx_data;
  2642. uint16_t rxdatacount;
  2643. /* Check that a Rx process is ongoing */
  2644. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  2645. {
  2646. for (nb_rx_data = hsmartcard->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
  2647. {
  2648. *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
  2649. hsmartcard->pRxBuffPtr++;
  2650. hsmartcard->RxXferCount--;
  2651. if (hsmartcard->RxXferCount == 0U)
  2652. {
  2653. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
  2654. /* Check if a transmit process is ongoing or not. If not disable ERR IT */
  2655. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  2656. {
  2657. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  2658. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2659. }
  2660. /* Disable the SMARTCARD Parity Error Interrupt */
  2661. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  2662. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2663. /* Clear RxISR function pointer */
  2664. hsmartcard->RxISR = NULL;
  2665. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2666. /* Call registered Rx complete callback */
  2667. hsmartcard->RxCpltCallback(hsmartcard);
  2668. #else
  2669. /* Call legacy weak Rx complete callback */
  2670. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  2671. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2672. }
  2673. }
  2674. /* When remaining number of bytes to receive is less than the RX FIFO
  2675. threshold, next incoming frames are processed as if FIFO mode was
  2676. disabled (i.e. one interrupt per received frame).
  2677. */
  2678. rxdatacount = hsmartcard->RxXferCount;
  2679. if (((rxdatacount != 0U)) && (rxdatacount < hsmartcard->NbRxDataToProcess))
  2680. {
  2681. /* Disable the UART RXFT interrupt*/
  2682. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE);
  2683. /* Update the RxISR function pointer */
  2684. hsmartcard->RxISR = SMARTCARD_RxISR;
  2685. /* Enable the UART Data Register Not Empty interrupt */
  2686. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
  2687. }
  2688. }
  2689. else
  2690. {
  2691. /* Clear RXNE interrupt flag */
  2692. __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
  2693. }
  2694. }
  2695. /**
  2696. * @}
  2697. */
  2698. #endif /* HAL_SMARTCARD_MODULE_ENABLED */
  2699. /**
  2700. * @}
  2701. */
  2702. /**
  2703. * @}
  2704. */
  2705. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/