1 /**
2   ******************************************************************************
3   * @file    stm32f2xx_hal_smartcard.h
4   * @author  MCD Application Team
5   * @brief   Header file of SMARTCARD HAL module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2016 STMicroelectronics.
10   * All rights reserved.
11   *
12   * This software is licensed under terms that can be found in the LICENSE file
13   * in the root directory of this software component.
14   * If no LICENSE file comes with this software, it is provided AS-IS.
15   *
16   ******************************************************************************
17   */
18 
19 /* Define to prevent recursive inclusion -------------------------------------*/
20 #ifndef __STM32F2xx_HAL_SMARTCARD_H
21 #define __STM32F2xx_HAL_SMARTCARD_H
22 
23 #ifdef __cplusplus
24  extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32f2xx_hal_def.h"
29 
30 /** @addtogroup STM32F2xx_HAL_Driver
31   * @{
32   */
33 
34 /** @addtogroup SMARTCARD
35   * @{
36   */
37 
38 /* Exported types ------------------------------------------------------------*/
39 /** @defgroup SMARTCARD_Exported_Types SMARTCARD Exported Types
40   * @{
41   */
42 
43 /**
44   * @brief SMARTCARD Init Structure definition
45   */
46 typedef struct
47 {
48   uint32_t BaudRate;                  /*!< This member configures the SmartCard communication baud rate.
49                                            The baud rate is computed using the following formula:
50                                            - IntegerDivider = ((PCLKx) / (16 * (hsc->Init.BaudRate)))
51                                            - FractionalDivider = ((IntegerDivider - ((uint32_t) IntegerDivider)) * 16) + 0.5 */
52 
53   uint32_t WordLength;                /*!< Specifies the number of data bits transmitted or received in a frame.
54                                            This parameter can be a value of @ref SMARTCARD_Word_Length */
55 
56   uint32_t StopBits;                  /*!< Specifies the number of stop bits transmitted.
57                                            This parameter can be a value of @ref SMARTCARD_Stop_Bits */
58 
59   uint32_t Parity;                    /*!< Specifies the parity mode.
60                                            This parameter can be a value of @ref SMARTCARD_Parity
61                                            @note When parity is enabled, the computed parity is inserted
62                                                  at the MSB position of the transmitted data (9th bit when
63                                                  the word length is set to 9 data bits; 8th bit when the
64                                                  word length is set to 8 data bits).*/
65 
66   uint32_t Mode;                      /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
67                                            This parameter can be a value of @ref SMARTCARD_Mode */
68 
69   uint32_t CLKPolarity;               /*!< Specifies the steady state of the serial clock.
70                                            This parameter can be a value of @ref SMARTCARD_Clock_Polarity */
71 
72   uint32_t CLKPhase;                  /*!< Specifies the clock transition on which the bit capture is made.
73                                            This parameter can be a value of @ref SMARTCARD_Clock_Phase */
74 
75   uint32_t CLKLastBit;                /*!< Specifies whether the clock pulse corresponding to the last transmitted
76                                            data bit (MSB) has to be output on the SCLK pin in synchronous mode.
77                                            This parameter can be a value of @ref SMARTCARD_Last_Bit */
78 
79   uint32_t Prescaler;                 /*!< Specifies the SmartCard Prescaler value used for dividing the system clock
80                                            to provide the smartcard clock. The value given in the register (5 significant bits)
81                                            is multiplied by 2 to give the division factor of the source clock frequency.
82                                            This parameter can be a value of @ref SMARTCARD_Prescaler */
83 
84   uint32_t GuardTime;                 /*!< Specifies the SmartCard Guard Time value in terms of number of baud clocks */
85 
86   uint32_t NACKState;                 /*!< Specifies the SmartCard NACK Transmission state.
87                                            This parameter can be a value of @ref SMARTCARD_NACK_State */
88 }SMARTCARD_InitTypeDef;
89 
90 /**
91   * @brief HAL SMARTCARD State structures definition
92   * @note  HAL SMARTCARD State value is a combination of 2 different substates: gState and RxState.
93   *        - gState contains SMARTCARD state information related to global Handle management
94   *          and also information related to Tx operations.
95   *          gState value coding follow below described bitmap :
96   *          b7-b6  Error information
97   *             00 : No Error
98   *             01 : (Not Used)
99   *             10 : Timeout
100   *             11 : Error
101   *          b5     IP initialization status
102   *             0  : Reset (IP not initialized)
103   *             1  : Init done (IP initialized. HAL SMARTCARD Init function already called)
104   *          b4-b3  (not used)
105   *             xx : Should be set to 00
106   *          b2     Intrinsic process state
107   *             0  : Ready
108   *             1  : Busy (IP busy with some configuration or internal operations)
109   *          b1     (not used)
110   *             x  : Should be set to 0
111   *          b0     Tx state
112   *             0  : Ready (no Tx operation ongoing)
113   *             1  : Busy (Tx operation ongoing)
114   *        - RxState contains information related to Rx operations.
115   *          RxState value coding follow below described bitmap :
116   *          b7-b6  (not used)
117   *             xx : Should be set to 00
118   *          b5     IP initialization status
119   *             0  : Reset (IP not initialized)
120   *             1  : Init done (IP initialized)
121   *          b4-b2  (not used)
122   *            xxx : Should be set to 000
123   *          b1     Rx state
124   *             0  : Ready (no Rx operation ongoing)
125   *             1  : Busy (Rx operation ongoing)
126   *          b0     (not used)
127   *             x  : Should be set to 0.
128   */
129 typedef enum
130 {
131   HAL_SMARTCARD_STATE_RESET             = 0x00U,    /*!< Peripheral is not yet Initialized
132                                                         Value is allowed for gState and RxState */
133   HAL_SMARTCARD_STATE_READY             = 0x20U,    /*!< Peripheral Initialized and ready for use
134                                                         Value is allowed for gState and RxState */
135   HAL_SMARTCARD_STATE_BUSY              = 0x24U,    /*!< an internal process is ongoing
136                                                         Value is allowed for gState only */
137   HAL_SMARTCARD_STATE_BUSY_TX           = 0x21U,    /*!< Data Transmission process is ongoing
138                                                         Value is allowed for gState only */
139   HAL_SMARTCARD_STATE_BUSY_RX           = 0x22U,    /*!< Data Reception process is ongoing
140                                                         Value is allowed for RxState only */
141   HAL_SMARTCARD_STATE_BUSY_TX_RX        = 0x23U,    /*!< Data Transmission and Reception process is ongoing
142                                                         Not to be used for neither gState nor RxState.
143                                                         Value is result of combination (Or) between gState and RxState values */
144   HAL_SMARTCARD_STATE_TIMEOUT           = 0xA0U,    /*!< Timeout state
145                                                         Value is allowed for gState only */
146   HAL_SMARTCARD_STATE_ERROR             = 0xE0U     /*!< Error
147                                                         Value is allowed for gState only */
148 }HAL_SMARTCARD_StateTypeDef;
149 
150 /**
151   * @brief  SMARTCARD handle Structure definition
152   */
153 typedef struct __SMARTCARD_HandleTypeDef
154 {
155   USART_TypeDef                    *Instance;        /*!< USART registers base address */
156 
157   SMARTCARD_InitTypeDef            Init;             /*!< SmartCard communication parameters */
158 
159   const uint8_t                    *pTxBuffPtr;      /*!< Pointer to SmartCard Tx transfer Buffer */
160 
161   uint16_t                         TxXferSize;       /*!< SmartCard Tx Transfer size */
162 
163   __IO uint16_t                    TxXferCount;      /*!< SmartCard Tx Transfer Counter */
164 
165   uint8_t                          *pRxBuffPtr;      /*!< Pointer to SmartCard Rx transfer Buffer */
166 
167   uint16_t                         RxXferSize;       /*!< SmartCard Rx Transfer size */
168 
169   __IO uint16_t                    RxXferCount;      /*!< SmartCard Rx Transfer Counter */
170 
171   DMA_HandleTypeDef                *hdmatx;          /*!< SmartCard Tx DMA Handle parameters */
172 
173   DMA_HandleTypeDef                *hdmarx;          /*!< SmartCard Rx DMA Handle parameters */
174 
175   HAL_LockTypeDef                  Lock;             /*!< Locking object */
176 
177   __IO HAL_SMARTCARD_StateTypeDef  gState;           /*!< SmartCard state information related to global Handle management
178                                                         and also related to Tx operations.
179                                                         This parameter can be a value of @ref HAL_SMARTCARD_StateTypeDef */
180 
181   __IO HAL_SMARTCARD_StateTypeDef  RxState;          /*!< SmartCard state information related to Rx operations.
182                                                         This parameter can be a value of @ref HAL_SMARTCARD_StateTypeDef */
183 
184   __IO uint32_t                    ErrorCode;        /*!< SmartCard Error code */
185 
186 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
187   void (* TxCpltCallback)(struct __SMARTCARD_HandleTypeDef *hsc);            /*!< SMARTCARD Tx Complete Callback             */
188 
189   void (* RxCpltCallback)(struct __SMARTCARD_HandleTypeDef *hsc);            /*!< SMARTCARD Rx Complete Callback             */
190 
191   void (* ErrorCallback)(struct __SMARTCARD_HandleTypeDef *hsc);             /*!< SMARTCARD Error Callback                   */
192 
193   void (* AbortCpltCallback)(struct __SMARTCARD_HandleTypeDef *hsc);         /*!< SMARTCARD Abort Complete Callback          */
194 
195   void (* AbortTransmitCpltCallback)(struct __SMARTCARD_HandleTypeDef *hsc); /*!< SMARTCARD Abort Transmit Complete Callback */
196 
197   void (* AbortReceiveCpltCallback)(struct __SMARTCARD_HandleTypeDef *hsc);  /*!< SMARTCARD Abort Receive Complete Callback  */
198 
199   void (* MspInitCallback)(struct __SMARTCARD_HandleTypeDef *hsc);           /*!< SMARTCARD Msp Init callback                */
200 
201   void (* MspDeInitCallback)(struct __SMARTCARD_HandleTypeDef *hsc);         /*!< SMARTCARD Msp DeInit callback              */
202 #endif  /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
203 
204 } SMARTCARD_HandleTypeDef;
205 
206 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
207 /**
208   * @brief  HAL SMARTCARD Callback ID enumeration definition
209   */
210 typedef enum
211 {
212   HAL_SMARTCARD_TX_COMPLETE_CB_ID             = 0x00U,    /*!< SMARTCARD Tx Complete Callback ID             */
213   HAL_SMARTCARD_RX_COMPLETE_CB_ID             = 0x01U,    /*!< SMARTCARD Rx Complete Callback ID             */
214   HAL_SMARTCARD_ERROR_CB_ID                   = 0x02U,    /*!< SMARTCARD Error Callback ID                   */
215   HAL_SMARTCARD_ABORT_COMPLETE_CB_ID          = 0x03U,    /*!< SMARTCARD Abort Complete Callback ID          */
216   HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID = 0x04U,    /*!< SMARTCARD Abort Transmit Complete Callback ID */
217   HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID  = 0x05U,    /*!< SMARTCARD Abort Receive Complete Callback ID  */
218 
219   HAL_SMARTCARD_MSPINIT_CB_ID                 = 0x08U,    /*!< SMARTCARD MspInit callback ID                 */
220   HAL_SMARTCARD_MSPDEINIT_CB_ID               = 0x09U     /*!< SMARTCARD MspDeInit callback ID               */
221 
222 } HAL_SMARTCARD_CallbackIDTypeDef;
223 
224 /**
225   * @brief  HAL SMARTCARD Callback pointer definition
226   */
227 typedef  void (*pSMARTCARD_CallbackTypeDef)(SMARTCARD_HandleTypeDef *hsc);  /*!< pointer to an SMARTCARD callback function */
228 
229 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
230 
231 /**
232   * @}
233   */
234 
235 /* Exported constants --------------------------------------------------------*/
236 /** @defgroup SMARTCARD_Exported_Constants  SMARTCARD Exported constants
237   * @{
238   */
239 
240 /** @defgroup SMARTCARD_Error_Code SMARTCARD Error Code
241   * @{
242   */
243 #define HAL_SMARTCARD_ERROR_NONE             0x00000000U   /*!< No error            */
244 #define HAL_SMARTCARD_ERROR_PE               0x00000001U   /*!< Parity error        */
245 #define HAL_SMARTCARD_ERROR_NE               0x00000002U   /*!< Noise error         */
246 #define HAL_SMARTCARD_ERROR_FE               0x00000004U   /*!< Frame error         */
247 #define HAL_SMARTCARD_ERROR_ORE              0x00000008U   /*!< Overrun error       */
248 #define HAL_SMARTCARD_ERROR_DMA              0x00000010U   /*!< DMA transfer error  */
249 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
250 #define HAL_SMARTCARD_ERROR_INVALID_CALLBACK 0x00000020U   /*!< Invalid Callback error  */
251 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
252 /**
253   * @}
254   */
255 
256 /** @defgroup SMARTCARD_Word_Length SMARTCARD Word Length
257   * @{
258   */
259 #define SMARTCARD_WORDLENGTH_9B             ((uint32_t)USART_CR1_M)
260 /**
261   * @}
262   */
263 
264 /** @defgroup SMARTCARD_Stop_Bits SMARTCARD Number of Stop Bits
265   * @{
266   */
267 #define SMARTCARD_STOPBITS_0_5              ((uint32_t)USART_CR2_STOP_0)
268 #define SMARTCARD_STOPBITS_1_5              ((uint32_t)(USART_CR2_STOP_0 | USART_CR2_STOP_1))
269 /**
270   * @}
271   */
272 
273 /** @defgroup SMARTCARD_Parity SMARTCARD Parity
274   * @{
275   */
276 #define SMARTCARD_PARITY_EVEN               ((uint32_t)USART_CR1_PCE)
277 #define SMARTCARD_PARITY_ODD                ((uint32_t)(USART_CR1_PCE | USART_CR1_PS))
278 /**
279   * @}
280   */
281 
282 /** @defgroup SMARTCARD_Mode SMARTCARD Mode
283   * @{
284   */
285 #define SMARTCARD_MODE_RX                   ((uint32_t)USART_CR1_RE)
286 #define SMARTCARD_MODE_TX                   ((uint32_t)USART_CR1_TE)
287 #define SMARTCARD_MODE_TX_RX                ((uint32_t)(USART_CR1_TE |USART_CR1_RE))
288 /**
289   * @}
290   */
291 
292 /** @defgroup SMARTCARD_Clock_Polarity SMARTCARD Clock Polarity
293   * @{
294   */
295 #define SMARTCARD_POLARITY_LOW              0x00000000U
296 #define SMARTCARD_POLARITY_HIGH             ((uint32_t)USART_CR2_CPOL)
297 /**
298   * @}
299   */
300 
301 /** @defgroup SMARTCARD_Clock_Phase  SMARTCARD Clock Phase
302   * @{
303   */
304 #define SMARTCARD_PHASE_1EDGE               0x00000000U
305 #define SMARTCARD_PHASE_2EDGE               ((uint32_t)USART_CR2_CPHA)
306 /**
307   * @}
308   */
309 
310 /** @defgroup SMARTCARD_Last_Bit  SMARTCARD Last Bit
311   * @{
312   */
313 #define SMARTCARD_LASTBIT_DISABLE           0x00000000U
314 #define SMARTCARD_LASTBIT_ENABLE            ((uint32_t)USART_CR2_LBCL)
315 /**
316   * @}
317   */
318 
319 /** @defgroup SMARTCARD_NACK_State  SMARTCARD NACK State
320   * @{
321   */
322 #define SMARTCARD_NACK_ENABLE               ((uint32_t)USART_CR3_NACK)
323 #define SMARTCARD_NACK_DISABLE              0x00000000U
324 /**
325   * @}
326   */
327 
328 /** @defgroup SMARTCARD_DMA_Requests   SMARTCARD DMA requests
329   * @{
330   */
331 #define SMARTCARD_DMAREQ_TX                 ((uint32_t)USART_CR3_DMAT)
332 #define SMARTCARD_DMAREQ_RX                 ((uint32_t)USART_CR3_DMAR)
333 /**
334   * @}
335   */
336 
337 /** @defgroup SMARTCARD_Prescaler SMARTCARD Prescaler
338   * @{
339   */
340 #define SMARTCARD_PRESCALER_SYSCLK_DIV2     0x00000001U          /*!< SYSCLK divided by 2 */
341 #define SMARTCARD_PRESCALER_SYSCLK_DIV4     0x00000002U          /*!< SYSCLK divided by 4 */
342 #define SMARTCARD_PRESCALER_SYSCLK_DIV6     0x00000003U          /*!< SYSCLK divided by 6 */
343 #define SMARTCARD_PRESCALER_SYSCLK_DIV8     0x00000004U          /*!< SYSCLK divided by 8 */
344 #define SMARTCARD_PRESCALER_SYSCLK_DIV10    0x00000005U          /*!< SYSCLK divided by 10 */
345 #define SMARTCARD_PRESCALER_SYSCLK_DIV12    0x00000006U          /*!< SYSCLK divided by 12 */
346 #define SMARTCARD_PRESCALER_SYSCLK_DIV14    0x00000007U          /*!< SYSCLK divided by 14 */
347 #define SMARTCARD_PRESCALER_SYSCLK_DIV16    0x00000008U          /*!< SYSCLK divided by 16 */
348 #define SMARTCARD_PRESCALER_SYSCLK_DIV18    0x00000009U          /*!< SYSCLK divided by 18 */
349 #define SMARTCARD_PRESCALER_SYSCLK_DIV20    0x0000000AU          /*!< SYSCLK divided by 20 */
350 #define SMARTCARD_PRESCALER_SYSCLK_DIV22    0x0000000BU          /*!< SYSCLK divided by 22 */
351 #define SMARTCARD_PRESCALER_SYSCLK_DIV24    0x0000000CU          /*!< SYSCLK divided by 24 */
352 #define SMARTCARD_PRESCALER_SYSCLK_DIV26    0x0000000DU          /*!< SYSCLK divided by 26 */
353 #define SMARTCARD_PRESCALER_SYSCLK_DIV28    0x0000000EU          /*!< SYSCLK divided by 28 */
354 #define SMARTCARD_PRESCALER_SYSCLK_DIV30    0x0000000FU          /*!< SYSCLK divided by 30 */
355 #define SMARTCARD_PRESCALER_SYSCLK_DIV32    0x00000010U          /*!< SYSCLK divided by 32 */
356 #define SMARTCARD_PRESCALER_SYSCLK_DIV34    0x00000011U          /*!< SYSCLK divided by 34 */
357 #define SMARTCARD_PRESCALER_SYSCLK_DIV36    0x00000012U          /*!< SYSCLK divided by 36 */
358 #define SMARTCARD_PRESCALER_SYSCLK_DIV38    0x00000013U          /*!< SYSCLK divided by 38 */
359 #define SMARTCARD_PRESCALER_SYSCLK_DIV40    0x00000014U          /*!< SYSCLK divided by 40 */
360 #define SMARTCARD_PRESCALER_SYSCLK_DIV42    0x00000015U          /*!< SYSCLK divided by 42 */
361 #define SMARTCARD_PRESCALER_SYSCLK_DIV44    0x00000016U          /*!< SYSCLK divided by 44 */
362 #define SMARTCARD_PRESCALER_SYSCLK_DIV46    0x00000017U          /*!< SYSCLK divided by 46 */
363 #define SMARTCARD_PRESCALER_SYSCLK_DIV48    0x00000018U          /*!< SYSCLK divided by 48 */
364 #define SMARTCARD_PRESCALER_SYSCLK_DIV50    0x00000019U          /*!< SYSCLK divided by 50 */
365 #define SMARTCARD_PRESCALER_SYSCLK_DIV52    0x0000001AU          /*!< SYSCLK divided by 52 */
366 #define SMARTCARD_PRESCALER_SYSCLK_DIV54    0x0000001BU          /*!< SYSCLK divided by 54 */
367 #define SMARTCARD_PRESCALER_SYSCLK_DIV56    0x0000001CU          /*!< SYSCLK divided by 56 */
368 #define SMARTCARD_PRESCALER_SYSCLK_DIV58    0x0000001DU          /*!< SYSCLK divided by 58 */
369 #define SMARTCARD_PRESCALER_SYSCLK_DIV60    0x0000001EU          /*!< SYSCLK divided by 60 */
370 #define SMARTCARD_PRESCALER_SYSCLK_DIV62    0x0000001FU          /*!< SYSCLK divided by 62 */
371 /**
372   * @}
373   */
374 
375 /** @defgroup SmartCard_Flags SMARTCARD Flags
376   *        Elements values convention: 0xXXXX
377   *           - 0xXXXX  : Flag mask in the SR register
378   * @{
379   */
380 #define SMARTCARD_FLAG_TXE                  ((uint32_t)USART_SR_TXE)
381 #define SMARTCARD_FLAG_TC                   ((uint32_t)USART_SR_TC)
382 #define SMARTCARD_FLAG_RXNE                 ((uint32_t)USART_SR_RXNE)
383 #define SMARTCARD_FLAG_IDLE                 ((uint32_t)USART_SR_IDLE)
384 #define SMARTCARD_FLAG_ORE                  ((uint32_t)USART_SR_ORE)
385 #define SMARTCARD_FLAG_NE                   ((uint32_t)USART_SR_NE)
386 #define SMARTCARD_FLAG_FE                   ((uint32_t)USART_SR_FE)
387 #define SMARTCARD_FLAG_PE                   ((uint32_t)USART_SR_PE)
388 /**
389   * @}
390   */
391 
392 /** @defgroup SmartCard_Interrupt_definition SMARTCARD Interrupts Definition
393   *        Elements values convention: 0xY000XXXX
394   *           - XXXX  : Interrupt mask in the Y register
395   *           - Y  : Interrupt source register (2bits)
396   *                 - 01: CR1 register
397   *                 - 11: CR3 register
398   * @{
399   */
400 #define SMARTCARD_IT_PE                     ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28U | USART_CR1_PEIE))
401 #define SMARTCARD_IT_TXE                    ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28U | USART_CR1_TXEIE))
402 #define SMARTCARD_IT_TC                     ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28U | USART_CR1_TCIE))
403 #define SMARTCARD_IT_RXNE                   ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28U | USART_CR1_RXNEIE))
404 #define SMARTCARD_IT_IDLE                   ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28U | USART_CR1_IDLEIE))
405 #define SMARTCARD_IT_ERR                    ((uint32_t)(SMARTCARD_CR3_REG_INDEX << 28U | USART_CR3_EIE))
406 /**
407   * @}
408   */
409 
410 /**
411   * @}
412   */
413 
414 /* Exported macro ------------------------------------------------------------*/
415 /** @defgroup SMARTCARD_Exported_Macros SMARTCARD Exported Macros
416   * @{
417   */
418 
419 /** @brief Reset SMARTCARD handle gstate & RxState
420   * @param  __HANDLE__ specifies the SMARTCARD Handle.
421   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
422   * @retval None
423   */
424 #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
425 #define __HAL_SMARTCARD_RESET_HANDLE_STATE(__HANDLE__)  do{                                                       \
426                                                            (__HANDLE__)->gState = HAL_SMARTCARD_STATE_RESET;      \
427                                                            (__HANDLE__)->RxState = HAL_SMARTCARD_STATE_RESET;     \
428                                                            (__HANDLE__)->MspInitCallback = NULL;                  \
429                                                            (__HANDLE__)->MspDeInitCallback = NULL;                \
430                                                           } while(0U)
431 #else
432 #define __HAL_SMARTCARD_RESET_HANDLE_STATE(__HANDLE__)  do{                                                       \
433                                                            (__HANDLE__)->gState = HAL_SMARTCARD_STATE_RESET;      \
434                                                            (__HANDLE__)->RxState = HAL_SMARTCARD_STATE_RESET;     \
435                                                           } while(0U)
436 #endif /*USE_HAL_SMARTCARD_REGISTER_CALLBACKS  */
437 
438 /** @brief  Flush the Smartcard DR register
439   * @param  __HANDLE__ specifies the SMARTCARD Handle.
440   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
441   * @retval None
442   */
443 #define __HAL_SMARTCARD_FLUSH_DRREGISTER(__HANDLE__) ((__HANDLE__)->Instance->DR)
444 
445 /** @brief  Check whether the specified Smartcard flag is set or not.
446   * @param  __HANDLE__ specifies the SMARTCARD Handle.
447   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
448   * @param  __FLAG__ specifies the flag to check.
449   *        This parameter can be one of the following values:
450   *            @arg SMARTCARD_FLAG_TXE:  Transmit data register empty flag
451   *            @arg SMARTCARD_FLAG_TC:   Transmission Complete flag
452   *            @arg SMARTCARD_FLAG_RXNE: Receive data register not empty flag
453   *            @arg SMARTCARD_FLAG_IDLE: Idle Line detection flag
454   *            @arg SMARTCARD_FLAG_ORE:  Overrun Error flag
455   *            @arg SMARTCARD_FLAG_NE:   Noise Error flag
456   *            @arg SMARTCARD_FLAG_FE:   Framing Error flag
457   *            @arg SMARTCARD_FLAG_PE:   Parity Error flag
458   * @retval The new state of __FLAG__ (TRUE or FALSE).
459   */
460 #define __HAL_SMARTCARD_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__))
461 
462 /** @brief  Clear the specified Smartcard pending flags.
463   * @param  __HANDLE__ specifies the SMARTCARD Handle.
464   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
465   * @param  __FLAG__ specifies the flag to check.
466   *          This parameter can be any combination of the following values:
467   *            @arg SMARTCARD_FLAG_TC:   Transmission Complete flag.
468   *            @arg SMARTCARD_FLAG_RXNE: Receive data register not empty flag.
469   *
470   * @note   PE (Parity error), FE (Framing error), NE (Noise error) and ORE (Overrun
471   *          error) flags are cleared by software sequence: a read operation to
472   *          USART_SR register followed by a read operation to USART_DR register.
473   * @note   RXNE flag can be also cleared by a read to the USART_DR register.
474   * @note   TC flag can be also cleared by software sequence: a read operation to
475   *          USART_SR register followed by a write operation to USART_DR register.
476   * @note   TXE flag is cleared only by a write to the USART_DR register.
477   * @retval None
478   */
479 #define __HAL_SMARTCARD_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->SR = ~(__FLAG__))
480 
481 /** @brief  Clear the SMARTCARD PE pending flag.
482   * @param  __HANDLE__ specifies the USART Handle.
483   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
484   * @retval None
485   */
486 #define __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__)  \
487     do{                                           \
488       __IO uint32_t tmpreg = 0x00U;               \
489       tmpreg = (__HANDLE__)->Instance->SR;        \
490       tmpreg = (__HANDLE__)->Instance->DR;        \
491       UNUSED(tmpreg);                             \
492     } while(0U)
493 
494 /** @brief  Clear the SMARTCARD FE pending flag.
495   * @param  __HANDLE__ specifies the USART Handle.
496   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
497   * @retval None
498   */
499 #define __HAL_SMARTCARD_CLEAR_FEFLAG(__HANDLE__) __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__)
500 
501 /** @brief  Clear the SMARTCARD NE pending flag.
502   * @param  __HANDLE__ specifies the USART Handle.
503   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
504   * @retval None
505   */
506 #define __HAL_SMARTCARD_CLEAR_NEFLAG(__HANDLE__) __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__)
507 
508 /** @brief  Clear the SMARTCARD ORE pending flag.
509   * @param  __HANDLE__ specifies the USART Handle.
510   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
511   * @retval None
512   */
513 #define __HAL_SMARTCARD_CLEAR_OREFLAG(__HANDLE__) __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__)
514 
515 /** @brief  Clear the SMARTCARD IDLE pending flag.
516   * @param  __HANDLE__ specifies the USART Handle.
517   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
518   * @retval None
519   */
520 #define __HAL_SMARTCARD_CLEAR_IDLEFLAG(__HANDLE__) __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__)
521 
522 /** @brief  Enable the specified SmartCard interrupt.
523   * @param  __HANDLE__ specifies the SMARTCARD Handle.
524   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
525   * @param  __INTERRUPT__ specifies the SMARTCARD interrupt to enable.
526   *          This parameter can be one of the following values:
527   *            @arg SMARTCARD_IT_TXE:  Transmit Data Register empty interrupt
528   *            @arg SMARTCARD_IT_TC:   Transmission complete interrupt
529   *            @arg SMARTCARD_IT_RXNE: Receive Data register not empty interrupt
530   *            @arg SMARTCARD_IT_IDLE: Idle line detection interrupt
531   *            @arg SMARTCARD_IT_PE:   Parity Error interrupt
532   *            @arg SMARTCARD_IT_ERR:  Error interrupt(Frame error, noise error, overrun error)
533   * @retval None
534   */
535 #define __HAL_SMARTCARD_ENABLE_IT(__HANDLE__, __INTERRUPT__)   ((((__INTERRUPT__) >> 28U) == SMARTCARD_CR1_REG_INDEX)? ((__HANDLE__)->Instance->CR1 |= ((__INTERRUPT__) & SMARTCARD_IT_MASK)): \
536                                                                  ((__HANDLE__)->Instance->CR3 |= ((__INTERRUPT__) & SMARTCARD_IT_MASK)))
537 
538 /** @brief  Disable the specified SmartCard interrupt.
539   * @param  __HANDLE__ specifies the SMARTCARD Handle.
540   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
541   * @param  __INTERRUPT__ specifies the SMARTCARD interrupt to disable.
542   *          This parameter can be one of the following values:
543   *            @arg SMARTCARD_IT_TXE:  Transmit Data Register empty interrupt
544   *            @arg SMARTCARD_IT_TC:   Transmission complete interrupt
545   *            @arg SMARTCARD_IT_RXNE: Receive Data register not empty interrupt
546   *            @arg SMARTCARD_IT_IDLE: Idle line detection interrupt
547   *            @arg SMARTCARD_IT_PE:   Parity Error interrupt
548   *            @arg SMARTCARD_IT_ERR:  Error interrupt(Frame error, noise error, overrun error)
549   * @retval None
550   */
551 #define __HAL_SMARTCARD_DISABLE_IT(__HANDLE__, __INTERRUPT__)  ((((__INTERRUPT__) >> 28U) == SMARTCARD_CR1_REG_INDEX)? ((__HANDLE__)->Instance->CR1 &= ~((__INTERRUPT__) & SMARTCARD_IT_MASK)): \
552                                                                  ((__HANDLE__)->Instance->CR3 &= ~ ((__INTERRUPT__) & SMARTCARD_IT_MASK)))
553 
554 /** @brief  Checks whether the specified SmartCard interrupt has occurred or not.
555   * @param  __HANDLE__ specifies the SmartCard Handle.
556   * @param  __IT__ specifies the SMARTCARD interrupt source to check.
557   *          This parameter can be one of the following values:
558   *            @arg SMARTCARD_IT_TXE: Transmit Data Register empty interrupt
559   *            @arg SMARTCARD_IT_TC:  Transmission complete interrupt
560   *            @arg SMARTCARD_IT_RXNE: Receive Data register not empty interrupt
561   *            @arg SMARTCARD_IT_IDLE: Idle line detection interrupt
562   *            @arg SMARTCARD_IT_ERR: Error interrupt
563   *            @arg SMARTCARD_IT_PE: Parity Error interrupt
564   * @retval The new state of __IT__ (TRUE or FALSE).
565   */
566 #define __HAL_SMARTCARD_GET_IT_SOURCE(__HANDLE__, __IT__) (((((__IT__) >> 28U) == SMARTCARD_CR1_REG_INDEX)? (__HANDLE__)->Instance->CR1: (__HANDLE__)->Instance->CR3) & (((uint32_t)(__IT__)) & SMARTCARD_IT_MASK))
567 
568 /** @brief  Macro to enable the SMARTCARD's one bit sample method
569   * @param  __HANDLE__ specifies the SMARTCARD Handle.
570   * @retval None
571   */
572 #define __HAL_SMARTCARD_ONE_BIT_SAMPLE_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3|= USART_CR3_ONEBIT)
573 
574 /** @brief  Macro to disable the SMARTCARD's one bit sample method
575   * @param  __HANDLE__ specifies the SMARTCARD Handle.
576   * @retval None
577   */
578 #define __HAL_SMARTCARD_ONE_BIT_SAMPLE_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_ONEBIT))
579 
580 /** @brief  Enable the USART associated to the SMARTCARD Handle
581   * @param  __HANDLE__ specifies the SMARTCARD Handle.
582   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
583   * @retval None
584   */
585 #define __HAL_SMARTCARD_ENABLE(__HANDLE__)  ((__HANDLE__)->Instance->CR1 |=  USART_CR1_UE)
586 
587 /** @brief  Disable the USART associated to the SMARTCARD Handle
588   * @param  __HANDLE__ specifies the SMARTCARD Handle.
589   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
590   * @retval None
591   */
592 #define __HAL_SMARTCARD_DISABLE(__HANDLE__)  ((__HANDLE__)->Instance->CR1 &=  ~USART_CR1_UE)
593 
594 /** @brief  Macros to enable the SmartCard DMA request.
595   * @param  __HANDLE__ specifies the SmartCard Handle.
596   * @param  __REQUEST__ specifies the SmartCard DMA request.
597   *          This parameter can be one of the following values:
598   *            @arg SMARTCARD_DMAREQ_TX: SmartCard DMA transmit request
599   *            @arg SMARTCARD_DMAREQ_RX: SmartCard DMA receive request
600   * @retval None
601   */
602 #define __HAL_SMARTCARD_DMA_REQUEST_ENABLE(__HANDLE__, __REQUEST__)    ((__HANDLE__)->Instance->CR3 |=  (__REQUEST__))
603 
604 /** @brief  Macros to disable the SmartCard DMA request.
605   * @param  __HANDLE__ specifies the SmartCard Handle.
606   * @param  __REQUEST__ specifies the SmartCard DMA request.
607   *          This parameter can be one of the following values:
608   *            @arg SMARTCARD_DMAREQ_TX: SmartCard DMA transmit request
609   *            @arg SMARTCARD_DMAREQ_RX: SmartCard DMA receive request
610   * @retval None
611   */
612 #define __HAL_SMARTCARD_DMA_REQUEST_DISABLE(__HANDLE__, __REQUEST__)   ((__HANDLE__)->Instance->CR3 &=  ~(__REQUEST__))
613 
614 /**
615   * @}
616   */
617 
618 /* Exported functions --------------------------------------------------------*/
619 /** @addtogroup SMARTCARD_Exported_Functions
620   * @{
621   */
622 
623 /** @addtogroup SMARTCARD_Exported_Functions_Group1
624   * @{
625   */
626 /* Initialization/de-initialization functions  **********************************/
627 HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc);
628 HAL_StatusTypeDef HAL_SMARTCARD_ReInit(SMARTCARD_HandleTypeDef *hsc);
629 HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc);
630 void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc);
631 void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc);
632 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
633 /* Callbacks Register/UnRegister functions  ***********************************/
634 HAL_StatusTypeDef HAL_SMARTCARD_RegisterCallback(SMARTCARD_HandleTypeDef *hsc, HAL_SMARTCARD_CallbackIDTypeDef CallbackID, pSMARTCARD_CallbackTypeDef pCallback);
635 HAL_StatusTypeDef HAL_SMARTCARD_UnRegisterCallback(SMARTCARD_HandleTypeDef *hsc, HAL_SMARTCARD_CallbackIDTypeDef CallbackID);
636 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
637 /**
638   * @}
639   */
640 
641 /** @addtogroup SMARTCARD_Exported_Functions_Group2
642   * @{
643   */
644 /* IO operation functions *******************************************************/
645 HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, const uint8_t *pData, uint16_t Size, uint32_t Timeout);
646 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout);
647 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, const uint8_t *pData, uint16_t Size);
648 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size);
649 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, const uint8_t *pData, uint16_t Size);
650 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size);
651 /* Transfer Abort functions */
652 HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsc);
653 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsc);
654 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsc);
655 HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsc);
656 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsc);
657 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsc);
658 
659 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc);
660 void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc);
661 void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc);
662 void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc);
663 void HAL_SMARTCARD_AbortCpltCallback(SMARTCARD_HandleTypeDef *hsc);
664 void HAL_SMARTCARD_AbortTransmitCpltCallback(SMARTCARD_HandleTypeDef *hsc);
665 void HAL_SMARTCARD_AbortReceiveCpltCallback(SMARTCARD_HandleTypeDef *hsc);
666 /**
667   * @}
668   */
669 
670 /** @addtogroup SMARTCARD_Exported_Functions_Group3
671   * @{
672   */
673 /* Peripheral State functions  **************************************************/
674 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(const SMARTCARD_HandleTypeDef *hsc);
675 uint32_t HAL_SMARTCARD_GetError(const SMARTCARD_HandleTypeDef *hsc);
676 /**
677   * @}
678   */
679 
680 /**
681   * @}
682   */
683 /* Private types -------------------------------------------------------------*/
684 /* Private variables ---------------------------------------------------------*/
685 /* Private constants ---------------------------------------------------------*/
686 /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants
687   * @{
688   */
689 
690 /** @brief SMARTCARD interruptions flag mask
691   *
692   */
693 #define SMARTCARD_IT_MASK                   ((uint32_t) USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RXNEIE | \
694                                                         USART_CR1_IDLEIE | USART_CR3_EIE )
695 
696 #define SMARTCARD_CR1_REG_INDEX             1U
697 #define SMARTCARD_CR3_REG_INDEX             3U
698 /**
699   * @}
700   */
701 
702 /* Private macros --------------------------------------------------------*/
703 /** @defgroup SMARTCARD_Private_Macros   SMARTCARD Private Macros
704   * @{
705   */
706 #define IS_SMARTCARD_WORD_LENGTH(LENGTH)    ((LENGTH) == SMARTCARD_WORDLENGTH_9B)
707 #define IS_SMARTCARD_STOPBITS(STOPBITS)     (((STOPBITS) == SMARTCARD_STOPBITS_0_5) || \
708                                              ((STOPBITS) == SMARTCARD_STOPBITS_1_5))
709 #define IS_SMARTCARD_PARITY(PARITY)         (((PARITY) == SMARTCARD_PARITY_EVEN) || \
710                                              ((PARITY) == SMARTCARD_PARITY_ODD))
711 #define IS_SMARTCARD_MODE(MODE)             ((((MODE) & 0x0000FFF3U) == 0x00U) && ((MODE) != 0x000000U))
712 #define IS_SMARTCARD_POLARITY(CPOL)         (((CPOL) == SMARTCARD_POLARITY_LOW) || ((CPOL) == SMARTCARD_POLARITY_HIGH))
713 #define IS_SMARTCARD_PHASE(CPHA)            (((CPHA) == SMARTCARD_PHASE_1EDGE) || ((CPHA) == SMARTCARD_PHASE_2EDGE))
714 #define IS_SMARTCARD_LASTBIT(LASTBIT)       (((LASTBIT) == SMARTCARD_LASTBIT_DISABLE) || \
715                                              ((LASTBIT) == SMARTCARD_LASTBIT_ENABLE))
716 #define IS_SMARTCARD_NACK_STATE(NACK)       (((NACK) == SMARTCARD_NACK_ENABLE) || \
717                                              ((NACK) == SMARTCARD_NACK_DISABLE))
718 #define IS_SMARTCARD_BAUDRATE(BAUDRATE)     ((BAUDRATE) < 10500001U)
719 
720 #define SMARTCARD_DIV(__PCLK__, __BAUD__)                (((__PCLK__)*25U)/(4U*(__BAUD__)))
721 #define SMARTCARD_DIVMANT(__PCLK__, __BAUD__)            (SMARTCARD_DIV((__PCLK__), (__BAUD__))/100U)
722 #define SMARTCARD_DIVFRAQ(__PCLK__, __BAUD__)            ((((SMARTCARD_DIV((__PCLK__), (__BAUD__)) - (SMARTCARD_DIVMANT((__PCLK__), (__BAUD__)) * 100U)) * 16U) + 50U) / 100U)
723 /* SMARTCARD BRR = mantissa + overflow + fraction
724             = (SMARTCARD DIVMANT << 4) + (SMARTCARD DIVFRAQ & 0xF0) + (SMARTCARD DIVFRAQ & 0x0FU) */
725 #define SMARTCARD_BRR(__PCLK__, __BAUD__)       (((SMARTCARD_DIVMANT((__PCLK__), (__BAUD__)) << 4U) + \
726                                                  (SMARTCARD_DIVFRAQ((__PCLK__), (__BAUD__)) & 0xF0U)) + \
727                                                  (SMARTCARD_DIVFRAQ((__PCLK__), (__BAUD__)) & 0x0FU))
728 
729 /**
730   * @}
731   */
732 
733 /* Private functions ---------------------------------------------------------*/
734 /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
735   * @{
736   */
737 
738 /**
739   * @}
740   */
741 
742 /**
743   * @}
744   */
745 
746 /**
747   * @}
748   */
749 
750 #ifdef __cplusplus
751 }
752 #endif
753 
754 #endif /* __STM32F2xx_HAL_SMARTCARD_H */
755 
756