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.
 
 
 

2304 lines
89 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_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. @endverbatim
  91. ******************************************************************************
  92. * @attention
  93. *
  94. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  95. *
  96. * Redistribution and use in source and binary forms, with or without modification,
  97. * are permitted provided that the following conditions are met:
  98. * 1. Redistributions of source code must retain the above copyright notice,
  99. * this list of conditions and the following disclaimer.
  100. * 2. Redistributions in binary form must reproduce the above copyright notice,
  101. * this list of conditions and the following disclaimer in the documentation
  102. * and/or other materials provided with the distribution.
  103. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  104. * may be used to endorse or promote products derived from this software
  105. * without specific prior written permission.
  106. *
  107. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  108. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  109. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  110. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  111. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  112. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  113. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  114. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  115. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  116. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  117. *
  118. ******************************************************************************
  119. */
  120. /* Includes ------------------------------------------------------------------*/
  121. #include "stm32f0xx_hal.h"
  122. #if !defined(STM32F030x6) && !defined(STM32F030x8) && !defined(STM32F070x6) && !defined(STM32F070xB) && !defined(STM32F030xC)
  123. /** @addtogroup STM32F0xx_HAL_Driver
  124. * @{
  125. */
  126. /** @defgroup SMARTCARD SMARTCARD
  127. * @brief HAL SMARTCARD module driver
  128. * @{
  129. */
  130. #ifdef HAL_SMARTCARD_MODULE_ENABLED
  131. /* Private typedef -----------------------------------------------------------*/
  132. /* Private define ------------------------------------------------------------*/
  133. /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants
  134. * @{
  135. */
  136. #define SMARTCARD_TEACK_REACK_TIMEOUT 1000U /*!< SMARTCARD TX or RX enable acknowledge time-out value */
  137. #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  138. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8)) /*!< USART CR1 fields of parameters set by SMARTCARD_SetConfig API */
  139. #define USART_CR2_CLK_FIELDS ((uint32_t)(USART_CR2_CLKEN|USART_CR2_CPOL|USART_CR2_CPHA|USART_CR2_LBCL)) /*!< SMARTCARD clock-related USART CR2 fields of parameters */
  140. #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 */
  141. #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 */
  142. /**
  143. * @}
  144. */
  145. /* Private macros ------------------------------------------------------------*/
  146. /* Private variables ---------------------------------------------------------*/
  147. /* Private function prototypes -----------------------------------------------*/
  148. /** @addtogroup SMARTCARD_Private_Functions
  149. * @{
  150. */
  151. static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard);
  152. static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard);
  153. static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard);
  154. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
  155. static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard);
  156. static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard);
  157. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  158. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  159. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
  160. static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  161. static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  162. static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  163. static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  164. static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  165. static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
  166. static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
  167. static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard);
  168. /**
  169. * @}
  170. */
  171. /* Exported functions --------------------------------------------------------*/
  172. /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
  173. * @{
  174. */
  175. /** @defgroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions
  176. * @brief Initialization and Configuration functions
  177. *
  178. @verbatim
  179. ==============================================================================
  180. ##### Initialization and Configuration functions #####
  181. ==============================================================================
  182. [..]
  183. This subsection provides a set of functions allowing to initialize the USARTx
  184. associated to the SmartCard.
  185. [..]
  186. The Smartcard interface is designed to support asynchronous protocol Smartcards as
  187. defined in the ISO 7816-3 standard.
  188. [..]
  189. The USART can provide a clock to the smartcard through the SCLK output.
  190. In smartcard mode, SCLK is not associated to the communication but is simply derived
  191. from the internal peripheral input clock through a 5-bit prescaler.
  192. [..]
  193. (+) These parameters can be configured:
  194. (++) Baud Rate
  195. (++) Parity: should be enabled
  196. (++) Receiver/transmitter modes
  197. (++) Synchronous mode (and if enabled, phase, polarity and last bit parameters)
  198. (++) Prescaler value
  199. (++) Guard bit time
  200. (++) NACK enabling or disabling on transmission error
  201. (+) The following advanced features can be configured as well:
  202. (++) TX and/or RX pin level inversion
  203. (++) data logical level inversion
  204. (++) RX and TX pins swap
  205. (++) RX overrun detection disabling
  206. (++) DMA disabling on RX error
  207. (++) MSB first on communication line
  208. (++) Time out enabling (and if activated, timeout value)
  209. (++) Block length
  210. (++) Auto-retry counter
  211. [..]
  212. The HAL_SMARTCARD_Init() API follows the USART synchronous configuration procedures
  213. (details for the procedures are available in reference manual).
  214. @endverbatim
  215. * @{
  216. */
  217. /*
  218. Additional Table:
  219. Frame Length is fixed to 8 bits plus parity:
  220. SMARTCARD frame format is given in the following table
  221. +---------------------------------------------------------------+
  222. | M1M0 bits | PCE bit | SMARTCARD frame |
  223. |-----------------------|---------------------------------------|
  224. | 01 | 1 | | SB | 8 bit data | PB | STB | |
  225. +---------------------------------------------------------------+
  226. */
  227. /**
  228. * @brief Initialize the SMARTCARD mode according to the specified
  229. * parameters in the SMARTCARD_HandleTypeDef and initialize the associated handle.
  230. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  231. * the configuration information for the specified SMARTCARD module.
  232. * @retval HAL status
  233. */
  234. HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsmartcard)
  235. {
  236. /* Check the SMARTCARD handle allocation */
  237. if(hsmartcard == NULL)
  238. {
  239. return HAL_ERROR;
  240. }
  241. /* Check the USART associated to the SMARTCARD handle */
  242. assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
  243. if(hsmartcard->gState == HAL_SMARTCARD_STATE_RESET)
  244. {
  245. /* Allocate lock resource and initialize it */
  246. hsmartcard->Lock = HAL_UNLOCKED;
  247. /* Init the low level hardware : GPIO, CLOCK */
  248. HAL_SMARTCARD_MspInit(hsmartcard);
  249. }
  250. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  251. /* Disable the Peripheral to set smartcard mode */
  252. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  253. /* In SmartCard mode, the following bits must be kept cleared:
  254. - LINEN in the USART_CR2 register,
  255. - HDSEL and IREN bits in the USART_CR3 register.*/
  256. CLEAR_BIT(hsmartcard->Instance->CR2, USART_CR2_LINEN);
  257. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN));
  258. /* set the USART in SMARTCARD mode */
  259. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_SCEN);
  260. /* Set the SMARTCARD Communication parameters */
  261. if (SMARTCARD_SetConfig(hsmartcard) == HAL_ERROR)
  262. {
  263. return HAL_ERROR;
  264. }
  265. if (hsmartcard->AdvancedInit.AdvFeatureInit != SMARTCARD_ADVFEATURE_NO_INIT)
  266. {
  267. SMARTCARD_AdvFeatureConfig(hsmartcard);
  268. }
  269. /* Enable the Peripheral */
  270. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  271. /* TEACK and/or REACK to check before moving hsmartcard->gState and hsmartcard->RxState to Ready */
  272. return (SMARTCARD_CheckIdleState(hsmartcard));
  273. }
  274. /**
  275. * @brief DeInitialize the SMARTCARD peripheral.
  276. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  277. * the configuration information for the specified SMARTCARD module.
  278. * @retval HAL status
  279. */
  280. HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsmartcard)
  281. {
  282. /* Check the SMARTCARD handle allocation */
  283. if(hsmartcard == NULL)
  284. {
  285. return HAL_ERROR;
  286. }
  287. /* Check the USART/UART associated to the SMARTCARD handle */
  288. assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
  289. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  290. /* Disable the Peripheral */
  291. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  292. WRITE_REG(hsmartcard->Instance->CR1, 0x0U);
  293. WRITE_REG(hsmartcard->Instance->CR2, 0x0U);
  294. WRITE_REG(hsmartcard->Instance->CR3, 0x0U);
  295. WRITE_REG(hsmartcard->Instance->RTOR, 0x0U);
  296. WRITE_REG(hsmartcard->Instance->GTPR, 0x0U);
  297. /* DeInit the low level hardware */
  298. HAL_SMARTCARD_MspDeInit(hsmartcard);
  299. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  300. hsmartcard->gState = HAL_SMARTCARD_STATE_RESET;
  301. hsmartcard->RxState = HAL_SMARTCARD_STATE_RESET;
  302. /* Process Unlock */
  303. __HAL_UNLOCK(hsmartcard);
  304. return HAL_OK;
  305. }
  306. /**
  307. * @brief Initialize the SMARTCARD MSP.
  308. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  309. * the configuration information for the specified SMARTCARD module.
  310. * @retval None
  311. */
  312. __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsmartcard)
  313. {
  314. /* Prevent unused argument(s) compilation warning */
  315. UNUSED(hsmartcard);
  316. /* NOTE : This function should not be modified, when the callback is needed,
  317. the HAL_SMARTCARD_MspInit can be implemented in the user file
  318. */
  319. }
  320. /**
  321. * @brief DeInitialize the SMARTCARD MSP.
  322. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  323. * the configuration information for the specified SMARTCARD module.
  324. * @retval None
  325. */
  326. __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsmartcard)
  327. {
  328. /* Prevent unused argument(s) compilation warning */
  329. UNUSED(hsmartcard);
  330. /* NOTE : This function should not be modified, when the callback is needed,
  331. the HAL_SMARTCARD_MspDeInit can be implemented in the user file
  332. */
  333. }
  334. /**
  335. * @}
  336. */
  337. /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
  338. * @brief SMARTCARD Transmit and Receive functions
  339. *
  340. @verbatim
  341. ==============================================================================
  342. ##### IO operation functions #####
  343. ==============================================================================
  344. [..]
  345. This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
  346. [..]
  347. Smartcard is a single wire half duplex communication protocol.
  348. The Smartcard interface is designed to support asynchronous protocol Smartcards as
  349. defined in the ISO 7816-3 standard. The USART should be configured as:
  350. (+) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
  351. (+) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
  352. [..]
  353. (#) There are two modes of transfer:
  354. (++) Blocking mode: The communication is performed in polling mode.
  355. The HAL status of all data processing is returned by the same function
  356. after finishing transfer.
  357. (++) Non-Blocking mode: The communication is performed using Interrupts
  358. or DMA, the relevant API's return the HAL status.
  359. The end of the data processing will be indicated through the
  360. dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
  361. using DMA mode.
  362. (++) The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
  363. will be executed respectively at the end of the Transmit or Receive process
  364. The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication
  365. error is detected.
  366. (#) Blocking mode APIs are :
  367. (++) HAL_SMARTCARD_Transmit()
  368. (++) HAL_SMARTCARD_Receive()
  369. (#) Non Blocking mode APIs with Interrupt are :
  370. (++) HAL_SMARTCARD_Transmit_IT()
  371. (++) HAL_SMARTCARD_Receive_IT()
  372. (++) HAL_SMARTCARD_IRQHandler()
  373. (#) Non Blocking mode functions with DMA are :
  374. (++) HAL_SMARTCARD_Transmit_DMA()
  375. (++) HAL_SMARTCARD_Receive_DMA()
  376. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  377. (++) HAL_SMARTCARD_TxCpltCallback()
  378. (++) HAL_SMARTCARD_RxCpltCallback()
  379. (++) HAL_SMARTCARD_ErrorCallback()
  380. (#) Non-Blocking mode transfers could be aborted using Abort API's :
  381. (++) HAL_SMARTCARD_Abort()
  382. (++) HAL_SMARTCARD_AbortTransmit()
  383. (++) HAL_SMARTCARD_AbortReceive()
  384. (++) HAL_SMARTCARD_Abort_IT()
  385. (++) HAL_SMARTCARD_AbortTransmit_IT()
  386. (++) HAL_SMARTCARD_AbortReceive_IT()
  387. (#) For Abort services based on interrupts (HAL_SMARTCARD_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
  388. (++) HAL_SMARTCARD_AbortCpltCallback()
  389. (++) HAL_SMARTCARD_AbortTransmitCpltCallback()
  390. (++) HAL_SMARTCARD_AbortReceiveCpltCallback()
  391. (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  392. Errors are handled as follows :
  393. (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  394. to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
  395. Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
  396. and HAL_SMARTCARD_ErrorCallback() user callback is executed. Transfer is kept ongoing on SMARTCARD side.
  397. If user wants to abort it, Abort services should be called by user.
  398. (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  399. This concerns Frame Error in Interrupt mode tranmission, Overrun Error in Interrupt mode reception and all errors in DMA mode.
  400. Error code is set to allow user to identify error type, and HAL_SMARTCARD_ErrorCallback() user callback is executed.
  401. @endverbatim
  402. * @{
  403. */
  404. /**
  405. * @brief Send an amount of data in blocking mode.
  406. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  407. * the configuration information for the specified SMARTCARD module.
  408. * @param pData pointer to data buffer.
  409. * @param Size amount of data to be sent.
  410. * @param Timeout Timeout duration.
  411. * @retval HAL status
  412. */
  413. HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  414. {
  415. uint32_t tickstart = 0U;
  416. /* Check that a Tx process is not already ongoing */
  417. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  418. {
  419. if((pData == NULL) || (Size == 0U))
  420. {
  421. return HAL_ERROR;
  422. }
  423. /* Process Locked */
  424. __HAL_LOCK(hsmartcard);
  425. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  426. /* Init tickstart for timeout managment*/
  427. tickstart = HAL_GetTick();
  428. /* Disable the Peripheral first to update mode for TX master */
  429. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  430. /* Disable Rx, enable Tx */
  431. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  432. SET_BIT(hsmartcard->Instance->RQR, SMARTCARD_RXDATA_FLUSH_REQUEST);
  433. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  434. /* Enable the Peripheral */
  435. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  436. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  437. hsmartcard->TxXferSize = Size;
  438. hsmartcard->TxXferCount = Size;
  439. while(hsmartcard->TxXferCount > 0U)
  440. {
  441. hsmartcard->TxXferCount--;
  442. if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  443. {
  444. return HAL_TIMEOUT;
  445. }
  446. hsmartcard->Instance->TDR = (*pData++ & (uint8_t)0xFFU);
  447. }
  448. if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  449. {
  450. return HAL_TIMEOUT;
  451. }
  452. /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */
  453. if(hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
  454. {
  455. /* Disable the Peripheral first to update modes */
  456. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  457. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  458. /* Enable the Peripheral */
  459. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  460. }
  461. /* At end of Tx process, restore hsmartcard->gState to Ready */
  462. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  463. /* Process Unlocked */
  464. __HAL_UNLOCK(hsmartcard);
  465. return HAL_OK;
  466. }
  467. else
  468. {
  469. return HAL_BUSY;
  470. }
  471. }
  472. /**
  473. * @brief Receive an amount of data in blocking mode.
  474. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  475. * the configuration information for the specified SMARTCARD module.
  476. * @param pData pointer to data buffer.
  477. * @param Size amount of data to be received.
  478. * @param Timeout Timeout duration.
  479. * @retval HAL status
  480. */
  481. HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  482. {
  483. uint32_t tickstart = 0U;
  484. /* Check that a Rx process is not already ongoing */
  485. if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  486. {
  487. if((pData == NULL) || (Size == 0U))
  488. {
  489. return HAL_ERROR;
  490. }
  491. /* Process Locked */
  492. __HAL_LOCK(hsmartcard);
  493. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  494. hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  495. /* Init tickstart for timeout managment*/
  496. tickstart = HAL_GetTick();
  497. hsmartcard->RxXferSize = Size;
  498. hsmartcard->RxXferCount = Size;
  499. /* Check the remain data to be received */
  500. while(hsmartcard->RxXferCount > 0U)
  501. {
  502. hsmartcard->RxXferCount--;
  503. if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  504. {
  505. return HAL_TIMEOUT;
  506. }
  507. *pData++ = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0x00FFU);
  508. }
  509. /* At end of Rx process, restore hsmartcard->RxState to Ready */
  510. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  511. /* Process Unlocked */
  512. __HAL_UNLOCK(hsmartcard);
  513. return HAL_OK;
  514. }
  515. else
  516. {
  517. return HAL_BUSY;
  518. }
  519. }
  520. /**
  521. * @brief Send an amount of data in interrupt mode.
  522. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  523. * the configuration information for the specified SMARTCARD module.
  524. * @param pData pointer to data buffer.
  525. * @param Size amount of data to be sent.
  526. * @retval HAL status
  527. */
  528. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  529. {
  530. /* Check that a Tx process is not already ongoing */
  531. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  532. {
  533. if((pData == NULL) || (Size == 0))
  534. {
  535. return HAL_ERROR;
  536. }
  537. /* Process Locked */
  538. __HAL_LOCK(hsmartcard);
  539. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  540. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  541. hsmartcard->pTxBuffPtr = pData;
  542. hsmartcard->TxXferSize = Size;
  543. hsmartcard->TxXferCount = Size;
  544. /* Disable the Peripheral first to update mode for TX master */
  545. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  546. /* Disable Rx, enable Tx */
  547. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  548. SET_BIT(hsmartcard->Instance->RQR, SMARTCARD_RXDATA_FLUSH_REQUEST);
  549. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  550. /* Enable the Peripheral */
  551. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  552. /* Process Unlocked */
  553. __HAL_UNLOCK(hsmartcard);
  554. /* Enable the SMARTCARD Error Interrupt: (Frame error) */
  555. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  556. /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */
  557. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE);
  558. return HAL_OK;
  559. }
  560. else
  561. {
  562. return HAL_BUSY;
  563. }
  564. }
  565. /**
  566. * @brief Receive an amount of data in interrupt mode.
  567. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  568. * the configuration information for the specified SMARTCARD module.
  569. * @param pData pointer to data buffer.
  570. * @param Size amount of data to be received.
  571. * @retval HAL status
  572. */
  573. HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  574. {
  575. /* Check that a Rx process is not already ongoing */
  576. if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  577. {
  578. if((pData == NULL) || (Size == 0U))
  579. {
  580. return HAL_ERROR;
  581. }
  582. /* Process Locked */
  583. __HAL_LOCK(hsmartcard);
  584. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  585. hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  586. hsmartcard->pRxBuffPtr = pData;
  587. hsmartcard->RxXferSize = Size;
  588. hsmartcard->RxXferCount = Size;
  589. /* Process Unlocked */
  590. __HAL_UNLOCK(hsmartcard);
  591. /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
  592. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE| USART_CR1_RXNEIE);
  593. /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  594. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  595. return HAL_OK;
  596. }
  597. else
  598. {
  599. return HAL_BUSY;
  600. }
  601. }
  602. /**
  603. * @brief Send an amount of data in DMA mode.
  604. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  605. * the configuration information for the specified SMARTCARD module.
  606. * @param pData pointer to data buffer.
  607. * @param Size amount of data to be sent.
  608. * @retval HAL status
  609. */
  610. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  611. {
  612. /* Check that a Tx process is not already ongoing */
  613. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  614. {
  615. if((pData == NULL) || (Size == 0U))
  616. {
  617. return HAL_ERROR;
  618. }
  619. /* Process Locked */
  620. __HAL_LOCK(hsmartcard);
  621. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  622. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  623. hsmartcard->pTxBuffPtr = pData;
  624. hsmartcard->TxXferSize = Size;
  625. hsmartcard->TxXferCount = Size;
  626. /* Disable the Peripheral first to update mode for TX master */
  627. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  628. /* Disable Rx, enable Tx */
  629. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  630. SET_BIT(hsmartcard->Instance->RQR, SMARTCARD_RXDATA_FLUSH_REQUEST);
  631. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  632. /* Enable the Peripheral */
  633. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  634. /* Set the SMARTCARD DMA transfer complete callback */
  635. hsmartcard->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
  636. /* Set the SMARTCARD error callback */
  637. hsmartcard->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
  638. /* Set the DMA abort callback */
  639. hsmartcard->hdmatx->XferAbortCallback = NULL;
  640. /* Enable the SMARTCARD transmit DMA channel */
  641. HAL_DMA_Start_IT(hsmartcard->hdmatx, (uint32_t)hsmartcard->pTxBuffPtr, (uint32_t)&hsmartcard->Instance->TDR, Size);
  642. /* Clear the TC flag in the ICR register */
  643. CLEAR_BIT(hsmartcard->Instance->ICR, USART_ICR_TCCF);
  644. /* Process Unlocked */
  645. __HAL_UNLOCK(hsmartcard);
  646. /* Enable the UART Error Interrupt: (Frame error) */
  647. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  648. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  649. in the SMARTCARD associated USART CR3 register */
  650. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  651. return HAL_OK;
  652. }
  653. else
  654. {
  655. return HAL_BUSY;
  656. }
  657. }
  658. /**
  659. * @brief Receive an amount of data in DMA mode.
  660. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  661. * the configuration information for the specified SMARTCARD module.
  662. * @param pData pointer to data buffer.
  663. * @param Size amount of data to be received.
  664. * @note The SMARTCARD-associated USART parity is enabled (PCE = 1),
  665. * the received data contain the parity bit (MSB position).
  666. * @retval HAL status
  667. */
  668. HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  669. {
  670. /* Check that a Rx process is not already ongoing */
  671. if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  672. {
  673. if((pData == NULL) || (Size == 0U))
  674. {
  675. return HAL_ERROR;
  676. }
  677. /* Process Locked */
  678. __HAL_LOCK(hsmartcard);
  679. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  680. hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  681. hsmartcard->pRxBuffPtr = pData;
  682. hsmartcard->RxXferSize = Size;
  683. /* Set the SMARTCARD DMA transfer complete callback */
  684. hsmartcard->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
  685. /* Set the SMARTCARD DMA error callback */
  686. hsmartcard->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
  687. /* Set the DMA abort callback */
  688. hsmartcard->hdmarx->XferAbortCallback = NULL;
  689. /* Enable the DMA channel */
  690. HAL_DMA_Start_IT(hsmartcard->hdmarx, (uint32_t)&hsmartcard->Instance->RDR, (uint32_t)hsmartcard->pRxBuffPtr, Size);
  691. /* Process Unlocked */
  692. __HAL_UNLOCK(hsmartcard);
  693. /* Enable the UART Parity Error Interrupt */
  694. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  695. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  696. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  697. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  698. in the SMARTCARD associated USART CR3 register */
  699. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  700. return HAL_OK;
  701. }
  702. else
  703. {
  704. return HAL_BUSY;
  705. }
  706. }
  707. /**
  708. * @brief Abort ongoing transfers (blocking mode).
  709. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  710. * the configuration information for the specified SMARTCARD module.
  711. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  712. * This procedure performs following operations :
  713. * - Disable SMARTCARD Interrupts (Tx and Rx)
  714. * - Disable the DMA transfer in the peripheral register (if enabled)
  715. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  716. * - Set handle State to READY
  717. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  718. * @retval HAL status
  719. */
  720. HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsmartcard)
  721. {
  722. /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  723. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  724. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  725. /* Disable the SMARTCARD DMA Tx request if enabled */
  726. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  727. {
  728. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  729. /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  730. if(hsmartcard->hdmatx != NULL)
  731. {
  732. /* Set the SMARTCARD DMA Abort callback to Null.
  733. No call back execution at end of DMA abort procedure */
  734. hsmartcard->hdmatx->XferAbortCallback = NULL;
  735. HAL_DMA_Abort(hsmartcard->hdmatx);
  736. }
  737. }
  738. /* Disable the SMARTCARD DMA Rx request if enabled */
  739. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  740. {
  741. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  742. /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  743. if(hsmartcard->hdmarx != NULL)
  744. {
  745. /* Set the SMARTCARD DMA Abort callback to Null.
  746. No call back execution at end of DMA abort procedure */
  747. hsmartcard->hdmarx->XferAbortCallback = NULL;
  748. HAL_DMA_Abort(hsmartcard->hdmarx);
  749. }
  750. }
  751. /* Reset Tx and Rx transfer counters */
  752. hsmartcard->TxXferCount = 0U;
  753. hsmartcard->RxXferCount = 0U;
  754. /* Clear the Error flags in the ICR register */
  755. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  756. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  757. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  758. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  759. /* Reset Handle ErrorCode to No Error */
  760. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  761. return HAL_OK;
  762. }
  763. /**
  764. * @brief Abort ongoing Transmit transfer (blocking mode).
  765. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  766. * the configuration information for the specified SMARTCARD module.
  767. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  768. * This procedure performs following operations :
  769. * - Disable SMARTCARD Interrupts (Tx)
  770. * - Disable the DMA transfer in the peripheral register (if enabled)
  771. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  772. * - Set handle State to READY
  773. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  774. * @retval HAL status
  775. */
  776. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsmartcard)
  777. {
  778. /* Disable TXEIE and TCIE interrupts */
  779. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  780. /* Check if a receive process is ongoing or not. If not disable ERR IT */
  781. if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  782. {
  783. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  784. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  785. }
  786. /* Disable the SMARTCARD DMA Tx request if enabled */
  787. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  788. {
  789. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  790. /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  791. if(hsmartcard->hdmatx != NULL)
  792. {
  793. /* Set the SMARTCARD DMA Abort callback to Null.
  794. No call back execution at end of DMA abort procedure */
  795. hsmartcard->hdmatx->XferAbortCallback = NULL;
  796. HAL_DMA_Abort(hsmartcard->hdmatx);
  797. }
  798. }
  799. /* Reset Tx transfer counter */
  800. hsmartcard->TxXferCount = 0U;
  801. /* Clear the Error flags in the ICR register */
  802. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  803. /* Restore hsmartcard->gState to Ready */
  804. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  805. return HAL_OK;
  806. }
  807. /**
  808. * @brief Abort ongoing Receive transfer (blocking mode).
  809. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  810. * the configuration information for the specified SMARTCARD module.
  811. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  812. * This procedure performs following operations :
  813. * - Disable SMARTCARD Interrupts (Rx)
  814. * - Disable the DMA transfer in the peripheral register (if enabled)
  815. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  816. * - Set handle State to READY
  817. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  818. * @retval HAL status
  819. */
  820. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsmartcard)
  821. {
  822. /* Disable RTOIE, EOBIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  823. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  824. /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
  825. if(hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  826. {
  827. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  828. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  829. }
  830. /* Disable the SMARTCARD DMA Rx request if enabled */
  831. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  832. {
  833. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  834. /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  835. if(hsmartcard->hdmarx != NULL)
  836. {
  837. /* Set the SMARTCARD DMA Abort callback to Null.
  838. No call back execution at end of DMA abort procedure */
  839. hsmartcard->hdmarx->XferAbortCallback = NULL;
  840. HAL_DMA_Abort(hsmartcard->hdmarx);
  841. }
  842. }
  843. /* Reset Rx transfer counter */
  844. hsmartcard->RxXferCount = 0U;
  845. /* Clear the Error flags in the ICR register */
  846. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  847. /* Restore hsmartcard->RxState to Ready */
  848. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  849. return HAL_OK;
  850. }
  851. /**
  852. * @brief Abort ongoing transfers (Interrupt mode).
  853. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  854. * the configuration information for the specified SMARTCARD module.
  855. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  856. * This procedure performs following operations :
  857. * - Disable SMARTCARD Interrupts (Tx and Rx)
  858. * - Disable the DMA transfer in the peripheral register (if enabled)
  859. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  860. * - Set handle State to READY
  861. * - At abort completion, call user abort complete callback
  862. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  863. * considered as completed only when user abort complete callback is executed (not when exiting function).
  864. * @retval HAL status
  865. */
  866. HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  867. {
  868. uint32_t abortcplt = 1U;
  869. /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  870. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  871. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  872. /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
  873. before any call to DMA Abort functions */
  874. /* DMA Tx Handle is valid */
  875. if(hsmartcard->hdmatx != NULL)
  876. {
  877. /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
  878. Otherwise, set it to NULL */
  879. if(HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  880. {
  881. hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
  882. }
  883. else
  884. {
  885. hsmartcard->hdmatx->XferAbortCallback = NULL;
  886. }
  887. }
  888. /* DMA Rx Handle is valid */
  889. if(hsmartcard->hdmarx != NULL)
  890. {
  891. /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
  892. Otherwise, set it to NULL */
  893. if(HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  894. {
  895. hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
  896. }
  897. else
  898. {
  899. hsmartcard->hdmarx->XferAbortCallback = NULL;
  900. }
  901. }
  902. /* Disable the SMARTCARD DMA Tx request if enabled */
  903. if(HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  904. {
  905. /* Disable DMA Tx at UART level */
  906. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  907. /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
  908. if(hsmartcard->hdmatx != NULL)
  909. {
  910. /* SMARTCARD Tx DMA Abort callback has already been initialised :
  911. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  912. /* Abort DMA TX */
  913. if(HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  914. {
  915. hsmartcard->hdmatx->XferAbortCallback = NULL;
  916. }
  917. else
  918. {
  919. abortcplt = 0U;
  920. }
  921. }
  922. }
  923. /* Disable the SMARTCARD DMA Rx request if enabled */
  924. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  925. {
  926. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  927. /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
  928. if(hsmartcard->hdmarx != NULL)
  929. {
  930. /* SMARTCARD Rx DMA Abort callback has already been initialised :
  931. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  932. /* Abort DMA RX */
  933. if(HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  934. {
  935. hsmartcard->hdmarx->XferAbortCallback = NULL;
  936. abortcplt = 1U;
  937. }
  938. else
  939. {
  940. abortcplt = 0U;
  941. }
  942. }
  943. }
  944. /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  945. if (abortcplt == 1U)
  946. {
  947. /* Reset Tx and Rx transfer counters */
  948. hsmartcard->TxXferCount = 0U;
  949. hsmartcard->RxXferCount = 0U;
  950. /* Reset errorCode */
  951. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  952. /* Clear the Error flags in the ICR register */
  953. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  954. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  955. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  956. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  957. /* As no DMA to be aborted, call directly user Abort complete callback */
  958. HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  959. }
  960. return HAL_OK;
  961. }
  962. /**
  963. * @brief Abort ongoing Transmit transfer (Interrupt mode).
  964. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  965. * the configuration information for the specified SMARTCARD module.
  966. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  967. * This procedure performs following operations :
  968. * - Disable SMARTCARD Interrupts (Tx)
  969. * - Disable the DMA transfer in the peripheral register (if enabled)
  970. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  971. * - Set handle State to READY
  972. * - At abort completion, call user abort complete callback
  973. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  974. * considered as completed only when user abort complete callback is executed (not when exiting function).
  975. * @retval HAL status
  976. */
  977. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  978. {
  979. /* Disable TXEIE and TCIE interrupts */
  980. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  981. /* Check if a receive process is ongoing or not. If not disable ERR IT */
  982. if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  983. {
  984. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  985. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  986. }
  987. /* Disable the SMARTCARD DMA Tx request if enabled */
  988. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  989. {
  990. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  991. /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
  992. if(hsmartcard->hdmatx != NULL)
  993. {
  994. /* Set the SMARTCARD DMA Abort callback :
  995. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  996. hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
  997. /* Abort DMA TX */
  998. if(HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  999. {
  1000. /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
  1001. hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
  1002. }
  1003. }
  1004. else
  1005. {
  1006. /* Reset Tx transfer counter */
  1007. hsmartcard->TxXferCount = 0U;
  1008. /* Restore hsmartcard->gState to Ready */
  1009. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1010. /* As no DMA to be aborted, call directly user Abort complete callback */
  1011. HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  1012. }
  1013. }
  1014. else
  1015. {
  1016. /* Reset Tx transfer counter */
  1017. hsmartcard->TxXferCount = 0U;
  1018. /* Clear the Error flags in the ICR register */
  1019. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  1020. /* Restore hsmartcard->gState to Ready */
  1021. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1022. /* As no DMA to be aborted, call directly user Abort complete callback */
  1023. HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  1024. }
  1025. return HAL_OK;
  1026. }
  1027. /**
  1028. * @brief Abort ongoing Receive transfer (Interrupt mode).
  1029. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1030. * the configuration information for the specified SMARTCARD module.
  1031. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1032. * This procedure performs following operations :
  1033. * - Disable SMARTCARD Interrupts (Rx)
  1034. * - Disable the DMA transfer in the peripheral register (if enabled)
  1035. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1036. * - Set handle State to READY
  1037. * - At abort completion, call user abort complete callback
  1038. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1039. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1040. * @retval HAL status
  1041. */
  1042. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1043. {
  1044. /* Disable RTOIE, EOBIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1045. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  1046. /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
  1047. if(hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  1048. {
  1049. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1050. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1051. }
  1052. /* Disable the SMARTCARD DMA Rx request if enabled */
  1053. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1054. {
  1055. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1056. /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
  1057. if(hsmartcard->hdmarx != NULL)
  1058. {
  1059. /* Set the SMARTCARD DMA Abort callback :
  1060. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1061. hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
  1062. /* Abort DMA RX */
  1063. if(HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  1064. {
  1065. /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
  1066. hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
  1067. }
  1068. }
  1069. else
  1070. {
  1071. /* Reset Rx transfer counter */
  1072. hsmartcard->RxXferCount = 0U;
  1073. /* Clear the Error flags in the ICR register */
  1074. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1075. /* Restore hsmartcard->RxState to Ready */
  1076. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1077. /* As no DMA to be aborted, call directly user Abort complete callback */
  1078. HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  1079. }
  1080. }
  1081. else
  1082. {
  1083. /* Reset Rx transfer counter */
  1084. hsmartcard->RxXferCount = 0U;
  1085. /* Clear the Error flags in the ICR register */
  1086. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1087. /* Restore hsmartcard->RxState to Ready */
  1088. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1089. /* As no DMA to be aborted, call directly user Abort complete callback */
  1090. HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  1091. }
  1092. return HAL_OK;
  1093. }
  1094. /**
  1095. * @brief Handle SMARTCARD interrupt requests.
  1096. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1097. * the configuration information for the specified SMARTCARD module.
  1098. * @retval None
  1099. */
  1100. void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsmartcard)
  1101. {
  1102. uint32_t isrflags = READ_REG(hsmartcard->Instance->ISR);
  1103. uint32_t cr1its = READ_REG(hsmartcard->Instance->CR1);
  1104. uint32_t cr3its;
  1105. uint32_t errorflags;
  1106. /* If no error occurs */
  1107. errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
  1108. if (errorflags == RESET)
  1109. {
  1110. /* SMARTCARD in mode Receiver ---------------------------------------------------*/
  1111. if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1112. {
  1113. SMARTCARD_Receive_IT(hsmartcard);
  1114. /* Clear RXNE interrupt flag done by reading RDR in SMARTCARD_Receive_IT() */
  1115. return;
  1116. }
  1117. }
  1118. /* If some errors occur */
  1119. cr3its = READ_REG(hsmartcard->Instance->CR3);
  1120. if( (errorflags != RESET)
  1121. && ( ((cr3its & USART_CR3_EIE) != RESET)
  1122. || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_RTOIE)) != RESET)) )
  1123. {
  1124. /* SMARTCARD parity error interrupt occurred -------------------------------------*/
  1125. if(((isrflags & USART_ISR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
  1126. {
  1127. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_PEF);
  1128. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
  1129. }
  1130. /* SMARTCARD frame error interrupt occurred --------------------------------------*/
  1131. if(((isrflags & USART_ISR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1132. {
  1133. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_FEF);
  1134. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
  1135. }
  1136. /* SMARTCARD noise error interrupt occurred --------------------------------------*/
  1137. if(((isrflags & USART_ISR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1138. {
  1139. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_NEF);
  1140. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
  1141. }
  1142. /* SMARTCARD Over-Run interrupt occurred -----------------------------------------*/
  1143. if(((isrflags & USART_ISR_ORE) != RESET) &&
  1144. (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET)))
  1145. {
  1146. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_OREF);
  1147. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
  1148. }
  1149. /* SMARTCARD receiver timeout interrupt occurred -----------------------------------------*/
  1150. if(((isrflags & USART_ISR_RTOF) != RESET) && ((cr1its & USART_CR1_RTOIE) != RESET))
  1151. {
  1152. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_RTOF);
  1153. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_RTO;
  1154. }
  1155. /* Call SMARTCARD Error Call back function if need be --------------------------*/
  1156. if(hsmartcard->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
  1157. {
  1158. /* SMARTCARD in mode Receiver ---------------------------------------------------*/
  1159. if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1160. {
  1161. SMARTCARD_Receive_IT(hsmartcard);
  1162. }
  1163. /* If Error is to be considered as blocking :
  1164. - Receiver Timeout error in Reception
  1165. - Overrun error in Reception
  1166. - any error occurs in DMA mode reception
  1167. */
  1168. if ( ((hsmartcard->ErrorCode & (HAL_SMARTCARD_ERROR_RTO | HAL_SMARTCARD_ERROR_ORE)) != RESET)
  1169. || (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)))
  1170. {
  1171. /* Blocking error : transfer is aborted
  1172. Set the SMARTCARD state ready to be able to start again the process,
  1173. Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1174. SMARTCARD_EndRxTransfer(hsmartcard);
  1175. /* Disable the SMARTCARD DMA Rx request if enabled */
  1176. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1177. {
  1178. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1179. /* Abort the SMARTCARD DMA Rx channel */
  1180. if(hsmartcard->hdmarx != NULL)
  1181. {
  1182. /* Set the SMARTCARD DMA Abort callback :
  1183. will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
  1184. hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
  1185. /* Abort DMA RX */
  1186. if(HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  1187. {
  1188. /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
  1189. hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
  1190. }
  1191. }
  1192. else
  1193. {
  1194. /* Call user error callback */
  1195. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1196. }
  1197. }
  1198. else
  1199. {
  1200. /* Call user error callback */
  1201. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1202. }
  1203. }
  1204. /* other error type to be considered as blocking :
  1205. - Frame error in Transmission
  1206. */
  1207. else if ((hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX) && ((hsmartcard->ErrorCode & HAL_SMARTCARD_ERROR_FE) != RESET))
  1208. {
  1209. /* Blocking error : transfer is aborted
  1210. Set the SMARTCARD state ready to be able to start again the process,
  1211. Disable Tx Interrupts, and disable Tx DMA request, if ongoing */
  1212. SMARTCARD_EndTxTransfer(hsmartcard);
  1213. /* Disable the SMARTCARD DMA Tx request if enabled */
  1214. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1215. {
  1216. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1217. /* Abort the SMARTCARD DMA Tx channel */
  1218. if(hsmartcard->hdmatx != NULL)
  1219. {
  1220. /* Set the SMARTCARD DMA Abort callback :
  1221. will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
  1222. hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
  1223. /* Abort DMA TX */
  1224. if(HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  1225. {
  1226. /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
  1227. hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
  1228. }
  1229. }
  1230. else
  1231. {
  1232. /* Call user error callback */
  1233. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1234. }
  1235. }
  1236. else
  1237. {
  1238. /* Call user error callback */
  1239. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1240. }
  1241. }
  1242. else
  1243. {
  1244. /* Non Blocking error : transfer could go on.
  1245. Error is notified to user through user error callback */
  1246. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1247. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1248. }
  1249. }
  1250. return;
  1251. } /* End if some error occurs */
  1252. /* SMARTCARD in mode Receiver, end of block interruption ------------------------*/
  1253. if(((isrflags & USART_ISR_EOBF) != RESET) && ((cr1its & USART_CR1_EOBIE) != RESET))
  1254. {
  1255. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1256. __HAL_UNLOCK(hsmartcard);
  1257. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  1258. /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
  1259. * to be available during HAL_SMARTCARD_RxCpltCallback() processing */
  1260. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_EOBF);
  1261. return;
  1262. }
  1263. /* SMARTCARD in mode Transmitter ------------------------------------------------*/
  1264. if(((isrflags & USART_ISR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
  1265. {
  1266. SMARTCARD_Transmit_IT(hsmartcard);
  1267. return;
  1268. }
  1269. /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
  1270. if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_TC) != RESET) &&(__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_TC) != RESET))
  1271. {
  1272. SMARTCARD_EndTransmit_IT(hsmartcard);
  1273. return;
  1274. }
  1275. }
  1276. /**
  1277. * @brief Tx Transfer completed callback.
  1278. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1279. * the configuration information for the specified SMARTCARD module.
  1280. * @retval None
  1281. */
  1282. __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1283. {
  1284. /* Prevent unused argument(s) compilation warning */
  1285. UNUSED(hsmartcard);
  1286. /* NOTE : This function should not be modified, when the callback is needed,
  1287. the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
  1288. */
  1289. }
  1290. /**
  1291. * @brief Rx Transfer completed callback.
  1292. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1293. * the configuration information for the specified SMARTCARD module.
  1294. * @retval None
  1295. */
  1296. __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1297. {
  1298. /* Prevent unused argument(s) compilation warning */
  1299. UNUSED(hsmartcard);
  1300. /* NOTE : This function should not be modified, when the callback is needed,
  1301. the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
  1302. */
  1303. }
  1304. /**
  1305. * @brief SMARTCARD error callback.
  1306. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1307. * the configuration information for the specified SMARTCARD module.
  1308. * @retval None
  1309. */
  1310. __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1311. {
  1312. /* Prevent unused argument(s) compilation warning */
  1313. UNUSED(hsmartcard);
  1314. /* NOTE : This function should not be modified, when the callback is needed,
  1315. the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
  1316. */
  1317. }
  1318. /**
  1319. * @brief SMARTCARD Abort Complete callback.
  1320. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1321. * the configuration information for the specified SMARTCARD module.
  1322. * @retval None
  1323. */
  1324. __weak void HAL_SMARTCARD_AbortCpltCallback (SMARTCARD_HandleTypeDef *hsmartcard)
  1325. {
  1326. /* Prevent unused argument(s) compilation warning */
  1327. UNUSED(hsmartcard);
  1328. /* NOTE : This function should not be modified, when the callback is needed,
  1329. the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
  1330. */
  1331. }
  1332. /**
  1333. * @brief SMARTCARD Abort Complete callback.
  1334. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1335. * the configuration information for the specified SMARTCARD module.
  1336. * @retval None
  1337. */
  1338. __weak void HAL_SMARTCARD_AbortTransmitCpltCallback (SMARTCARD_HandleTypeDef *hsmartcard)
  1339. {
  1340. /* Prevent unused argument(s) compilation warning */
  1341. UNUSED(hsmartcard);
  1342. /* NOTE : This function should not be modified, when the callback is needed,
  1343. the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
  1344. */
  1345. }
  1346. /**
  1347. * @brief SMARTCARD Abort Receive Complete callback.
  1348. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1349. * the configuration information for the specified SMARTCARD module.
  1350. * @retval None
  1351. */
  1352. __weak void HAL_SMARTCARD_AbortReceiveCpltCallback (SMARTCARD_HandleTypeDef *hsmartcard)
  1353. {
  1354. /* Prevent unused argument(s) compilation warning */
  1355. UNUSED(hsmartcard);
  1356. /* NOTE : This function should not be modified, when the callback is needed,
  1357. the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
  1358. */
  1359. }
  1360. /**
  1361. * @}
  1362. */
  1363. /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions
  1364. * @brief SMARTCARD State and Errors functions
  1365. *
  1366. @verbatim
  1367. ==============================================================================
  1368. ##### Peripheral State and Errors functions #####
  1369. ==============================================================================
  1370. [..]
  1371. This subsection provides a set of functions allowing to return the State of SmartCard
  1372. handle and also return Peripheral Errors occurred during communication process
  1373. (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
  1374. of the SMARTCARD peripheral.
  1375. (+) HAL_SMARTCARD_GetError() checks in run-time errors that could occur during
  1376. communication.
  1377. @endverbatim
  1378. * @{
  1379. */
  1380. /**
  1381. * @brief Return the SMARTCARD handle state.
  1382. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1383. * the configuration information for the specified SMARTCARD module.
  1384. * @retval SMARTCARD handle state
  1385. */
  1386. HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsmartcard)
  1387. {
  1388. /* Return SMARTCARD handle state */
  1389. uint32_t temp1= 0x00U, temp2 = 0x00U;
  1390. temp1 = hsmartcard->gState;
  1391. temp2 = hsmartcard->RxState;
  1392. return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
  1393. }
  1394. /**
  1395. * @brief Return the SMARTCARD handle error code.
  1396. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1397. * the configuration information for the specified SMARTCARD module.
  1398. * @retval SMARTCARD handle Error Code
  1399. */
  1400. uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsmartcard)
  1401. {
  1402. return hsmartcard->ErrorCode;
  1403. }
  1404. /**
  1405. * @}
  1406. */
  1407. /**
  1408. * @}
  1409. */
  1410. /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
  1411. * @{
  1412. */
  1413. /**
  1414. * @brief Configure the SMARTCARD associated USART peripheral.
  1415. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1416. * the configuration information for the specified SMARTCARD module.
  1417. * @retval HAL status
  1418. */
  1419. static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard)
  1420. {
  1421. uint32_t tmpreg = 0x00000000U;
  1422. SMARTCARD_ClockSourceTypeDef clocksource = SMARTCARD_CLOCKSOURCE_UNDEFINED;
  1423. HAL_StatusTypeDef ret = HAL_OK;
  1424. /* Check the parameters */
  1425. assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
  1426. assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard->Init.BaudRate));
  1427. assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard->Init.WordLength));
  1428. assert_param(IS_SMARTCARD_STOPBITS(hsmartcard->Init.StopBits));
  1429. assert_param(IS_SMARTCARD_PARITY(hsmartcard->Init.Parity));
  1430. assert_param(IS_SMARTCARD_MODE(hsmartcard->Init.Mode));
  1431. assert_param(IS_SMARTCARD_POLARITY(hsmartcard->Init.CLKPolarity));
  1432. assert_param(IS_SMARTCARD_PHASE(hsmartcard->Init.CLKPhase));
  1433. assert_param(IS_SMARTCARD_LASTBIT(hsmartcard->Init.CLKLastBit));
  1434. assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsmartcard->Init.OneBitSampling));
  1435. assert_param(IS_SMARTCARD_NACK(hsmartcard->Init.NACKEnable));
  1436. assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard->Init.TimeOutEnable));
  1437. assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard->Init.AutoRetryCount));
  1438. /*-------------------------- USART CR1 Configuration -----------------------*/
  1439. /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
  1440. * Oversampling is forced to 16 (OVER8 = 0).
  1441. * Configure the Parity and Mode:
  1442. * set PS bit according to hsmartcard->Init.Parity value
  1443. * set TE and RE bits according to hsmartcard->Init.Mode value */
  1444. tmpreg = (uint32_t) hsmartcard->Init.Parity | hsmartcard->Init.Mode;
  1445. tmpreg |= (uint32_t) hsmartcard->Init.WordLength;
  1446. MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_FIELDS, tmpreg);
  1447. /*-------------------------- USART CR2 Configuration -----------------------*/
  1448. tmpreg = hsmartcard->Init.StopBits;
  1449. /* Synchronous mode is activated by default */
  1450. tmpreg |= (uint32_t) USART_CR2_CLKEN | hsmartcard->Init.CLKPolarity;
  1451. tmpreg |= (uint32_t) hsmartcard->Init.CLKPhase | hsmartcard->Init.CLKLastBit;
  1452. tmpreg |= (uint32_t) hsmartcard->Init.TimeOutEnable;
  1453. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_FIELDS, tmpreg);
  1454. /*-------------------------- USART CR3 Configuration -----------------------*/
  1455. /* Configure
  1456. * - one-bit sampling method versus three samples' majority rule
  1457. * according to hsmartcard->Init.OneBitSampling
  1458. * - NACK transmission in case of parity error according
  1459. * to hsmartcard->Init.NACKEnable
  1460. * - autoretry counter according to hsmartcard->Init.AutoRetryCount */
  1461. tmpreg = (uint32_t) hsmartcard->Init.OneBitSampling | hsmartcard->Init.NACKEnable;
  1462. tmpreg |= ((uint32_t)hsmartcard->Init.AutoRetryCount << SMARTCARD_CR3_SCARCNT_LSB_POS);
  1463. MODIFY_REG(hsmartcard->Instance-> CR3,USART_CR3_FIELDS, tmpreg);
  1464. /*-------------------------- USART GTPR Configuration ----------------------*/
  1465. tmpreg = (hsmartcard->Init.Prescaler | ((uint32_t)hsmartcard->Init.GuardTime << SMARTCARD_GTPR_GT_LSB_POS));
  1466. MODIFY_REG(hsmartcard->Instance->GTPR, (USART_GTPR_GT|USART_GTPR_PSC), tmpreg);
  1467. /*-------------------------- USART RTOR Configuration ----------------------*/
  1468. tmpreg = ((uint32_t)hsmartcard->Init.BlockLength << SMARTCARD_RTOR_BLEN_LSB_POS);
  1469. if (hsmartcard->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE)
  1470. {
  1471. assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue));
  1472. tmpreg |= (uint32_t) hsmartcard->Init.TimeOutValue;
  1473. }
  1474. MODIFY_REG(hsmartcard->Instance->RTOR, (USART_RTOR_RTO|USART_RTOR_BLEN), tmpreg);
  1475. /*-------------------------- USART BRR Configuration -----------------------*/
  1476. SMARTCARD_GETCLOCKSOURCE(hsmartcard, clocksource);
  1477. switch (clocksource)
  1478. {
  1479. case SMARTCARD_CLOCKSOURCE_PCLK1:
  1480. hsmartcard->Instance->BRR = (uint16_t)((HAL_RCC_GetPCLK1Freq() + (hsmartcard->Init.BaudRate/2U)) / hsmartcard->Init.BaudRate);
  1481. break;
  1482. case SMARTCARD_CLOCKSOURCE_HSI:
  1483. hsmartcard->Instance->BRR = (uint16_t)((HSI_VALUE + (hsmartcard->Init.BaudRate/2U)) / hsmartcard->Init.BaudRate);
  1484. break;
  1485. case SMARTCARD_CLOCKSOURCE_SYSCLK:
  1486. hsmartcard->Instance->BRR = (uint16_t)((HAL_RCC_GetSysClockFreq() + (hsmartcard->Init.BaudRate/2U)) / hsmartcard->Init.BaudRate);
  1487. break;
  1488. case SMARTCARD_CLOCKSOURCE_LSE:
  1489. hsmartcard->Instance->BRR = (uint16_t)((LSE_VALUE + (hsmartcard->Init.BaudRate/2U)) / hsmartcard->Init.BaudRate);
  1490. break;
  1491. case SMARTCARD_CLOCKSOURCE_UNDEFINED:
  1492. default:
  1493. ret = HAL_ERROR;
  1494. break;
  1495. }
  1496. return ret;
  1497. }
  1498. /**
  1499. * @brief Configure the SMARTCARD associated USART peripheral advanced features.
  1500. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1501. * the configuration information for the specified SMARTCARD module.
  1502. * @retval None
  1503. */
  1504. static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard)
  1505. {
  1506. /* Check whether the set of advanced features to configure is properly set */
  1507. assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard->AdvancedInit.AdvFeatureInit));
  1508. /* if required, configure TX pin active level inversion */
  1509. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT))
  1510. {
  1511. assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard->AdvancedInit.TxPinLevelInvert));
  1512. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_TXINV, hsmartcard->AdvancedInit.TxPinLevelInvert);
  1513. }
  1514. /* if required, configure RX pin active level inversion */
  1515. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT))
  1516. {
  1517. assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard->AdvancedInit.RxPinLevelInvert));
  1518. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_RXINV, hsmartcard->AdvancedInit.RxPinLevelInvert);
  1519. }
  1520. /* if required, configure data inversion */
  1521. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT))
  1522. {
  1523. assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard->AdvancedInit.DataInvert));
  1524. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_DATAINV, hsmartcard->AdvancedInit.DataInvert);
  1525. }
  1526. /* if required, configure RX/TX pins swap */
  1527. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT))
  1528. {
  1529. assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard->AdvancedInit.Swap));
  1530. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_SWAP, hsmartcard->AdvancedInit.Swap);
  1531. }
  1532. /* if required, configure RX overrun detection disabling */
  1533. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT))
  1534. {
  1535. assert_param(IS_SMARTCARD_OVERRUN(hsmartcard->AdvancedInit.OverrunDisable));
  1536. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_OVRDIS, hsmartcard->AdvancedInit.OverrunDisable);
  1537. }
  1538. /* if required, configure DMA disabling on reception error */
  1539. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT))
  1540. {
  1541. assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard->AdvancedInit.DMADisableonRxError));
  1542. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_DDRE, hsmartcard->AdvancedInit.DMADisableonRxError);
  1543. }
  1544. /* if required, configure MSB first on communication line */
  1545. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT))
  1546. {
  1547. assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard->AdvancedInit.MSBFirst));
  1548. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_MSBFIRST, hsmartcard->AdvancedInit.MSBFirst);
  1549. }
  1550. }
  1551. /**
  1552. * @brief Check the SMARTCARD Idle State.
  1553. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1554. * the configuration information for the specified SMARTCARD module.
  1555. * @retval HAL status
  1556. */
  1557. static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard)
  1558. {
  1559. uint32_t tickstart = 0U;
  1560. /* Initialize the SMARTCARD ErrorCode */
  1561. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1562. /* Init tickstart for timeout managment*/
  1563. tickstart = HAL_GetTick();
  1564. /* TEACK and REACK bits in ISR are checked only when available (not available on all F0 devices).
  1565. Bits are defined for some specific devices, and are available only for UART instances supporting WakeUp from Stop Mode feature.
  1566. */
  1567. #if !defined(STM32F030x6) && !defined(STM32F030x8)&& !defined(STM32F070xB)&& !defined(STM32F070x6)&& !defined(STM32F030xC)
  1568. if (IS_UART_WAKEUP_FROMSTOP_INSTANCE(hsmartcard->Instance))
  1569. {
  1570. /* Check if the Transmitter is enabled */
  1571. if((hsmartcard->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  1572. {
  1573. /* Wait until TEACK flag is set */
  1574. if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_TEACK, RESET, tickstart, SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
  1575. {
  1576. /* Timeout occurred */
  1577. return HAL_TIMEOUT;
  1578. }
  1579. }
  1580. /* Check if the Receiver is enabled */
  1581. if((hsmartcard->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  1582. {
  1583. /* Wait until REACK flag is set */
  1584. if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_REACK, RESET, tickstart, SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
  1585. {
  1586. /* Timeout occurred */
  1587. return HAL_TIMEOUT;
  1588. }
  1589. }
  1590. }
  1591. #endif /* !defined(STM32F030x6) && !defined(STM32F030x8)&& !defined(STM32F070xB)&& !defined(STM32F070x6)&& !defined(STM32F030xC) */
  1592. /* Initialize the SMARTCARD states */
  1593. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1594. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1595. /* Process Unlocked */
  1596. __HAL_UNLOCK(hsmartcard);
  1597. return HAL_OK;
  1598. }
  1599. /**
  1600. * @brief Handle SMARTCARD Communication Timeout.
  1601. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1602. * the configuration information for the specified SMARTCARD module.
  1603. * @param Flag Specifies the SMARTCARD flag to check.
  1604. * @param Status The new Flag status (SET or RESET).
  1605. * @param Tickstart Tick start value
  1606. * @param Timeout Timeout duration.
  1607. * @retval HAL status
  1608. */
  1609. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  1610. {
  1611. /* Wait until flag is set */
  1612. while((__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) ? SET : RESET) == Status)
  1613. {
  1614. /* Check for the Timeout */
  1615. if(Timeout != HAL_MAX_DELAY)
  1616. {
  1617. if((Timeout == 0U) || ((HAL_GetTick()-Tickstart) > Timeout))
  1618. {
  1619. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1620. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
  1621. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1622. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1623. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1624. /* Process Unlocked */
  1625. __HAL_UNLOCK(hsmartcard);
  1626. return HAL_TIMEOUT;
  1627. }
  1628. }
  1629. }
  1630. return HAL_OK;
  1631. }
  1632. /**
  1633. * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
  1634. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1635. * the configuration information for the specified SMARTCARD module.
  1636. * @retval None
  1637. */
  1638. static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
  1639. {
  1640. /* Disable TXEIE, TCIE and ERR (Frame error, noise error, overrun error) interrupts */
  1641. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1642. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1643. /* At end of Tx process, restore hsmartcard->gState to Ready */
  1644. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1645. }
  1646. /**
  1647. * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
  1648. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1649. * the configuration information for the specified SMARTCARD module.
  1650. * @retval None
  1651. */
  1652. static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
  1653. {
  1654. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1655. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1656. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1657. /* At end of Rx process, restore hsmartcard->RxState to Ready */
  1658. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1659. }
  1660. /**
  1661. * @brief DMA SMARTCARD transmit process complete callback.
  1662. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1663. * the configuration information for the specified DMA module.
  1664. * @retval None
  1665. */
  1666. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1667. {
  1668. SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef*)(hdma->Parent);
  1669. hsmartcard->TxXferCount = 0U;
  1670. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  1671. in the SMARTCARD associated USART CR3 register */
  1672. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1673. /* Enable the SMARTCARD Transmit Complete Interrupt */
  1674. __HAL_SMARTCARD_ENABLE_IT(hsmartcard, SMARTCARD_IT_TC);
  1675. }
  1676. /**
  1677. * @brief DMA SMARTCARD receive process complete callback.
  1678. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1679. * the configuration information for the specified DMA module.
  1680. * @retval None
  1681. */
  1682. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1683. {
  1684. SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef*)(hdma->Parent);
  1685. hsmartcard->RxXferCount = 0U;
  1686. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1687. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  1688. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1689. /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
  1690. in the SMARTCARD associated USART CR3 register */
  1691. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1692. /* At end of Rx process, restore hsmartcard->RxState to Ready */
  1693. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1694. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  1695. }
  1696. /**
  1697. * @brief DMA SMARTCARD communication error callback.
  1698. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1699. * the configuration information for the specified DMA module.
  1700. * @retval None
  1701. */
  1702. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
  1703. {
  1704. SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef*)(hdma->Parent);
  1705. /* Stop SMARTCARD DMA Tx request if ongoing */
  1706. if ( (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  1707. &&(HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT)) )
  1708. {
  1709. hsmartcard->TxXferCount = 0U;
  1710. SMARTCARD_EndTxTransfer(hsmartcard);
  1711. }
  1712. /* Stop SMARTCARD DMA Rx request if ongoing */
  1713. if ( (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  1714. &&(HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) )
  1715. {
  1716. hsmartcard->RxXferCount = 0U;
  1717. SMARTCARD_EndRxTransfer(hsmartcard);
  1718. }
  1719. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
  1720. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1721. }
  1722. /**
  1723. * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
  1724. * (To be called at end of DMA Abort procedure following error occurrence).
  1725. * @param hdma DMA handle.
  1726. * @retval None
  1727. */
  1728. static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  1729. {
  1730. SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef*)(hdma->Parent);
  1731. hsmartcard->RxXferCount = 0U;
  1732. hsmartcard->TxXferCount = 0U;
  1733. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1734. }
  1735. /**
  1736. * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user
  1737. * (To be called at end of DMA Tx Abort procedure following user abort request).
  1738. * @note When this callback is executed, User Abort complete call back is called only if no
  1739. * Abort still ongoing for Rx DMA Handle.
  1740. * @param hdma DMA handle.
  1741. * @retval None
  1742. */
  1743. static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  1744. {
  1745. SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef* )(hdma->Parent);
  1746. hsmartcard->hdmatx->XferAbortCallback = NULL;
  1747. /* Check if an Abort process is still ongoing */
  1748. if(hsmartcard->hdmarx != NULL)
  1749. {
  1750. if(hsmartcard->hdmarx->XferAbortCallback != NULL)
  1751. {
  1752. return;
  1753. }
  1754. }
  1755. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  1756. hsmartcard->TxXferCount = 0U;
  1757. hsmartcard->RxXferCount = 0U;
  1758. /* Reset errorCode */
  1759. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1760. /* Clear the Error flags in the ICR register */
  1761. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1762. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  1763. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1764. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1765. /* Call user Abort complete callback */
  1766. HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  1767. }
  1768. /**
  1769. * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user
  1770. * (To be called at end of DMA Rx Abort procedure following user abort request).
  1771. * @note When this callback is executed, User Abort complete call back is called only if no
  1772. * Abort still ongoing for Tx DMA Handle.
  1773. * @param hdma DMA handle.
  1774. * @retval None
  1775. */
  1776. static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  1777. {
  1778. SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef* )(hdma->Parent);
  1779. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1780. /* Check if an Abort process is still ongoing */
  1781. if(hsmartcard->hdmatx != NULL)
  1782. {
  1783. if(hsmartcard->hdmatx->XferAbortCallback != NULL)
  1784. {
  1785. return;
  1786. }
  1787. }
  1788. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  1789. hsmartcard->TxXferCount = 0U;
  1790. hsmartcard->RxXferCount = 0U;
  1791. /* Reset errorCode */
  1792. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1793. /* Clear the Error flags in the ICR register */
  1794. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1795. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  1796. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1797. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1798. /* Call user Abort complete callback */
  1799. HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  1800. }
  1801. /**
  1802. * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
  1803. * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
  1804. * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
  1805. * and leads to user Tx Abort Complete callback execution).
  1806. * @param hdma DMA handle.
  1807. * @retval None
  1808. */
  1809. static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  1810. {
  1811. SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef*)(hdma->Parent);
  1812. hsmartcard->TxXferCount = 0U;
  1813. /* Clear the Error flags in the ICR register */
  1814. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  1815. /* Restore hsmartcard->gState to Ready */
  1816. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1817. /* Call user Abort complete callback */
  1818. HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  1819. }
  1820. /**
  1821. * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
  1822. * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
  1823. * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
  1824. * and leads to user Rx Abort Complete callback execution).
  1825. * @param hdma DMA handle.
  1826. * @retval None
  1827. */
  1828. static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  1829. {
  1830. SMARTCARD_HandleTypeDef* hsmartcard = ( SMARTCARD_HandleTypeDef* )(hdma->Parent);
  1831. hsmartcard->RxXferCount = 0U;
  1832. /* Clear the Error flags in the ICR register */
  1833. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1834. /* Restore hsmartcard->RxState to Ready */
  1835. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1836. /* Call user Abort complete callback */
  1837. HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  1838. }
  1839. /**
  1840. * @brief Send an amount of data in non-blocking mode.
  1841. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1842. * the configuration information for the specified SMARTCARD module.
  1843. * Function called under interruption only, once
  1844. * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
  1845. * @retval HAL status
  1846. */
  1847. static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1848. {
  1849. /* Check that a Tx process is ongoing */
  1850. if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  1851. {
  1852. if(hsmartcard->TxXferCount == 0U)
  1853. {
  1854. /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
  1855. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE);
  1856. /* Enable the SMARTCARD Transmit Complete Interrupt */
  1857. __HAL_SMARTCARD_ENABLE_IT(hsmartcard, SMARTCARD_IT_TC);
  1858. return HAL_OK;
  1859. }
  1860. else
  1861. {
  1862. hsmartcard->Instance->TDR = (*hsmartcard->pTxBuffPtr++ & (uint8_t)0xFFU);
  1863. hsmartcard->TxXferCount--;
  1864. return HAL_OK;
  1865. }
  1866. }
  1867. else
  1868. {
  1869. return HAL_BUSY;
  1870. }
  1871. }
  1872. /**
  1873. * @brief Wrap up transmission in non-blocking mode.
  1874. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1875. * the configuration information for the specified SMARTCARD module.
  1876. * @retval HAL status
  1877. */
  1878. static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1879. {
  1880. /* Disable the SMARTCARD Transmit Complete Interrupt */
  1881. __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_TC);
  1882. /* Check if a receive process is ongoing or not. If not disable ERR IT */
  1883. if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  1884. {
  1885. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1886. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1887. }
  1888. /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */
  1889. if(hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
  1890. {
  1891. /* Disable the Peripheral first to update modes */
  1892. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  1893. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  1894. /* Enable the Peripheral */
  1895. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  1896. }
  1897. /* Tx process is ended, restore hsmartcard->gState to Ready */
  1898. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1899. HAL_SMARTCARD_TxCpltCallback(hsmartcard);
  1900. return HAL_OK;
  1901. }
  1902. /**
  1903. * @brief Receive an amount of data in non-blocking mode.
  1904. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1905. * the configuration information for the specified SMARTCARD module.
  1906. * Function called under interruption only, once
  1907. * interruptions have been enabled by HAL_SMARTCARD_Receive_IT().
  1908. * @retval HAL status
  1909. */
  1910. static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1911. {
  1912. /* Check that a Rx process is ongoing */
  1913. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  1914. {
  1915. *hsmartcard->pRxBuffPtr++ = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFFU);
  1916. if(--hsmartcard->RxXferCount == 0U)
  1917. {
  1918. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE);
  1919. /* Check if a transmit process is ongoing or not. If not disable ERR IT */
  1920. if(hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  1921. {
  1922. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  1923. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1924. }
  1925. /* Disable the SMARTCARD Parity Error Interrupt */
  1926. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  1927. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1928. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  1929. return HAL_OK;
  1930. }
  1931. return HAL_OK;
  1932. }
  1933. else
  1934. {
  1935. /* Clear RXNE interrupt flag */
  1936. __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
  1937. return HAL_BUSY;
  1938. }
  1939. }
  1940. /**
  1941. * @}
  1942. */
  1943. #endif /* HAL_SMARTCARD_MODULE_ENABLED */
  1944. /**
  1945. * @}
  1946. */
  1947. /**
  1948. * @}
  1949. */
  1950. #endif /* !defined(STM32F030x6) && !defined(STM32F030x8)&& !defined(STM32F070x6) && !defined(STM32F070xB) && !defined(STM32F030xC) */
  1951. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/