]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L0/stm32l0xx_hal_smartcard.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L0 / stm32l0xx_hal_smartcard.c
1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_hal_smartcard.c
4   * @author  MCD Application Team
5   * @version V1.2.0
6   * @date    06-February-2015
7   * @brief   SMARTCARD HAL module driver.
8   *
9   *          This file provides firmware functions to manage the following 
10   *          functionalities of the SmartCard.
11   *           + Initialization and de-initialization methods
12   *           + IO operation methods
13   *           + Peripheral Control methods
14   *
15   *           
16   @verbatim       
17  ===============================================================================
18                         ##### How to use this driver #####
19  ===============================================================================
20     [..]
21     The SMARTCARD HAL driver can be used as follow:
22     
23     (#) Declare a SMARTCARD_HandleTypeDef handle structure.
24     (#) Associate a USART to the SMARTCARD handle hsc.
25     (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit ()API:
26         (##) Enable the USARTx interface clock.
27         (##) USART pins configuration:
28             (+) Enable the clock for the USART GPIOs.
29             (+) Configure these USART pins as alternate function pull-up.
30         (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
31              and HAL_SMARTCARD_Receive_IT() APIs):
32             (+) Configure the USARTx interrupt priority.
33             (+) Enable the NVIC USART IRQ handle.
34             (@) The specific USART interrupts (Transmission complete interrupt, 
35                 RXNE interrupt and Error Interrupts) will be managed using the macros
36                 __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
37         (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
38              and HAL_SMARTCARD_Receive_DMA() APIs):
39             (+) Declare a DMA handle structure for the Tx/Rx stream.
40             (+) Enable the DMAx interface clock.
41             (+) Configure the declared DMA handle structure with the required Tx/Rx parameters.                
42             (+) Configure the DMA Tx/Rx Stream.
43             (+) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
44             (+) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream.
45
46     (#) Program the Baud Rate, Parity, Mode(Receiver/Transmitter), clock enabling/disabling and accordingly,
47         the clock parameters (parity, phase, last bit), prescaler value, guard time and NACK on transmission
48         error enabling or disabling in the hsc Init structure.
49         
50     (#) If required, program SMARTCARD advanced features (TX/RX pins swap, TimeOut, auto-retry counter,...)
51         in the hsc AdvancedInit structure.
52
53     (#) Initialize the SMARTCARD associated USART registers by calling
54         the HAL_SMARTCARD_Init() API.                                 
55         
56     (@) HAL_SMARTCARD_Init() API also configure also the low level Hardware GPIO, CLOCK, CORTEX...etc) by 
57         calling the customized HAL_SMARTCARD_MspInit() API.
58
59   @endverbatim
60   ******************************************************************************
61   * @attention
62   *
63   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
64   *
65   * Redistribution and use in source and binary forms, with or without modification,
66   * are permitted provided that the following conditions are met:
67   *   1. Redistributions of source code must retain the above copyright notice,
68   *      this list of conditions and the following disclaimer.
69   *   2. Redistributions in binary form must reproduce the above copyright notice,
70   *      this list of conditions and the following disclaimer in the documentation
71   *      and/or other materials provided with the distribution.
72   *   3. Neither the name of STMicroelectronics nor the names of its contributors
73   *      may be used to endorse or promote products derived from this software
74   *      without specific prior written permission.
75   *
76   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
77   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
78   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
79   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
80   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
81   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
82   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
83   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
84   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
85   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
86   *
87   ******************************************************************************  
88   */ 
89
90 /* Includes ------------------------------------------------------------------*/
91 #include "stm32l0xx_hal.h"
92
93 /** @addtogroup STM32L0xx_HAL_Driver
94   * @{
95   */
96
97 /** @addtogroup SMARTCARD
98   * @brief HAL SMARTCARD module driver
99   * @{
100   */
101 #ifdef HAL_SMARTCARD_MODULE_ENABLED
102     
103 /* Private typedef -----------------------------------------------------------*/
104 /* Private define ------------------------------------------------------------*/
105 #define TEACK_REACK_TIMEOUT               1000
106 #define HAL_SMARTCARD_TXDMA_TIMEOUTVALUE  22000
107 #define USART_CR1_FIELDS      ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
108                                           USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8))
109 #define USART_CR2_CLK_FIELDS  ((uint32_t)(USART_CR2_CLKEN|USART_CR2_CPOL|USART_CR2_CPHA|USART_CR2_LBCL))   
110 #define USART_CR2_FIELDS      ((uint32_t)(USART_CR2_RTOEN|USART_CR2_CLK_FIELDS|USART_CR2_STOP))
111 #define USART_CR3_FIELDS      ((uint32_t)(USART_CR3_ONEBIT|USART_CR3_NACK|USART_CR3_SCARCNT))  
112 /* Private macro -------------------------------------------------------------*/
113 /* Private variables ---------------------------------------------------------*/
114 /* Private function prototypes -----------------------------------------------*/
115 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
116 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
117 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma); 
118 static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc);
119 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
120 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsc);
121 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc);
122 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc);
123 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsc);
124 /* Private functions ---------------------------------------------------------*/
125
126 /** @addtogroup SMARTCARD_Exported_Functions
127   * @{
128   */
129
130 /** @addtogroup SMARTCARD_Exported_Functions_Group1
131   *  @brief    Initialization and Configuration functions 
132   *
133 @verbatim    
134 ===============================================================================
135             ##### Initialization and Configuration functions #####
136  ===============================================================================
137     [..]
138     This subsection provides a set of functions allowing to initialize the USARTx 
139     associated to the SmartCard.
140       (+) These parameters can be configured: 
141         (++) Baud Rate
142         (++) Parity: parity should be enabled,
143              Frame Length is fixed to 8 bits plus parity:
144              the USART frame format is given in the following table:
145    +---------------------------------------------------------------+
146    | M1M0 bits |  PCE bit  |            USART frame                |
147    |-----------------------|---------------------------------------|
148    |     01    |    1      |    | SB | 8 bit data | PB | STB |     |
149    +---------------------------------------------------------------+
150         (++) Receiver/transmitter modes
151         (++) Synchronous mode (and if enabled, phase, polarity and last bit parameters)
152         (++) Prescaler value
153         (++) Guard bit time 
154         (++) NACK enabling or disabling on transmission error               
155
156       (+) The following advanced features can be configured as well:
157         (++) TX and/or RX pin level inversion
158         (++) data logical level inversion
159         (++) RX and TX pins swap
160         (++) RX overrun detection disabling
161         (++) DMA disabling on RX error
162         (++) MSB first on communication line
163         (++) Time out enabling (and if activated, timeout value)
164         (++) Block length
165         (++) Auto-retry counter       
166         
167     [..]                                                  
168     The HAL_SMARTCARD_Init() API follow respectively the USART (a)synchronous configuration procedures 
169     (details for the procedures are available in reference manual).
170
171 @endverbatim
172   * @{
173   */
174
175 /**
176   * @brief Initializes the SMARTCARD mode according to the specified
177   *         parameters in the SMARTCARD_InitTypeDef and creates the associated handle .
178   * @param hsc: SMARTCARD handle
179   * @retval HAL status
180   */
181 HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc)
182 {
183   /* Check the SMARTCARD handle allocation */
184   if(hsc == NULL)
185   {
186     return HAL_ERROR;
187   }
188   
189   /* Check the USART associated to the SmartCard */
190   assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
191   
192   if(hsc->State == HAL_SMARTCARD_STATE_RESET)
193   {  
194     /* Init the low level hardware : GPIO, CLOCK, CORTEX */
195     HAL_SMARTCARD_MspInit(hsc);
196   }
197   
198   hsc->State = HAL_SMARTCARD_STATE_BUSY;
199   
200   /* Disable the Peripheral */
201   __HAL_SMARTCARD_DISABLE(hsc);
202   
203   /* Set the SMARTCARD Communication parameters */
204   SMARTCARD_SetConfig(hsc);
205   
206   if(hsc->AdvancedInit.AdvFeatureInit != SMARTCARD_ADVFEATURE_NO_INIT)
207   {
208     SMARTCARD_AdvFeatureConfig(hsc);
209   }
210   
211   /* In SmartCard mode, the following bits must be kept cleared: 
212   - LINEN in the USART_CR2 register,
213   - HDSEL and IREN  bits in the USART_CR3 register.*/
214   hsc->Instance->CR2 &= ~(USART_CR2_LINEN); 
215   hsc->Instance->CR3 &= ~(USART_CR3_HDSEL | USART_CR3_IREN); 
216   
217   /* set the USART in SMARTCARD mode */ 
218   hsc->Instance->CR3 |= USART_CR3_SCEN; 
219   
220   /* Enable the Peripheral */
221   __HAL_SMARTCARD_ENABLE(hsc);
222   
223   /* TEACK and/or REACK to check before moving hsc->State to Ready */
224   return (SMARTCARD_CheckIdleState(hsc));
225 }
226
227 /**
228   * @brief DeInitializes the SMARTCARD peripheral 
229   * @param hsc: SMARTCARD handle
230   * @retval HAL status
231   */
232 HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc)
233 {
234   /* Check the SMARTCARD handle allocation */
235   if(hsc == NULL)
236   {
237     return HAL_ERROR;
238   }
239   
240   /* Check the parameters */
241   assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
242   
243   hsc->State = HAL_SMARTCARD_STATE_BUSY;
244   
245   /* DeInit the low level hardware */
246   HAL_SMARTCARD_MspDeInit(hsc);
247   
248   hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
249   hsc->State = HAL_SMARTCARD_STATE_RESET;
250   
251   /* Release Lock */
252   __HAL_UNLOCK(hsc);
253   
254   return HAL_OK;
255 }
256
257 /**
258   * @brief SMARTCARD MSP Init
259   * @param hsc: SMARTCARD handle
260   * @retval None
261   */
262  __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc)
263 {
264   /* NOTE : This function Should not be modified, when the callback is needed,
265             the HAL_SMARTCARD_MspInit could be implenetd in the user file
266    */ 
267 }
268
269 /**
270   * @brief SMARTCARD MSP DeInit
271   * @param hsc: SMARTCARD handle
272   * @retval None
273   */
274  __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc)
275 {
276   /* NOTE : This function Should not be modified, when the callback is needed,
277             the HAL_SMARTCARD_MspDeInit could be implenetd in the user file
278    */ 
279 }
280
281 /**
282   * @}
283   */
284
285 /** @addtogroup SMARTCARD_Exported_Functions_Group2
286   *  @brief SMARTCARD Transmit/Receive functions 
287   *
288 @verbatim   
289  ===============================================================================
290                       ##### IO operation functions #####
291  ===============================================================================  
292     This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
293
294     (#) There are two mode of transfer:
295        (+) Blocking mode: The communication is performed in polling mode. 
296             The HAL status of all data processing is returned by the same function 
297             after finishing transfer.  
298        (+) No-Blocking mode: The communication is performed using Interrupts 
299            or DMA, These API's return the HAL status.
300            The end of the data processing will be indicated through the 
301            dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when 
302            using DMA mode.
303            The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks 
304            will be executed respectivelly at the end of the transmit or Receive process
305            The HAL_SMARTCARD_ErrorCallback()user callback will be executed when a communication error is detected
306
307     (#) Blocking mode API's are :
308         (+) HAL_SMARTCARD_Transmit()
309         (+) HAL_SMARTCARD_Receive() 
310         
311     (#) Non-Blocking mode API's with Interrupt are :
312         (+) HAL_SMARTCARD_Transmit_IT()
313         (+) HAL_SMARTCARD_Receive_IT()
314         (+) HAL_SMARTCARD_IRQHandler()
315         (+) SMARTCARD_Transmit_IT()
316         (+) SMARTCARD_Receive_IT()    
317
318     (#) No-Blocking mode functions with DMA are :
319         (+) HAL_SMARTCARD_Transmit_DMA()
320         (+) HAL_SMARTCARD_Receive_DMA()
321
322     (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
323         (+) HAL_SMARTCARD_TxCpltCallback()
324         (+) HAL_SMARTCARD_RxCpltCallback()
325         (+) HAL_SMARTCARD_ErrorCallback()
326       
327 @endverbatim
328   * @{
329   */
330
331 /**
332   * @brief Send an amount of data in blocking mode 
333   * @param hsc: SMARTCARD handle
334   * @param pData: pointer to data buffer
335   * @param Size: amount of data to be sent
336   * @param Timeout : Timeout duration
337   * @retval HAL status
338   */
339 HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
340 {
341   if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_RX))
342   {
343     if((pData == NULL) || (Size == 0)) 
344     {
345       return  HAL_ERROR;
346     }
347     
348     /* Process Locked */
349     __HAL_LOCK(hsc);
350     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;    
351     /* Check if a non-blocking receive process is ongoing or not */
352     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX) 
353     {
354       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
355     }
356     else
357     {
358       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
359     }
360     
361     hsc->TxXferSize = Size;
362     hsc->TxXferCount = Size;
363     while(hsc->TxXferCount > 0)
364     {
365       hsc->TxXferCount--;      
366       if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, Timeout) != HAL_OK)  
367       { 
368         return HAL_TIMEOUT;
369       }
370       hsc->Instance->TDR = (*pData++ & (uint8_t)0xFF);     
371     }
372     if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, Timeout) != HAL_OK)  
373     { 
374       return HAL_TIMEOUT;
375     }
376     /* Check if a non-blocking receive Process is ongoing or not */
377     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX) 
378     {
379       hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
380     }
381     else
382     {
383       hsc->State = HAL_SMARTCARD_STATE_READY;
384     }
385     
386     /* Process Unlocked */
387     __HAL_UNLOCK(hsc);
388     
389     return HAL_OK;
390   }
391   else
392   {
393     return HAL_BUSY;   
394   }
395 }
396
397 /**
398   * @brief Receive an amount of data in blocking mode 
399   * @param hsc: SMARTCARD handle
400   * @param pData: pointer to data buffer
401   * @param Size: amount of data to be received
402   * @param Timeout : Timeout duration
403   * @retval HAL status
404   */
405 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
406
407   if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_TX))
408   { 
409     if((pData == NULL) || (Size == 0)) 
410     {
411       return  HAL_ERROR;
412     }
413     
414     /* Process Locked */
415     __HAL_LOCK(hsc);
416     
417     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
418     /* Check if a non-blocking transmit process is ongoing or not */
419     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX) 
420     {
421       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
422     }
423     else
424     {
425       hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
426     }    
427     
428     hsc->RxXferSize = Size; 
429     hsc->RxXferCount = Size;
430     /* Check the remain data to be received */
431     while(hsc->RxXferCount > 0)
432     {
433       hsc->RxXferCount--;    
434       if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, Timeout) != HAL_OK)  
435       { 
436         return HAL_TIMEOUT;
437       }          
438       *pData++ = (uint8_t)(hsc->Instance->RDR & (uint8_t)0x00FF);              
439     }
440     
441     /* Check if a non-blocking transmit Process is ongoing or not */
442     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX) 
443     {
444       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
445     }
446     else
447     {
448       hsc->State = HAL_SMARTCARD_STATE_READY;
449     }    
450     
451     /* Process Unlocked */
452     __HAL_UNLOCK(hsc);
453     
454     return HAL_OK;
455   }
456   else
457   {
458     return HAL_BUSY;   
459   }
460 }
461
462 /**
463   * @brief Send an amount of data in interrupt mode 
464   * @param hsc: SMARTCARD handle
465   * @param pData: pointer to data buffer
466   * @param Size: amount of data to be sent
467   * @retval HAL status
468   */
469 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
470 {
471   if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_RX))
472   {
473     if((pData == NULL) || (Size == 0)) 
474     {
475       return HAL_ERROR;
476     }
477     
478     /* Process Locked */
479     __HAL_LOCK(hsc);
480     
481     hsc->pTxBuffPtr = pData;
482     hsc->TxXferSize = Size;
483     hsc->TxXferCount = Size;
484     
485     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
486     /* Check if a receive process is ongoing or not */
487     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX) 
488     {
489       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
490     }
491     else
492     {
493       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
494     }
495
496     /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
497     __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR);
498     
499     /* Process Unlocked */
500     __HAL_UNLOCK(hsc);    
501     
502     /* Enable the SMARTCARD Transmit Complete Interrupt */
503     __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TC);
504     
505     return HAL_OK;
506   }
507   else
508   {
509     return HAL_BUSY;
510   }
511 }
512
513 /**
514   * @brief Receive an amount of data in interrupt mode 
515   * @param hsc: SMARTCARD handle
516   * @param pData: pointer to data buffer
517   * @param Size: amount of data to be received
518   * @retval HAL status
519   */
520 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
521 {
522   if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_TX))
523   {
524     if((pData == NULL) || (Size == 0)) 
525     {
526       return HAL_ERROR;
527     }
528     
529     /* Process Locked */
530   __HAL_LOCK(hsc);
531   
532     hsc->pRxBuffPtr = pData;
533     hsc->RxXferSize = Size;
534     hsc->RxXferCount = Size;
535     
536     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE; 
537     /* Check if a transmit process is ongoing or not */
538     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX) 
539     {
540       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
541     }
542     else
543     {
544       hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
545     }    
546     
547     /* Enable the SMARTCARD Parity Error Interrupt */
548     __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_PE);
549     
550     /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
551     __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR);
552     
553     /* Process Unlocked */
554     __HAL_UNLOCK(hsc);
555     
556     /* Enable the SMARTCARD Data Register not empty Interrupt */
557     __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_RXNE);
558     
559     return HAL_OK;
560   }
561   else
562   {
563     return HAL_BUSY; 
564   }
565 }
566
567 /**
568   * @brief Send an amount of data in DMA mode 
569   * @param hsc: SMARTCARD handle
570   * @param pData: pointer to data buffer
571   * @param Size: amount of data to be sent
572   * @retval HAL status
573   */
574 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
575 {
576   uint32_t *tmp;
577   
578   if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_RX))
579   {
580     if((pData == NULL) || (Size == 0)) 
581     {
582       return HAL_ERROR;
583     }
584     
585     /* Process Locked */
586     __HAL_LOCK(hsc);
587     
588     hsc->pTxBuffPtr = pData;
589     hsc->TxXferSize = Size;
590     hsc->TxXferCount = Size; 
591   
592     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;  
593     /* Check if a receive process is ongoing or not */
594     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX) 
595     {
596       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
597     }
598     else
599     {
600       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
601     }
602     
603     /* Set the SMARTCARD DMA transfert complete callback */
604     hsc->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
605     
606     /* Set the SMARTCARD error callback */
607     hsc->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
608
609     /* Enable the SMARTCARD transmit DMA Stream */
610     tmp = (uint32_t*)&pData;
611     HAL_DMA_Start_IT(hsc->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsc->Instance->TDR, Size);
612     
613     /* Clear the TC flag in the SR register by writing 0 to it */
614     __HAL_SMARTCARD_CLEAR_FLAG(hsc, SMARTCARD_FLAG_TC);
615
616     /* Enable the DMA transfer for transmit request by setting the DMAT bit
617        in the SMARTCARD associated USART CR3 register */
618     hsc->Instance->CR3 |= USART_CR3_DMAT;
619     
620     /* Process Unlocked */
621     __HAL_UNLOCK(hsc);
622     
623     return HAL_OK;
624   }
625   else
626   {
627     return HAL_BUSY;   
628   }
629 }
630
631 /**
632   * @brief Receive an amount of data in DMA mode 
633   * @param hsc: SMARTCARD handle
634   * @param pData: pointer to data buffer
635   * @param Size: amount of data to be received
636   * @note   The SMARTCARD-associated USART parity is enabled (PCE = 1), 
637   *         the received data contain the parity bit (MSB position)   
638   * @retval HAL status
639   */
640 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
641 {
642   uint32_t *tmp;
643   
644   if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_TX))
645   {
646     if((pData == NULL) || (Size == 0)) 
647     {
648       return HAL_ERROR;
649     }
650     
651     /* Process Locked */
652     __HAL_LOCK(hsc);
653     
654     hsc->pRxBuffPtr = pData;
655     hsc->RxXferSize = Size;
656     
657     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
658     /* Check if a transmit rocess is ongoing or not */
659     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX) 
660     {
661       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
662     }
663     else
664     {
665       hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
666     }    
667     
668     /* Set the SMARTCARD DMA transfert complete callback */
669     hsc->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
670     
671     /* Set the SMARTCARD DMA error callback */
672     hsc->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
673     
674     /* Enable the DMA Stream */
675     tmp = (uint32_t*)&pData;
676     HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->RDR, *(uint32_t*)tmp, Size);
677     
678     /* Enable the DMA transfer for the receiver request by setting the DMAR bit 
679     in the SMARTCARD associated USART CR3 register */
680     hsc->Instance->CR3 |= USART_CR3_DMAR;
681     
682     /* Process Unlocked */
683     __HAL_UNLOCK(hsc);
684     
685     return HAL_OK;
686   }
687   else
688   {
689     return HAL_BUSY; 
690   }
691 }
692     
693 /**
694   * @brief SMARTCARD interrupt requests handling.
695   * @param hsc: SMARTCARD handle
696   * @retval None
697   */
698 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc)
699 {
700   /* SMARTCARD parity error interrupt occurred -------------------------------*/
701   if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_PE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_PE) != RESET))
702   { 
703     __HAL_SMARTCARD_CLEAR_PEFLAG(hsc);
704     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
705     /* Set the SMARTCARD state ready to be able to start again the process */
706     hsc->State = HAL_SMARTCARD_STATE_READY;
707   }
708   
709   /* SMARTCARD frame error interrupt occured ---------------------------------*/
710   if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_FE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR) != RESET))
711   { 
712     __HAL_SMARTCARD_CLEAR_FEFLAG(hsc);
713     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
714     /* Set the SMARTCARD state ready to be able to start again the process */
715     hsc->State = HAL_SMARTCARD_STATE_READY;
716   }
717   
718   /* SMARTCARD noise error interrupt occured ---------------------------------*/
719   if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_NE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR) != RESET))
720   { 
721     __HAL_SMARTCARD_CLEAR_NEFLAG(hsc);
722     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_NE; 
723     /* Set the SMARTCARD state ready to be able to start again the process */
724     hsc->State = HAL_SMARTCARD_STATE_READY;
725   }
726   
727   /* SMARTCARD Over-Run interrupt occured ------------------------------------*/
728   if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_ORE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR) != RESET))
729   { 
730     __HAL_SMARTCARD_CLEAR_OREFLAG(hsc);
731     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_ORE; 
732     /* Set the SMARTCARD state ready to be able to start again the process */
733     hsc->State = HAL_SMARTCARD_STATE_READY;
734   }
735   
736   /* SMARTCARD receiver timeout interrupt occured ----------------------------*/
737   if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_RTO) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_RTO) != RESET))
738   { 
739     __HAL_SMARTCARD_CLEAR_IT(hsc, SMARTCARD_CLEAR_RTOF);
740     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_RTO; 
741     /* Set the SMARTCARD state ready to be able to start again the process */
742     hsc->State = HAL_SMARTCARD_STATE_READY;
743   }
744   
745   /* Call SMARTCARD Error Call back function if need be ----------------------*/
746   if(hsc->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
747   {
748     HAL_SMARTCARD_ErrorCallback(hsc);
749   } 
750   
751   /* SMARTCARD in mode Receiver ----------------------------------------------*/
752   if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_RXNE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_RXNE) != RESET))
753   { 
754     SMARTCARD_Receive_IT(hsc);
755     /* Clear RXNE interrupt flag */
756     __HAL_SMARTCARD_SEND_REQ(hsc, SMARTCARD_RXDATA_FLUSH_REQUEST);
757   }
758   
759   /* SMARTCARD in mode Receiver, end of block interruption -------------------*/
760   if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_EOB) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_EOB) != RESET))
761   { 
762     hsc->State = HAL_SMARTCARD_STATE_READY;
763     HAL_SMARTCARD_RxCpltCallback(hsc);
764     /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
765     * to be available during HAL_SMARTCARD_RxCpltCallback() processing */
766     __HAL_SMARTCARD_CLEAR_IT(hsc, SMARTCARD_CLEAR_EOBF);
767   }  
768   
769   /* SMARTCARD in mode Transmitter -------------------------------------------*/
770   if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_TC) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_TC) != RESET))
771   {
772     SMARTCARD_Transmit_IT(hsc);
773   } 
774
775
776 /**
777   * @brief Tx Transfer completed callbacks
778   * @param hsc: SMARTCARD handle
779   * @retval None
780   */
781  __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
782 {
783   /* NOTE : This function Should not be modified, when the callback is needed,
784             the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file
785    */ 
786 }
787
788 /**
789   * @brief Rx Transfer completed callbacks
790   * @param hsc: SMARTCARD handle
791   * @retval None
792   */
793 __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
794 {
795   /* NOTE : This function Should not be modified, when the callback is needed,
796             the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file
797    */
798 }
799
800 /**
801   * @brief SMARTCARD error callbacks
802   * @param hsc: SMARTCARD handle
803   * @retval None
804   */
805  __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc)
806 {
807   /* NOTE : This function Should not be modified, when the callback is needed,
808             the HAL_SMARTCARD_ErrorCallback could be implemented in the user file
809    */
810 }
811
812 /**
813   * @}
814   */  
815
816 /** @addtogroup SMARTCARD_Exported_Functions_Group3
817   *  @brief   SMARTCARD State functions 
818   *
819 @verbatim   
820  ===============================================================================
821                       ##### Peripheral State functions #####
822  ===============================================================================
823     [..]
824     This subsection provides a set of functions allowing to initialize the SMARTCARD.
825      (+) HAL_SMARTCARD_GetState() API is helpful to check in run-time the state of the SMARTCARD peripheral 
826      (+) SMARTCARD_SetConfig() API configures the SMARTCARD peripheral  
827      (+) SMARTCARD_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization 
828
829 @endverbatim
830   * @{
831   */
832
833
834 /**
835   * @brief return the SMARTCARD state
836   * @param hsc: SMARTCARD handle
837   * @retval HAL state
838   */
839 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc)
840 {
841   return hsc->State;
842 }
843
844 /**
845 * @brief  Return the SMARTCARD error code
846 * @param  hsc : pointer to a SMARTCARD_HandleTypeDef structure that contains
847   *              the configuration information for the specified SMARTCARD.
848 * @retval SMARTCARD Error Code
849 */
850 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc)
851 {
852   return hsc->ErrorCode;
853 }
854
855 /**
856   * @}
857   */
858   
859 /**
860   * @brief Send an amount of data in non blocking mode 
861   * @param hsc: SMARTCARD handle.
862   *         Function called under interruption only, once
863   *         interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()      
864   * @retval HAL status
865   */
866 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc)
867 {
868   if((hsc->State == HAL_SMARTCARD_STATE_BUSY_TX) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)) 
869   {
870     if(hsc->TxXferCount == 0)
871     {
872       /* Disable the SMARTCARD Transmit Complete Interrupt */
873       __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TC);
874       
875       /* Check if a receive Process is ongoing or not */
876       if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX) 
877       {
878         hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
879       }
880       else
881       { 
882         /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
883         __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
884       
885         hsc->State = HAL_SMARTCARD_STATE_READY;
886       }
887       
888       HAL_SMARTCARD_TxCpltCallback(hsc);
889       
890       return HAL_OK;
891     }
892     else
893     {    
894       hsc->Instance->TDR = (*hsc->pTxBuffPtr++ & (uint8_t)0xFF);     
895       hsc->TxXferCount--;
896   
897       return HAL_OK;
898     }
899   }
900   else
901   {
902     return HAL_BUSY;   
903   }
904 }
905
906 /**
907   * @brief Receive an amount of data in non blocking mode 
908   * @param hsc: SMARTCARD handle.
909   *         Function called under interruption only, once
910   *         interruptions have been enabled by HAL_SMARTCARD_Receive_IT()      
911   * @retval HAL status
912   */
913 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc)
914 {
915   if((hsc->State == HAL_SMARTCARD_STATE_BUSY_RX) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX))
916   {
917     *hsc->pRxBuffPtr++ = (uint8_t)(hsc->Instance->RDR & (uint8_t)0xFF);  
918     
919     if(--hsc->RxXferCount == 0)
920     {
921       while(HAL_IS_BIT_SET(hsc->Instance->ISR, SMARTCARD_FLAG_RXNE))
922       {
923       }
924       __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
925       
926       /* Check if a transmit Process is ongoing or not */
927       if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX) 
928       {
929         hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
930       }
931       else
932       {
933         /* Disable the SMARTCARD Parity Error Interrupt */
934         __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_PE);
935          
936         /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
937         __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
938         
939         hsc->State = HAL_SMARTCARD_STATE_READY;
940       }
941       
942       HAL_SMARTCARD_RxCpltCallback(hsc);
943       
944       return HAL_OK;
945     }
946     return HAL_OK;
947   }
948   else
949   {
950     return HAL_BUSY; 
951   }
952 }
953
954 /**
955   * @brief Configure the SMARTCARD associated USART peripheral 
956   * @param hsc: SMARTCARD handle
957   * @retval None
958   */
959 static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc)
960 {
961   uint32_t tmpreg = 0x00000000;
962   uint32_t clocksource = 0x00000000;
963   
964   /* Check the parameters */ 
965   assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
966   assert_param(IS_SMARTCARD_BAUDRATE(hsc->Init.BaudRate)); 
967   assert_param(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength));  
968   assert_param(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits));   
969   assert_param(IS_SMARTCARD_PARITY(hsc->Init.Parity));
970   assert_param(IS_SMARTCARD_MODE(hsc->Init.Mode));
971   assert_param(IS_SMARTCARD_POLARITY(hsc->Init.CLKPolarity));
972   assert_param(IS_SMARTCARD_PHASE(hsc->Init.CLKPhase));
973   assert_param(IS_SMARTCARD_LASTBIT(hsc->Init.CLKLastBit));    
974   assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsc->Init.OneBitSampling));
975   assert_param(IS_SMARTCARD_NACK(hsc->Init.NACKState));
976   assert_param(IS_SMARTCARD_TIMEOUT(hsc->Init.TimeOutEnable));
977   assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsc->Init.AutoRetryCount)); 
978
979   /*-------------------------- USART CR1 Configuration -----------------------*/
980   /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
981    * Oversampling is forced to 16 (OVER8 = 0).
982    * Configure the Parity and Mode: 
983    *  set PS bit according to hsc->Init.Parity value
984    *  set TE and RE bits according to hsc->Init.Mode value */
985   tmpreg = (uint32_t) hsc->Init.Parity | hsc->Init.Mode;
986   /* in case of TX-only mode, if NACK is enabled, the USART must be able to monitor 
987      the bidirectional line to detect a NACK signal in case of parity error. 
988      Therefore, the receiver block must be enabled as well (RE bit must be set). */
989   if((hsc->Init.Mode == SMARTCARD_MODE_TX) && (hsc->Init.NACKState == SMARTCARD_NACK_ENABLE))
990   {
991     tmpreg |= USART_CR1_RE;   
992   }
993   tmpreg |= (uint32_t) hsc->Init.WordLength;
994   MODIFY_REG(hsc->Instance->CR1, USART_CR1_FIELDS, tmpreg);
995
996   /*-------------------------- USART CR2 Configuration -----------------------*/
997   /* Stop bits are forced to 1.5 (STOP = 11) */
998   tmpreg = hsc->Init.StopBits;
999   /* Synchronous mode is activated by default */
1000   tmpreg |= (uint32_t) USART_CR2_CLKEN | hsc->Init.CLKPolarity; 
1001   tmpreg |= (uint32_t) hsc->Init.CLKPhase | hsc->Init.CLKLastBit;
1002   tmpreg |= (uint32_t) hsc->Init.TimeOutEnable;
1003   MODIFY_REG(hsc->Instance->CR2, USART_CR2_FIELDS, tmpreg); 
1004     
1005   /*-------------------------- USART CR3 Configuration -----------------------*/
1006   /* Configure 
1007    * - one-bit sampling method versus three samples' majority rule 
1008    *   according to hsc->Init.OneBitSampling 
1009    * - NACK transmission in case of parity error according 
1010    *   to hsc->Init.NACKEnable   
1011    * - autoretry counter according to hsc->Init.AutoRetryCount     */
1012   tmpreg =  (uint32_t) hsc->Init.OneBitSampling | hsc->Init.NACKState;
1013   tmpreg |= (uint32_t) (hsc->Init.AutoRetryCount << SMARTCARD_CR3_SCARCNT_LSB_POS);
1014   MODIFY_REG(hsc->Instance-> CR3,USART_CR3_FIELDS, tmpreg);
1015   
1016   /*-------------------------- USART GTPR Configuration ----------------------*/
1017   tmpreg = (uint32_t) (hsc->Init.Prescaler | (hsc->Init.GuardTime << SMARTCARD_GTPR_GT_LSB_POS));
1018   MODIFY_REG(hsc->Instance->GTPR, (uint32_t)(USART_GTPR_GT|USART_GTPR_PSC), tmpreg); 
1019   
1020   /*-------------------------- USART RTOR Configuration ----------------------*/ 
1021   tmpreg =   (uint32_t) (hsc->Init.BlockLength << SMARTCARD_RTOR_BLEN_LSB_POS);
1022   if(hsc->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE)
1023   {
1024     assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsc->Init.TimeOutValue));
1025     tmpreg |=  (uint32_t) hsc->Init.TimeOutValue;
1026   }
1027   MODIFY_REG(hsc->Instance->RTOR, (USART_RTOR_RTO|USART_RTOR_BLEN), tmpreg);
1028   
1029   /*-------------------------- USART BRR Configuration -----------------------*/
1030   SMARTCARD_GETCLOCKSOURCE(hsc, clocksource);
1031   switch (clocksource)
1032   {
1033   case SMARTCARD_CLOCKSOURCE_PCLK1: 
1034     hsc->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK1Freq() / hsc->Init.BaudRate);
1035     break;
1036   case SMARTCARD_CLOCKSOURCE_PCLK2: 
1037     hsc->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK2Freq() / hsc->Init.BaudRate);
1038     break;
1039   case SMARTCARD_CLOCKSOURCE_HSI: 
1040     hsc->Instance->BRR = (uint16_t)(HSI_VALUE / hsc->Init.BaudRate); 
1041     break; 
1042   case SMARTCARD_CLOCKSOURCE_SYSCLK:  
1043     hsc->Instance->BRR = (uint16_t)(HAL_RCC_GetSysClockFreq() / hsc->Init.BaudRate);
1044     break;  
1045   case SMARTCARD_CLOCKSOURCE_LSE:                
1046     hsc->Instance->BRR = (uint16_t)(LSE_VALUE / hsc->Init.BaudRate); 
1047     break;
1048   default:
1049     break;
1050   } 
1051 }
1052
1053 /**
1054   * @brief Check the SMARTCARD Idle State
1055   * @param hsc: SMARTCARD handle
1056   * @retval HAL status
1057   */
1058 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsc)
1059 {
1060   
1061   /* Initialize the SMARTCARD ErrorCode */
1062   hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1063
1064   /* Check if the Transmitter is enabled */
1065   if((hsc->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
1066   {
1067     /* Wait until TEACK flag is set */
1068     if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, USART_ISR_TEACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)  
1069     { 
1070       return HAL_TIMEOUT;
1071     } 
1072   }
1073   /* Check if the Receiver is enabled */
1074   if((hsc->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
1075   {
1076     /* Wait until REACK flag is set */
1077     if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, USART_ISR_REACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)  
1078     { 
1079       return HAL_TIMEOUT;
1080     }
1081   }
1082   
1083   /* Process Unlocked */
1084   __HAL_UNLOCK(hsc);
1085         
1086   /* Initialize the SMARTCARD state*/
1087   hsc->State= HAL_SMARTCARD_STATE_READY;
1088   
1089   return HAL_OK;
1090 }
1091
1092 /**
1093   * @brief Configure the SMARTCARD associated USART peripheral advanced feautures 
1094   * @param hsc: SMARTCARD handle  
1095   * @retval None
1096   */
1097 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsc)
1098 {  
1099   /* Check whether the set of advanced features to configure is properly set */ 
1100   assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsc->AdvancedInit.AdvFeatureInit));
1101   
1102   /* if required, configure TX pin active level inversion */
1103   if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT))
1104   {
1105     assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsc->AdvancedInit.TxPinLevelInvert));
1106     MODIFY_REG(hsc->Instance->CR2, USART_CR2_TXINV, hsc->AdvancedInit.TxPinLevelInvert);
1107   }
1108   
1109   /* if required, configure RX pin active level inversion */
1110   if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT))
1111   {
1112     assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsc->AdvancedInit.RxPinLevelInvert));
1113     MODIFY_REG(hsc->Instance->CR2, USART_CR2_RXINV, hsc->AdvancedInit.RxPinLevelInvert);
1114   }
1115   
1116   /* if required, configure data inversion */
1117   if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT))
1118   {
1119     assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsc->AdvancedInit.DataInvert));
1120     MODIFY_REG(hsc->Instance->CR2, USART_CR2_DATAINV, hsc->AdvancedInit.DataInvert);
1121   }
1122   
1123   /* if required, configure RX/TX pins swap */
1124   if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT))
1125   {
1126     assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsc->AdvancedInit.Swap));
1127     MODIFY_REG(hsc->Instance->CR2, USART_CR2_SWAP, hsc->AdvancedInit.Swap);
1128   }
1129   
1130   /* if required, configure RX overrun detection disabling */
1131   if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT))
1132   {
1133     assert_param(IS_SMARTCARD_OVERRUN(hsc->AdvancedInit.OverrunDisable));  
1134     MODIFY_REG(hsc->Instance->CR3, USART_CR3_OVRDIS, hsc->AdvancedInit.OverrunDisable);
1135   }
1136   
1137   /* if required, configure DMA disabling on reception error */
1138   if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT))
1139   {
1140     assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsc->AdvancedInit.DMADisableonRxError));   
1141     MODIFY_REG(hsc->Instance->CR3, USART_CR3_DDRE, hsc->AdvancedInit.DMADisableonRxError);
1142   }
1143   
1144   /* if required, configure MSB first on communication line */  
1145   if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT))
1146   {
1147     assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsc->AdvancedInit.MSBFirst));   
1148     MODIFY_REG(hsc->Instance->CR2, USART_CR2_MSBFIRST, hsc->AdvancedInit.MSBFirst);
1149   }
1150 }
1151   
1152 /**
1153   * @brief  This function handles SMARTCARD Communication Timeout.
1154   * @param  hsc: SMARTCARD handle
1155   * @param  Flag: specifies the SMARTCARD flag to check.
1156   * @param  Status: The new Flag status (SET or RESET).
1157   * @param  Timeout: Timeout duration
1158   * @retval HAL status
1159   */
1160 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout)  
1161 {
1162   uint32_t tickstart = 0x00;
1163   tickstart = HAL_GetTick();
1164
1165   /* Wait until flag is set */
1166   if(Status == RESET)
1167   {    
1168     while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) == RESET)
1169     {
1170       /* Check for the Timeout */
1171       if(Timeout != HAL_MAX_DELAY)
1172       {
1173         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1174         {
1175           /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1176           __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
1177           __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
1178           __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_PE);
1179           __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
1180
1181           hsc->State= HAL_SMARTCARD_STATE_READY;
1182
1183           /* Process Unlocked */
1184           __HAL_UNLOCK(hsc);
1185
1186           return HAL_TIMEOUT;
1187         }
1188       }
1189     }
1190   }
1191   else
1192   {
1193     while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) != RESET)
1194     {
1195       /* Check for the Timeout */
1196       if(Timeout != HAL_MAX_DELAY)
1197       {
1198         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1199         {
1200           /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1201           __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
1202           __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
1203           __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_PE);
1204           __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
1205
1206           hsc->State= HAL_SMARTCARD_STATE_READY;
1207
1208           /* Process Unlocked */
1209           __HAL_UNLOCK(hsc);
1210
1211           return HAL_TIMEOUT;
1212         }
1213       }
1214     }
1215   }
1216   return HAL_OK;
1217 }
1218
1219 /**
1220   * @brief DMA SMARTCARD transmit process complete callback 
1221   * @param hdma: DMA handle
1222   * @retval None
1223   */
1224 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)     
1225 {
1226   SMARTCARD_HandleTypeDef* hsmartcard = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1227   hsmartcard->TxXferCount = 0;
1228
1229   /* Disable the DMA transfer for transmit request by resetting the DMAT bit
1230   in the SMARTCARD associated USART CR3 register */
1231   hsmartcard->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_DMAT);
1232
1233   /* Enable the SMARTCARD Transmit Complete Interrupt */
1234   __HAL_SMARTCARD_ENABLE_IT(hsmartcard, SMARTCARD_IT_TC);
1235 }
1236
1237 /**
1238   * @brief DMA SMARTCARD receive process complete callback 
1239   * @param hdma: DMA handle
1240   * @retval None
1241   */
1242 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)  
1243 {
1244   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1245   hsc->RxXferCount = 0;
1246   
1247   /* Disable the DMA transfer for the receiver request by setting the DMAR bit 
1248      in the SMARTCARD associated USART CR3 register */
1249   hsc->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_DMAR);
1250   
1251   /* Check if a transmit Process is ongoing or not */
1252   if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX) 
1253   {
1254     hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
1255   }
1256   else
1257   {
1258     hsc->State = HAL_SMARTCARD_STATE_READY;
1259   }
1260   
1261   HAL_SMARTCARD_RxCpltCallback(hsc);
1262 }
1263
1264 /**
1265   * @brief DMA SMARTCARD communication error callback 
1266   * @param hdma: DMA handle
1267   * @retval None
1268   */
1269 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
1270 {
1271   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1272   hsc->RxXferCount = 0;
1273   hsc->TxXferCount = 0;
1274   hsc->State= HAL_SMARTCARD_STATE_READY;
1275   hsc->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
1276   HAL_SMARTCARD_ErrorCallback(hsc);
1277 }
1278 /**
1279   * @}
1280   */
1281
1282 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
1283 /**
1284   * @}
1285   */
1286
1287 /**
1288   * @}
1289   */
1290
1291 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
1292