1 /**
2   ******************************************************************************
3   * @file    lib_iso7816.h
4   * @author  Application Team
5   * @version V4.4.0
6   * @date    2018-09-27
7   * @brief   ISO7816 library.
8   ******************************************************************************
9   * @attention
10   *
11   ******************************************************************************
12   */
13 #ifndef __LIB_ISO7816_H
14 #define __LIB_ISO7816_H
15 
16 #ifdef __cplusplus
17  extern "C" {
18 #endif
19 
20 #include "target.h"
21 
22 typedef struct
23 {
24   uint32_t FirstBit;
25   uint32_t ACKLen;
26   uint32_t Parity;
27   uint32_t Baudrate;
28 } ISO7816_InitType;
29 
30 //FirstBit
31 #define ISO7816_FIRSTBIT_LSB    ISO7816_INFO_LSB
32 #define ISO7816_FIRSTBIT_MSB    0
33 #define IS_ISO7816_FIRSTBIT(__FIRSTBIT__)  (((__FIRSTBIT__) == ISO7816_FIRSTBIT_LSB) ||\
34                                             ((__FIRSTBIT__) == ISO7816_FIRSTBIT_MSB))
35 //ACKLen
36 #define ISO7816_ACKLEN_1        0
37 #define ISO7816_ACKLEN_2        ISO7816_CFG_ACKLEN
38 #define IS_ISO7816_ACKLEN(__ACKLEN__)  (((__ACKLEN__) == ISO7816_ACKLEN_1) ||\
39                                         ((__ACKLEN__) == ISO7816_ACKLEN_2))
40 //Parity
41 #define ISO7816_PARITY_EVEN     0
42 #define ISO7816_PARITY_ODD      ISO7816_CFG_CHKP
43 #define IS_ISO7816_PARITY(__PARITY__)  (((__PARITY__) == ISO7816_PARITY_EVEN) || ((__PARITY__) == ISO7816_PARITY_ODD))
44 
45 #define IS_ISO7816_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) > 299UL)
46 #define IS_ISO7816_PRESCALER(__PRESCALER__)  (((__PRESCALER__) <= 0x80) && ((__PRESCALER__) > 0U))
47 
48 //interrupt
49 #define ISO7816_INT_RXOV        ISO7816_CFG_OVIE
50 #define ISO7816_INT_TX          ISO7816_CFG_SDIE
51 #define ISO7816_INT_RX          ISO7816_CFG_RCIE
52 #define ISO7816_INT_Msk         (ISO7816_INT_RXOV \
53                                 |ISO7816_INT_TX   \
54                                 |ISO7816_INT_RX)
55 #define IS_ISO7816_INT(__INT__)  ((((__INT__) & ISO7816_INT_Msk) != 0U) &&\
56                                   (((__INT__) & ~ISO7816_INT_Msk) == 0U))
57 
58 //INTStatus
59 #define ISO7816_INTSTS_RXOV     ISO7816_INFO_OVIF
60 #define ISO7816_INTSTS_TX       ISO7816_INFO_SDIF
61 #define ISO7816_INTSTS_RX       ISO7816_INFO_RCIF
62 #define ISO7816_INTSTS_Msk      (ISO7816_INTSTS_RXOV \
63                                  |ISO7816_INTSTS_TX  \
64                                  |ISO7816_INTSTS_RX)
65 #define IS_ISO7816_INTFLAGR(__INTFLAG__)  (((__INTFLAG__) == ISO7816_INTSTS_RXOV)   ||\
66                                            ((__INTFLAG__) == ISO7816_INTSTS_TX)     ||\
67                                            ((__INTFLAG__) == ISO7816_INTSTS_RX))
68 
69 #define IS_ISO7816_INTFLAGC(__INTFLAG__)  ((((__INTFLAG__)&ISO7816_INTSTS_Msk) != 0U) &&\
70                                            (((__INTFLAG__)&(~ISO7816_INTSTS_Msk)) == 0U))
71 //status
72 #define ISO7816_FLAG_SDERR      ISO7816_INFO_SDERR
73 #define ISO7816_FLAG_RCERR      ISO7816_INFO_RCERR
74 #define ISO7816_FLAG_Msk        (ISO7816_FLAG_SDERR|ISO7816_FLAG_RCERR)
75 #define IS_ISO7816_FLAGR(__FLAG__)  (((__FLAG__) == ISO7816_FLAG_SDERR) || ((__FLAG__) == ISO7816_FLAG_RCERR))
76 #define IS_ISO7816_FLAGC(__FLAG__)  ((((__FLAG__) & ISO7816_FLAG_Msk) != 0U) &&\
77                                      (((__FLAG__) & (~ISO7816_FLAG_Msk)) == 0U))
78 
79 /* Exported Functions ------------------------------------------------------- */
80 void ISO7816_DeInit(ISO7816_TypeDef *ISO7816x);
81 void ISO7816_StructInit(ISO7816_InitType *InitStruct);
82 void ISO7816_Init(ISO7816_TypeDef *ISO7816x, ISO7816_InitType *Init_Struct);
83 void ISO7816_Cmd(ISO7816_TypeDef *ISO7816x, uint32_t NewState);
84 void ISO7816_BaudrateConfig(ISO7816_TypeDef *ISO7816x, uint32_t BaudRate);
85 void ISO7816_CLKDIVConfig(ISO7816_TypeDef *ISO7816x, uint32_t Prescaler);
86 void ISO7816_CLKOutputCmd(ISO7816_TypeDef *ISO7816x, uint32_t NewState);
87 void ISO7816_SendData(ISO7816_TypeDef *ISO7816x, uint8_t ch);
88 uint8_t ISO7816_ReceiveData(ISO7816_TypeDef *ISO7816x);
89 void ISO7816_INTConfig(ISO7816_TypeDef *ISO7816x, uint32_t INTMask, uint8_t NewState);
90 uint8_t ISO7816_GetINTStatus(ISO7816_TypeDef *ISO7816x, uint32_t INTMask);
91 void ISO7816_ClearINTStatus(ISO7816_TypeDef *ISO7816x, uint32_t INTMask);
92 uint8_t ISO7816_GetFlag(ISO7816_TypeDef *ISO7816x, uint32_t FlagMask);
93 void ISO7816_ClearFlag(ISO7816_TypeDef *ISO7816x, uint32_t FlagMask);
94 uint8_t ISO7816_GetLastTransmitACK(ISO7816_TypeDef *ISO7816x);
95 uint8_t ISO7816_GetLastReceiveCHKSUM(ISO7816_TypeDef *ISO7816x);
96 
97 
98 #ifdef __cplusplus
99 }
100 #endif
101 
102 #endif /* __LIB_ISO7816_H */
103 
104 /*********************************** END OF FILE ******************************/
105