]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L1/stm32l1xx_hal_smartcard.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L1 / stm32l1xx_hal_smartcard.c
1 /**
2   ******************************************************************************
3   * @file    stm32l1xx_hal_smartcard.c
4   * @author  MCD Application Team
5   * @version V1.0.0
6   * @date    5-September-2014
7   * @brief   SMARTCARD HAL module driver.
8   *          This file provides firmware functions to manage the following 
9   *          functionalities of the SMARTCARD peripheral:
10   *           + Initialization and de-initialization functions
11   *           + IO operation functions
12   *           + Peripheral State and Errors functions
13   *           + Peripheral Control functions
14   *
15   @verbatim
16   ==============================================================================
17                         ##### How to use this driver #####
18   ==============================================================================
19   [..]
20     The SMARTCARD HAL driver can be used as follows:
21
22     (#) Declare a SMARTCARD_HandleTypeDef handle structure.
23     (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
24         (##) Enable the USARTx interface clock.
25         (##) SMARTCARD pins configuration:
26             (+++) Enable the clock for the SMARTCARD GPIOs.
27             (+++) Configure these SMARTCARD pins as alternate function pull-up.
28         (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
29              and HAL_SMARTCARD_Receive_IT() APIs):
30             (+++) Configure the USARTx interrupt priority.
31             (+++) Enable the NVIC USART IRQ handle.
32         (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
33              and HAL_SMARTCARD_Receive_DMA() APIs):
34             (+++) Declare a DMA handle structure for the Tx/Rx channel.
35             (+++) Enable the DMAx interface clock.
36             (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
37             (+++) Configure the DMA Tx/Rx channel.
38             (+++) Associate the initilalized DMA handle to the SMARTCARD DMA Tx/Rx handle.
39             (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
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         (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
46              by calling the customed HAL_SMARTCARD_MspInit(&hsc) 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     (#) Three operation modes are available within this driver :
53  
54      *** Polling mode IO operation ***
55      =================================
56      [..]    
57        (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit() 
58        (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
59        
60      *** Interrupt mode IO operation ***    
61      ===================================
62      [..]    
63        (+) Send an amount of data in non blocking mode using HAL_SMARTCARD_Transmit_IT() 
64        (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can 
65             add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
66        (+) Receive an amount of data in non blocking mode using HAL_SMARTCARD_Receive_IT() 
67        (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can 
68             add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback                                      
69        (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can 
70             add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
71
72      *** DMA mode IO operation ***    
73      ==============================
74      [..] 
75        (+) Send an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA() 
76        (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can 
77             add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
78        (+) Receive an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA() 
79        (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can 
80             add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback                                      
81        (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can 
82             add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
83
84      *** SMARTCARD HAL driver macros list ***
85      ========================================
86      [..]
87        Below the list of most used macros in SMARTCARD HAL driver.
88        
89        (+) __HAL_SMARTCARD_ENABLE: Enable the SMARTCARD peripheral 
90        (+) __HAL_SMARTCARD_DISABLE: Disable the SMARTCARD peripheral     
91        (+) __HAL_SMARTCARD_GET_FLAG : Check whether the specified SMARTCARD flag is set or not
92        (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
93        (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
94        (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
95       
96      [..] 
97        (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
98
99   @endverbatim
100   ******************************************************************************
101   * @attention
102   *
103   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
104   *
105   * Redistribution and use in source and binary forms, with or without modification,
106   * are permitted provided that the following conditions are met:
107   *   1. Redistributions of source code must retain the above copyright notice,
108   *      this list of conditions and the following disclaimer.
109   *   2. Redistributions in binary form must reproduce the above copyright notice,
110   *      this list of conditions and the following disclaimer in the documentation
111   *      and/or other materials provided with the distribution.
112   *   3. Neither the name of STMicroelectronics nor the names of its contributors
113   *      may be used to endorse or promote products derived from this software
114   *      without specific prior written permission.
115   *
116   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
117   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
118   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
119   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
120   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
121   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
122   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
123   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
124   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
125   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
126   *
127   ******************************************************************************
128   */
129
130 /* Includes ------------------------------------------------------------------*/
131 #include "stm32l1xx_hal.h"
132
133 /** @addtogroup STM32L1xx_HAL_Driver
134   * @{
135   */
136
137 /** @defgroup SMARTCARD SMARTCARD
138   * @brief HAL SMARTCARD module driver
139   * @{
140   */
141
142 #ifdef HAL_SMARTCARD_MODULE_ENABLED
143
144 /* Private typedef -----------------------------------------------------------*/
145 /* Private define ------------------------------------------------------------*/
146 /** @defgroup SMARTCARD_Private_Constants   SMARTCARD Private Constants
147   * @{
148   */
149 #define SMARTCARD_TIMEOUT_VALUE  22000
150 /**
151   * @}
152   */
153
154 /* Private macro -------------------------------------------------------------*/
155 /* Private variables ---------------------------------------------------------*/
156 /* Private function prototypes -----------------------------------------------*/
157 /** @addtogroup SMARTCARD_Private_Functions   SMARTCARD Private Functions
158   * @{
159   */
160 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc);
161 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
162 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc);
163 static void SMARTCARD_SetConfig (SMARTCARD_HandleTypeDef *hsc);
164 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
165 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
166 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
167 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
168 /**
169   * @}
170   */
171
172 /* Exported functions ---------------------------------------------------------*/
173
174 /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
175   * @{
176   */
177
178 /** @defgroup SMARTCARD_Exported_Functions_Group1 SmartCard Initialization and de-initialization functions 
179   *  @brief    Initialization and Configuration functions 
180   *
181 @verbatim
182
183   ==============================================================================
184               ##### Initialization and Configuration functions #####
185   ==============================================================================
186   [..]
187   This subsection provides a set of functions allowing to initialize the USART 
188   in Smartcard mode.
189   [..]
190   The Smartcard interface is designed to support asynchronous protocol Smartcards as
191   defined in the ISO 7816-3 standard.
192   [..]
193   The USART can provide a clock to the smartcard through the SCLK output.
194   In smartcard mode, SCLK is not associated to the communication but is simply derived 
195   from the internal peripheral input clock through a 5-bit prescaler.
196   [..]
197   (+) For the Smartcard mode only these parameters can be configured:
198       (++) Baud Rate
199       (++) Word Length => Should be 9 bits (8 bits + parity)
200       (++) Stop Bit
201       (++) Parity: => Should be enabled 
202    +-------------------------------------------------------------+
203    |   M bit |  PCE bit  |        SMARTCARD frame                |
204    |---------------------|---------------------------------------|
205    |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
206    +-------------------------------------------------------------+
207       (++) USART polarity
208       (++) USART phase
209       (++) USART LastBit
210       (++) Receiver/transmitter modes
211       (++) Prescaler
212       (++) GuardTime
213       (++) NACKState: The Smartcard NACK state
214
215      (+) Recommended SmartCard interface configuration to get the Answer to Reset from the Card:
216         (++) Word Length = 9 Bits
217         (++) 1.5 Stop Bit
218         (++) Even parity
219         (++) BaudRate = 12096 baud
220         (++) Tx and Rx enabled
221   [..]
222   Please refer to the ISO 7816-3 specification for more details.
223
224     -@- It is also possible to choose 0.5 stop bit for receiving but it is recommended 
225         to use 1.5 stop bits for both transmitting and receiving to avoid switching 
226         between the two configurations.
227   [..]
228   The HAL_SMARTCARD_Init() function follows the USART  SmartCard configuration 
229   procedure (details for the procedure are available in reference manual (RM0038)).
230
231 @endverbatim
232   * @{
233   */
234
235 /**
236   * @brief  Initializes the SmartCard mode according to the specified
237   *         parameters in the SMARTCARD_HandleTypeDef and create the associated handle.
238   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
239   *              the configuration information for the specified SMARTCARD module.
240   * @retval HAL status
241   */
242 HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc)
243 {
244   /* Check the SMARTCARD handle allocation */
245   if(hsc == HAL_NULL)
246   {
247     return HAL_ERROR;
248   }
249
250   /* Check the parameters */
251   assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
252   assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
253
254   if(hsc->State == HAL_SMARTCARD_STATE_RESET)
255   {  
256     /* Init the low level hardware */
257     HAL_SMARTCARD_MspInit(hsc);
258   }
259   
260   hsc->State = HAL_SMARTCARD_STATE_BUSY;
261
262   /* Set the Prescaler */
263   MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_PSC, hsc->Init.Prescaler);
264
265   /* Set the Guard Time */
266   MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_GT, ((hsc->Init.GuardTime)<<8));
267
268   /* Set the Smartcard Communication parameters */
269   SMARTCARD_SetConfig(hsc);
270
271   /* In SmartCard mode, the following bits must be kept cleared: 
272   - LINEN bit in the USART_CR2 register
273   - HDSEL and IREN bits in the USART_CR3 register.*/
274   CLEAR_BIT(hsc->Instance->CR2, USART_CR2_LINEN);
275   CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_IREN | USART_CR3_HDSEL));
276
277   /* Enable the SMARTCARD Parity Error Interrupt */
278   __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_PE);
279
280   /* Enable the SMARTCARD Framing Error Interrupt */
281   __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR);
282
283   /* Enable the Peripharal */
284   __HAL_SMARTCARD_ENABLE(hsc);
285
286   /* Configure the Smartcard NACK state */
287   MODIFY_REG(hsc->Instance->CR3, USART_CR3_NACK, hsc->Init.NACKState);
288
289   /* Enable the SC mode by setting the SCEN bit in the CR3 register */
290   SET_BIT(hsc->Instance->CR3, USART_CR3_SCEN);
291
292   /* Initialize the SMARTCARD state*/
293   hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
294   hsc->State= HAL_SMARTCARD_STATE_READY;
295
296   return HAL_OK;
297 }
298
299 /**
300   * @brief  DeInitializes the SMARTCARD peripheral
301   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
302   *              the configuration information for the specified SMARTCARD module.
303   * @retval HAL status
304   */
305 HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc)
306 {
307   /* Check the SMARTCARD handle allocation */
308   if(hsc == HAL_NULL)
309   {
310     return HAL_ERROR;
311   }
312
313   /* Check the parameters */
314   assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
315
316   hsc->State = HAL_SMARTCARD_STATE_BUSY;
317
318   /* Disable the Peripheral */
319   __HAL_SMARTCARD_DISABLE(hsc);
320
321   /* DeInit the low level hardware */
322   HAL_SMARTCARD_MspDeInit(hsc);
323
324   hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
325   hsc->State = HAL_SMARTCARD_STATE_RESET;
326
327   /* Release Lock */
328   __HAL_UNLOCK(hsc);
329
330   return HAL_OK;
331 }
332
333 /**
334   * @brief  SMARTCARD MSP Init.
335   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
336   *                the configuration information for the specified SMARTCARD module.
337   * @retval None
338   */
339  __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc)
340 {
341   /* NOTE: This function should not be modified, when the callback is needed,
342            the HAL_SMARTCARD_MspInit can be implemented in the user file
343    */ 
344 }
345
346 /**
347   * @brief  SMARTCARD MSP DeInit.
348   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
349   *                the configuration information for the specified SMARTCARD module.
350   * @retval None
351   */
352  __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc)
353 {
354   /* NOTE: This function should not be modified, when the callback is needed,
355            the HAL_SMARTCARD_MspDeInit can be implemented in the user file
356    */ 
357 }
358
359 /**
360   * @}
361   */
362
363 /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions 
364   *  @brief   SMARTCARD Transmit and Receive functions 
365   *
366 @verbatim
367   ==============================================================================
368                          ##### IO operation functions #####
369   ==============================================================================
370   [..]
371     This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
372
373   [..]
374     Smartcard is a single wire half duplex communication protocol. 
375     The Smartcard interface is designed to support asynchronous protocol Smartcards as
376     defined in the ISO 7816-3 standard. The USART should be configured as:
377     - 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
378     - 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
379
380     (#) There are two modes of transfer:
381         (++) Blocking mode: The communication is performed in polling mode. 
382              The HAL status of all data processing is returned by the same function 
383              after finishing transfer.  
384         (++) No-Blocking mode: The communication is performed using Interrupts 
385              or DMA, These API's return the HAL status.
386              The end of the data processing will be indicated through the 
387              dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when 
388              using DMA mode.
389              The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks 
390              will be executed respectivelly at the end of the Transmit or Receive process
391              The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication 
392              error is detected
393
394     (#) Blocking mode APIs are :
395         (++) HAL_SMARTCARD_Transmit()
396         (++) HAL_SMARTCARD_Receive()
397
398     (#) Non Blocking mode APIs with Interrupt are :
399         (++) HAL_SMARTCARD_Transmit_IT()
400         (++) HAL_SMARTCARD_Receive_IT()
401         (++) HAL_SMARTCARD_IRQHandler()
402
403     (#) Non Blocking mode functions with DMA are :
404         (++) HAL_SMARTCARD_Transmit_DMA()
405         (++) HAL_SMARTCARD_Receive_DMA()
406
407     (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
408         (++) HAL_SMARTCARD_TxCpltCallback()
409         (++) HAL_SMARTCARD_RxCpltCallback()
410         (++) HAL_SMARTCARD_ErrorCallback()
411
412 @endverbatim
413   * @{
414   */
415
416 /**
417   * @brief  Sends an amount of data in blocking mode.
418   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
419   *                the configuration information for the specified SMARTCARD module.
420   * @param  pData: Pointer to data buffer
421   * @param  Size: Amount of data to be sent
422   * @param  Timeout: Specify timeout value  
423   * @retval HAL status
424   */
425 HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
426 {
427   uint16_t* tmp = 0;
428   uint32_t tmp1 = 0;
429   
430   tmp1 = hsc->State;
431   if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_RX))
432   {
433     if((pData == HAL_NULL) || (Size == 0)) 
434     {
435       return  HAL_ERROR;
436     }
437
438     /* Process Locked */
439     __HAL_LOCK(hsc);
440
441     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
442     /* Check if a non-blocking receive process is ongoing or not */
443     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX) 
444     {
445       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
446     }
447     else
448     {
449       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
450     }
451     
452     hsc->TxXferSize = Size;
453     hsc->TxXferCount = Size;
454     while(hsc->TxXferCount > 0)
455     {
456       if(hsc->Init.WordLength == SMARTCARD_WORDLENGTH_9B)
457       {
458         if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, Timeout) != HAL_OK)
459         {
460           return HAL_TIMEOUT;
461         }
462         tmp = (uint16_t*) pData;
463         WRITE_REG(hsc->Instance->DR, (*tmp & (uint16_t)0x01FF));
464         if(hsc->Init.Parity == SMARTCARD_PARITY_NONE)
465         {
466           pData +=2;
467         }
468         else
469         {
470           pData +=1;
471         }
472       }
473       else
474       {
475         if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, Timeout) != HAL_OK)
476         {
477           return HAL_TIMEOUT;
478         }
479         WRITE_REG(hsc->Instance->DR, (*pData++ & (uint8_t)0xFF));
480       }
481       hsc->TxXferCount--;
482     }
483
484     if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, Timeout) != HAL_OK)
485     {
486       return HAL_TIMEOUT;
487     }
488
489     /* Check if a non-blocking receive process is ongoing or not */
490     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX) 
491     {
492       hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
493     }
494     else
495     {
496       hsc->State = HAL_SMARTCARD_STATE_READY;
497     }
498     /* Process Unlocked */
499     __HAL_UNLOCK(hsc);
500
501     return HAL_OK;
502   }
503   else
504   {
505     return HAL_BUSY;
506   }
507 }
508
509 /**
510   * @brief  Receive an amount of data in blocking mode. 
511   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
512   *                the configuration information for the specified SMARTCARD module.
513   * @param  pData: Pointer to data buffer
514   * @param  Size: Amount of data to be received
515   * @param  Timeout: Specify timeout value    
516   * @retval HAL status
517   */
518 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
519 {
520   uint16_t* tmp = 0;
521   uint32_t tmp1 = 0;
522   
523   tmp1 = hsc->State;
524   if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_TX))
525   {
526     if((pData == HAL_NULL) || (Size == 0)) 
527     {
528       return  HAL_ERROR;
529     }
530
531     /* Process Locked */
532     __HAL_LOCK(hsc);
533     
534     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
535
536     /* Check if a non-blocking transmit process is ongoing or not */
537     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX) 
538     {
539       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
540     }
541     else
542     {
543       hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
544     }
545
546     hsc->RxXferSize = Size;
547     hsc->RxXferCount = Size;
548     /* Check the remain data to be received */
549     while(hsc->RxXferCount > 0)
550     {
551       if(hsc->Init.WordLength == SMARTCARD_WORDLENGTH_9B)
552       {
553         if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, Timeout) != HAL_OK)
554         {
555           return HAL_TIMEOUT;
556         }
557         tmp = (uint16_t*) pData;
558         if(hsc->Init.Parity == SMARTCARD_PARITY_NONE)
559         {
560           *tmp = (uint16_t)(hsc->Instance->DR & (uint16_t)0x01FF);
561           pData +=2;
562         }
563         else
564         {
565           *tmp = (uint16_t)(hsc->Instance->DR & (uint16_t)0x00FF);
566           pData +=1;
567         }
568       }
569       else
570       {
571         if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, Timeout) != HAL_OK)
572         {
573           return HAL_TIMEOUT;
574         }
575         if(hsc->Init.Parity == SMARTCARD_PARITY_NONE)
576         {
577           *pData++ = (uint8_t)(hsc->Instance->DR & (uint8_t)0x00FF);
578         }
579         else
580         {
581           *pData++ = (uint8_t)(hsc->Instance->DR & (uint8_t)0x007F);
582         }
583       }
584       hsc->RxXferCount--;
585     }
586
587     /* Check if a non-blocking transmit process is ongoing or not */
588     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX) 
589     {
590       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
591     }
592     else
593     {
594       hsc->State = HAL_SMARTCARD_STATE_READY;
595     }
596
597     /* Process Unlocked */
598     __HAL_UNLOCK(hsc);
599
600     return HAL_OK;
601   }
602   else
603   {
604     return HAL_BUSY;
605   }
606 }
607
608 /**
609   * @brief  Sends an amount of data in non-blocking mode. 
610   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
611   *                the configuration information for the specified SMARTCARD module.
612   * @param  pData: Pointer to data buffer
613   * @param  Size: Amount of data to be sent
614   * @retval HAL status
615   */
616 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
617 {
618   uint32_t tmp1 = 0;
619   
620   tmp1 = hsc->State;
621   if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_RX))
622   {
623     if((pData == HAL_NULL) || (Size == 0))
624     {
625       return HAL_ERROR;
626     }
627
628     /* Process Locked */
629     __HAL_LOCK(hsc);
630
631     hsc->pTxBuffPtr = pData;
632     hsc->TxXferSize = Size;
633     hsc->TxXferCount = Size;
634
635     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
636     /* Check if a non-blocking receive process is ongoing or not */
637     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX) 
638     {
639       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
640     }
641     else
642     {
643       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
644     }
645
646     /* Enable the SMARTCARD Parity Error Interrupt */
647     __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_PE);
648
649     /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
650     __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR);
651
652     /* Process Unlocked */
653     __HAL_UNLOCK(hsc);
654
655     /* Enable the SMARTCARD Transmit data register empty Interrupt */
656     __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TXE);
657
658     return HAL_OK;
659   }
660   else
661   {
662     return HAL_BUSY;
663   }
664 }
665
666 /**
667   * @brief  Receives an amount of data in non-blocking mode. 
668   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
669   *                the configuration information for the specified SMARTCARD module.
670   * @param  pData: Pointer to data buffer
671   * @param  Size: Amount of data to be received
672   * @retval HAL status
673   */
674 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
675 {
676   uint32_t tmp1 = 0;
677   
678   tmp1 = hsc->State;
679   if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_TX))
680   {
681     if((pData == HAL_NULL) || (Size == 0))
682     {
683       return HAL_ERROR;
684     }
685
686     /* Process Locked */
687     __HAL_LOCK(hsc);
688
689     hsc->pRxBuffPtr = pData;
690     hsc->RxXferSize = Size;
691     hsc->RxXferCount = Size;
692
693     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
694     /* Check if a non-blocking transmit process is ongoing or not */
695     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX) 
696     {
697       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
698     }
699     else
700     {
701       hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
702     }
703
704     /* Enable the SMARTCARD Data Register not empty Interrupt */
705     __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_RXNE); 
706
707     /* Enable the SMARTCARD Parity Error Interrupt */
708     __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_PE);
709
710     /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
711     __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR);
712
713     /* Process Unlocked */
714     __HAL_UNLOCK(hsc);
715
716     return HAL_OK;
717   }
718   else
719   {
720     return HAL_BUSY;
721   }
722 }
723
724 /**
725   * @brief  Sends an amount of data in non-blocking mode. 
726   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
727   *                the configuration information for the specified SMARTCARD module.
728   * @param  pData: Pointer to data buffer
729   * @param  Size: Amount of data to be sent
730   * @retval HAL status
731   */
732 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
733 {
734   uint32_t *tmp = 0;
735   uint32_t tmp1 = 0;
736   
737   tmp1 = hsc->State;
738   if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_RX))
739   {
740     if((pData == HAL_NULL) || (Size == 0)) 
741     {
742       return HAL_ERROR;
743     }
744
745     /* Process Locked */
746     __HAL_LOCK(hsc);
747
748     hsc->pTxBuffPtr = pData;
749     hsc->TxXferSize = Size;
750     hsc->TxXferCount = Size;
751
752     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
753     /* Check if a non-blocking receive process is ongoing or not */
754     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX) 
755     {
756       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
757     }
758     else
759     {
760       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
761     }
762
763     /* Set the SMARTCARD DMA transfer complete callback */
764     hsc->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
765
766     /* Set the DMA error callback */
767     hsc->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
768
769     /* Enable the SMARTCARD transmit DMA channel */
770     tmp = (uint32_t*)&pData;
771     HAL_DMA_Start_IT(hsc->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsc->Instance->DR, Size);
772
773     /* Enable the DMA transfer for transmit request by setting the DMAT bit
774     in the SMARTCARD CR3 register */
775     SET_BIT(hsc->Instance->CR3,USART_CR3_DMAT);
776
777     /* Process Unlocked */
778     __HAL_UNLOCK(hsc);
779
780     return HAL_OK;
781   }
782   else
783   {
784     return HAL_BUSY;
785   }
786 }
787
788 /**
789   * @brief  Receive an amount of data in non-blocking mode. 
790   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
791   *                the configuration information for the specified SMARTCARD module.
792   * @param  pData: Pointer to data buffer
793   * @param  Size: Amount of data to be received
794   * @note   When the SMARTCARD parity is enabled (PCE = 1) the data received contain the parity bit.
795   * @retval HAL status
796   */
797 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
798 {
799   uint32_t *tmp = 0;
800   uint32_t tmp1 = 0;
801   
802   tmp1 = hsc->State;
803   if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_TX))
804   {
805     if((pData == HAL_NULL) || (Size == 0))
806     {
807       return HAL_ERROR;
808     }
809
810     /* Process Locked */
811     __HAL_LOCK(hsc);
812
813     hsc->pRxBuffPtr = pData;
814     hsc->RxXferSize = Size;
815
816     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
817     /* Check if a non-blocking transmit process is ongoing or not */
818     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX) 
819     {
820       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
821     }
822     else
823     {
824       hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
825     }
826
827     /* Set the SMARTCARD DMA transfer complete callback */
828     hsc->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
829
830     /* Set the DMA error callback */
831     hsc->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
832
833     /* Enable the DMA channel */
834     tmp = (uint32_t*)&pData;
835     HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->DR, *(uint32_t*)tmp, Size);
836
837     /* Enable the DMA transfer for the receiver request by setting the DMAR bit 
838     in the SMARTCARD CR3 register */
839     SET_BIT(hsc->Instance->CR3,USART_CR3_DMAR);
840
841     /* Process Unlocked */
842     __HAL_UNLOCK(hsc);
843
844     return HAL_OK;
845   }
846   else
847   {
848     return HAL_BUSY;
849   }
850 }
851
852 /**
853   * @brief  This function handles SMARTCARD interrupt request.
854   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
855   *                the configuration information for the specified SMARTCARD module.
856   * @retval None
857   */
858 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc)
859 {
860   uint32_t  tmp1 = 0, tmp2 = 0;
861   
862   tmp1 = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_PE);
863   tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_PE);
864   /* SMARTCARD parity error interrupt occurred -----------------------------------*/
865   if((tmp1 != RESET) && (tmp2 != RESET))
866   {
867     __HAL_SMARTCARD_CLEAR_PEFLAG(hsc);
868     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
869   }
870
871   tmp1 = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_FE);
872   tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR);
873   /* SMARTCARD frame error interrupt occurred ------------------------------------*/
874   if((tmp1 != RESET) && (tmp2 != RESET))
875   {
876     __HAL_SMARTCARD_CLEAR_FEFLAG(hsc);
877     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
878   }
879
880   tmp1 = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_NE);
881   tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR);
882   /* SMARTCARD noise error interrupt occurred ------------------------------------*/
883   if((tmp1 != RESET) && (tmp2 != RESET))
884   {
885     __HAL_SMARTCARD_CLEAR_NEFLAG(hsc);
886     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
887   }
888
889   tmp1 = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_ORE);
890   tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR);
891   /* SMARTCARD Over-Run interrupt occurred ---------------------------------------*/
892   if((tmp1 != RESET) && (tmp2 != RESET))
893   {
894     __HAL_SMARTCARD_CLEAR_OREFLAG(hsc);
895     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
896   }
897   
898   tmp1 = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_RXNE);
899   tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_RXNE);
900   /* SMARTCARD in mode Receiver --------------------------------------------------*/
901   if((tmp1 != RESET) && (tmp2 != RESET))
902   {
903     SMARTCARD_Receive_IT(hsc);
904   }
905
906   tmp1 = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_TXE);
907   tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_TXE);
908   /* SMARTCARD in mode Transmitter -----------------------------------------------*/
909   if((tmp1 != RESET) && (tmp2 != RESET))
910   {
911     SMARTCARD_Transmit_IT(hsc);
912   }
913   
914   tmp1 = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_TC);
915   tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_TC);
916   /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
917   if((tmp1 != RESET) && (tmp2 != RESET))
918   {
919     SMARTCARD_EndTransmit_IT(hsc);
920   } 
921
922   /* Call the Error call Back in case of Errors */
923   if(hsc->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
924   {
925     /* Set the SMARTCARD state ready to be able to start again the process */
926     hsc->State= HAL_SMARTCARD_STATE_READY;
927     HAL_SMARTCARD_ErrorCallback(hsc);
928   }
929 }
930
931 /**
932   * @brief  Tx Transfer completed callbacks.
933   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
934   *                the configuration information for the specified SMARTCARD module.
935   * @retval None
936   */
937  __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
938 {
939   /* NOTE: This function should not be modified, when the callback is needed,
940            the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file
941    */
942 }
943
944 /**
945   * @brief  Rx Transfer completed callbacks.
946   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
947   *                the configuration information for the specified SMARTCARD module.
948   * @retval None
949   */
950 __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
951 {
952   /* NOTE: This function should not be modified, when the callback is needed,
953            the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file
954    */
955 }
956
957 /**
958   * @brief  SMARTCARD error callbacks.
959   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
960   *                the configuration information for the specified SMARTCARD module.
961   * @retval None
962   */
963  __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc)
964 {
965   /* NOTE: This function should not be modified, when the callback is needed,
966            the HAL_SMARTCARD_ErrorCallback can be implemented in the user file
967    */ 
968 }
969
970 /**
971   * @}
972   */
973
974 /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions 
975   *  @brief   SMARTCARD State and Errors functions 
976   *
977 @verbatim   
978   ==============================================================================
979                   ##### Peripheral State and Errors functions #####
980   ==============================================================================  
981   [..]
982     This subsection provides a set of functions allowing to return the State of SmartCard 
983     communication process and also return Peripheral Errors occurred during communication process
984      (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state 
985          of the SMARTCARD peripheral.
986      (+) HAL_SMARTCARD_GetError() check in run-time errors that could be occurred during 
987          communication.
988
989 @endverbatim
990   * @{
991   */
992
993 /**
994   * @brief  Returns the SMARTCARD state.
995   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
996   *                the configuration information for the specified SMARTCARD module.
997   * @retval HAL state
998   */
999 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc)
1000 {
1001   return hsc->State;
1002 }
1003
1004 /**
1005   * @brief  Return the SMARTCARD error code
1006   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
1007   *                the configuration information for the specified SMARTCARD module.
1008   * @retval SMARTCARD Error Code
1009   */
1010 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc)
1011 {
1012   return hsc->ErrorCode;
1013 }
1014
1015 /**
1016   * @}
1017   */
1018   
1019 /**
1020   * @}
1021   */
1022
1023 /** @defgroup SMARTCARD_Private_Functions   SMARTCARD Private Functions
1024   *  @brief   SMARTCARD Private functions 
1025   * @{
1026   */
1027 /**
1028   * @brief  DMA SMARTCARD transmit process complete callback. 
1029   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
1030   *               the configuration information for the specified DMA module.
1031   * @retval None
1032   */
1033 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
1034 {
1035   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1036
1037   hsc->TxXferCount = 0;
1038
1039   /* Disable the DMA transfer for transmit request by setting the DMAT bit
1040      in the SMARTCARD CR3 register */
1041   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
1042
1043   /* Wait for SMARTCARD TC Flag */
1044   if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, SMARTCARD_TIMEOUT_VALUE) != HAL_OK)
1045   {
1046     /* Timeout occurred */ 
1047     hsc->State = HAL_SMARTCARD_STATE_TIMEOUT;
1048     HAL_SMARTCARD_ErrorCallback(hsc);
1049   }
1050   else
1051   {
1052     /* No Timeout */
1053     /* Check if a non-blocking receive process is ongoing or not */
1054     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX) 
1055     {
1056       hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
1057     }
1058     else
1059     {
1060       hsc->State = HAL_SMARTCARD_STATE_READY;
1061     }
1062     HAL_SMARTCARD_TxCpltCallback(hsc);
1063   }
1064 }
1065
1066 /**
1067   * @brief  DMA SMARTCARD receive process complete callback. 
1068   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
1069   *               the configuration information for the specified DMA module.
1070   * @retval None
1071   */
1072 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)   
1073 {
1074   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1075
1076   hsc->RxXferCount = 0;
1077
1078   /* Disable the DMA transfer for the receiver request by setting the DMAR bit 
1079      in the USART CR3 register */
1080   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1081
1082   /* Check if a non-blocking transmit process is ongoing or not */
1083   if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX) 
1084   {
1085     hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
1086   }
1087   else
1088   {
1089     hsc->State = HAL_SMARTCARD_STATE_READY;
1090   }
1091
1092   HAL_SMARTCARD_RxCpltCallback(hsc);
1093 }
1094
1095 /**
1096   * @brief  DMA SMARTCARD communication error callback. 
1097   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
1098   *               the configuration information for the specified DMA module.
1099   * @retval None
1100   */
1101 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)   
1102 {
1103   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1104   
1105   hsc->RxXferCount = 0;
1106   hsc->TxXferCount = 0;
1107   hsc->ErrorCode = HAL_SMARTCARD_ERROR_DMA; 
1108   hsc->State= HAL_SMARTCARD_STATE_READY;
1109   
1110   HAL_SMARTCARD_ErrorCallback(hsc);
1111 }
1112
1113 /**
1114   * @brief  This function handles SMARTCARD Communication Timeout.
1115   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
1116   *                the configuration information for the specified SMARTCARD module.
1117   * @param  Flag: specifies the SMARTCARD flag to check.
1118   * @param  Status: The new Flag status (SET or RESET).
1119   * @param  Timeout: Timeout duration
1120   * @retval HAL status
1121   */
1122 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
1123 {
1124   uint32_t tickstart = 0;
1125
1126   /* Get tick */ 
1127   tickstart = HAL_GetTick();
1128
1129   /* Wait until flag is set */
1130   if(Status == RESET)
1131   {
1132     while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) == RESET)
1133     {
1134       /* Check for the Timeout */
1135       if(Timeout != HAL_MAX_DELAY)
1136       {
1137         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1138         {
1139           /* Disable TXE and RXNE interrupts for the interrupt process */
1140           __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
1141           __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
1142
1143           hsc->State= HAL_SMARTCARD_STATE_READY;
1144
1145           /* Process Unlocked */
1146           __HAL_UNLOCK(hsc);
1147
1148           return HAL_TIMEOUT;
1149         }
1150       }
1151     }
1152   }
1153   else
1154   {
1155     while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) != RESET)
1156     {
1157       /* Check for the Timeout */
1158       if(Timeout != HAL_MAX_DELAY)
1159       {
1160         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1161         {
1162           /* Disable TXE and RXNE interrupts for the interrupt process */
1163           __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
1164           __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
1165
1166           hsc->State= HAL_SMARTCARD_STATE_READY;
1167
1168           /* Process Unlocked */
1169           __HAL_UNLOCK(hsc);
1170         
1171           return HAL_TIMEOUT;
1172         }
1173       }
1174     }
1175   }
1176   return HAL_OK;
1177 }
1178
1179 /**
1180   * @brief  Send an amount of data in non-blocking mode. 
1181   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
1182   *                the configuration information for the specified SMARTCARD module.
1183   *         Function called under interruption only, once
1184   *         interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()      
1185   * @retval HAL status
1186   */
1187 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc)
1188 {
1189   uint16_t* tmp = 0;
1190   uint32_t tmp1 = 0;
1191
1192   tmp1 = hsc->State;
1193   if((tmp1 == HAL_SMARTCARD_STATE_BUSY_TX) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_TX_RX))
1194   {
1195     if(hsc->Init.WordLength == SMARTCARD_WORDLENGTH_9B)
1196     {
1197       tmp = (uint16_t*) hsc->pTxBuffPtr;
1198       WRITE_REG(hsc->Instance->DR, (uint16_t)(*tmp & (uint16_t)0x01FF));
1199       if(hsc->Init.Parity == SMARTCARD_PARITY_NONE)
1200       {
1201         hsc->pTxBuffPtr += 2;
1202       }
1203       else
1204       {
1205         hsc->pTxBuffPtr += 1;
1206       }
1207     } 
1208     else
1209     {
1210       WRITE_REG(hsc->Instance->DR, (uint8_t)(*hsc->pTxBuffPtr++ & (uint8_t)0x00FF));
1211     }
1212     
1213     if(--hsc->TxXferCount == 0)
1214     {
1215       /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
1216       __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
1217
1218       /* Enable the SMARTCARD Transmit Complete Interrupt */    
1219       __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TC);
1220     }
1221     
1222     return HAL_OK;
1223   }
1224   else
1225   {
1226     return HAL_BUSY;
1227   }
1228 }
1229
1230
1231 /**
1232   * @brief  Wraps up transmission in non blocking mode.
1233   * @param  hsmartcard: pointer to a SMARTCARD_HandleTypeDef structure that contains
1234   *                the configuration information for the specified SMARTCARD module.
1235   * @retval HAL status
1236   */
1237 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1238 {
1239   /* Disable the SMARTCARD Transmit Complete Interrupt */    
1240   __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_TC);
1241   
1242   /* Check if a receive process is ongoing or not */
1243   if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX) 
1244   {
1245     hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_RX;
1246   }
1247   else
1248   {
1249     /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1250     __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_ERR);
1251     
1252     /* Disable the SMARTCARD Parity Error Interrupt */
1253     __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_PE);
1254         
1255     hsmartcard->State = HAL_SMARTCARD_STATE_READY;
1256   }
1257   
1258   HAL_SMARTCARD_TxCpltCallback(hsmartcard);
1259   
1260   return HAL_OK;
1261 }
1262
1263
1264 /**
1265   * @brief  Receive an amount of data in non-blocking mode. 
1266   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
1267   *                the configuration information for the specified SMARTCARD module.
1268   * @retval HAL status
1269   */
1270 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc)
1271 {
1272   uint16_t* tmp = 0;
1273   uint32_t tmp1 = 0;
1274
1275   tmp1 = hsc->State;
1276   if((tmp1 == HAL_SMARTCARD_STATE_BUSY_RX) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_TX_RX))
1277   {
1278     if(hsc->Init.WordLength == SMARTCARD_WORDLENGTH_9B)
1279     {
1280       tmp = (uint16_t*) hsc->pRxBuffPtr;
1281       if(hsc->Init.Parity == SMARTCARD_PARITY_NONE)
1282       {
1283         *tmp = (uint16_t)(hsc->Instance->DR & (uint16_t)0x01FF);
1284         hsc->pRxBuffPtr += 2;
1285       }
1286       else
1287       {
1288         *tmp = (uint16_t)(hsc->Instance->DR & (uint16_t)0x00FF);
1289         hsc->pRxBuffPtr += 1;
1290       }
1291     } 
1292     else
1293     {
1294       if(hsc->Init.Parity == SMARTCARD_PARITY_NONE)
1295       {
1296         *hsc->pRxBuffPtr++ = (uint8_t)(hsc->Instance->DR & (uint8_t)0x00FF);
1297       }
1298       else
1299       {
1300         *hsc->pRxBuffPtr++ = (uint8_t)(hsc->Instance->DR & (uint8_t)0x007F);
1301       }
1302     }
1303     
1304     if(--hsc->RxXferCount == 0)
1305     {
1306       __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
1307       
1308       /* Disable the SMARTCARD Parity Error Interrupt */
1309       __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_PE);
1310
1311       /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1312       __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
1313
1314       /* Check if a non-blocking transmit process is ongoing or not */
1315       if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX) 
1316       {
1317         hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
1318       }
1319       else
1320       {
1321         hsc->State = HAL_SMARTCARD_STATE_READY;
1322       }
1323
1324       HAL_SMARTCARD_RxCpltCallback(hsc);
1325
1326       return HAL_OK;
1327     }
1328     return HAL_OK;
1329   }
1330   else
1331   {
1332     return HAL_BUSY; 
1333   }
1334 }
1335
1336 /**
1337   * @brief  Configures the SMARTCARD peripheral. 
1338   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
1339   *                the configuration information for the specified SMARTCARD module.
1340   * @retval None
1341   */
1342 static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc)
1343 {
1344   /* Check the parameters */
1345   assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
1346   assert_param(IS_SMARTCARD_POLARITY(hsc->Init.CLKPolarity));
1347   assert_param(IS_SMARTCARD_PHASE(hsc->Init.CLKPhase));
1348   assert_param(IS_SMARTCARD_LASTBIT(hsc->Init.CLKLastBit));
1349   assert_param(IS_SMARTCARD_BAUDRATE(hsc->Init.BaudRate));  
1350   assert_param(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength));
1351   assert_param(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits));
1352   assert_param(IS_SMARTCARD_PARITY(hsc->Init.Parity));
1353   assert_param(IS_SMARTCARD_MODE(hsc->Init.Mode));
1354   assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
1355
1356   /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
1357      receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
1358   CLEAR_BIT(hsc->Instance->CR1, (uint32_t)(USART_CR1_TE | USART_CR1_RE));
1359   
1360   /*-------------------------- SMARTCARD CR2 Configuration ------------------------*/
1361   /* Clear CLKEN, CPOL, CPHA and LBCL bits */
1362   /* Configure the SMARTCARD Clock, CPOL, CPHA and LastBit -----------------------*/
1363   /* Set CPOL bit according to hsc->Init.CLKPolarity value */
1364   /* Set CPHA bit according to hsc->Init.CLKPhase value */
1365   /* Set LBCL bit according to hsc->Init.CLKLastBit value */
1366   MODIFY_REG(hsc->Instance->CR2, 
1367              ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL)),
1368              ((uint32_t)(USART_CR2_CLKEN | hsc->Init.CLKPolarity | hsc->Init.CLKPhase| hsc->Init.CLKLastBit)) );
1369   
1370   /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
1371   MODIFY_REG(hsc->Instance->CR2, USART_CR2_STOP,(uint32_t)(hsc->Init.StopBits));
1372
1373   /*-------------------------- SMARTCARD CR1 Configuration -----------------------*/
1374   /* Clear M, PCE, PS, TE and RE bits */
1375   /* Configure the SMARTCARD Word Length, Parity and mode: 
1376      Set the M bits according to hsc->Init.WordLength value 
1377      Set PCE and PS bits according to hsc->Init.Parity value
1378      Set TE and RE bits according to hsc->Init.Mode value */
1379   MODIFY_REG(hsc->Instance->CR1, 
1380              ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE)),
1381              ((uint32_t)(hsc->Init.WordLength | hsc->Init.Parity | hsc->Init.Mode)) );
1382
1383   /*-------------------------- USART CR3 Configuration -----------------------*/  
1384   /* Clear CTSE and RTSE bits */
1385   CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
1386
1387   /*-------------------------- USART BRR Configuration -----------------------*/
1388   if(hsc->Instance == USART1)
1389   {
1390     hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK2Freq(), hsc->Init.BaudRate);
1391   }
1392   else
1393   {
1394     hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK1Freq(), hsc->Init.BaudRate);
1395   }
1396 }
1397
1398 /**
1399   * @}
1400   */
1401
1402 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
1403 /**
1404   * @}
1405   */
1406
1407 /**
1408   * @}
1409   */
1410
1411 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/