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.
 
 
 

2843 lines
107 KiB

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