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