NANO102/112 BSP V3.03.003
The Board Support Package for Nano102/112 Series
i2c.h
Go to the documentation of this file.
1/****************************************************************************/
12#ifndef __I2C_H__
13#define __I2C_H__
14
15#ifdef __cplusplus
16extern "C"
17{
18#endif
19
20
33#define I2C_STA 0x08
34#define I2C_STO 0x04
35#define I2C_SI 0x10
36#define I2C_AA 0x02
38#define I2C_GCMODE_ENABLE 1
39#define I2C_GCMODE_DISABLE 0 /* end of group NANO1X2_I2C_EXPORTED_CONSTANTS */
42
43
55#define I2C_SET_CONTROL_REG(i2c, u8Ctrl) ( (i2c)->CON = ((i2c)->CON & ~0x1e) | u8Ctrl )
56
63#define I2C_START(i2c) ( (i2c)->CON = ((i2c)->CON & ~I2C_CON_I2C_STS_Msk) | I2C_CON_START_Msk )
64
72static __INLINE int32_t I2C_STOP(I2C_T *i2c)
73{
74 int32_t tout = (SystemCoreClock / 10);
75
77 while((i2c->CON & I2C_CON_STOP_Msk) && (tout-- > 0));
78 if (i2c->CON & I2C_CON_STOP_Msk)
79 return -1;
80 return 0;
81}
82
90static __INLINE int32_t I2C_WAIT_READY(I2C_T *i2c)
91{
92 int32_t tout = (SystemCoreClock / 10);
93
94 while(!(i2c->INTSTS & I2C_INTSTS_INTSTS_Msk) && (tout-- > 0));
95 if (!(i2c->INTSTS & I2C_INTSTS_INTSTS_Msk))
96 return -1;
98 return 0;
99}
100
107#define I2C_GET_DATA(i2c) ((i2c)->DATA )
108
116#define I2C_SET_DATA(i2c, u8Data) ( (i2c)->DATA = u8Data )
117
124#define I2C_GET_STATUS(i2c) ( (i2c)->STATUS )
125
134#define I2C_GET_TIMEOUT_FLAG(i2c) ( ((i2c)->INTSTS & I2C_INTSTS_TIF_Msk) == I2C_INTSTS_TIF_Msk ? 1:0 )
135
142#define I2C_CLEAR_TIMEOUT_FLAG(i2c) ( (i2c)->INTSTS |= I2C_INTSTS_TIF_Msk )
143
152#define I2C_GET_WAKEUP_FLAG(i2c) ( ((i2c)->STATUS2 & I2C_STATUS2_WKUPIF_Msk) == I2C_STATUS2_WKUPIF_Msk ? 1:0 )
153
162#define I2C_GET_WAKEUP_ACK_DONE_FLAG(i2c) ( ((i2c)->INTSTS & I2C_INTSTS_WAKEUP_ACK_DONE_Msk) == I2C_INTSTS_WAKEUP_ACK_DONE_Msk ? 1:0 )
163
170#define I2C_CLEAR_WAKEUP_FLAG(i2c) ( (i2c)->STATUS2 |= I2C_STATUS2_WKUPIF_Msk )
171
179#define I2C_CLEAR_WAKEUP_ACK_DONE_FLAG(i2c) ( (i2c)->INTSTS |= I2C_INTSTS_WAKEUP_ACK_DONE_Msk )
180
187#define I2C_DISABLE_FIFO(i2c) ( (i2c)->CON2 &= ~I2C_CON2_TWOFF_EN_Msk )
188
195#define I2C_ENABLE_FIFO(i2c) ( (i2c)->CON2 |= I2C_CON2_TWOFF_EN_Msk )
196
203#define I2C_DISABLE_CLOCK_STRETCH(i2c) ( (i2c)->CON2 |= I2C_CON2_NOSTRETCH_Msk )
204
211#define I2C_ENABLE_CLOCK_STRETCH(i2c) ( (i2c)->CON2 &= ~I2C_CON2_NOSTRETCH_Msk )
212
219#define I2C_DISABLE_OVERRUN_INT(i2c) ( (i2c)->CON2 &= ~I2C_CON2_OVER_INTEN_Msk )
220
227#define I2C_ENABLE_OVERRUN_INT(i2c) ( (i2c)->CON2 |= I2C_CON2_OVER_INTEN_Msk )
228
235#define I2C_ENABLE_UNDERRUN_INT(i2c) ( (i2c)->CON2 |= I2C_CON2_UNDER_INTEN_Msk )
236
243#define I2C_DISABLE_UNDERRUN_INT(i2c) ((i2c)->CON2 &= ~I2C_CON2_UNDER_INTEN_Msk )
244
253#define I2C_GET_BUS_FREE_FLAG(i2c) ( ((i2c)->STATUS2 & I2C_STATUS2_BUS_FREE_Msk) == I2C_STATUS2_BUS_FREE_Msk ? 1:0 )
254
264#define I2C_GET_WAKEUP_RW_FLAG(i2c) ( ((i2c)->STATUS2 & I2C_STATUS2_WR_STATUS_Msk) == I2C_STATUS2_WR_STATUS_Msk ? 1:0 )
265
266uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock);
267void I2C_Close(I2C_T *i2c);
268void I2C_ClearTimeoutFlag(I2C_T *i2c);
269void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack);
270void I2C_DisableInt(I2C_T *i2c);
271void I2C_EnableInt(I2C_T *i2c);
272uint32_t I2C_GetBusClockFreq(I2C_T *i2c);
273uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock);
274uint32_t I2C_GetIntFlag(I2C_T *i2c);
275void I2C_ClearIntFlag(I2C_T *i2c);
276uint32_t I2C_GetStatus(I2C_T *i2c);
277uint32_t I2C_GetData(I2C_T *i2c);
278void I2C_SetData(I2C_T *i2c, uint8_t u8Data);
279void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode);
280void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask);
281void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout);
282void I2C_DisableTimeout(I2C_T *i2c);
283void I2C_EnableWakeup(I2C_T *i2c);
284void I2C_DisableWakeup(I2C_T *i2c);
285 /* end of group NANO1X2_I2C_EXPORTED_FUNCTIONS */
287 /* end of group NANO1X2_I2C_Driver */
289 /* end of group NANO1X2_Device_Driver */
291
292#ifdef __cplusplus
293}
294#endif
295
296#endif //__I2C_H__
297
298/*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
#define I2C_CON_I2C_STS_Msk
#define I2C_INTSTS_INTSTS_Msk
#define I2C_CON_STOP_Msk
void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask)
Configure the mask of slave address. The corresponding address bit is "Don't Care".
Definition: i2c.c:241
static __INLINE int32_t I2C_STOP(I2C_T *i2c)
This macro only set STOP bit to the control register of I2C module.
Definition: i2c.h:72
void I2C_Close(I2C_T *i2c)
This function closes the I2C module.
Definition: i2c.c:53
uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
This function sets bus frequency of I2C module.
Definition: i2c.c:145
void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack)
This function sets the control bit of the I2C module.
Definition: i2c.c:90
void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout)
This function enables timeout function and configures DIV4 function to support long timeout.
Definition: i2c.c:262
void I2C_ClearTimeoutFlag(I2C_T *i2c)
This function clears the timeout flag.
Definition: i2c.c:76
void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode)
Configure slave address and enable GC mode.
Definition: i2c.c:219
void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
This function writes the data to data register of I2C module.
Definition: i2c.c:204
uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
This function returns the real bus clock of I2C module.
Definition: i2c.c:132
void I2C_EnableInt(I2C_T *i2c)
This function enables the interrupt (EI bit) of I2C module.
Definition: i2c.c:122
void I2C_DisableWakeup(I2C_T *i2c)
This function disables the wakeup function of I2C module.
Definition: i2c.c:297
uint32_t I2C_GetStatus(I2C_T *i2c)
This function returns the status of I2C module.
Definition: i2c.c:183
void I2C_DisableInt(I2C_T *i2c)
This function disables the interrupt of I2C module.
Definition: i2c.c:112
uint32_t I2C_GetData(I2C_T *i2c)
This function returns the data stored in data register of I2C module.
Definition: i2c.c:193
static __INLINE int32_t I2C_WAIT_READY(I2C_T *i2c)
This macro will return when I2C module is ready.
Definition: i2c.h:90
void I2C_EnableWakeup(I2C_T *i2c)
This function enables the wakeup function of I2C module.
Definition: i2c.c:287
void I2C_ClearIntFlag(I2C_T *i2c)
This function clears the interrupt flag of I2C module.
Definition: i2c.c:173
uint32_t I2C_GetIntFlag(I2C_T *i2c)
This function gets the interrupt flag of I2C module.
Definition: i2c.c:163
uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock)
This function make I2C module be ready and set the wanted bus clock.
Definition: i2c.c:34
void I2C_DisableTimeout(I2C_T *i2c)
This function disables timeout function.
Definition: i2c.c:277
__IO uint32_t INTSTS
__IO uint32_t CON
uint32_t SystemCoreClock