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.
 
 
 

3137 lines
120 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_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 @ref HAL_SMARTCARD_RegisterCallback() to register a user callback.
  97. Function @ref 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 @ref 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 @ref HAL_SMARTCARD_TxCpltCallback(), @ref HAL_SMARTCARD_RxCpltCallback().
  130. Exception done for MspInit and MspDeInit functions that are respectively
  131. reset to the legacy weak (surcharged) functions in the @ref HAL_SMARTCARD_Init()
  132. and @ref HAL_SMARTCARD_DeInit() only when these callbacks are null (not registered beforehand).
  133. If not, MspInit or MspDeInit are not null, the @ref HAL_SMARTCARD_Init() and @ref 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 @ref HAL_SMARTCARD_RegisterCallback() before calling @ref HAL_SMARTCARD_DeInit()
  142. or @ref 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) 2017 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 "stm32h7xx_hal.h"
  163. /** @addtogroup STM32H7xx_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. [..]
  641. (#) Non-Blocking mode transfers could be aborted using Abort API's :
  642. (++) HAL_SMARTCARD_Abort()
  643. (++) HAL_SMARTCARD_AbortTransmit()
  644. (++) HAL_SMARTCARD_AbortReceive()
  645. (++) HAL_SMARTCARD_Abort_IT()
  646. (++) HAL_SMARTCARD_AbortTransmit_IT()
  647. (++) HAL_SMARTCARD_AbortReceive_IT()
  648. (#) For Abort services based on interrupts (HAL_SMARTCARD_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
  649. (++) HAL_SMARTCARD_AbortCpltCallback()
  650. (++) HAL_SMARTCARD_AbortTransmitCpltCallback()
  651. (++) HAL_SMARTCARD_AbortReceiveCpltCallback()
  652. (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  653. Errors are handled as follows :
  654. (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  655. to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
  656. Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
  657. and HAL_SMARTCARD_ErrorCallback() user callback is executed. Transfer is kept ongoing on SMARTCARD side.
  658. If user wants to abort it, Abort services should be called by user.
  659. (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  660. This concerns Frame Error in Interrupt mode tranmission, Overrun Error in Interrupt mode reception and all errors in DMA mode.
  661. Error code is set to allow user to identify error type, and HAL_SMARTCARD_ErrorCallback() user callback is executed.
  662. @endverbatim
  663. * @{
  664. */
  665. /**
  666. * @brief Send an amount of data in blocking mode.
  667. * @note When FIFO mode is enabled, writing a data in the TDR register adds one
  668. * data to the TXFIFO. Write operations to the TDR register are performed
  669. * when TXFNF flag is set. From hardware perspective, TXFNF flag and
  670. * TXE are mapped on the same bit-field.
  671. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  672. * the configuration information for the specified SMARTCARD module.
  673. * @param pData pointer to data buffer.
  674. * @param Size amount of data to be sent.
  675. * @param Timeout Timeout duration.
  676. * @retval HAL status
  677. */
  678. HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size,
  679. uint32_t Timeout)
  680. {
  681. uint32_t tickstart;
  682. uint8_t *ptmpdata = pData;
  683. /* Check that a Tx process is not already ongoing */
  684. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  685. {
  686. if ((ptmpdata == NULL) || (Size == 0U))
  687. {
  688. return HAL_ERROR;
  689. }
  690. /* Process Locked */
  691. __HAL_LOCK(hsmartcard);
  692. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  693. /* Init tickstart for timeout management */
  694. tickstart = HAL_GetTick();
  695. /* Disable the Peripheral first to update mode for TX master */
  696. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  697. /* Disable Rx, enable Tx */
  698. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  699. SET_BIT(hsmartcard->Instance->RQR, (uint16_t)SMARTCARD_RXDATA_FLUSH_REQUEST);
  700. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  701. /* Enable the Peripheral */
  702. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  703. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  704. hsmartcard->TxXferSize = Size;
  705. hsmartcard->TxXferCount = Size;
  706. while (hsmartcard->TxXferCount > 0U)
  707. {
  708. hsmartcard->TxXferCount--;
  709. if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  710. {
  711. return HAL_TIMEOUT;
  712. }
  713. hsmartcard->Instance->TDR = (uint8_t)(*ptmpdata & 0xFFU);
  714. ptmpdata++;
  715. }
  716. if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_TRANSMISSION_COMPLETION_FLAG(hsmartcard), RESET, tickstart,
  717. Timeout) != HAL_OK)
  718. {
  719. return HAL_TIMEOUT;
  720. }
  721. /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */
  722. if (hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
  723. {
  724. /* Disable the Peripheral first to update modes */
  725. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  726. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  727. /* Enable the Peripheral */
  728. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  729. }
  730. /* At end of Tx process, restore hsmartcard->gState to Ready */
  731. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  732. /* Process Unlocked */
  733. __HAL_UNLOCK(hsmartcard);
  734. return HAL_OK;
  735. }
  736. else
  737. {
  738. return HAL_BUSY;
  739. }
  740. }
  741. /**
  742. * @brief Receive an amount of data in blocking mode.
  743. * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
  744. * is not empty. Read operations from the RDR register are performed when
  745. * RXFNE flag is set. From hardware perspective, RXFNE flag and
  746. * RXNE are mapped on the same bit-field.
  747. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  748. * the configuration information for the specified SMARTCARD module.
  749. * @param pData pointer to data buffer.
  750. * @param Size amount of data to be received.
  751. * @param Timeout Timeout duration.
  752. * @retval HAL status
  753. */
  754. HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size,
  755. uint32_t Timeout)
  756. {
  757. uint32_t tickstart;
  758. uint8_t *ptmpdata = pData;
  759. /* Check that a Rx process is not already ongoing */
  760. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  761. {
  762. if ((ptmpdata == NULL) || (Size == 0U))
  763. {
  764. return HAL_ERROR;
  765. }
  766. /* Process Locked */
  767. __HAL_LOCK(hsmartcard);
  768. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  769. hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  770. /* Init tickstart for timeout management */
  771. tickstart = HAL_GetTick();
  772. hsmartcard->RxXferSize = Size;
  773. hsmartcard->RxXferCount = Size;
  774. /* Check the remain data to be received */
  775. while (hsmartcard->RxXferCount > 0U)
  776. {
  777. hsmartcard->RxXferCount--;
  778. if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  779. {
  780. return HAL_TIMEOUT;
  781. }
  782. *ptmpdata = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0x00FF);
  783. ptmpdata++;
  784. }
  785. /* At end of Rx process, restore hsmartcard->RxState to Ready */
  786. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  787. /* Process Unlocked */
  788. __HAL_UNLOCK(hsmartcard);
  789. return HAL_OK;
  790. }
  791. else
  792. {
  793. return HAL_BUSY;
  794. }
  795. }
  796. /**
  797. * @brief Send an amount of data in interrupt mode.
  798. * @note When FIFO mode is disabled, USART interrupt is generated whenever
  799. * USART_TDR register is empty, i.e one interrupt per data to transmit.
  800. * @note When FIFO mode is enabled, USART interrupt is generated whenever
  801. * TXFIFO threshold reached. In that case the interrupt rate depends on
  802. * TXFIFO threshold configuration.
  803. * @note This function sets the hsmartcard->TxIsr function pointer according to
  804. * the FIFO mode (data transmission processing depends on FIFO mode).
  805. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  806. * the configuration information for the specified SMARTCARD module.
  807. * @param pData pointer to data buffer.
  808. * @param Size amount of data to be sent.
  809. * @retval HAL status
  810. */
  811. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  812. {
  813. /* Check that a Tx process is not already ongoing */
  814. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  815. {
  816. if ((pData == NULL) || (Size == 0U))
  817. {
  818. return HAL_ERROR;
  819. }
  820. /* Process Locked */
  821. __HAL_LOCK(hsmartcard);
  822. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  823. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  824. hsmartcard->pTxBuffPtr = pData;
  825. hsmartcard->TxXferSize = Size;
  826. hsmartcard->TxXferCount = Size;
  827. hsmartcard->TxISR = NULL;
  828. /* Disable the Peripheral first to update mode for TX master */
  829. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  830. /* Disable Rx, enable Tx */
  831. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  832. SET_BIT(hsmartcard->Instance->RQR, (uint16_t)SMARTCARD_RXDATA_FLUSH_REQUEST);
  833. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  834. /* Enable the Peripheral */
  835. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  836. /* Configure Tx interrupt processing */
  837. if (hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE)
  838. {
  839. /* Set the Tx ISR function pointer */
  840. hsmartcard->TxISR = SMARTCARD_TxISR_FIFOEN;
  841. /* Process Unlocked */
  842. __HAL_UNLOCK(hsmartcard);
  843. /* Enable the SMARTCARD Error Interrupt: (Frame error) */
  844. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  845. /* Enable the TX FIFO threshold interrupt */
  846. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
  847. }
  848. else
  849. {
  850. /* Set the Tx ISR function pointer */
  851. hsmartcard->TxISR = SMARTCARD_TxISR;
  852. /* Process Unlocked */
  853. __HAL_UNLOCK(hsmartcard);
  854. /* Enable the SMARTCARD Error Interrupt: (Frame error) */
  855. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  856. /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */
  857. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  858. }
  859. return HAL_OK;
  860. }
  861. else
  862. {
  863. return HAL_BUSY;
  864. }
  865. }
  866. /**
  867. * @brief Receive an amount of data in interrupt mode.
  868. * @note When FIFO mode is disabled, USART interrupt is generated whenever
  869. * USART_RDR register can be read, i.e one interrupt per data to receive.
  870. * @note When FIFO mode is enabled, USART interrupt is generated whenever
  871. * RXFIFO threshold reached. In that case the interrupt rate depends on
  872. * RXFIFO threshold configuration.
  873. * @note This function sets the hsmartcard->RxIsr function pointer according to
  874. * the FIFO mode (data reception processing depends on FIFO mode).
  875. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  876. * the configuration information for the specified SMARTCARD module.
  877. * @param pData pointer to data buffer.
  878. * @param Size amount of data to be received.
  879. * @retval HAL status
  880. */
  881. HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  882. {
  883. /* Check that a Rx process is not already ongoing */
  884. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  885. {
  886. if ((pData == NULL) || (Size == 0U))
  887. {
  888. return HAL_ERROR;
  889. }
  890. /* Process Locked */
  891. __HAL_LOCK(hsmartcard);
  892. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  893. hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  894. hsmartcard->pRxBuffPtr = pData;
  895. hsmartcard->RxXferSize = Size;
  896. hsmartcard->RxXferCount = Size;
  897. /* Configure Rx interrupt processing */
  898. if ((hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE) && (Size >= hsmartcard->NbRxDataToProcess))
  899. {
  900. /* Set the Rx ISR function pointer */
  901. hsmartcard->RxISR = SMARTCARD_RxISR_FIFOEN;
  902. /* Process Unlocked */
  903. __HAL_UNLOCK(hsmartcard);
  904. /* Enable the SMARTCART Parity Error interrupt and RX FIFO Threshold interrupt */
  905. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  906. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE);
  907. }
  908. else
  909. {
  910. /* Set the Rx ISR function pointer */
  911. hsmartcard->RxISR = SMARTCARD_RxISR;
  912. /* Process Unlocked */
  913. __HAL_UNLOCK(hsmartcard);
  914. /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
  915. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
  916. }
  917. /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  918. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  919. return HAL_OK;
  920. }
  921. else
  922. {
  923. return HAL_BUSY;
  924. }
  925. }
  926. /**
  927. * @brief Send an amount of data in DMA mode.
  928. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  929. * the configuration information for the specified SMARTCARD module.
  930. * @param pData pointer to data buffer.
  931. * @param Size amount of data to be sent.
  932. * @retval HAL status
  933. */
  934. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  935. {
  936. /* Check that a Tx process is not already ongoing */
  937. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  938. {
  939. if ((pData == NULL) || (Size == 0U))
  940. {
  941. return HAL_ERROR;
  942. }
  943. /* Process Locked */
  944. __HAL_LOCK(hsmartcard);
  945. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  946. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  947. hsmartcard->pTxBuffPtr = pData;
  948. hsmartcard->TxXferSize = Size;
  949. hsmartcard->TxXferCount = Size;
  950. /* Disable the Peripheral first to update mode for TX master */
  951. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  952. /* Disable Rx, enable Tx */
  953. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  954. SET_BIT(hsmartcard->Instance->RQR, (uint16_t)SMARTCARD_RXDATA_FLUSH_REQUEST);
  955. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  956. /* Enable the Peripheral */
  957. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  958. /* Set the SMARTCARD DMA transfer complete callback */
  959. hsmartcard->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
  960. /* Set the SMARTCARD error callback */
  961. hsmartcard->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
  962. /* Set the DMA abort callback */
  963. hsmartcard->hdmatx->XferAbortCallback = NULL;
  964. /* Enable the SMARTCARD transmit DMA channel */
  965. if (HAL_DMA_Start_IT(hsmartcard->hdmatx, (uint32_t)hsmartcard->pTxBuffPtr, (uint32_t)&hsmartcard->Instance->TDR,
  966. Size) == HAL_OK)
  967. {
  968. /* Clear the TC flag in the ICR register */
  969. CLEAR_BIT(hsmartcard->Instance->ICR, USART_ICR_TCCF);
  970. /* Process Unlocked */
  971. __HAL_UNLOCK(hsmartcard);
  972. /* Enable the UART Error Interrupt: (Frame error) */
  973. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  974. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  975. in the SMARTCARD associated USART CR3 register */
  976. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  977. return HAL_OK;
  978. }
  979. else
  980. {
  981. /* Set error code to DMA */
  982. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  983. /* Process Unlocked */
  984. __HAL_UNLOCK(hsmartcard);
  985. /* Restore hsmartcard->State to ready */
  986. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  987. return HAL_ERROR;
  988. }
  989. }
  990. else
  991. {
  992. return HAL_BUSY;
  993. }
  994. }
  995. /**
  996. * @brief Receive an amount of data in DMA mode.
  997. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  998. * the configuration information for the specified SMARTCARD module.
  999. * @param pData pointer to data buffer.
  1000. * @param Size amount of data to be received.
  1001. * @note The SMARTCARD-associated USART parity is enabled (PCE = 1),
  1002. * the received data contain the parity bit (MSB position).
  1003. * @retval HAL status
  1004. */
  1005. HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  1006. {
  1007. /* Check that a Rx process is not already ongoing */
  1008. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  1009. {
  1010. if ((pData == NULL) || (Size == 0U))
  1011. {
  1012. return HAL_ERROR;
  1013. }
  1014. /* Process Locked */
  1015. __HAL_LOCK(hsmartcard);
  1016. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1017. hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  1018. hsmartcard->pRxBuffPtr = pData;
  1019. hsmartcard->RxXferSize = Size;
  1020. /* Set the SMARTCARD DMA transfer complete callback */
  1021. hsmartcard->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
  1022. /* Set the SMARTCARD DMA error callback */
  1023. hsmartcard->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
  1024. /* Set the DMA abort callback */
  1025. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1026. /* Enable the DMA channel */
  1027. if (HAL_DMA_Start_IT(hsmartcard->hdmarx, (uint32_t)&hsmartcard->Instance->RDR, (uint32_t)hsmartcard->pRxBuffPtr,
  1028. Size) == HAL_OK)
  1029. {
  1030. /* Process Unlocked */
  1031. __HAL_UNLOCK(hsmartcard);
  1032. /* Enable the SMARTCARD Parity Error Interrupt */
  1033. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  1034. /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  1035. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1036. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  1037. in the SMARTCARD associated USART CR3 register */
  1038. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1039. return HAL_OK;
  1040. }
  1041. else
  1042. {
  1043. /* Set error code to DMA */
  1044. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1045. /* Process Unlocked */
  1046. __HAL_UNLOCK(hsmartcard);
  1047. /* Restore hsmartcard->State to ready */
  1048. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1049. return HAL_ERROR;
  1050. }
  1051. }
  1052. else
  1053. {
  1054. return HAL_BUSY;
  1055. }
  1056. }
  1057. /**
  1058. * @brief Abort ongoing transfers (blocking mode).
  1059. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1060. * the configuration information for the specified SMARTCARD module.
  1061. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1062. * This procedure performs following operations :
  1063. * - Disable SMARTCARD Interrupts (Tx and Rx)
  1064. * - Disable the DMA transfer in the peripheral register (if enabled)
  1065. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1066. * - Set handle State to READY
  1067. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1068. * @retval HAL status
  1069. */
  1070. HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsmartcard)
  1071. {
  1072. /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
  1073. CLEAR_BIT(hsmartcard->Instance->CR1,
  1074. (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE |
  1075. USART_CR1_EOBIE));
  1076. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
  1077. /* Disable the SMARTCARD DMA Tx request if enabled */
  1078. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1079. {
  1080. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1081. /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  1082. if (hsmartcard->hdmatx != NULL)
  1083. {
  1084. /* Set the SMARTCARD DMA Abort callback to Null.
  1085. No call back execution at end of DMA abort procedure */
  1086. hsmartcard->hdmatx->XferAbortCallback = NULL;
  1087. if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK)
  1088. {
  1089. if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1090. {
  1091. /* Set error code to DMA */
  1092. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1093. return HAL_TIMEOUT;
  1094. }
  1095. }
  1096. }
  1097. }
  1098. /* Disable the SMARTCARD DMA Rx request if enabled */
  1099. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1100. {
  1101. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1102. /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  1103. if (hsmartcard->hdmarx != NULL)
  1104. {
  1105. /* Set the SMARTCARD DMA Abort callback to Null.
  1106. No call back execution at end of DMA abort procedure */
  1107. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1108. if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK)
  1109. {
  1110. if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1111. {
  1112. /* Set error code to DMA */
  1113. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1114. return HAL_TIMEOUT;
  1115. }
  1116. }
  1117. }
  1118. }
  1119. /* Reset Tx and Rx transfer counters */
  1120. hsmartcard->TxXferCount = 0U;
  1121. hsmartcard->RxXferCount = 0U;
  1122. /* Clear the Error flags in the ICR register */
  1123. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1124. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
  1125. SMARTCARD_CLEAR_EOBF);
  1126. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  1127. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1128. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1129. /* Reset Handle ErrorCode to No Error */
  1130. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1131. return HAL_OK;
  1132. }
  1133. /**
  1134. * @brief Abort ongoing Transmit transfer (blocking mode).
  1135. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1136. * the configuration information for the specified SMARTCARD module.
  1137. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1138. * This procedure performs following operations :
  1139. * - Disable SMARTCARD Interrupts (Tx)
  1140. * - Disable the DMA transfer in the peripheral register (if enabled)
  1141. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1142. * - Set handle State to READY
  1143. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1144. * @retval HAL status
  1145. */
  1146. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsmartcard)
  1147. {
  1148. /* Disable TCIE, TXEIE and TXFTIE interrupts */
  1149. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  1150. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
  1151. /* Check if a receive process is ongoing or not. If not disable ERR IT */
  1152. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  1153. {
  1154. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1155. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1156. }
  1157. /* Disable the SMARTCARD DMA Tx request if enabled */
  1158. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1159. {
  1160. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1161. /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  1162. if (hsmartcard->hdmatx != NULL)
  1163. {
  1164. /* Set the SMARTCARD DMA Abort callback to Null.
  1165. No call back execution at end of DMA abort procedure */
  1166. hsmartcard->hdmatx->XferAbortCallback = NULL;
  1167. if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK)
  1168. {
  1169. if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1170. {
  1171. /* Set error code to DMA */
  1172. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1173. return HAL_TIMEOUT;
  1174. }
  1175. }
  1176. }
  1177. }
  1178. /* Reset Tx transfer counter */
  1179. hsmartcard->TxXferCount = 0U;
  1180. /* Clear the Error flags in the ICR register */
  1181. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  1182. /* Restore hsmartcard->gState to Ready */
  1183. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1184. return HAL_OK;
  1185. }
  1186. /**
  1187. * @brief Abort ongoing Receive transfer (blocking mode).
  1188. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1189. * the configuration information for the specified SMARTCARD module.
  1190. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1191. * This procedure performs following operations :
  1192. * - Disable SMARTCARD Interrupts (Rx)
  1193. * - Disable the DMA transfer in the peripheral register (if enabled)
  1194. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1195. * - Set handle State to READY
  1196. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1197. * @retval HAL status
  1198. */
  1199. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsmartcard)
  1200. {
  1201. /* Disable RTOIE, EOBIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
  1202. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  1203. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
  1204. /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
  1205. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  1206. {
  1207. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1208. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1209. }
  1210. /* Disable the SMARTCARD DMA Rx request if enabled */
  1211. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1212. {
  1213. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1214. /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  1215. if (hsmartcard->hdmarx != NULL)
  1216. {
  1217. /* Set the SMARTCARD DMA Abort callback to Null.
  1218. No call back execution at end of DMA abort procedure */
  1219. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1220. if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK)
  1221. {
  1222. if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1223. {
  1224. /* Set error code to DMA */
  1225. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1226. return HAL_TIMEOUT;
  1227. }
  1228. }
  1229. }
  1230. }
  1231. /* Reset Rx transfer counter */
  1232. hsmartcard->RxXferCount = 0U;
  1233. /* Clear the Error flags in the ICR register */
  1234. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1235. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
  1236. SMARTCARD_CLEAR_EOBF);
  1237. /* Restore hsmartcard->RxState to Ready */
  1238. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1239. return HAL_OK;
  1240. }
  1241. /**
  1242. * @brief Abort ongoing transfers (Interrupt mode).
  1243. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1244. * the configuration information for the specified SMARTCARD module.
  1245. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1246. * This procedure performs following operations :
  1247. * - Disable SMARTCARD Interrupts (Tx and Rx)
  1248. * - Disable the DMA transfer in the peripheral register (if enabled)
  1249. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1250. * - Set handle State to READY
  1251. * - At abort completion, call user abort complete callback
  1252. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1253. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1254. * @retval HAL status
  1255. */
  1256. HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1257. {
  1258. uint32_t abortcplt = 1U;
  1259. /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
  1260. CLEAR_BIT(hsmartcard->Instance->CR1,
  1261. (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE |
  1262. USART_CR1_EOBIE));
  1263. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
  1264. /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
  1265. before any call to DMA Abort functions */
  1266. /* DMA Tx Handle is valid */
  1267. if (hsmartcard->hdmatx != NULL)
  1268. {
  1269. /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
  1270. Otherwise, set it to NULL */
  1271. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1272. {
  1273. hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
  1274. }
  1275. else
  1276. {
  1277. hsmartcard->hdmatx->XferAbortCallback = NULL;
  1278. }
  1279. }
  1280. /* DMA Rx Handle is valid */
  1281. if (hsmartcard->hdmarx != NULL)
  1282. {
  1283. /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
  1284. Otherwise, set it to NULL */
  1285. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1286. {
  1287. hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
  1288. }
  1289. else
  1290. {
  1291. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1292. }
  1293. }
  1294. /* Disable the SMARTCARD DMA Tx request if enabled */
  1295. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1296. {
  1297. /* Disable DMA Tx at UART level */
  1298. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1299. /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
  1300. if (hsmartcard->hdmatx != NULL)
  1301. {
  1302. /* SMARTCARD Tx DMA Abort callback has already been initialised :
  1303. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1304. /* Abort DMA TX */
  1305. if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  1306. {
  1307. hsmartcard->hdmatx->XferAbortCallback = NULL;
  1308. }
  1309. else
  1310. {
  1311. abortcplt = 0U;
  1312. }
  1313. }
  1314. }
  1315. /* Disable the SMARTCARD DMA Rx request if enabled */
  1316. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1317. {
  1318. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1319. /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
  1320. if (hsmartcard->hdmarx != NULL)
  1321. {
  1322. /* SMARTCARD Rx DMA Abort callback has already been initialised :
  1323. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1324. /* Abort DMA RX */
  1325. if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  1326. {
  1327. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1328. abortcplt = 1U;
  1329. }
  1330. else
  1331. {
  1332. abortcplt = 0U;
  1333. }
  1334. }
  1335. }
  1336. /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  1337. if (abortcplt == 1U)
  1338. {
  1339. /* Reset Tx and Rx transfer counters */
  1340. hsmartcard->TxXferCount = 0U;
  1341. hsmartcard->RxXferCount = 0U;
  1342. /* Clear ISR function pointers */
  1343. hsmartcard->RxISR = NULL;
  1344. hsmartcard->TxISR = NULL;
  1345. /* Reset errorCode */
  1346. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1347. /* Clear the Error flags in the ICR register */
  1348. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1349. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
  1350. SMARTCARD_CLEAR_EOBF);
  1351. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  1352. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1353. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1354. /* As no DMA to be aborted, call directly user Abort complete callback */
  1355. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1356. /* Call registered Abort complete callback */
  1357. hsmartcard->AbortCpltCallback(hsmartcard);
  1358. #else
  1359. /* Call legacy weak Abort complete callback */
  1360. HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  1361. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1362. }
  1363. return HAL_OK;
  1364. }
  1365. /**
  1366. * @brief Abort ongoing Transmit transfer (Interrupt mode).
  1367. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1368. * the configuration information for the specified SMARTCARD module.
  1369. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1370. * This procedure performs following operations :
  1371. * - Disable SMARTCARD Interrupts (Tx)
  1372. * - Disable the DMA transfer in the peripheral register (if enabled)
  1373. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1374. * - Set handle State to READY
  1375. * - At abort completion, call user abort complete callback
  1376. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1377. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1378. * @retval HAL status
  1379. */
  1380. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1381. {
  1382. /* Disable TCIE, TXEIE and TXFTIE interrupts */
  1383. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  1384. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
  1385. /* Check if a receive process is ongoing or not. If not disable ERR IT */
  1386. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  1387. {
  1388. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1389. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1390. }
  1391. /* Disable the SMARTCARD DMA Tx request if enabled */
  1392. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1393. {
  1394. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1395. /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
  1396. if (hsmartcard->hdmatx != NULL)
  1397. {
  1398. /* Set the SMARTCARD DMA Abort callback :
  1399. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1400. hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
  1401. /* Abort DMA TX */
  1402. if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  1403. {
  1404. /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
  1405. hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
  1406. }
  1407. }
  1408. else
  1409. {
  1410. /* Reset Tx transfer counter */
  1411. hsmartcard->TxXferCount = 0U;
  1412. /* Clear TxISR function pointers */
  1413. hsmartcard->TxISR = NULL;
  1414. /* Restore hsmartcard->gState to Ready */
  1415. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1416. /* As no DMA to be aborted, call directly user Abort complete callback */
  1417. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1418. /* Call registered Abort Transmit Complete Callback */
  1419. hsmartcard->AbortTransmitCpltCallback(hsmartcard);
  1420. #else
  1421. /* Call legacy weak Abort Transmit Complete Callback */
  1422. HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  1423. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1424. }
  1425. }
  1426. else
  1427. {
  1428. /* Reset Tx transfer counter */
  1429. hsmartcard->TxXferCount = 0U;
  1430. /* Clear TxISR function pointers */
  1431. hsmartcard->TxISR = NULL;
  1432. /* Clear the Error flags in the ICR register */
  1433. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  1434. /* Restore hsmartcard->gState to Ready */
  1435. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1436. /* As no DMA to be aborted, call directly user Abort complete callback */
  1437. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1438. /* Call registered Abort Transmit Complete Callback */
  1439. hsmartcard->AbortTransmitCpltCallback(hsmartcard);
  1440. #else
  1441. /* Call legacy weak Abort Transmit Complete Callback */
  1442. HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  1443. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1444. }
  1445. return HAL_OK;
  1446. }
  1447. /**
  1448. * @brief Abort ongoing Receive transfer (Interrupt mode).
  1449. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1450. * the configuration information for the specified SMARTCARD module.
  1451. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1452. * This procedure performs following operations :
  1453. * - Disable SMARTCARD Interrupts (Rx)
  1454. * - Disable the DMA transfer in the peripheral register (if enabled)
  1455. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1456. * - Set handle State to READY
  1457. * - At abort completion, call user abort complete callback
  1458. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1459. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1460. * @retval HAL status
  1461. */
  1462. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1463. {
  1464. /* Disable RTOIE, EOBIE, RXNE, PE, RXFT and ERR (Frame error, noise error, overrun error) interrupts */
  1465. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  1466. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
  1467. /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
  1468. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  1469. {
  1470. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1471. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1472. }
  1473. /* Disable the SMARTCARD DMA Rx request if enabled */
  1474. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1475. {
  1476. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1477. /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
  1478. if (hsmartcard->hdmarx != NULL)
  1479. {
  1480. /* Set the SMARTCARD DMA Abort callback :
  1481. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1482. hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
  1483. /* Abort DMA RX */
  1484. if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  1485. {
  1486. /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
  1487. hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
  1488. }
  1489. }
  1490. else
  1491. {
  1492. /* Reset Rx transfer counter */
  1493. hsmartcard->RxXferCount = 0U;
  1494. /* Clear RxISR function pointer */
  1495. hsmartcard->RxISR = NULL;
  1496. /* Clear the Error flags in the ICR register */
  1497. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1498. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
  1499. SMARTCARD_CLEAR_EOBF);
  1500. /* Restore hsmartcard->RxState to Ready */
  1501. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1502. /* As no DMA to be aborted, call directly user Abort complete callback */
  1503. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1504. /* Call registered Abort Receive Complete Callback */
  1505. hsmartcard->AbortReceiveCpltCallback(hsmartcard);
  1506. #else
  1507. /* Call legacy weak Abort Receive Complete Callback */
  1508. HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  1509. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1510. }
  1511. }
  1512. else
  1513. {
  1514. /* Reset Rx transfer counter */
  1515. hsmartcard->RxXferCount = 0U;
  1516. /* Clear RxISR function pointer */
  1517. hsmartcard->RxISR = NULL;
  1518. /* Clear the Error flags in the ICR register */
  1519. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1520. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
  1521. SMARTCARD_CLEAR_EOBF);
  1522. /* Restore hsmartcard->RxState to Ready */
  1523. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1524. /* As no DMA to be aborted, call directly user Abort complete callback */
  1525. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1526. /* Call registered Abort Receive Complete Callback */
  1527. hsmartcard->AbortReceiveCpltCallback(hsmartcard);
  1528. #else
  1529. /* Call legacy weak Abort Receive Complete Callback */
  1530. HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  1531. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1532. }
  1533. return HAL_OK;
  1534. }
  1535. /**
  1536. * @brief Handle SMARTCARD interrupt requests.
  1537. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1538. * the configuration information for the specified SMARTCARD module.
  1539. * @retval None
  1540. */
  1541. void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsmartcard)
  1542. {
  1543. uint32_t isrflags = READ_REG(hsmartcard->Instance->ISR);
  1544. uint32_t cr1its = READ_REG(hsmartcard->Instance->CR1);
  1545. uint32_t cr3its = READ_REG(hsmartcard->Instance->CR3);
  1546. uint32_t errorflags;
  1547. uint32_t errorcode;
  1548. /* If no error occurs */
  1549. errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
  1550. if (errorflags == 0U)
  1551. {
  1552. /* SMARTCARD in mode Receiver ---------------------------------------------------*/
  1553. if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
  1554. && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
  1555. || ((cr3its & USART_CR3_RXFTIE) != 0U)))
  1556. {
  1557. if (hsmartcard->RxISR != NULL)
  1558. {
  1559. hsmartcard->RxISR(hsmartcard);
  1560. }
  1561. return;
  1562. }
  1563. }
  1564. /* If some errors occur */
  1565. if ((errorflags != 0U)
  1566. && ((((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)
  1567. || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != 0U))))
  1568. {
  1569. /* SMARTCARD parity error interrupt occurred -------------------------------------*/
  1570. if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
  1571. {
  1572. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_PEF);
  1573. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
  1574. }
  1575. /* SMARTCARD frame error interrupt occurred --------------------------------------*/
  1576. if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  1577. {
  1578. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_FEF);
  1579. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
  1580. }
  1581. /* SMARTCARD noise error interrupt occurred --------------------------------------*/
  1582. if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  1583. {
  1584. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_NEF);
  1585. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
  1586. }
  1587. /* SMARTCARD Over-Run interrupt occurred -----------------------------------------*/
  1588. if (((isrflags & USART_ISR_ORE) != 0U)
  1589. && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
  1590. || ((cr3its & USART_CR3_RXFTIE) != 0U)
  1591. || ((cr3its & USART_CR3_EIE) != 0U)))
  1592. {
  1593. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_OREF);
  1594. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
  1595. }
  1596. /* SMARTCARD receiver timeout interrupt occurred -----------------------------------------*/
  1597. if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U))
  1598. {
  1599. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_RTOF);
  1600. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_RTO;
  1601. }
  1602. /* Call SMARTCARD Error Call back function if need be --------------------------*/
  1603. if (hsmartcard->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
  1604. {
  1605. /* SMARTCARD in mode Receiver ---------------------------------------------------*/
  1606. if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
  1607. && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
  1608. || ((cr3its & USART_CR3_RXFTIE) != 0U)))
  1609. {
  1610. if (hsmartcard->RxISR != NULL)
  1611. {
  1612. hsmartcard->RxISR(hsmartcard);
  1613. }
  1614. }
  1615. /* If Error is to be considered as blocking :
  1616. - Receiver Timeout error in Reception
  1617. - Overrun error in Reception
  1618. - any error occurs in DMA mode reception
  1619. */
  1620. errorcode = hsmartcard->ErrorCode;
  1621. if ((HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1622. || ((errorcode & (HAL_SMARTCARD_ERROR_RTO | HAL_SMARTCARD_ERROR_ORE)) != 0U))
  1623. {
  1624. /* Blocking error : transfer is aborted
  1625. Set the SMARTCARD state ready to be able to start again the process,
  1626. Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1627. SMARTCARD_EndRxTransfer(hsmartcard);
  1628. /* Disable the SMARTCARD DMA Rx request if enabled */
  1629. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1630. {
  1631. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1632. /* Abort the SMARTCARD DMA Rx channel */
  1633. if (hsmartcard->hdmarx != NULL)
  1634. {
  1635. /* Set the SMARTCARD DMA Abort callback :
  1636. will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
  1637. hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
  1638. /* Abort DMA RX */
  1639. if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  1640. {
  1641. /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
  1642. hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
  1643. }
  1644. }
  1645. else
  1646. {
  1647. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1648. /* Call registered user error callback */
  1649. hsmartcard->ErrorCallback(hsmartcard);
  1650. #else
  1651. /* Call legacy weak user error callback */
  1652. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1653. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1654. }
  1655. }
  1656. else
  1657. {
  1658. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1659. /* Call registered user error callback */
  1660. hsmartcard->ErrorCallback(hsmartcard);
  1661. #else
  1662. /* Call legacy weak user error callback */
  1663. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1664. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1665. }
  1666. }
  1667. /* other error type to be considered as blocking :
  1668. - Frame error in Transmission
  1669. */
  1670. else if ((hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  1671. && ((errorcode & HAL_SMARTCARD_ERROR_FE) != 0U))
  1672. {
  1673. /* Blocking error : transfer is aborted
  1674. Set the SMARTCARD state ready to be able to start again the process,
  1675. Disable Tx Interrupts, and disable Tx DMA request, if ongoing */
  1676. SMARTCARD_EndTxTransfer(hsmartcard);
  1677. /* Disable the SMARTCARD DMA Tx request if enabled */
  1678. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1679. {
  1680. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1681. /* Abort the SMARTCARD DMA Tx channel */
  1682. if (hsmartcard->hdmatx != NULL)
  1683. {
  1684. /* Set the SMARTCARD DMA Abort callback :
  1685. will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
  1686. hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
  1687. /* Abort DMA TX */
  1688. if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  1689. {
  1690. /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
  1691. hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
  1692. }
  1693. }
  1694. else
  1695. {
  1696. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1697. /* Call registered user error callback */
  1698. hsmartcard->ErrorCallback(hsmartcard);
  1699. #else
  1700. /* Call legacy weak user error callback */
  1701. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1702. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1703. }
  1704. }
  1705. else
  1706. {
  1707. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1708. /* Call registered user error callback */
  1709. hsmartcard->ErrorCallback(hsmartcard);
  1710. #else
  1711. /* Call legacy weak user error callback */
  1712. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1713. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1714. }
  1715. }
  1716. else
  1717. {
  1718. /* Non Blocking error : transfer could go on.
  1719. Error is notified to user through user error callback */
  1720. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1721. /* Call registered user error callback */
  1722. hsmartcard->ErrorCallback(hsmartcard);
  1723. #else
  1724. /* Call legacy weak user error callback */
  1725. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1726. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1727. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1728. }
  1729. }
  1730. return;
  1731. } /* End if some error occurs */
  1732. /* SMARTCARD in mode Receiver, end of block interruption ------------------------*/
  1733. if (((isrflags & USART_ISR_EOBF) != 0U) && ((cr1its & USART_CR1_EOBIE) != 0U))
  1734. {
  1735. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1736. __HAL_UNLOCK(hsmartcard);
  1737. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1738. /* Call registered Rx complete callback */
  1739. hsmartcard->RxCpltCallback(hsmartcard);
  1740. #else
  1741. /* Call legacy weak Rx complete callback */
  1742. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  1743. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1744. /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
  1745. to be available during HAL_SMARTCARD_RxCpltCallback() processing */
  1746. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_EOBF);
  1747. return;
  1748. }
  1749. /* SMARTCARD in mode Transmitter ------------------------------------------------*/
  1750. if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
  1751. && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
  1752. || ((cr3its & USART_CR3_TXFTIE) != 0U)))
  1753. {
  1754. if (hsmartcard->TxISR != NULL)
  1755. {
  1756. hsmartcard->TxISR(hsmartcard);
  1757. }
  1758. return;
  1759. }
  1760. /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
  1761. if (__HAL_SMARTCARD_GET_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET)
  1762. {
  1763. if (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET)
  1764. {
  1765. SMARTCARD_EndTransmit_IT(hsmartcard);
  1766. return;
  1767. }
  1768. }
  1769. /* SMARTCARD TX Fifo Empty occurred ----------------------------------------------*/
  1770. if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U))
  1771. {
  1772. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1773. /* Call registered Tx Fifo Empty Callback */
  1774. hsmartcard->TxFifoEmptyCallback(hsmartcard);
  1775. #else
  1776. /* Call legacy weak Tx Fifo Empty Callback */
  1777. HAL_SMARTCARDEx_TxFifoEmptyCallback(hsmartcard);
  1778. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1779. return;
  1780. }
  1781. /* SMARTCARD RX Fifo Full occurred ----------------------------------------------*/
  1782. if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U))
  1783. {
  1784. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1785. /* Call registered Rx Fifo Full Callback */
  1786. hsmartcard->RxFifoFullCallback(hsmartcard);
  1787. #else
  1788. /* Call legacy weak Rx Fifo Full Callback */
  1789. HAL_SMARTCARDEx_RxFifoFullCallback(hsmartcard);
  1790. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1791. return;
  1792. }
  1793. }
  1794. /**
  1795. * @brief Tx Transfer completed callback.
  1796. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1797. * the configuration information for the specified SMARTCARD module.
  1798. * @retval None
  1799. */
  1800. __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1801. {
  1802. /* Prevent unused argument(s) compilation warning */
  1803. UNUSED(hsmartcard);
  1804. /* NOTE : This function should not be modified, when the callback is needed,
  1805. the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
  1806. */
  1807. }
  1808. /**
  1809. * @brief Rx Transfer completed callback.
  1810. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1811. * the configuration information for the specified SMARTCARD module.
  1812. * @retval None
  1813. */
  1814. __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1815. {
  1816. /* Prevent unused argument(s) compilation warning */
  1817. UNUSED(hsmartcard);
  1818. /* NOTE : This function should not be modified, when the callback is needed,
  1819. the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
  1820. */
  1821. }
  1822. /**
  1823. * @brief SMARTCARD error callback.
  1824. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1825. * the configuration information for the specified SMARTCARD module.
  1826. * @retval None
  1827. */
  1828. __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1829. {
  1830. /* Prevent unused argument(s) compilation warning */
  1831. UNUSED(hsmartcard);
  1832. /* NOTE : This function should not be modified, when the callback is needed,
  1833. the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
  1834. */
  1835. }
  1836. /**
  1837. * @brief SMARTCARD Abort Complete callback.
  1838. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1839. * the configuration information for the specified SMARTCARD module.
  1840. * @retval None
  1841. */
  1842. __weak void HAL_SMARTCARD_AbortCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1843. {
  1844. /* Prevent unused argument(s) compilation warning */
  1845. UNUSED(hsmartcard);
  1846. /* NOTE : This function should not be modified, when the callback is needed,
  1847. the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
  1848. */
  1849. }
  1850. /**
  1851. * @brief SMARTCARD Abort Complete callback.
  1852. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1853. * the configuration information for the specified SMARTCARD module.
  1854. * @retval None
  1855. */
  1856. __weak void HAL_SMARTCARD_AbortTransmitCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1857. {
  1858. /* Prevent unused argument(s) compilation warning */
  1859. UNUSED(hsmartcard);
  1860. /* NOTE : This function should not be modified, when the callback is needed,
  1861. the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
  1862. */
  1863. }
  1864. /**
  1865. * @brief SMARTCARD Abort Receive Complete callback.
  1866. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1867. * the configuration information for the specified SMARTCARD module.
  1868. * @retval None
  1869. */
  1870. __weak void HAL_SMARTCARD_AbortReceiveCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1871. {
  1872. /* Prevent unused argument(s) compilation warning */
  1873. UNUSED(hsmartcard);
  1874. /* NOTE : This function should not be modified, when the callback is needed,
  1875. the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
  1876. */
  1877. }
  1878. /**
  1879. * @}
  1880. */
  1881. /** @defgroup SMARTCARD_Exported_Functions_Group4 Peripheral State and Errors functions
  1882. * @brief SMARTCARD State and Errors functions
  1883. *
  1884. @verbatim
  1885. ==============================================================================
  1886. ##### Peripheral State and Errors functions #####
  1887. ==============================================================================
  1888. [..]
  1889. This subsection provides a set of functions allowing to return the State of SmartCard
  1890. handle and also return Peripheral Errors occurred during communication process
  1891. (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
  1892. of the SMARTCARD peripheral.
  1893. (+) HAL_SMARTCARD_GetError() checks in run-time errors that could occur during
  1894. communication.
  1895. @endverbatim
  1896. * @{
  1897. */
  1898. /**
  1899. * @brief Return the SMARTCARD handle state.
  1900. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1901. * the configuration information for the specified SMARTCARD module.
  1902. * @retval SMARTCARD handle state
  1903. */
  1904. HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsmartcard)
  1905. {
  1906. /* Return SMARTCARD handle state */
  1907. uint32_t temp1;
  1908. uint32_t temp2;
  1909. temp1 = (uint32_t)hsmartcard->gState;
  1910. temp2 = (uint32_t)hsmartcard->RxState;
  1911. return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
  1912. }
  1913. /**
  1914. * @brief Return the SMARTCARD handle error code.
  1915. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1916. * the configuration information for the specified SMARTCARD module.
  1917. * @retval SMARTCARD handle Error Code
  1918. */
  1919. uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsmartcard)
  1920. {
  1921. return hsmartcard->ErrorCode;
  1922. }
  1923. /**
  1924. * @}
  1925. */
  1926. /**
  1927. * @}
  1928. */
  1929. /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
  1930. * @{
  1931. */
  1932. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1933. /**
  1934. * @brief Initialize the callbacks to their default values.
  1935. * @param hsmartcard SMARTCARD handle.
  1936. * @retval none
  1937. */
  1938. void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsmartcard)
  1939. {
  1940. /* Init the SMARTCARD Callback settings */
  1941. hsmartcard->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */
  1942. hsmartcard->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */
  1943. hsmartcard->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */
  1944. hsmartcard->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  1945. hsmartcard->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
  1946. hsmartcard->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
  1947. hsmartcard->RxFifoFullCallback = HAL_SMARTCARDEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */
  1948. hsmartcard->TxFifoEmptyCallback = HAL_SMARTCARDEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
  1949. }
  1950. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  1951. /**
  1952. * @brief Configure the SMARTCARD associated USART peripheral.
  1953. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1954. * the configuration information for the specified SMARTCARD module.
  1955. * @retval HAL status
  1956. */
  1957. static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard)
  1958. {
  1959. uint32_t tmpreg;
  1960. SMARTCARD_ClockSourceTypeDef clocksource;
  1961. HAL_StatusTypeDef ret = HAL_OK;
  1962. const uint16_t SMARTCARDPrescTable[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U};
  1963. PLL2_ClocksTypeDef pll2_clocks;
  1964. PLL3_ClocksTypeDef pll3_clocks;
  1965. uint32_t pclk;
  1966. /* Check the parameters */
  1967. assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
  1968. assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard->Init.BaudRate));
  1969. assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard->Init.WordLength));
  1970. assert_param(IS_SMARTCARD_STOPBITS(hsmartcard->Init.StopBits));
  1971. assert_param(IS_SMARTCARD_PARITY(hsmartcard->Init.Parity));
  1972. assert_param(IS_SMARTCARD_MODE(hsmartcard->Init.Mode));
  1973. assert_param(IS_SMARTCARD_POLARITY(hsmartcard->Init.CLKPolarity));
  1974. assert_param(IS_SMARTCARD_PHASE(hsmartcard->Init.CLKPhase));
  1975. assert_param(IS_SMARTCARD_LASTBIT(hsmartcard->Init.CLKLastBit));
  1976. assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsmartcard->Init.OneBitSampling));
  1977. assert_param(IS_SMARTCARD_NACK(hsmartcard->Init.NACKEnable));
  1978. assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard->Init.TimeOutEnable));
  1979. assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard->Init.AutoRetryCount));
  1980. assert_param(IS_SMARTCARD_CLOCKPRESCALER(hsmartcard->Init.ClockPrescaler));
  1981. /*-------------------------- USART CR1 Configuration -----------------------*/
  1982. /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
  1983. * Oversampling is forced to 16 (OVER8 = 0).
  1984. * Configure the Parity and Mode:
  1985. * set PS bit according to hsmartcard->Init.Parity value
  1986. * set TE and RE bits according to hsmartcard->Init.Mode value */
  1987. tmpreg = (uint32_t) hsmartcard->Init.Parity | hsmartcard->Init.Mode;
  1988. tmpreg |= (uint32_t) hsmartcard->Init.WordLength | hsmartcard->FifoMode;
  1989. MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_FIELDS, tmpreg);
  1990. /*-------------------------- USART CR2 Configuration -----------------------*/
  1991. tmpreg = hsmartcard->Init.StopBits;
  1992. /* Synchronous mode is activated by default */
  1993. tmpreg |= (uint32_t) USART_CR2_CLKEN | hsmartcard->Init.CLKPolarity;
  1994. tmpreg |= (uint32_t) hsmartcard->Init.CLKPhase | hsmartcard->Init.CLKLastBit;
  1995. tmpreg |= (uint32_t) hsmartcard->Init.TimeOutEnable;
  1996. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_FIELDS, tmpreg);
  1997. /*-------------------------- USART CR3 Configuration -----------------------*/
  1998. /* Configure
  1999. * - one-bit sampling method versus three samples' majority rule
  2000. * according to hsmartcard->Init.OneBitSampling
  2001. * - NACK transmission in case of parity error according
  2002. * to hsmartcard->Init.NACKEnable
  2003. * - autoretry counter according to hsmartcard->Init.AutoRetryCount */
  2004. tmpreg = (uint32_t) hsmartcard->Init.OneBitSampling | hsmartcard->Init.NACKEnable;
  2005. tmpreg |= ((uint32_t)hsmartcard->Init.AutoRetryCount << USART_CR3_SCARCNT_Pos);
  2006. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_FIELDS, tmpreg);
  2007. /*--------------------- SMARTCARD clock PRESC Configuration ----------------*/
  2008. /* Configure
  2009. * - SMARTCARD Clock Prescaler: set PRESCALER according to hsmartcard->Init.ClockPrescaler value */
  2010. MODIFY_REG(hsmartcard->Instance->PRESC, USART_PRESC_PRESCALER, hsmartcard->Init.ClockPrescaler);
  2011. /*-------------------------- USART GTPR Configuration ----------------------*/
  2012. tmpreg = (hsmartcard->Init.Prescaler | ((uint32_t)hsmartcard->Init.GuardTime << USART_GTPR_GT_Pos));
  2013. MODIFY_REG(hsmartcard->Instance->GTPR, (uint16_t)(USART_GTPR_GT | USART_GTPR_PSC), (uint16_t)tmpreg);
  2014. /*-------------------------- USART RTOR Configuration ----------------------*/
  2015. tmpreg = ((uint32_t)hsmartcard->Init.BlockLength << USART_RTOR_BLEN_Pos);
  2016. if (hsmartcard->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE)
  2017. {
  2018. assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue));
  2019. tmpreg |= (uint32_t) hsmartcard->Init.TimeOutValue;
  2020. }
  2021. MODIFY_REG(hsmartcard->Instance->RTOR, (USART_RTOR_RTO | USART_RTOR_BLEN), tmpreg);
  2022. /*-------------------------- USART BRR Configuration -----------------------*/
  2023. SMARTCARD_GETCLOCKSOURCE(hsmartcard, clocksource);
  2024. tmpreg = 0U;
  2025. switch (clocksource)
  2026. {
  2027. case SMARTCARD_CLOCKSOURCE_D2PCLK1:
  2028. pclk = HAL_RCC_GetPCLK1Freq();
  2029. tmpreg = (uint16_t)(((pclk / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2030. break;
  2031. case SMARTCARD_CLOCKSOURCE_D2PCLK2:
  2032. pclk = HAL_RCC_GetPCLK2Freq();
  2033. tmpreg = (uint16_t)(((pclk / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2034. break;
  2035. case SMARTCARD_CLOCKSOURCE_PLL2Q:
  2036. HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks);
  2037. tmpreg = (uint16_t)(((pll2_clocks.PLL2_Q_Frequency / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2038. break;
  2039. case SMARTCARD_CLOCKSOURCE_PLL3Q:
  2040. HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks);
  2041. tmpreg = (uint16_t)(((pll3_clocks.PLL3_Q_Frequency / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2042. break;
  2043. case SMARTCARD_CLOCKSOURCE_HSI:
  2044. if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
  2045. {
  2046. tmpreg = (uint16_t)((((HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U)) / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2047. }
  2048. else
  2049. {
  2050. tmpreg = (uint16_t)(((HSI_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2051. }
  2052. break;
  2053. case SMARTCARD_CLOCKSOURCE_CSI:
  2054. tmpreg = (uint16_t)(((CSI_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2055. break;
  2056. case SMARTCARD_CLOCKSOURCE_LSE:
  2057. tmpreg = (uint16_t)(((uint16_t)(LSE_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2058. break;
  2059. default:
  2060. ret = HAL_ERROR;
  2061. break;
  2062. }
  2063. /* USARTDIV must be greater than or equal to 0d16 */
  2064. if ((tmpreg >= USART_BRR_MIN) && (tmpreg <= USART_BRR_MAX))
  2065. {
  2066. hsmartcard->Instance->BRR = tmpreg;
  2067. }
  2068. else
  2069. {
  2070. ret = HAL_ERROR;
  2071. }
  2072. /* Initialize the number of data to process during RX/TX ISR execution */
  2073. hsmartcard->NbTxDataToProcess = 1U;
  2074. hsmartcard->NbRxDataToProcess = 1U;
  2075. /* Clear ISR function pointers */
  2076. hsmartcard->RxISR = NULL;
  2077. hsmartcard->TxISR = NULL;
  2078. return ret;
  2079. }
  2080. /**
  2081. * @brief Configure the SMARTCARD associated USART peripheral advanced features.
  2082. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2083. * the configuration information for the specified SMARTCARD module.
  2084. * @retval None
  2085. */
  2086. static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard)
  2087. {
  2088. /* Check whether the set of advanced features to configure is properly set */
  2089. assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard->AdvancedInit.AdvFeatureInit));
  2090. /* if required, configure TX pin active level inversion */
  2091. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT))
  2092. {
  2093. assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard->AdvancedInit.TxPinLevelInvert));
  2094. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_TXINV, hsmartcard->AdvancedInit.TxPinLevelInvert);
  2095. }
  2096. /* if required, configure RX pin active level inversion */
  2097. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT))
  2098. {
  2099. assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard->AdvancedInit.RxPinLevelInvert));
  2100. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_RXINV, hsmartcard->AdvancedInit.RxPinLevelInvert);
  2101. }
  2102. /* if required, configure data inversion */
  2103. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT))
  2104. {
  2105. assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard->AdvancedInit.DataInvert));
  2106. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_DATAINV, hsmartcard->AdvancedInit.DataInvert);
  2107. }
  2108. /* if required, configure RX/TX pins swap */
  2109. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT))
  2110. {
  2111. assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard->AdvancedInit.Swap));
  2112. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_SWAP, hsmartcard->AdvancedInit.Swap);
  2113. }
  2114. /* if required, configure RX overrun detection disabling */
  2115. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT))
  2116. {
  2117. assert_param(IS_SMARTCARD_OVERRUN(hsmartcard->AdvancedInit.OverrunDisable));
  2118. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_OVRDIS, hsmartcard->AdvancedInit.OverrunDisable);
  2119. }
  2120. /* if required, configure DMA disabling on reception error */
  2121. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT))
  2122. {
  2123. assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard->AdvancedInit.DMADisableonRxError));
  2124. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_DDRE, hsmartcard->AdvancedInit.DMADisableonRxError);
  2125. }
  2126. /* if required, configure MSB first on communication line */
  2127. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT))
  2128. {
  2129. assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard->AdvancedInit.MSBFirst));
  2130. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_MSBFIRST, hsmartcard->AdvancedInit.MSBFirst);
  2131. }
  2132. }
  2133. /**
  2134. * @brief Check the SMARTCARD Idle State.
  2135. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2136. * the configuration information for the specified SMARTCARD module.
  2137. * @retval HAL status
  2138. */
  2139. static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard)
  2140. {
  2141. uint32_t tickstart;
  2142. /* Initialize the SMARTCARD ErrorCode */
  2143. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  2144. /* Init tickstart for timeout management */
  2145. tickstart = HAL_GetTick();
  2146. /* Check if the Transmitter is enabled */
  2147. if ((hsmartcard->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  2148. {
  2149. /* Wait until TEACK flag is set */
  2150. if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_TEACK, RESET, tickstart,
  2151. SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
  2152. {
  2153. /* Timeout occurred */
  2154. return HAL_TIMEOUT;
  2155. }
  2156. }
  2157. /* Check if the Receiver is enabled */
  2158. if ((hsmartcard->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  2159. {
  2160. /* Wait until REACK flag is set */
  2161. if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_REACK, RESET, tickstart,
  2162. SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
  2163. {
  2164. /* Timeout occurred */
  2165. return HAL_TIMEOUT;
  2166. }
  2167. }
  2168. /* Initialize the SMARTCARD states */
  2169. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2170. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2171. /* Process Unlocked */
  2172. __HAL_UNLOCK(hsmartcard);
  2173. return HAL_OK;
  2174. }
  2175. /**
  2176. * @brief Handle SMARTCARD Communication Timeout.
  2177. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2178. * the configuration information for the specified SMARTCARD module.
  2179. * @param Flag Specifies the SMARTCARD flag to check.
  2180. * @param Status The new Flag status (SET or RESET).
  2181. * @param Tickstart Tick start value
  2182. * @param Timeout Timeout duration.
  2183. * @retval HAL status
  2184. */
  2185. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag,
  2186. FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  2187. {
  2188. /* Wait until flag is set */
  2189. while ((__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) ? SET : RESET) == Status)
  2190. {
  2191. /* Check for the Timeout */
  2192. if (Timeout != HAL_MAX_DELAY)
  2193. {
  2194. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  2195. {
  2196. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  2197. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE));
  2198. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2199. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2200. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2201. /* Process Unlocked */
  2202. __HAL_UNLOCK(hsmartcard);
  2203. return HAL_TIMEOUT;
  2204. }
  2205. }
  2206. }
  2207. return HAL_OK;
  2208. }
  2209. /**
  2210. * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
  2211. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2212. * the configuration information for the specified SMARTCARD module.
  2213. * @retval None
  2214. */
  2215. static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
  2216. {
  2217. /* Disable TXEIE, TCIE and ERR (Frame error, noise error, overrun error) interrupts */
  2218. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  2219. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2220. /* At end of Tx process, restore hsmartcard->gState to Ready */
  2221. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2222. }
  2223. /**
  2224. * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
  2225. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2226. * the configuration information for the specified SMARTCARD module.
  2227. * @retval None
  2228. */
  2229. static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
  2230. {
  2231. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  2232. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
  2233. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2234. /* At end of Rx process, restore hsmartcard->RxState to Ready */
  2235. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2236. }
  2237. /**
  2238. * @brief DMA SMARTCARD transmit process complete callback.
  2239. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  2240. * the configuration information for the specified DMA module.
  2241. * @retval None
  2242. */
  2243. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  2244. {
  2245. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2246. hsmartcard->TxXferCount = 0U;
  2247. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  2248. in the SMARTCARD associated USART CR3 register */
  2249. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  2250. /* Enable the SMARTCARD Transmit Complete Interrupt */
  2251. __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2252. }
  2253. /**
  2254. * @brief DMA SMARTCARD receive process complete callback.
  2255. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  2256. * the configuration information for the specified DMA module.
  2257. * @retval None
  2258. */
  2259. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  2260. {
  2261. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2262. hsmartcard->RxXferCount = 0U;
  2263. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  2264. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  2265. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2266. /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
  2267. in the SMARTCARD associated USART CR3 register */
  2268. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  2269. /* At end of Rx process, restore hsmartcard->RxState to Ready */
  2270. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2271. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2272. /* Call registered Rx complete callback */
  2273. hsmartcard->RxCpltCallback(hsmartcard);
  2274. #else
  2275. /* Call legacy weak Rx complete callback */
  2276. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  2277. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2278. }
  2279. /**
  2280. * @brief DMA SMARTCARD communication error callback.
  2281. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  2282. * the configuration information for the specified DMA module.
  2283. * @retval None
  2284. */
  2285. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
  2286. {
  2287. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2288. /* Stop SMARTCARD DMA Tx request if ongoing */
  2289. if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  2290. {
  2291. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  2292. {
  2293. hsmartcard->TxXferCount = 0U;
  2294. SMARTCARD_EndTxTransfer(hsmartcard);
  2295. }
  2296. }
  2297. /* Stop SMARTCARD DMA Rx request if ongoing */
  2298. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  2299. {
  2300. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  2301. {
  2302. hsmartcard->RxXferCount = 0U;
  2303. SMARTCARD_EndRxTransfer(hsmartcard);
  2304. }
  2305. }
  2306. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
  2307. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2308. /* Call registered user error callback */
  2309. hsmartcard->ErrorCallback(hsmartcard);
  2310. #else
  2311. /* Call legacy weak user error callback */
  2312. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  2313. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2314. }
  2315. /**
  2316. * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
  2317. * (To be called at end of DMA Abort procedure following error occurrence).
  2318. * @param hdma DMA handle.
  2319. * @retval None
  2320. */
  2321. static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  2322. {
  2323. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2324. hsmartcard->RxXferCount = 0U;
  2325. hsmartcard->TxXferCount = 0U;
  2326. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2327. /* Call registered user error callback */
  2328. hsmartcard->ErrorCallback(hsmartcard);
  2329. #else
  2330. /* Call legacy weak user error callback */
  2331. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  2332. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2333. }
  2334. /**
  2335. * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user
  2336. * (To be called at end of DMA Tx Abort procedure following user abort request).
  2337. * @note When this callback is executed, User Abort complete call back is called only if no
  2338. * Abort still ongoing for Rx DMA Handle.
  2339. * @param hdma DMA handle.
  2340. * @retval None
  2341. */
  2342. static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  2343. {
  2344. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2345. hsmartcard->hdmatx->XferAbortCallback = NULL;
  2346. /* Check if an Abort process is still ongoing */
  2347. if (hsmartcard->hdmarx != NULL)
  2348. {
  2349. if (hsmartcard->hdmarx->XferAbortCallback != NULL)
  2350. {
  2351. return;
  2352. }
  2353. }
  2354. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2355. hsmartcard->TxXferCount = 0U;
  2356. hsmartcard->RxXferCount = 0U;
  2357. /* Reset errorCode */
  2358. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  2359. /* Clear the Error flags in the ICR register */
  2360. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  2361. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
  2362. SMARTCARD_CLEAR_EOBF);
  2363. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  2364. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2365. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2366. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2367. /* Call registered Abort complete callback */
  2368. hsmartcard->AbortCpltCallback(hsmartcard);
  2369. #else
  2370. /* Call legacy weak Abort complete callback */
  2371. HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  2372. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2373. }
  2374. /**
  2375. * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user
  2376. * (To be called at end of DMA Rx Abort procedure following user abort request).
  2377. * @note When this callback is executed, User Abort complete call back is called only if no
  2378. * Abort still ongoing for Tx DMA Handle.
  2379. * @param hdma DMA handle.
  2380. * @retval None
  2381. */
  2382. static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  2383. {
  2384. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2385. hsmartcard->hdmarx->XferAbortCallback = NULL;
  2386. /* Check if an Abort process is still ongoing */
  2387. if (hsmartcard->hdmatx != NULL)
  2388. {
  2389. if (hsmartcard->hdmatx->XferAbortCallback != NULL)
  2390. {
  2391. return;
  2392. }
  2393. }
  2394. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2395. hsmartcard->TxXferCount = 0U;
  2396. hsmartcard->RxXferCount = 0U;
  2397. /* Reset errorCode */
  2398. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  2399. /* Clear the Error flags in the ICR register */
  2400. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  2401. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
  2402. SMARTCARD_CLEAR_EOBF);
  2403. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  2404. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2405. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2406. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2407. /* Call registered Abort complete callback */
  2408. hsmartcard->AbortCpltCallback(hsmartcard);
  2409. #else
  2410. /* Call legacy weak Abort complete callback */
  2411. HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  2412. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2413. }
  2414. /**
  2415. * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
  2416. * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
  2417. * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
  2418. * and leads to user Tx Abort Complete callback execution).
  2419. * @param hdma DMA handle.
  2420. * @retval None
  2421. */
  2422. static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  2423. {
  2424. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2425. hsmartcard->TxXferCount = 0U;
  2426. /* Clear the Error flags in the ICR register */
  2427. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  2428. /* Restore hsmartcard->gState to Ready */
  2429. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2430. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2431. /* Call registered Abort Transmit Complete Callback */
  2432. hsmartcard->AbortTransmitCpltCallback(hsmartcard);
  2433. #else
  2434. /* Call legacy weak Abort Transmit Complete Callback */
  2435. HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  2436. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2437. }
  2438. /**
  2439. * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
  2440. * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
  2441. * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
  2442. * and leads to user Rx Abort Complete callback execution).
  2443. * @param hdma DMA handle.
  2444. * @retval None
  2445. */
  2446. static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  2447. {
  2448. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2449. hsmartcard->RxXferCount = 0U;
  2450. /* Clear the Error flags in the ICR register */
  2451. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  2452. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
  2453. SMARTCARD_CLEAR_EOBF);
  2454. /* Restore hsmartcard->RxState to Ready */
  2455. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2456. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2457. /* Call registered Abort Receive Complete Callback */
  2458. hsmartcard->AbortReceiveCpltCallback(hsmartcard);
  2459. #else
  2460. /* Call legacy weak Abort Receive Complete Callback */
  2461. HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  2462. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2463. }
  2464. /**
  2465. * @brief Send an amount of data in non-blocking mode.
  2466. * @note Function called under interruption only, once
  2467. * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
  2468. * and when the FIFO mode is disabled.
  2469. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2470. * the configuration information for the specified SMARTCARD module.
  2471. * @retval None
  2472. */
  2473. static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard)
  2474. {
  2475. /* Check that a Tx process is ongoing */
  2476. if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  2477. {
  2478. if (hsmartcard->TxXferCount == 0U)
  2479. {
  2480. /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
  2481. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  2482. /* Enable the SMARTCARD Transmit Complete Interrupt */
  2483. __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2484. }
  2485. else
  2486. {
  2487. hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
  2488. hsmartcard->pTxBuffPtr++;
  2489. hsmartcard->TxXferCount--;
  2490. }
  2491. }
  2492. }
  2493. /**
  2494. * @brief Send an amount of data in non-blocking mode.
  2495. * @note Function called under interruption only, once
  2496. * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
  2497. * and when the FIFO mode is enabled.
  2498. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2499. * the configuration information for the specified SMARTCARD module.
  2500. * @retval None
  2501. */
  2502. static void SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
  2503. {
  2504. uint16_t nb_tx_data;
  2505. /* Check that a Tx process is ongoing */
  2506. if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  2507. {
  2508. for (nb_tx_data = hsmartcard->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
  2509. {
  2510. if (hsmartcard->TxXferCount == 0U)
  2511. {
  2512. /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
  2513. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  2514. /* Enable the SMARTCARD Transmit Complete Interrupt */
  2515. __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2516. }
  2517. else if (READ_BIT(hsmartcard->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
  2518. {
  2519. hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
  2520. hsmartcard->pTxBuffPtr++;
  2521. hsmartcard->TxXferCount--;
  2522. }
  2523. else
  2524. {
  2525. /* Nothing to do */
  2526. }
  2527. }
  2528. }
  2529. }
  2530. /**
  2531. * @brief Wrap up transmission in non-blocking mode.
  2532. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2533. * the configuration information for the specified SMARTCARD module.
  2534. * @retval None
  2535. */
  2536. static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  2537. {
  2538. /* Disable the SMARTCARD Transmit Complete Interrupt */
  2539. __HAL_SMARTCARD_DISABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2540. /* Check if a receive process is ongoing or not. If not disable ERR IT */
  2541. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  2542. {
  2543. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  2544. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2545. }
  2546. /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */
  2547. if (hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
  2548. {
  2549. /* Disable the Peripheral first to update modes */
  2550. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  2551. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  2552. /* Enable the Peripheral */
  2553. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  2554. }
  2555. /* Tx process is ended, restore hsmartcard->gState to Ready */
  2556. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2557. /* Clear TxISR function pointer */
  2558. hsmartcard->TxISR = NULL;
  2559. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2560. /* Call registered Tx complete callback */
  2561. hsmartcard->TxCpltCallback(hsmartcard);
  2562. #else
  2563. /* Call legacy weak Tx complete callback */
  2564. HAL_SMARTCARD_TxCpltCallback(hsmartcard);
  2565. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2566. }
  2567. /**
  2568. * @brief Receive an amount of data in non-blocking mode.
  2569. * @note Function called under interruption only, once
  2570. * interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
  2571. * and when the FIFO mode is disabled.
  2572. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2573. * the configuration information for the specified SMARTCARD module.
  2574. * @retval None
  2575. */
  2576. static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard)
  2577. {
  2578. /* Check that a Rx process is ongoing */
  2579. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  2580. {
  2581. *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
  2582. hsmartcard->pRxBuffPtr++;
  2583. hsmartcard->RxXferCount--;
  2584. if (hsmartcard->RxXferCount == 0U)
  2585. {
  2586. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
  2587. /* Check if a transmit process is ongoing or not. If not disable ERR IT */
  2588. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  2589. {
  2590. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  2591. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2592. }
  2593. /* Disable the SMARTCARD Parity Error Interrupt */
  2594. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  2595. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2596. /* Clear RxISR function pointer */
  2597. hsmartcard->RxISR = NULL;
  2598. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2599. /* Call registered Rx complete callback */
  2600. hsmartcard->RxCpltCallback(hsmartcard);
  2601. #else
  2602. /* Call legacy weak Rx complete callback */
  2603. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  2604. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2605. }
  2606. }
  2607. else
  2608. {
  2609. /* Clear RXNE interrupt flag */
  2610. __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
  2611. }
  2612. }
  2613. /**
  2614. * @brief Receive an amount of data in non-blocking mode.
  2615. * @note Function called under interruption only, once
  2616. * interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
  2617. * and when the FIFO mode is enabled.
  2618. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2619. * the configuration information for the specified SMARTCARD module.
  2620. * @retval None
  2621. */
  2622. static void SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
  2623. {
  2624. uint16_t nb_rx_data;
  2625. uint16_t rxdatacount;
  2626. /* Check that a Rx process is ongoing */
  2627. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  2628. {
  2629. for (nb_rx_data = hsmartcard->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
  2630. {
  2631. *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
  2632. hsmartcard->pRxBuffPtr++;
  2633. hsmartcard->RxXferCount--;
  2634. if (hsmartcard->RxXferCount == 0U)
  2635. {
  2636. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
  2637. /* Check if a transmit process is ongoing or not. If not disable ERR IT */
  2638. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  2639. {
  2640. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  2641. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2642. }
  2643. /* Disable the SMARTCARD Parity Error Interrupt */
  2644. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  2645. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2646. /* Clear RxISR function pointer */
  2647. hsmartcard->RxISR = NULL;
  2648. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2649. /* Call registered Rx complete callback */
  2650. hsmartcard->RxCpltCallback(hsmartcard);
  2651. #else
  2652. /* Call legacy weak Rx complete callback */
  2653. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  2654. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2655. }
  2656. }
  2657. /* When remaining number of bytes to receive is less than the RX FIFO
  2658. threshold, next incoming frames are processed as if FIFO mode was
  2659. disabled (i.e. one interrupt per received frame).
  2660. */
  2661. rxdatacount = hsmartcard->RxXferCount;
  2662. if (((rxdatacount != 0U)) && (rxdatacount < hsmartcard->NbRxDataToProcess))
  2663. {
  2664. /* Disable the UART RXFT interrupt*/
  2665. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE);
  2666. /* Update the RxISR function pointer */
  2667. hsmartcard->RxISR = SMARTCARD_RxISR;
  2668. /* Enable the UART Data Register Not Empty interrupt */
  2669. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
  2670. }
  2671. }
  2672. else
  2673. {
  2674. /* Clear RXNE interrupt flag */
  2675. __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
  2676. }
  2677. }
  2678. /**
  2679. * @}
  2680. */
  2681. #endif /* HAL_SMARTCARD_MODULE_ENABLED */
  2682. /**
  2683. * @}
  2684. */
  2685. /**
  2686. * @}
  2687. */
  2688. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/