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