Subversion Repositories DashDisplay

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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