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