Subversion Repositories dashGPS

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