1 /**
2   ******************************************************************************
3   * @file    lib_i2c.h
4   * @author  Application Team
5   * @version V4.5.0
6   * @date    2019-05-14
7   * @brief   IIC library.
8   ******************************************************************************
9   * @attention
10   *
11   ******************************************************************************
12   */
13 #ifndef __LIB_I2C_H
14 #define __LIB_I2C_H
15 
16 #ifdef __cplusplus
17  extern "C" {
18 #endif
19 
20 #include "target.h"
21 
22 typedef struct
23 {
24   uint32_t SlaveAddr;
25   uint32_t GeneralCallAck;
26   uint32_t AssertAcknowledge;
27   uint32_t ClockSource;
28 } I2C_InitType;
29 //GeneralCallAck
30 #define I2C_GENERALCALLACK_ENABLE   I2C_ADDR_GC
31 #define I2C_GENERALCALLACK_DISABLE  0
32 //AssertAcknowledge
33 #define I2C_ASSERTACKNOWLEDGE_ENABLE    I2C_CTRL_AA
34 #define I2C_ASSERTACKNOWLEDGE_DISABLE   0
35 //ClockSource
36 #define I2C_CLOCKSOURCE_APBD256     I2C_CTRL_CR_0
37 #define I2C_CLOCKSOURCE_APBD224     I2C_CTRL_CR_1
38 #define I2C_CLOCKSOURCE_APBD192     I2C_CTRL_CR_2
39 #define I2C_CLOCKSOURCE_APBD160     I2C_CTRL_CR_3
40 #define I2C_CLOCKSOURCE_APBD960     I2C_CTRL_CR_4
41 #define I2C_CLOCKSOURCE_APBD120     I2C_CTRL_CR_5
42 #define I2C_CLOCKSOURCE_APBD60      I2C_CTRL_CR_6
43 #define I2C_CLOCKSOURCE_TIM3OFD8    I2C_CTRL_CR_7
44 
45 typedef struct
46 {
47   uint16_t SlaveAddr;
48   uint8_t SubAddrType;
49   uint32_t PageRange;
50   uint32_t SubAddress;
51   uint8_t *pBuffer;
52   uint32_t Length;
53 } I2C_WRType;
54 //SubAddrType
55 #define I2C_SUBADDR_1BYTE   1
56 #define I2C_SUBADDR_2BYTE   2
57 #define I2C_SUBADDR_OTHER   3
58 
59 //remap
60 #define I2C_REMAP_ENABLE    1
61 #define I2C_REMAP_DISABLE   0
62 
63 /* Private macros ------------------------------------------------------------*/
64 
65 #define IS_I2C_GC(__GC__)  (((__GC__) == I2C_GENERALCALLACK_ENABLE) ||\
66                             ((__GC__) == I2C_GENERALCALLACK_DISABLE))
67 
68 #define IS_I2C_AA(__AA__)  (((__AA__) == I2C_ASSERTACKNOWLEDGE_ENABLE) ||\
69                             ((__AA__) == I2C_ASSERTACKNOWLEDGE_DISABLE))
70 
71 #define IS_I2C_CLKSRC(__CLKSRC__)  (((__CLKSRC__) == I2C_CLOCKSOURCE_APBD256) ||\
72                                     ((__CLKSRC__) == I2C_CLOCKSOURCE_APBD224) ||\
73                                     ((__CLKSRC__) == I2C_CLOCKSOURCE_APBD192) ||\
74                                     ((__CLKSRC__) == I2C_CLOCKSOURCE_APBD160) ||\
75                                     ((__CLKSRC__) == I2C_CLOCKSOURCE_APBD960) ||\
76                                     ((__CLKSRC__) == I2C_CLOCKSOURCE_APBD120) ||\
77                                     ((__CLKSRC__) == I2C_CLOCKSOURCE_APBD60)  ||\
78                                     ((__CLKSRC__) == I2C_CLOCKSOURCE_TIM3OFD8))
79 
80 #define I2C_SUBADDR_TYPE(__TYPE__)  (((__TYPE__) == I2C_SUBADDR_1BYTE) ||\
81                                      ((__TYPE__) == I2C_SUBADDR_2BYTE) ||\
82                                      ((__TYPE__) == I2C_SUBADDR_OTHER))
83 
84 /* Exported Functions ------------------------------------------------------- */
85 /* I2C Exported Functions Group1:
86                                    (De)Initialization ------------------------*/
87 void I2C_DeInit(uint32_t remap);
88 void I2C_StructInit(I2C_InitType *InitStruct);
89 void I2C_Init(I2C_InitType *InitStruct);
90 /* I2C Exported Functions Group2:
91                                    Interrupt ---------------------------------*/
92 void I2C_INTConfig(uint32_t NewState);
93 uint8_t I2C_GetINTStatus(void);
94 void I2C_ClearINTStatus(void);
95 /* I2C Exported Functions Group3:
96                                    Transfer datas ----------------------------*/
97 uint16_t I2C_MasterReadBytes(I2C_WRType *InitStruct);
98 uint16_t I2C_MasterWriteBytes(I2C_WRType *InitStruct);
99 /* I2C Exported Functions Group4:
100                                    MISC Configuration ------------------------*/
101 void I2C_Cmd(uint32_t NewState);
102 
103 /* I2C Exported Functions Group5:
104                                    Others ------------------------------------*/
105 void I2C_AssertAcknowledgeConfig(uint32_t NewState);
106 uint8_t I2C_ReceiveData(void);
107 void I2C_SendData(uint8_t Dat);
108 void I2C_GenerateSTART(uint32_t NewState);
109 void I2C_GenerateSTOP(uint32_t NewState);
110 uint8_t I2C_GetStatusCode(void);
111 
112 
113 #ifdef __cplusplus
114 }
115 #endif
116 
117 #endif  /* __LIB_I2C_H */
118 
119 /*********************************** END OF FILE ******************************/
120