Subversion Repositories AFRtranscoder

Rev

Details | Last modification | View Log | RSS feed

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