1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
|
/******************************************************************************
* @brief header file for I2C module utilities (I2C).
*
*******************************************************************************
*
* provide APIs for accessing I2C module (I2C)
******************************************************************************/
#ifndef _I2C_H__
#define _I2C_H__
#ifdef __cplusplus
extern "C" {
#endif
/******************************************************************************
* Global variables
******************************************************************************/
/******************************************************************************
* Constants and macros
******************************************************************************/
/******************************************************************************
* define I2C work read or write
*
*//*! @addtogroup i2c_read_write_list
* @{
*******************************************************************************/
#define I2C_READ 0x01 /*!< I2C read */
#define I2C_WRITE 0x0 /*!< I2C write */
/*! @} End of i2c_read_write_list */
#define I2C_SEND_ACK 0 /*!< I2C send ACK */
#define I2C_SEND_NACK 1 /*!< I2C send NACK */
#define I2C_WAIT_STATUS_ETMEOUT 200000
/******************************************************************************
* define I2C error state
*
*//*! @addtogroup i2c_error_state_list
* @{
*******************************************************************************/
#define I2C_ERROR_NULL 0x00 /*!< I2C sucess*/
#define I2C_ERROR_NO_WAIT_TCF_FLAG 0x01 /*!< I2C wait TCF overETMe*/
#define I2C_ERROR_NO_WAIT_IICIF_FLAG 0x02 /*!< I2C wait IICIF overETMe */
#define I2C_ERROR_NO_GET_ACK 0x04 /*!< I2C no get ACK */
#define I2C_ERROR_START_NO_BUSY_FLAG 0x10 /*!< I2C fail to send start signals */
#define I2C_ERROR_STOP_BUSY_FLAG 0x20 /*!< I2C fail to send stop signal */
#define I2C_ERROR_BUS_BUSY 0x80 /*!< I2C bus busy error */
/*! @} End of i2c_error_state_list */
/******************************************************************************
* define I2C bus state
*
*//*! @addtogroup i2c_bus_state_list
* @{
*******************************************************************************/
#define I2C_BUS_NORMAL 0x00 /*!< I2C bus normal */
#define I2C_BUS_SLTF 0x01 /*!< I2C bus SLTF flag */
#define I2C_BUS_SHTF2 0x02 /*!< I2C bus SHTF2 flag */
/*! @} End of i2c_bus_state_list */
/******************************************************************************
* Local types
******************************************************************************/
/******************************************************************************
* Local function prototypes
******************************************************************************/
/******************************************************************************
* Local variables
******************************************************************************/
#define I2C_MODE_MASTER 1
#define I2C_MODE_SLAVE 0
#define I2C_ADDRESS_7BIT 0
#define I2C_ADDRESS_10BIT 1
#define I2C_ETMEOUT_BUS_CLOCK_DIV64 0
#define I2C_ETMEOUT_BUS_CLOCK 1
/******************************************************************************
*
*//*! @addtogroup i2c_setting_type
* @{
*******************************************************************************/
/*!
* @brief I2C setting type.
*
*/
typedef struct
{
uint16_t bI2CEn :1; /*!< enable I2C module */
uint16_t bIntEn :1; /*!< enable I2C enable */
uint16_t bWakeUpEn :1; /*!< I2C wake up enable */
uint16_t bGCAEn :1; /*!< I2C General call address enable */
uint16_t bAddressExt :1; /*!< I2C extertion address selection */
uint16_t bRangeAddEn :1; /*!< enable range address */
uint16_t bFackEn :1; /*!< enable fast ack */
uint16_t bSMB_AlertEn :1; /*!< SMB Alert enable */
uint16_t bSecondAddressEn:1; /*!< enable the second address */
uint16_t bETMeoutCountClockSelect:1; /*!< ETMeoutCountClockSelect */
uint16_t bSHTF2IntEn :1; /*!< SHTF2 interrupt enable */
uint16_t Reserve :5;
}I2C_SettingType;
/*! @} End of i2c_setting_type */
/******************************************************************************
*
*//*! @addtogroup i2c_config_type
* @{
*******************************************************************************/
/*!
* @brief I2C configure type.
*
*/
typedef struct
{
I2C_SettingType sSetting;
uint16_t u16F; /*!< setting the band rate for I2C */
uint16_t u16OwnA1; /*!< slave address */
uint16_t u16OwnA2; /*!< the second slave address */
uint16_t u16RangeA; /*!< range address */
uint16_t u16Filt; /*!< Filter for I2C */
uint16_t u16Slt; /*!< SCL Low ETMeout register low */
}I2C_ConfigType, *I2C_ConfigPtr;
/*! @} End of i2c_config_type */
/******************************************************************************
*
*//*! @addtogroup i2c_callback
* @{
*******************************************************************************/
typedef void (*I2C_CallbackType)(void); /*!< I2C call back function */
/*! @} End of i2c_callback */
/******************************************************************************
* inline functions
******************************************************************************/
/******************************************************************************
*
*//*! @addtogroup i2c_api_list
* @{
*******************************************************************************/
/*****************************************************************************//*!
*
* @brief enable I2C to transmit data.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
__STATIC_INLINE void I2C_TxEnable(I2C_Type *pI2Cx)
{
pI2Cx->C1 |= I2C_C1_TX_MASK;
}
/*****************************************************************************//*!
*
* @brief enable I2C to receive data.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*****************************************************************************/
__STATIC_INLINE void I2C_RxEnable(I2C_Type *pI2Cx)
{
pI2Cx->C1 &= ~I2C_C1_TX_MASK;
}
/*****************************************************************************//*!
*
* @brief set IIC band rate.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
__STATIC_INLINE void I2C_SetBaudRate(I2C_Type *pI2Cx,uint32_t u32Bps)
{
pI2Cx->F = (uint8_t)u32Bps;
}
/*****************************************************************************//*!
*
* @brief enable general call.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*****************************************************************************/
__STATIC_INLINE void I2C_GeneralCallEnable(I2C_Type *pI2Cx)
{
pI2Cx->C2 |= I2C_C2_GCAEN_MASK;
}
/*****************************************************************************//*!
*
* @brief SMBus Alert Response Address Enable.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
__STATIC_INLINE void I2C_SMBusAlertEnable(I2C_Type *pI2Cx)
{
pI2Cx->SMB|= I2C_SMB_ALERTEN_MASK;
}
/*****************************************************************************//*!
*
* @brief Range Address Matching Enable.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
__STATIC_INLINE void I2C_RangeAddressEnable(I2C_Type *pI2Cx)
{
pI2Cx->C2 |= I2C_C2_RMEN_MASK;
}
/*****************************************************************************//*!
*
* @brief SHTF2 Interrupt Enable.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
__STATIC_INLINE void I2C_SHTF2IntEnable(I2C_Type *pI2Cx)
{
pI2Cx->SMB |= I2C_SMB_SHTF2IE_MASK;
}
/*****************************************************************************//*!
*
* @brief ETMeout Counter Clock Select.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
__STATIC_INLINE void I2C_ETMeoutCounterClockSelect(I2C_Type *pI2Cx, uint8_t u8Clock)
{
if( u8Clock )
{
pI2Cx->SMB |= I2C_SMB_TCKSEL_MASK;
}
else
{
pI2Cx->SMB &= ~I2C_SMB_TCKSEL_MASK;
}
}
/*****************************************************************************//*!
*
* @brief get I2C status.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return I2C status
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
__STATIC_INLINE uint8_t I2C_GetStatus(I2C_Type *pI2Cx)
{
return pI2Cx->S;
}
/*****************************************************************************//*!
*
* @brief clear specified status.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
__STATIC_INLINE void I2C_ClearStatus(I2C_Type *pI2Cx, uint8_t u8ClearFlag)
{
pI2Cx->S |= u8ClearFlag;
}
/*****************************************************************************//*!
*
* @brief write data to data register.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
__STATIC_INLINE void I2C_WriteDataReg(I2C_Type *pI2Cx, uint8_t u8DataBuff)
{
pI2Cx->D = u8DataBuff;
}
/*****************************************************************************//*!
*
* @brief read data from data register.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return I2C data register value
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
__STATIC_INLINE uint8_t I2C_ReadDataReg(I2C_Type *pI2Cx )
{
return pI2Cx->D;
}
/*****************************************************************************//*!
*
* @brief check if is Tx mode.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return result TRUE or FALSE
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
__STATIC_INLINE uint8_t I2C_IsTxMode(I2C_Type *pI2Cx )
{
return(pI2Cx->C1 & I2C_C1_TX_MASK);
}
/*****************************************************************************//*!
*
* @brief check I2C if busy.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return result TRUE or FALSE
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
__STATIC_INLINE uint8_t I2C_IsBusy(I2C_Type *pI2Cx )
{
return (pI2Cx->S & I2C_S_BUSY_MASK);
}
/*****************************************************************************//*!
*
* @brief check I2C receive ack or nack.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return result TRUE or FALSE
*
* @ Pass/ Fail criteria: none.
*****************************************************************************/
__STATIC_INLINE uint8_t I2C_IsReceivedAck(I2C_Type *pI2Cx )
{
return (pI2Cx->S & I2C_S_RXAK_MASK);
}
/*****************************************************************************//*!
*
* @brief check I2C if is master mode.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return result TRUE or FALSE.
*
* @ Pass/ Fail criteria: none.
*****************************************************************************/
__STATIC_INLINE uint8_t I2C_IsMasterMode(I2C_Type *pI2Cx )
{
return(pI2Cx->C1 & I2C_C1_MST_MASK);
}
/*****************************************************************************//*!
*
* @brief check SCL Low ETMeout Flag.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return result TRUE or FALSE.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
__STATIC_INLINE uint8_t I2C_IsSMB_SLTF(I2C_Type *pI2Cx )
{
return (pI2Cx->SMB & I2C_SMB_SLTF_MASK);
}
/*****************************************************************************//*!
*
* @brief check SCL High ETMeout Flag is set or clear.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return result TRUE or FALSE.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
__STATIC_INLINE uint8_t I2C_IsSMB_SHTF2(I2C_Type *pI2Cx )
{
return(pI2Cx->SMB & I2C_SMB_SHTF2_MASK);
}
/*****************************************************************************//*!
*
* @brief clear SLTF flag.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*****************************************************************************/
__STATIC_INLINE void I2C_ClearSLTF(I2C_Type *pI2Cx )
{
pI2Cx->SMB |= I2C_SMB_SLTF_MASK;
}
/*****************************************************************************//*!
*
* @brief clear SHTF2 flag.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
__STATIC_INLINE void I2C_ClearSHTF2(I2C_Type *pI2Cx )
{
pI2Cx->SMB |= I2C_SMB_SHTF2_MASK;
}
/*****************************************************************************//*!
*
* @brief send out ACK.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*****************************************************************************/
__STATIC_INLINE void I2C_SendAck(I2C_Type *pI2Cx )
{
pI2Cx->C1 &= ~I2C_C1_TXAK_MASK;
}
/*****************************************************************************//*!
*
* @brief send out NACK.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*****************************************************************************/
__STATIC_INLINE void I2C_SendNack(I2C_Type *pI2Cx )
{
pI2Cx->C1 |= I2C_C1_TXAK_MASK;
}
/*****************************************************************************//*!
*
* @brief Second I2C Address Enable.
*
* @param[in] pI2Cx point to I2C module type.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*****************************************************************************/
__STATIC_INLINE void I2C_SecondAddressEnable(I2C_Type *pI2Cx)
{
pI2Cx->SMB |= I2C_SMB_SIICAEN_MASK;
}
/******************************************************************************
* Global functions
******************************************************************************/
void I2C_Init(I2C_Type *pI2Cx,I2C_ConfigPtr pI2CConfig);
uint8_t I2C_Start(I2C_Type *pI2Cx);
uint8_t I2C_Stop(I2C_Type *pI2Cx);
uint8_t I2C_RepeatStart(I2C_Type *pI2Cx);
uint8_t I2C_IsTxMode(I2C_Type *pI2Cx );
uint8_t I2C_IsBusy(I2C_Type *pI2Cx );
uint8_t I2C_IsReceivedAck(I2C_Type *pI2Cx );
uint8_t I2C_IsMasterMode(I2C_Type *pI2Cx );
void I2C_ClearSHTF2(I2C_Type *pI2Cx );
void I2C_ClearSLTF(I2C_Type *pI2Cx );
uint8_t I2C_IsSMB_SHTF2(I2C_Type *pI2Cx );
uint8_t I2C_IsSMB_SLTF(I2C_Type *pI2Cx );
void I2C_TxEnable(I2C_Type *pI2Cx);
void I2C_RxEnable(I2C_Type *pI2Cx);
void I2C_IntEnable(I2C_Type *pI2Cx);
void I2C_IntDisable(I2C_Type *pI2Cx);
void I2C_SetBaudRate(I2C_Type *pI2Cx,uint32_t u32Bps);
void I2C_SetSlaveAddress(I2C_Type *pI2Cx,uint16_t u16SlaveAddress);
void I2C_GeneralCallEnable(I2C_Type *pI2Cx);
void I2C_SMBusAlertEnable(I2C_Type *pI2Cx);
void I2C_RangeAddressEnable(I2C_Type *pI2Cx);
void I2C_SHTF2IntEnable(I2C_Type *pI2Cx);
void I2C_ETMeoutCounterClockSelect(I2C_Type *pI2Cx, uint8_t u8Clock);
void I2C_SetSCLLowETMeout(I2C_Type *pI2Cx, uint16_t u16ETMeout);
uint8_t I2C_GetStatus(I2C_Type *pI2Cx);
void I2C_ClearStatus(I2C_Type *pI2Cx, uint8_t u8ClearFlag);
void I2C_SendAck(I2C_Type *pI2Cx );
void I2C_SendNack(I2C_Type *pI2Cx );
void I2C_SecondAddressEnable(I2C_Type *pI2Cx);
void I2C_ClearStatus(I2C_Type *pI2Cx, uint8_t u8ClearFlag);
void I2C_WriteDataReg(I2C_Type *pI2Cx, uint8_t u8DataBuff);
uint8_t I2C_ReadDataReg(I2C_Type *pI2Cx );
void I2C_Deinit(I2C_Type *pI2Cx);
uint8_t I2C_WriteOneByte(I2C_Type *pI2Cx, uint8_t u8WrBuff);
uint8_t I2C_ReadOneByte(I2C_Type *pI2Cx, uint8_t *pRdBuff, uint8_t u8Ack);
uint8_t I2C_MasterSendWait(I2C_Type *pI2Cx,uint16_t u16SlaveAddress,uint8_t *pWrBuff,uint32_t u32Length);
uint8_t I2C_MasterReadWait(I2C_Type *pI2Cx,uint16_t u16SlaveAddress,uint8_t *pRdBuff,uint32_t u32Length);
void I2C0_SetCallBack( I2C_CallbackType pCallBack );
void I2C1_SetCallBack( I2C_CallbackType pCallBack );
/*! @} End of i2c_bus_state_list */
#ifdef __cplusplus
}
#endif
#endif //
|