BLE5.2 1.0
开发文档说明
xc_drv_i2c.h
浏览该文件的文档.
1
25#ifndef _XC_DRV_I2C_H_
26#define _XC_DRV_I2C_H_
27
28/*-----------------------------------------------------------------------------------
29 INCLUDE HEADE FILES
30 ------------------------------------------------------------------------------------*/
31#include <stdbool.h>
32#include <stdio.h>
33
34#include "xc6xxx.h"
35
36#define I2C_FIFO_LEN 16
37
38/* I2C Init structure definition */
39typedef struct
40{
41
43
44 uint16_t I2C_Mode;
45
46 uint16_t I2C_SlaveAddr;
47
48 uint8_t DivClk;
50
51enum
52{
56};
57
58typedef void (*i2c_write_callback)(void);
59typedef void (*i2c_read_callback)(uint8_t* buff, uint16_t len);
60
61typedef struct{
65/*------------------------------------------------------------------------------------
66 Macros
67 -------------------------------------------------------------------------------------*/
68
69#define I2C_CON_MASTER_MODE_ENABLE (0x1UL)
70#define I2C_CON_MASTER_MODE_DISABLE (0x0UL)
71
72#define I2C_CON_SPEED_STANDARD_MODE (0x1UL)
73#define I2C_CON_SPEED_FAST_MODE (0x2UL)
74
75#define I2C_CON_10BITADDR_SLAVE_MODE (0x1UL)
76#define I2C_CON_7BITADDR_SLAVE_MODE (0x0UL)
77
78#define I2C_CON_10BITADDR_MASTER_MODE (0x1UL)
79#define I2C_CON_7BITADDR_MASTER_MODE (0x0UL)
80
81#define I2C_CON_RESTART_EN_ENABLE (0x1UL)
82#define I2C_CON_RESTART_EN_DISABLE (0x0UL)
83
84#define I2C_CON_SLAVE_DISABLE_ENABLE (0x0UL)
85#define I2C_CON_SLAVE_DISABLE_DISABLE (0x1UL)
86
87#define I2C_TAR_GC_OR_START_GC (0x0UL)
88#define I2C_TAR_GC_OR_START_Start (0x1UL)
89
90#define I2C_TAR_SPECIA_EXECUTE (0x1UL)
91#define I2C_TAR_SPECIA_IGNORE (0x0UL)
92
93#define I2C_TAR_I2C_10BITADDR_MODE (0x1UL)
94#define I2C_TAR_I2C_7BITADDR_MODE (0x0UL)
95
96#define I2C_DATA_CMD_CMD_READ (0x1UL << CMD_POS)
97#define I2C_DATA_CMD_CMD_WRITE (0x0UL << CMD_POS)
98
99#define I2C_DATA_CMD_STOP_SET (0x1UL << I2C_STOP_POS)
100#define I2C_DATA_CMD_STOP_RESET (0x0UL << I2C_STOP_POS)
101
102#define I2C_DATA_CMD_RESTART_SET (0x1UL << RESTART_POS)
103#define I2C_DATA_CMD_RESTART_RESET (0x0UL << RESTART_POS)
104
105#define I2C_DATA_CMD_DAT_SET (0x1UL << DAT_LSB)
106#define I2C_DATA_CMD_DAT_RESET (0x0UL << DAT_LSB)
107
108#define I2C_MLCK32M_FREQUENCY_HCNT_100K (0x8EUL)
109#define I2C_MLCK32M_FREQUENCY_LCNT_100K (0x9EUL)
110#define I2C_MLCK32M_FREQUENCY_HCNT_400K (0x18UL)
111#define I2C_MLCK32M_FREQUENCY_LCNT_400K (0x25UL)
112#define I2C_MLCK32M_FREQUENCY_HCNT_1M (0x07UL)
113#define I2C_MCLK32M_FREQUENCY_LCNT_1M (0x0AUL)
114
115#define I2C_MLCK48M_FREQUENCY_HCNT_100K (0xDBUL)
116#define I2C_MLCK48M_FREQUENCY_LCNT_100K (0xEBUL)
117#define I2C_MLCK48M_FREQUENCY_HCNT_400K (0x28UL)
118#define I2C_MLCK48M_FREQUENCY_LCNT_400K (0x38UL)
119#define I2C_MLCK48M_FREQUENCY_HCNT_1M (0x0DUL)
120#define I2C_MCLK48M_FREQUENCY_LCNT_1M (0x0FUL)
121
122#define I2C_MLCK64M_FREQUENCY_HCNT_100K (0x128UL)
123#define I2C_MLCK64M_FREQUENCY_LCNT_100K (0x138UL)
124#define I2C_MLCK64M_FREQUENCY_HCNT_400K (0x39UL)
125#define I2C_MLCK64M_FREQUENCY_LCNT_400K (0x49UL)
126#define I2C_MLCK64M_FREQUENCY_HCNT_1M (0x10UL)
127#define I2C_MCLK64M_FREQUENCY_LCNT_1M (0x14UL)
128
129#define I2C_INTR_STAT_RX_UNDER_RESET (0x0UL << R_RX_UNDER_POS)
130#define I2C_INTR_STAT_RX_UNDER_SET (0x1UL << R_RX_UNDER_POS)
131
132#define I2C_INTR_STAT_RX_OVER_RESET (0x0UL << R_RX_OVER_POS)
133#define I2C_INTR_STAT_RX_OVER_SET (0x1UL << R_RX_OVER_POS)
134
135#define I2C_INTR_STAT_RX_FULL_RESET (0x0UL << R_RX_FULL_POS)
136#define I2C_INTR_STAT_RX_FULL_SET (0x1UL << R_RX_FULL_POS)
137
138#define I2C_INTR_STAT_TX_OVER_RESET (0x0UL << R_TX_OVER_POS)
139#define I2C_INTR_STAT_TX_OVER_SET (0x1UL << R_TX_OVER_POS)
140
141#define I2C_INTR_STAT_TX_EMPTY_RESET (0x0UL << R_TX_EMPTY_POS)
142#define I2C_INTR_STAT_TX_EMPTY_SET (0x1UL << R_TX_EMPTY_POS)
143
144#define I2C_INTR_STAT_RD_REQ_RESET (0x0UL << R_RD_REQ_POS)
145#define I2C_INTR_STAT_RD_REQ_SET (0x1UL << R_RD_REQ_POS)
146
147#define I2C_INTR_STAT_TX_ABRT_RESET (0x0UL << R_TX_ABRT_POS)
148#define I2C_INTR_STAT_TX_ABRT_SET (0x1UL << R_TX_ABRT_POS)
149
150#define I2C_INTR_STAT_RX_DONE_RESET (0x0UL << R_RX_DONE_POS)
151#define I2C_INTR_STAT_RX_DONE_SET (0x1UL << R_RX_DONE_POS)
152
153#define I2C_INTR_STAT_ACTIVITY_RESET (0x0UL << R_ACTIVITY_POS)
154#define I2C_INTR_STAT_ACTIVITY_SET (0x1UL << R_ACTIVITY_POS)
155
156#define I2C_INTR_STAT_STOP_DET_RESET (0x0UL << R_STOP_DET_POS)
157#define I2C_INTR_STAT_STOP_DET_SET (0x1UL << R_STOP_DET_POS)
158
159#define I2C_INTR_STAT_START_DET_RESET (0x1UL << R_START_DET_POS)
160#define I2C_INTR_STAT_START_DET_SET (0x1UL << R_START_DET_POS)
161
162#define I2C_INTR_STAT_GEN_CALL_RESET (0x0UL << R_GEN_CALL_POS)
163#define I2C_INTR_STAT_GEN_CALL_SET (0x1UL << R_GEN_CALL_POS)
164
165#define I2C_INTR_EN_RX_UNDER_DISABLE (0x0UL << EN_RX_UNDER_POS)
166#define I2C_INTR_EN_RX_UNDER_ENABLE (0x1UL << EN_RX_UNDER_POS)
167
168#define I2C_INTR_EN_RX_OVER_DISABLE (0x0UL << EN_RX_OVER_POS)
169#define I2C_INTR_EN_RX_OVER_ENABLE (0x1UL << EN_RX_OVER_POS)
170
171#define I2C_INTR_EN_RX_FULL_DISABLE (0x0UL << EN_RX_FULL_POS)
172#define I2C_INTR_EN_RX_FULL_ENABLE (0x1UL << EN_RX_FULL_POS)
173
174#define I2C_INTR_EN_TX_OVER_DISABLE (0x0UL << EN_TX_OVER_POS)
175#define I2C_INTR_EN_TX_OVER_ENABLE (0x1UL << EN_TX_OVER_POS)
176
177#define I2C_INTR_EN_TX_EMPTY_DISABLE (0x0UL << EN_TX_EMPTY_POS)
178#define I2C_INTR_EN_TX_EMPTY_ENABLE (0x1UL << EN_TX_EMPTY_POS)
179
180#define I2C_INTR_EN_RD_REQ_DISABLE (0x0UL << EN_RD_REQ_POS)
181#define I2C_INTR_EN_RD_REQ_ENABLE (0x1UL << EN_RD_REQ_POS)
182
183#define I2C_INTR_EN_TX_ABRT_DISABLE (0x0UL << EN_TX_ABRT_POS)
184#define I2C_INTR_EN_TX_ABRT_ENABLE (0x1UL << EN_TX_ABRT_POS)
185
186#define I2C_INTR_EN_RX_DONE_DISABLE (0x0UL << EN_RX_DONE_POS)
187#define I2C_INTR_EN_RX_DONE_ENABLE (0x1UL << EN_RX_DONE_POS)
188
189#define I2C_INTR_EN_ACTIVITY_DISABLE (0x0UL << EN_ACTIVITY_POS)
190#define I2C_INTR_EN_ACTIVITY_ENABLE (0x1UL << EN_ACTIVITY_POS)
191
192#define I2C_INTR_EN_STOP_DET_DISABLE (0x0UL << EN_STOP_DET_POS)
193#define I2C_INTR_EN_STOP_DET_ENABLE (0x1UL << EN_STOP_DET_POS)
194
195#define I2C_INTR_EN_START_DET_DISABLE (0x0UL << EN_START_DET_POS)
196#define I2C_INTR_EN_START_DET_ENABLE (0x1UL << EN_START_DET_POS)
197
198#define I2C_INTR_EN_GEN_CALL_DISABLE (0x0UL << EN_GEN_CALL_POS)
199#define I2C_INTR_EN_GEN_CALL_ENABLE (0x1UL << EN_GEN_CALL_POS)
200
201#define I2C_RAW_INTR_STAT_RX_UNDER_RESET (0x0UL << RX_UNDER_POS)
202#define I2C_RAW_INTR_STAT_RX_UNDERL_SET (0x1UL << RX_UNDER_POS)
203
204#define I2C_RAW_INTR_STAT_RX_OVER_RESET (0x0UL << RX_OVER_POS)
205#define I2C_RAW_INTR_STAT_RX_OVER_SET (0x1UL << RX_OVER_POS)
206
207#define I2C_RAW_INTR_STAT_RX_FULL_RESET (0x0UL << RX_FULL_POS)
208#define I2C_RAW_INTR_STAT_RX_FULL_SET (0x1UL << RX_FULL_POS)
209
210#define I2C_RAW_INTR_STAT_TX_OVER_RESET (0x0UL << TX_OVER_POS)
211#define I2C_RAW_INTR_STAT_TX_OVER_SET (0x1UL << TX_OVER_POS)
212
213#define I2C_RAW_INTR_STAT_TX_EMPTY_RESET (0x0UL << TX_EMPTY_POS)
214#define I2C_RAW_INTR_STAT_TX_EMPTY_SET (0x1UL << TX_EMPTY_POS)
215
216#define I2C_RAW_INTR_STAT_RD_REQ_RESET (0x0UL << RD_REQ_POS)
217#define I2C_RAW_INTR_STAT_RD_REQ_SET (0x1UL << RD_REQ_POS)
218
219#define I2C_RAW_INTR_STAT_TX_ABRT_RESET (0x0UL << TX_ABRT_POS)
220#define I2C_RAW_INTR_STAT_TX_ABRT_SET (0x1UL << TX_ABRT_POS)
221
222#define I2C_RAW_INTR_STAT_RX_DONE_RESET (0x0UL << RX_DONE_POS)
223#define I2C_RAW_INTR_STAT_RX_DONE_SET (0x1UL << RX_DONE_POS)
224
225#define I2C_RAW_INTR_STAT_ACTIVITY_RESET (0x0UL << ACTIVITY_POS)
226#define I2C_RAW_INTR_STAT_ACTIVITY_SET (0x1UL << ACTIVITY_POS)
227
228#define I2C_RAW_INTR_STAT_STOP_DET_RESET (0x0UL << STOP_DET_POS)
229#define I2C_RAW_INTR_STAT_STOP_DET_SET (0x1UL << STOP_DET_POS)
230
231#define I2C_RAW_INTR_STAT_START_DET_RESET (0x0UL << START_DET_POS)
232#define I2C_RAW_INTR_STAT_START_DET_SET (0x1UL << START_DET_POS)
233
234#define I2C_RAW_INTR_STAT_GEN_CALL_RESET (0x0UL << GEN_CALL_POS)
235#define I2C_RAW_INTR_STAT_GEN_CALL_SET (0x1UL << GEN_CALL_POS)
236
237#define I2C_RX_TL_FIFO_1 (0x0UL)
238#define I2C_RX_TL_FIFO_2 (0x1UL)
239#define I2C_RX_TL_FIFO_3 (0x2UL)
240#define I2C_RX_TL_FIFO_4 (0x3UL)
241#define I2C_RX_TL_FIFO_5 (0x4UL)
242#define I2C_RX_TL_FIFO_6 (0x5UL)
243#define I2C_RX_TL_FIFO_7 (0x6UL)
244#define I2C_RX_TL_FIFO_8 (0x7UL)
245#define I2C_RX_TL_FIFO_9 (0x8UL)
246#define I2C_RX_TL_FIFO_10 (0x9UL)
247#define I2C_RX_TL_FIFO_11 (0xAUL)
248#define I2C_RX_TL_FIFO_12 (0xBUL)
249#define I2C_RX_TL_FIFO_13 (0xCUL)
250#define I2C_RX_TL_FIFO_14 (0xDUL)
251#define I2C_RX_TL_FIFO_15 (0xEUL)
252#define I2C_RX_TL_FIFO_16 (0xFUL)
253
254#define I2C_TX_TL_FIFO_0 (0x0UL)
255#define I2C_TX_TL_FIFO_1 (0x1UL)
256#define I2C_TX_TL_FIFO_2 (0x2UL)
257#define I2C_TX_TL_FIFO_3 (0x3UL)
258#define I2C_TX_TL_FIFO_4 (0x4UL)
259#define I2C_TX_TL_FIFO_5 (0x5UL)
260#define I2C_TX_TL_FIFO_6 (0x6UL)
261#define I2C_TX_TL_FIFO_7 (0x7UL)
262#define I2C_TX_TL_FIFO_8 (0x8UL)
263#define I2C_TX_TL_FIFO_9 (0x9UL)
264#define I2C_TX_TL_FIFO_10 (0xAUL)
265#define I2C_TX_TL_FIFO_11 (0xBUL)
266#define I2C_TX_TL_FIFO_12 (0xCUL)
267#define I2C_TX_TL_FIFO_13 (0xDUL)
268#define I2C_TX_TL_FIFO_14 (0xEUL)
269#define I2C_TX_TL_FIFO_15 (0xFUL)
270
271#define I2C_ENABLE_EN_DISABLE (0x0UL)
272#define I2C_ENABLE_EN_ENABLE (0x1UL)
273
274#define I2C_STATUS_ACTIVITY_RESET (0x0UL << I2C_ACTIVITY_POS)
275#define I2C_STATUS_ACTIVITY_SET (0x1UL << I2C_ACTIVITY_POS)
276
277#define I2C_STATUS_TFNF_RESET (0x0UL << TFNF_POS)
278#define I2C_STATUS_TFNF_SET (0x1UL << TFNF_POS)
279
280#define I2C_STATUS_TFE_RESET (0x0UL << TFE_POS)
281#define I2C_STATUS_TFE_SET (0x1UL << TFE_POS)
282
283#define I2C_STATUS_RFNE_RESET (0x0UL << RFNE_POS)
284#define I2C_STATUS_RFNE_SET (0x1UL << RFNE_POS)
285
286#define I2C_STATUS_RFF_RESET (0x0UL << RFF_POS)
287#define I2C_STATUS_RFF_SET (0x1UL << RFF_POS)
288
289#define I2C_STATUS_MST_ACTIVITY_RESET (0x0UL << 5)
290#define I2C_STATUS_MST_ACTIVITY_SET (0x1UL << 5)
291
292
293
294#define I2C_MODE_MASTER I2C_CON_MASTER_MODE_ENABLE
295#define I2C_MODE_SLAVE I2C_CON_SLAVE_DISABLE_ENABLE
296
297#define I2C_CLOCK_SPEED_100K I2C_FREQUENCY_100K
298#define I2C_CLOCK_SPEED_400K I2C_FREQUENCY_400K
299#define I2C_CLOCK_SPEED_1M I2C_FREQUENCY_1M
300
301void xc_i2c_init(I2C_InitCfg_t *i2c_cfg);
302void xc_i2c_enable_it(uint32_t val);
303void xc_i2c_fifo_rxtl_set(uint32_t val);
304void xc_i2c_fifo_txtl_set(uint32_t val);
305
306FlagStatus i2c_flag_status_get(uint32_t i2c_flag);
309
310uint32_t xc_i2c_write_block(uint8_t dev_addr, uint8_t *src, size_t len, uint16_t reset, uint16_t stop);
311uint32_t xc_i2c_read_block(uint8_t dev_addr, uint8_t *dst, size_t len, uint16_t reset, uint16_t stop);
312#endif
i2c_write_callback i2c_write_cb
Definition xc_drv_i2c.h:63
i2c_read_callback i2c_read_cb
Definition xc_drv_i2c.h:62
uint16_t I2C_Mode
Definition xc_drv_i2c.h:44
uint16_t I2C_SlaveAddr
Definition xc_drv_i2c.h:46
uint8_t DivClk
Definition xc_drv_i2c.h:48
uint32_t I2C_ClockSpeed
Definition xc_drv_i2c.h:42
void(* i2c_read_callback)(uint8_t *buff, uint16_t len)
Definition xc_drv_i2c.h:59
void xc_i2c_fifo_txtl_set(uint32_t val)
Definition xc_drv_i2c.c:145
void xc_i2c_register_write_cb(i2c_write_callback i2c_cb)
Definition xc_drv_i2c.c:156
FlagStatus i2c_flag_status_get(uint32_t i2c_flag)
Definition xc_drv_i2c.c:259
uint32_t xc_i2c_read_block(uint8_t dev_addr, uint8_t *dst, size_t len, uint16_t reset, uint16_t stop)
Definition xc_drv_i2c.c:195
void xc_i2c_fifo_rxtl_set(uint32_t val)
Definition xc_drv_i2c.c:143
void xc_i2c_enable_it(uint32_t val)
Definition xc_drv_i2c.c:141
void xc_i2c_register_read_cb(i2c_read_callback i2c_cb)
Definition xc_drv_i2c.c:154
@ I2C_FREQUENCY_400K
Definition xc_drv_i2c.h:54
@ I2C_FREQUENCY_1M
Definition xc_drv_i2c.h:55
@ I2C_FREQUENCY_100K
Definition xc_drv_i2c.h:53
void(* i2c_write_callback)(void)
Definition xc_drv_i2c.h:58
void xc_i2c_init(I2C_InitCfg_t *i2c_cfg)
Definition xc_drv_i2c.c:52
uint32_t xc_i2c_write_block(uint8_t dev_addr, uint8_t *src, size_t len, uint16_t reset, uint16_t stop)
Definition xc_drv_i2c.c:165