1 /**
2   ******************************************************************************
3   * @file    stm32l1xx_hal_smartcard.h
4   * @author  MCD Application Team
5   * @brief   This file contains all the functions prototypes for the SMARTCARD
6   *          firmware library.
7   ******************************************************************************
8   * @attention
9   *
10   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
11   *
12   * Redistribution and use in source and binary forms, with or without modification,
13   * are permitted provided that the following conditions are met:
14   *   1. Redistributions of source code must retain the above copyright notice,
15   *      this list of conditions and the following disclaimer.
16   *   2. Redistributions in binary form must reproduce the above copyright notice,
17   *      this list of conditions and the following disclaimer in the documentation
18   *      and/or other materials provided with the distribution.
19   *   3. Neither the name of STMicroelectronics nor the names of its contributors
20   *      may be used to endorse or promote products derived from this software
21   *      without specific prior written permission.
22   *
23   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *
34   ******************************************************************************
35   */
36 
37 /* Define to prevent recursive inclusion -------------------------------------*/
38 #ifndef __STM32L1xx_HAL_SMARTCARD_H
39 #define __STM32L1xx_HAL_SMARTCARD_H
40 
41 #ifdef __cplusplus
42  extern "C" {
43 #endif
44 
45 /* Includes ------------------------------------------------------------------*/
46 #include "stm32l1xx_hal_def.h"
47 
48 /** @addtogroup STM32L1xx_HAL_Driver
49   * @{
50   */
51 
52 /** @addtogroup SMARTCARD
53   * @{
54   */
55 
56 /* Exported types ------------------------------------------------------------*/
57 /** @defgroup SMARTCARD_Exported_Types SMARTCARD Exported Types
58   * @{
59   */
60 
61 
62 /**
63   * @brief SMARTCARD Init Structure definition
64   */
65 typedef struct
66 {
67   uint32_t BaudRate;                  /*!< This member configures the SmartCard communication baud rate.
68                                            The baud rate is computed using the following formula:
69                                            - IntegerDivider = ((PCLKx) / (16 * (hsmartcard->Init.BaudRate)))
70                                            - FractionalDivider = ((IntegerDivider - ((uint32_t) IntegerDivider)) * 16) + 0.5 */
71 
72   uint32_t WordLength;                /*!< Specifies the number of data bits transmitted or received in a frame.
73                                            This parameter can be a value of @ref SMARTCARD_Word_Length */
74 
75   uint32_t StopBits;                  /*!< Specifies the number of stop bits transmitted.
76                                            This parameter can be a value of @ref SMARTCARD_Stop_Bits */
77 
78   uint32_t Parity;                    /*!< Specifies the parity mode.
79                                            This parameter can be a value of @ref SMARTCARD_Parity
80                                            @note When parity is enabled, the computed parity is inserted
81                                                  at the MSB position of the transmitted data (9th bit when
82                                                  the word length is set to 9 data bits; 8th bit when the
83                                                  word length is set to 8 data bits).*/
84 
85   uint32_t Mode;                      /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
86                                            This parameter can be a value of @ref SMARTCARD_Mode */
87 
88   uint32_t CLKPolarity;               /*!< Specifies the steady state of the serial clock.
89                                            This parameter can be a value of @ref SMARTCARD_Clock_Polarity */
90 
91   uint32_t CLKPhase;                  /*!< Specifies the clock transition on which the bit capture is made.
92                                            This parameter can be a value of @ref SMARTCARD_Clock_Phase */
93 
94   uint32_t CLKLastBit;                /*!< Specifies whether the clock pulse corresponding to the last transmitted
95                                            data bit (MSB) has to be output on the SCLK pin in synchronous mode.
96                                            This parameter can be a value of @ref SMARTCARD_Last_Bit */
97 
98   uint32_t Prescaler;                 /*!< Specifies the SmartCard Prescaler value used for dividing the system clock
99                                            to provide the smartcard clock. The value given in the register
100                                            (5 significant bits) is multiplied by 2 to give the division factor of
101                                            the source clock frequency;
102                                            This parameter can be a value of @ref SMARTCARD_Prescaler */
103 
104   uint32_t GuardTime;                 /*!< Specifies the SmartCard Guard Time value in terms of number
105                                            of baud clocks */
106 
107   uint32_t NACKState;                 /*!< Specifies the SmartCard NACK Transmission state
108                                            This parameter can be a value of @ref SMARTCARD_NACK_State */
109 }SMARTCARD_InitTypeDef;
110 
111 /**
112   * @brief HAL State structures definition
113   */
114 typedef enum
115 {
116   HAL_SMARTCARD_STATE_RESET             = 0x00,    /*!< Peripheral is not yet Initialized */
117   HAL_SMARTCARD_STATE_READY             = 0x01,    /*!< Peripheral Initialized and ready for use */
118   HAL_SMARTCARD_STATE_BUSY              = 0x02,    /*!< an internal process is ongoing */
119   HAL_SMARTCARD_STATE_BUSY_TX           = 0x12,    /*!< Data Transmission process is ongoing */
120   HAL_SMARTCARD_STATE_BUSY_RX           = 0x22,    /*!< Data Reception process is ongoing */
121   HAL_SMARTCARD_STATE_BUSY_TX_RX        = 0x32,    /*!< Data Transmission and Reception process is ongoing */
122   HAL_SMARTCARD_STATE_TIMEOUT           = 0x03,    /*!< Timeout state */
123   HAL_SMARTCARD_STATE_ERROR             = 0x04     /*!< Error */
124 }HAL_SMARTCARD_StateTypeDef;
125 
126 
127 /**
128   * @brief  SMARTCARD handle Structure definition
129   */
130 typedef struct
131 {
132   USART_TypeDef                    *Instance;        /*!< USART registers base address */
133 
134   SMARTCARD_InitTypeDef            Init;             /*!< SmartCard communication parameters */
135 
136   uint8_t                          *pTxBuffPtr;      /*!< Pointer to SmartCard Tx transfer Buffer */
137 
138   uint16_t                         TxXferSize;       /*!< SmartCard Tx Transfer size */
139 
140   uint16_t                         TxXferCount;      /*!< SmartCard Tx Transfer Counter */
141 
142   uint8_t                          *pRxBuffPtr;      /*!< Pointer to SmartCard Rx transfer Buffer */
143 
144   uint16_t                         RxXferSize;       /*!< SmartCard Rx Transfer size */
145 
146   uint16_t                         RxXferCount;      /*!< SmartCard Rx Transfer Counter */
147 
148   DMA_HandleTypeDef                *hdmatx;          /*!< SmartCard Tx DMA Handle parameters */
149 
150   DMA_HandleTypeDef                *hdmarx;          /*!< SmartCard Rx DMA Handle parameters */
151 
152   HAL_LockTypeDef                  Lock;             /*!< Locking object */
153 
154   __IO HAL_SMARTCARD_StateTypeDef  State;            /*!< SmartCard communication state */
155 
156   __IO uint32_t                    ErrorCode;        /*!< SmartCard Error code */
157 }SMARTCARD_HandleTypeDef;
158 
159 /**
160   * @}
161   */
162 
163 /* Exported constants --------------------------------------------------------*/
164 /** @defgroup SMARTCARD_Exported_Constants  SMARTCARD Exported constants
165   * @{
166   */
167 
168 /** @defgroup SMARTCARD_Error_Codes SMARTCARD Error Codes
169   * @{
170   */
171 #define HAL_SMARTCARD_ERROR_NONE      (0x00U)    /*!< No error            */
172 #define HAL_SMARTCARD_ERROR_PE        (0x01U)    /*!< Parity error        */
173 #define HAL_SMARTCARD_ERROR_NE        (0x02U)    /*!< Noise error         */
174 #define HAL_SMARTCARD_ERROR_FE        (0x04U)    /*!< frame error         */
175 #define HAL_SMARTCARD_ERROR_ORE       (0x08U)    /*!< Overrun error       */
176 #define HAL_SMARTCARD_ERROR_DMA       (0x10U)    /*!< DMA transfer error  */
177 
178 /**
179   * @}
180   */
181 
182 
183 /** @defgroup SMARTCARD_Word_Length SMARTCARD Word Length
184   * @{
185   */
186 #define SMARTCARD_WORDLENGTH_9B                  ((uint32_t)USART_CR1_M)
187 
188 /**
189   * @}
190   */
191 
192 /** @defgroup SMARTCARD_Stop_Bits SMARTCARD Number of Stop Bits
193   * @{
194   */
195 #define SMARTCARD_STOPBITS_0_5                   ((uint32_t)USART_CR2_STOP_0)
196 #define SMARTCARD_STOPBITS_1_5                   ((uint32_t)(USART_CR2_STOP_0 | USART_CR2_STOP_1))
197 /**
198   * @}
199   */
200 
201 /** @defgroup SMARTCARD_Parity SMARTCARD Parity
202   * @{
203   */
204 #define SMARTCARD_PARITY_EVEN                    ((uint32_t)USART_CR1_PCE)
205 #define SMARTCARD_PARITY_ODD                     ((uint32_t)(USART_CR1_PCE | USART_CR1_PS))
206 /**
207   * @}
208   */
209 
210 /** @defgroup SMARTCARD_Mode SMARTCARD Mode
211   * @{
212   */
213 #define SMARTCARD_MODE_RX                        ((uint32_t)USART_CR1_RE)
214 #define SMARTCARD_MODE_TX                        ((uint32_t)USART_CR1_TE)
215 #define SMARTCARD_MODE_TX_RX                     ((uint32_t)(USART_CR1_TE |USART_CR1_RE))
216 /**
217   * @}
218   */
219 
220 /** @defgroup SMARTCARD_Clock_Polarity  SMARTCARD Clock Polarity
221   * @{
222   */
223 #define SMARTCARD_POLARITY_LOW                   (0x00000000U)
224 #define SMARTCARD_POLARITY_HIGH                  ((uint32_t)USART_CR2_CPOL)
225 /**
226   * @}
227   */
228 
229 /** @defgroup SMARTCARD_Clock_Phase SMARTCARD Clock Phase
230   * @{
231   */
232 #define SMARTCARD_PHASE_1EDGE                    (0x00000000U)
233 #define SMARTCARD_PHASE_2EDGE                    ((uint32_t)USART_CR2_CPHA)
234 /**
235   * @}
236   */
237 
238 /** @defgroup SMARTCARD_Last_Bit  SMARTCARD Last Bit
239   * @{
240   */
241 #define SMARTCARD_LASTBIT_DISABLE                (0x00000000U)
242 #define SMARTCARD_LASTBIT_ENABLE                 ((uint32_t)USART_CR2_LBCL)
243 /**
244   * @}
245   */
246 
247 /** @defgroup SMARTCARD_OneBit_Sampling SMARTCARD One Bit Sampling Method
248   * @{
249   */
250 #define SMARTCARD_ONE_BIT_SAMPLE_DISABLE    (0x00000000U)
251 #define SMARTCARD_ONE_BIT_SAMPLE_ENABLE     ((uint32_t)USART_CR3_ONEBIT)
252 /**
253   * @}
254   */
255 
256 /** @defgroup SMARTCARD_NACK_State   SMARTCARD NACK State
257   * @{
258   */
259 #define SMARTCARD_NACK_ENABLE                     ((uint32_t)USART_CR3_NACK)
260 #define SMARTCARD_NACK_DISABLE                    (0x00000000U)
261 /**
262   * @}
263   */
264 
265 /** @defgroup SMARTCARD_DMA_Requests SMARTCARD DMA requests
266   * @{
267   */
268 
269 #define SMARTCARD_DMAREQ_TX                      ((uint32_t)USART_CR3_DMAT)
270 #define SMARTCARD_DMAREQ_RX                      ((uint32_t)USART_CR3_DMAR)
271 
272 /**
273   * @}
274   */
275 
276 /** @defgroup SMARTCARD_Prescaler SMARTCARD Prescaler
277   * @{
278   */
279 #define SMARTCARD_PRESCALER_SYSCLK_DIV2         (0x00000001U)          /*!< SYSCLK divided by 2 */
280 #define SMARTCARD_PRESCALER_SYSCLK_DIV4         (0x00000002U)          /*!< SYSCLK divided by 4 */
281 #define SMARTCARD_PRESCALER_SYSCLK_DIV6         (0x00000003U)          /*!< SYSCLK divided by 6 */
282 #define SMARTCARD_PRESCALER_SYSCLK_DIV8         (0x00000004U)          /*!< SYSCLK divided by 8 */
283 #define SMARTCARD_PRESCALER_SYSCLK_DIV10        (0x00000005U)          /*!< SYSCLK divided by 10 */
284 #define SMARTCARD_PRESCALER_SYSCLK_DIV12        (0x00000006U)          /*!< SYSCLK divided by 12 */
285 #define SMARTCARD_PRESCALER_SYSCLK_DIV14        (0x00000007U)          /*!< SYSCLK divided by 14 */
286 #define SMARTCARD_PRESCALER_SYSCLK_DIV16        (0x00000008U)          /*!< SYSCLK divided by 16 */
287 #define SMARTCARD_PRESCALER_SYSCLK_DIV18        (0x00000009U)          /*!< SYSCLK divided by 18 */
288 #define SMARTCARD_PRESCALER_SYSCLK_DIV20        (0x0000000AU)          /*!< SYSCLK divided by 20 */
289 #define SMARTCARD_PRESCALER_SYSCLK_DIV22        (0x0000000BU)          /*!< SYSCLK divided by 22 */
290 #define SMARTCARD_PRESCALER_SYSCLK_DIV24        (0x0000000CU)          /*!< SYSCLK divided by 24 */
291 #define SMARTCARD_PRESCALER_SYSCLK_DIV26        (0x0000000DU)          /*!< SYSCLK divided by 26 */
292 #define SMARTCARD_PRESCALER_SYSCLK_DIV28        (0x0000000EU)          /*!< SYSCLK divided by 28 */
293 #define SMARTCARD_PRESCALER_SYSCLK_DIV30        (0x0000000FU)          /*!< SYSCLK divided by 30 */
294 #define SMARTCARD_PRESCALER_SYSCLK_DIV32        (0x00000010U)          /*!< SYSCLK divided by 32 */
295 #define SMARTCARD_PRESCALER_SYSCLK_DIV34        (0x00000011U)          /*!< SYSCLK divided by 34 */
296 #define SMARTCARD_PRESCALER_SYSCLK_DIV36        (0x00000012U)          /*!< SYSCLK divided by 36 */
297 #define SMARTCARD_PRESCALER_SYSCLK_DIV38        (0x00000013U)          /*!< SYSCLK divided by 38 */
298 #define SMARTCARD_PRESCALER_SYSCLK_DIV40        (0x00000014U)          /*!< SYSCLK divided by 40 */
299 #define SMARTCARD_PRESCALER_SYSCLK_DIV42        (0x00000015U)          /*!< SYSCLK divided by 42 */
300 #define SMARTCARD_PRESCALER_SYSCLK_DIV44        (0x00000016U)          /*!< SYSCLK divided by 44 */
301 #define SMARTCARD_PRESCALER_SYSCLK_DIV46        (0x00000017U)          /*!< SYSCLK divided by 46 */
302 #define SMARTCARD_PRESCALER_SYSCLK_DIV48        (0x00000018U)          /*!< SYSCLK divided by 48 */
303 #define SMARTCARD_PRESCALER_SYSCLK_DIV50        (0x00000019U)          /*!< SYSCLK divided by 50 */
304 #define SMARTCARD_PRESCALER_SYSCLK_DIV52        (0x0000001AU)          /*!< SYSCLK divided by 52 */
305 #define SMARTCARD_PRESCALER_SYSCLK_DIV54        (0x0000001BU)          /*!< SYSCLK divided by 54 */
306 #define SMARTCARD_PRESCALER_SYSCLK_DIV56        (0x0000001CU)          /*!< SYSCLK divided by 56 */
307 #define SMARTCARD_PRESCALER_SYSCLK_DIV58        (0x0000001DU)          /*!< SYSCLK divided by 58 */
308 #define SMARTCARD_PRESCALER_SYSCLK_DIV60        (0x0000001EU)          /*!< SYSCLK divided by 60 */
309 #define SMARTCARD_PRESCALER_SYSCLK_DIV62        (0x0000001FU)          /*!< SYSCLK divided by 62 */
310 /**
311   * @}
312   */
313 
314 
315 
316 /** @defgroup SMARTCARD_Flags    SMARTCARD Flags
317   *        Elements values convention: 0xXXXX
318   *           - 0xXXXX  : Flag mask in the SR register
319   * @{
320   */
321 
322 #define SMARTCARD_FLAG_TXE                      ((uint32_t)USART_SR_TXE)
323 #define SMARTCARD_FLAG_TC                       ((uint32_t)USART_SR_TC)
324 #define SMARTCARD_FLAG_RXNE                     ((uint32_t)USART_SR_RXNE)
325 #define SMARTCARD_FLAG_IDLE                     ((uint32_t)USART_SR_IDLE)
326 #define SMARTCARD_FLAG_ORE                      ((uint32_t)USART_SR_ORE)
327 #define SMARTCARD_FLAG_NE                       ((uint32_t)USART_SR_NE)
328 #define SMARTCARD_FLAG_FE                       ((uint32_t)USART_SR_FE)
329 #define SMARTCARD_FLAG_PE                       ((uint32_t)USART_SR_PE)
330 /**
331   * @}
332   */
333 
334 /** @defgroup SMARTCARD_Interrupt_definition     SMARTCARD Interrupts Definition
335   *        Elements values convention: 0xY000XXXX
336   *           - XXXX  : Interrupt mask (16 bits) in the Y register
337   *           - Y  : Interrupt source register (4 bits)
338   *                 - 0001: CR1 register
339   *                 - 0010: CR3 register
340 
341   *
342   * @{
343   */
344 
345 #define SMARTCARD_IT_PE                         ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28 | USART_CR1_PEIE))
346 #define SMARTCARD_IT_TXE                        ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28 | USART_CR1_TXEIE))
347 #define SMARTCARD_IT_TC                         ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28 | USART_CR1_TCIE))
348 #define SMARTCARD_IT_RXNE                       ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28 | USART_CR1_RXNEIE))
349 #define SMARTCARD_IT_IDLE                       ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28 | USART_CR1_IDLEIE))
350 #define SMARTCARD_IT_ERR                        ((uint32_t)(SMARTCARD_CR3_REG_INDEX << 28 | USART_CR3_EIE))
351 
352 /**
353   * @}
354   */
355 
356 /**
357   * @}
358   */
359 
360 
361 /* Exported macro ------------------------------------------------------------*/
362 /** @defgroup SMARTCARD_Exported_Macros SMARTCARD Exported Macros
363   * @{
364   */
365 
366 
367 /** @brief Reset SMARTCARD handle state
368   * @param  __HANDLE__: specifies the SMARTCARD Handle.
369   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
370   * @retval None
371   */
372 #define __HAL_SMARTCARD_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_SMARTCARD_STATE_RESET)
373 
374 /** @brief  Flush the Smartcard DR register
375   * @param  __HANDLE__: specifies the SMARTCARD Handle.
376   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
377   * @retval None
378   */
379 #define __HAL_SMARTCARD_FLUSH_DRREGISTER(__HANDLE__) ((__HANDLE__)->Instance->DR)
380 
381 /** @brief  Check whether the specified Smartcard flag is set or not.
382   * @param  __HANDLE__: specifies the SMARTCARD Handle.
383   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
384   * @param  __FLAG__: specifies the flag to check.
385   *         This parameter can be one of the following values:
386   *            @arg SMARTCARD_FLAG_TXE:  Transmit data register empty flag
387   *            @arg SMARTCARD_FLAG_TC:   Transmission Complete flag
388   *            @arg SMARTCARD_FLAG_RXNE: Receive data register not empty flag
389   *            @arg SMARTCARD_FLAG_IDLE: Idle Line detection flag
390   *            @arg SMARTCARD_FLAG_ORE:  OverRun Error flag
391   *            @arg SMARTCARD_FLAG_NE:   Noise Error flag
392   *            @arg SMARTCARD_FLAG_FE:   Framing Error flag
393   *            @arg SMARTCARD_FLAG_PE:   Parity Error flag
394   * @retval The new state of __FLAG__ (TRUE or FALSE).
395   */
396 #define __HAL_SMARTCARD_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__))
397 
398 /** @brief  Clear the specified Smartcard pending flags.
399   * @param  __HANDLE__: specifies the SMARTCARD Handle.
400   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
401   * @param  __FLAG__: specifies the flag to check.
402   *         This parameter can be any combination of the following values:
403   *            @arg SMARTCARD_FLAG_TC:   Transmission Complete flag.
404   *            @arg SMARTCARD_FLAG_RXNE: Receive data register not empty flag.
405   * @retval None
406   *
407   * @note   PE (Parity error), FE (Framing error), NE (Noise error) and ORE (OverRun
408   *          error) flags are cleared by software sequence: a read operation to
409   *          USART_SR register followed by a read operation to USART_DR register.
410   * @note   RXNE flag can be also cleared by a read to the USART_DR register.
411   * @note   TC flag can be also cleared by software sequence: a read operation to
412   *          USART_SR register followed by a write operation to USART_DR register.
413   * @note   TXE flag is cleared only by a write to the USART_DR register.
414   *
415   * @retval None
416   */
417 #define __HAL_SMARTCARD_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->SR = ~(__FLAG__))
418 
419 /** @brief  Clear the SMARTCARD PE pending flag.
420   * @param  __HANDLE__: specifies the USART Handle.
421   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
422   * @retval None
423   */
424 #define __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__) \
425 do{                                                        \
426     __IO uint32_t tmpreg;                                  \
427     tmpreg = (__HANDLE__)->Instance->SR;                   \
428     tmpreg = (__HANDLE__)->Instance->DR;                   \
429     UNUSED(tmpreg);                                        \
430 }while(0)
431 
432 
433 
434 /** @brief  Clear the SMARTCARD FE pending flag.
435   * @param  __HANDLE__: specifies the USART Handle.
436   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
437   * @retval None
438   */
439 #define __HAL_SMARTCARD_CLEAR_FEFLAG(__HANDLE__) __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__)
440 
441 /** @brief  Clear the SMARTCARD NE pending flag.
442   * @param  __HANDLE__: specifies the USART Handle.
443   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
444   * @retval None
445   */
446 #define __HAL_SMARTCARD_CLEAR_NEFLAG(__HANDLE__) __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__)
447 
448 /** @brief  Clear the SMARTCARD ORE pending flag.
449   * @param  __HANDLE__: specifies the USART Handle.
450   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
451   * @retval None
452   */
453 #define __HAL_SMARTCARD_CLEAR_OREFLAG(__HANDLE__) __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__)
454 
455 /** @brief  Clear the SMARTCARD IDLE pending flag.
456   * @param  __HANDLE__: specifies the USART Handle.
457   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
458   * @retval None
459   */
460 #define __HAL_SMARTCARD_CLEAR_IDLEFLAG(__HANDLE__) __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__)
461 
462 /** @brief  Enable the specified SmartCard interrupt.
463   * @param  __HANDLE__: specifies the SMARTCARD Handle.
464   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
465   * @param  __INTERRUPT__: specifies the SMARTCARD interrupt to enable.
466   *          This parameter can be one of the following values:
467   *            @arg SMARTCARD_IT_TXE:  Transmit Data Register empty interrupt
468   *            @arg SMARTCARD_IT_TC:   Transmission complete interrupt
469   *            @arg SMARTCARD_IT_RXNE: Receive Data register not empty interrupt
470   *            @arg SMARTCARD_IT_IDLE: Idle line detection interrupt
471   *            @arg SMARTCARD_IT_PE:   Parity Error interrupt
472   *            @arg SMARTCARD_IT_ERR:  Error interrupt(Frame error, noise error, overrun error)
473   * @retval None
474   */
475 #define __HAL_SMARTCARD_ENABLE_IT(__HANDLE__, __INTERRUPT__)   ((((__INTERRUPT__) >> 28) == SMARTCARD_CR1_REG_INDEX)? ((__HANDLE__)->Instance->CR1 |= ((__INTERRUPT__) & SMARTCARD_IT_MASK)): \
476                                                                ((__HANDLE__)->Instance->CR3 |= ((__INTERRUPT__) & SMARTCARD_IT_MASK)))
477 
478 /** @brief  Disable the specified SmartCard interrupts.
479   * @param  __HANDLE__: specifies the SMARTCARD Handle.
480   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
481   * @param  __INTERRUPT__: specifies the SMARTCARD interrupt to disable.
482   *          This parameter can be one of the following values:
483   *            @arg SMARTCARD_IT_TXE:  Transmit Data Register empty interrupt
484   *            @arg SMARTCARD_IT_TC:   Transmission complete interrupt
485   *            @arg SMARTCARD_IT_RXNE: Receive Data register not empty interrupt
486   *            @arg SMARTCARD_IT_IDLE: Idle line detection interrupt
487   *            @arg SMARTCARD_IT_PE:   Parity Error interrupt
488   *            @arg SMARTCARD_IT_ERR:  Error interrupt(Frame error, noise error, overrun error)
489   */
490 #define __HAL_SMARTCARD_DISABLE_IT(__HANDLE__, __INTERRUPT__)  ((((__INTERRUPT__) >> 28) == SMARTCARD_CR1_REG_INDEX)? ((__HANDLE__)->Instance->CR1 &= ~((__INTERRUPT__) & SMARTCARD_IT_MASK)): \
491                                                                ((__HANDLE__)->Instance->CR3 &= ~ ((__INTERRUPT__) & SMARTCARD_IT_MASK)))
492 
493 /** @brief  Check whether the specified SmartCard interrupt has occurred or not.
494   * @param  __HANDLE__: specifies the SMARTCARD Handle.
495   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
496   * @param  __IT__: specifies the SMARTCARD interrupt source to check.
497   *          This parameter can be one of the following values:
498   *            @arg SMARTCARD_IT_TXE: Transmit Data Register empty interrupt
499   *            @arg SMARTCARD_IT_TC:  Transmission complete interrupt
500   *            @arg SMARTCARD_IT_RXNE: Receive Data register not empty interrupt
501   *            @arg SMARTCARD_IT_IDLE: Idle line detection interrupt
502   *            @arg SMARTCARD_IT_ERR: Error interrupt
503   *            @arg SMARTCARD_IT_PE: Parity Error interrupt
504   * @retval The new state of __IT__ (TRUE or FALSE).
505   */
506 #define __HAL_SMARTCARD_GET_IT_SOURCE(__HANDLE__, __IT__) (((((__IT__) >> 28) == SMARTCARD_CR1_REG_INDEX)? (__HANDLE__)->Instance->CR1: (__HANDLE__)->Instance->CR3) & (((uint32_t)(__IT__)) & SMARTCARD_IT_MASK))
507 
508 /** @brief  Enables the SMARTCARD one bit sample method
509   * @param  __HANDLE__: specifies the SMARTCARD Handle.
510   * @retval None
511   */
512 #define __HAL_SMARTCARD_ONE_BIT_SAMPLE_ENABLE(__HANDLE__)  (SET_BIT((__HANDLE__)->Instance->CR3, (USART_CR3_ONEBIT)))
513 
514 /** @brief  Disables the SMARTCARD one bit sample method
515   * @param  __HANDLE__: specifies the SMARTCARD Handle.
516   * @retval None
517   */
518 #define __HAL_SMARTCARD_ONE_BIT_SAMPLE_DISABLE(__HANDLE__) (CLEAR_BIT((__HANDLE__)->Instance->CR3, (USART_CR3_ONEBIT)))
519 
520 /** @brief  Enable the USART associated to the SMARTCARD Handle
521   * @param  __HANDLE__: specifies the SMARTCARD Handle.
522   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
523   * @retval None
524   */
525 #define __HAL_SMARTCARD_ENABLE(__HANDLE__)               (SET_BIT((__HANDLE__)->Instance->CR1, USART_CR1_UE))
526 
527 /** @brief  Disable the USART associated to the SMARTCARD Handle
528   * @param  __HANDLE__: specifies the SMARTCARD Handle.
529   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
530   * @retval None
531   */
532 #define __HAL_SMARTCARD_DISABLE(__HANDLE__)              (CLEAR_BIT((__HANDLE__)->Instance->CR1, USART_CR1_UE))
533 
534 /** @brief  Enable the SmartCard DMA request.
535   * @param  __HANDLE__: specifies the SmartCard Handle.
536   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
537   * @param  __REQUEST__: specifies the SmartCard DMA request.
538   *         This parameter can be one of the following values:
539   *            @arg SMARTCARD_DMAREQ_TX: SmartCard DMA transmit request
540   *            @arg SMARTCARD_DMAREQ_RX: SmartCard DMA receive request
541   * @retval None
542   */
543 #define __HAL_SMARTCARD_DMA_REQUEST_ENABLE(__HANDLE__, __REQUEST__)    (SET_BIT((__HANDLE__)->Instance->CR3, (__REQUEST__)))
544 
545 /** @brief  Disable the SmartCard DMA request.
546   * @param  __HANDLE__: specifies the SmartCard Handle.
547   *         SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
548   * @param  __REQUEST__: specifies the SmartCard DMA request.
549   *         This parameter can be one of the following values:
550   *            @arg SMARTCARD_DMAREQ_TX: SmartCard DMA transmit request
551   *            @arg SMARTCARD_DMAREQ_RX: SmartCard DMA receive request
552   * @retval None
553   */
554 #define __HAL_SMARTCARD_DMA_REQUEST_DISABLE(__HANDLE__, __REQUEST__)   (CLEAR_BIT((__HANDLE__)->Instance->CR3, (__REQUEST__)))
555 
556 
557 /**
558   * @}
559   */
560 
561 
562 /* Private macros --------------------------------------------------------*/
563 /** @defgroup SMARTCARD_Private_Macros   SMARTCARD Private Macros
564   * @{
565   */
566 
567 #define SMARTCARD_CR1_REG_INDEX                 1
568 #define SMARTCARD_CR3_REG_INDEX                 3
569 
570 #define SMARTCARD_DIV(__PCLK__, __BAUD__)                (((__PCLK__)*25)/(4*(__BAUD__)))
571 #define SMARTCARD_DIVMANT(__PCLK__, __BAUD__)            (SMARTCARD_DIV((__PCLK__), (__BAUD__))/100)
572 #define SMARTCARD_DIVFRAQ(__PCLK__, __BAUD__)            (((SMARTCARD_DIV((__PCLK__), (__BAUD__)) - (SMARTCARD_DIVMANT((__PCLK__), (__BAUD__)) * 100)) * 16 + 50) / 100)
573 /* UART BRR = mantissa + overflow + fraction
574             = (UART DIVMANT << 4) + (UART DIVFRAQ & 0xF0) + (UART DIVFRAQ & 0x0F) */
575 #define SMARTCARD_BRR(_PCLK_, _BAUD_)            (((SMARTCARD_DIVMANT((_PCLK_), (_BAUD_)) << 4) + \
576                                                   (SMARTCARD_DIVFRAQ((_PCLK_), (_BAUD_)) & 0xF0)) + \
577                                                   (SMARTCARD_DIVFRAQ((_PCLK_), (_BAUD_)) & 0x0F))
578 
579 /** Check the Baud rate range.
580   *         The maximum Baud Rate is derived from the maximum clock on APB (i.e. 32 MHz)
581   *         divided by the smallest oversampling used on the USART (i.e. 16)
582   *         __BAUDRATE__: Baud rate set by the configuration function.
583   * Return : TRUE or FALSE
584   */
585 #define IS_SMARTCARD_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) < 2000001)
586 
587 #define IS_SMARTCARD_WORD_LENGTH(LENGTH)    ((LENGTH) == SMARTCARD_WORDLENGTH_9B)
588 
589 #define IS_SMARTCARD_STOPBITS(STOPBITS)     (((STOPBITS) == SMARTCARD_STOPBITS_0_5) || \
590                                              ((STOPBITS) == SMARTCARD_STOPBITS_1_5))
591 
592 #define IS_SMARTCARD_PARITY(PARITY)         (((PARITY) == SMARTCARD_PARITY_EVEN) || \
593                                              ((PARITY) == SMARTCARD_PARITY_ODD))
594 
595 #define IS_SMARTCARD_MODE(MODE)             ((((MODE) & (~((uint32_t)SMARTCARD_MODE_TX_RX))) == 0x00U) && \
596                                              ((MODE) != 0x00000000U))
597 
598 #define IS_SMARTCARD_POLARITY(CPOL)         (((CPOL) == SMARTCARD_POLARITY_LOW) || ((CPOL) == SMARTCARD_POLARITY_HIGH))
599 
600 #define IS_SMARTCARD_PHASE(CPHA)            (((CPHA) == SMARTCARD_PHASE_1EDGE) || ((CPHA) == SMARTCARD_PHASE_2EDGE))
601 
602 #define IS_SMARTCARD_LASTBIT(LASTBIT)       (((LASTBIT) == SMARTCARD_LASTBIT_DISABLE) || \
603                                              ((LASTBIT) == SMARTCARD_LASTBIT_ENABLE))
604 
605 #define IS_SMARTCARD_ONE_BIT_SAMPLE(ONEBIT) (((ONEBIT) == SMARTCARD_ONE_BIT_SAMPLE_DISABLE) || \
606                                              ((ONEBIT) == SMARTCARD_ONE_BIT_SAMPLE_ENABLE))
607 
608 #define IS_SMARTCARD_NACK_STATE(NACK)       (((NACK) == SMARTCARD_NACK_ENABLE) || \
609                                              ((NACK) == SMARTCARD_NACK_DISABLE))
610 
611 #define IS_SMARTCARD_PRESCALER(PRESCALER)   (((PRESCALER) >= SMARTCARD_PRESCALER_SYSCLK_DIV2) && \
612                                              ((PRESCALER) <= SMARTCARD_PRESCALER_SYSCLK_DIV62) )
613 
614 /** SMARTCARD interruptions flag mask
615   *
616   */
617 #define SMARTCARD_IT_MASK   ((uint32_t) USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RXNEIE | \
618                                         USART_CR1_IDLEIE | USART_CR3_EIE )
619 
620 
621 /**
622   * @}
623   */
624 
625 
626 /* Exported functions --------------------------------------------------------*/
627 
628 /** @addtogroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
629   * @{
630   */
631 
632 /** @addtogroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions
633   * @{
634   */
635 
636 /* Initialization/de-initialization functions  **********************************/
637 HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc);
638 HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc);
639 void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc);
640 void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc);
641 
642 /**
643   * @}
644   */
645 
646 /** @addtogroup SMARTCARD_Exported_Functions_Group2 IO operation functions
647   * @{
648   */
649 
650 /* IO operation functions *******************************************************/
651 HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout);
652 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout);
653 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size);
654 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size);
655 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size);
656 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size);
657 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc);
658 void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc);
659 void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc);
660 void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc);
661 
662 /**
663   * @}
664   */
665 
666 /** @addtogroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions
667   * @{
668   */
669 
670 /* Peripheral State and Errors functions functions  *****************************/
671 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc);
672 uint32_t                   HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc);
673 
674 /**
675   * @}
676   */
677 
678 /**
679   * @}
680   */
681 
682 /**
683   * @}
684   */
685 
686 /**
687   * @}
688   */
689 
690 #ifdef __cplusplus
691 }
692 #endif
693 
694 #endif /* __STM32L1xx_HAL_SMARTCARD_H */
695 
696 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
697