NANO103 BSP V3.01.004
The Board Support Package for Nano103 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
41#define I2C_TIMEOUT SystemCoreClock /* end of group NANO103_I2C_EXPORTED_CONSTANTS */
44
45
57#define I2C_SET_CONTROL_REG(i2c, u8Ctrl) ( (i2c)->CTL = ((i2c)->CTL & ~0x1e) | u8Ctrl )
58
65#define I2C_START(i2c) ( (i2c)->CTL = ((i2c)->CTL & ~I2C_CTL_SI_Msk) | I2C_CTL_STA_Msk )
66
73#define I2C_STOP(i2c) \
74do { \
75 (i2c)->CTL |= (I2C_CTL_SI_Msk | I2C_CTL_STO_Msk); \
76 while((i2c)->CTL & I2C_CTL_STO_Msk); \
77} while(0)
78
85#define I2C_WAIT_READY(i2c) \
86do { \
87 while(!((i2c)->INTSTS & I2C_INTSTS_INTSTS_Msk)); \
88 (i2c)->INTSTS |= I2C_INTSTS_INTSTS_Msk; \
89} while(0)
90
97#define I2C_GET_DATA(i2c) ((i2c)->DAT )
98
106#define I2C_SET_DATA(i2c, u8Data) ( (i2c)->DAT = u8Data )
107
114#define I2C_GET_STATUS(i2c) ( (i2c)->STATUS )
115
124#define I2C_GET_TIMEOUT_FLAG(i2c) ( ((i2c)->INTSTS & I2C_INTSTS_TOIF_Msk) == I2C_INTSTS_TOIF_Msk ? 1:0 )
125
132#define I2C_CLEAR_TIMEOUT_FLAG(i2c) ( (i2c)->INTSTS |= I2C_INTSTS_TOIF_Msk )
133
142#define I2C_GET_WAKEUP_FLAG(i2c) ( ((i2c)->STATUS2 & I2C_STATUS2_WKIF_Msk) == I2C_STATUS2_WKIF_Msk ? 1:0 )
143
152#define I2C_GET_WAKEUP_ACK_DONE_FLAG(i2c) ( ((i2c)->INTSTS & I2C_INTSTS_WKAKDONE_Msk) == I2C_INTSTS_WKAKDONE_Msk ? 1:0 )
153
160#define I2C_CLEAR_WAKEUP_FLAG(i2c) ( (i2c)->STATUS2 |= I2C_STATUS2_WKIF_Msk )
161
169#define I2C_CLEAR_WAKEUP_ACK_DONE_FLAG(i2c) ( (i2c)->INTSTS |= I2C_INTSTS_WKAKDONE_Msk )
170
177#define I2C_DISABLE_FIFO(i2c) ( (i2c)->CTL2 &= ~I2C_CTL2_TWOLVBUF_Msk )
178
185#define I2C_ENABLE_FIFO(i2c) ( (i2c)->CTL2 |= I2C_CTL2_TWOLVBUF_Msk )
186
193#define I2C_DISABLE_CLOCK_STRETCH(i2c) ( (i2c)->CTL2 |= I2C_CTL2_NOSTRETCH_Msk )
194
201#define I2C_ENABLE_CLOCK_STRETCH(i2c) ( (i2c)->CTL2 &= ~I2C_CTL2_NOSTRETCH_Msk )
202
209#define I2C_DISABLE_OVERRUN_INT(i2c) ( (i2c)->CTL2 &= ~I2C_CTL2_OVIEN_Msk )
210
217#define I2C_ENABLE_OVERRUN_INT(i2c) ( (i2c)->CTL2 |= I2C_CTL2_OVIEN_Msk )
218
225#define I2C_ENABLE_UNDERRUN_INT(i2c) ( (i2c)->CTL2 |= I2C_CTL2_URIEN_Msk )
226
233#define I2C_DISABLE_UNDERRUN_INT(i2c) ((i2c)->CTL2 &= ~I2C_CTL2_URIEN_Msk )
234
243#define I2C_GET_BUS_FREE_FLAG(i2c) ( ((i2c)->STATUS2 & I2C_STATUS2_BUSFREE_Msk) == I2C_STATUS2_BUSFREE_Msk ? 1:0 )
244
254#define I2C_GET_WAKEUP_RW_FLAG(i2c) ( ((i2c)->STATUS2 & I2C_STATUS2_WRSTSWK_Msk) == I2C_STATUS2_WRSTSWK_Msk ? 1:0 )
255
262#define I2C_DISABLE_DATAMODE(i2c) ( (i2c)->CTL2 &= ~I2C_CTL2_DATMODE_Msk )
263
270#define I2C_ENABLE_DATAMODE(i2c) ( (i2c)->CTL2 |= I2C_CTL2_DATMODE_Msk )
271
278#define I2C_SET_DATAMODE_WRITE(i2c) ( (i2c)->CTL2 &= ~I2C_CTL2_MSDAT_Msk )
279
286#define I2C_SET_DATAMODE_READ(i2c) ( (i2c)->CTL2 |= I2C_CTL2_MSDAT_Msk )
287
288uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock);
289void I2C_Close(I2C_T *i2c);
290void I2C_ClearTimeoutFlag(I2C_T *i2c);
291void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack);
292void I2C_DisableInt(I2C_T *i2c);
293void I2C_EnableInt(I2C_T *i2c);
294uint32_t I2C_GetBusClockFreq(I2C_T *i2c);
295uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock);
296uint32_t I2C_GetIntFlag(I2C_T *i2c);
297void I2C_ClearIntFlag(I2C_T *i2c);
298uint32_t I2C_GetStatus(I2C_T *i2c);
299uint32_t I2C_GetData(I2C_T *i2c);
300void I2C_SetData(I2C_T *i2c, uint8_t u8Data);
301void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode);
302void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask);
303void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout);
304void I2C_DisableTimeout(I2C_T *i2c);
305void I2C_EnableWakeup(I2C_T *i2c);
306void I2C_DisableWakeup(I2C_T *i2c);
307 /* end of group NANO103_I2C_EXPORTED_FUNCTIONS */
309 /* end of group NANO103_I2C_Driver */
311 /* end of group NANO103_Device_Driver */
313
314#ifdef __cplusplus
315}
316#endif
317
318#endif //__I2C_H__
319
320/*** (C) COPYRIGHT 2015 Nuvoton Technology Corp. ***/
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:251
void I2C_Close(I2C_T *i2c)
This function closes the I2C module.
Definition: i2c.c:51
uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
This function sets bus frequency of I2C module.
Definition: i2c.c:146
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:88
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:272
void I2C_ClearTimeoutFlag(I2C_T *i2c)
This function clears the timeout flag.
Definition: i2c.c:74
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:229
void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
This function writes the data to data register of I2C module.
Definition: i2c.c:214
uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
This function returns the real bus clock of I2C module.
Definition: i2c.c:130
void I2C_EnableInt(I2C_T *i2c)
This function enables the interrupt (EI bit) of I2C module.
Definition: i2c.c:120
void I2C_DisableWakeup(I2C_T *i2c)
This function disables the wakeup function of I2C module.
Definition: i2c.c:307
uint32_t I2C_GetStatus(I2C_T *i2c)
This function returns the status of I2C module.
Definition: i2c.c:193
void I2C_DisableInt(I2C_T *i2c)
This function disables the interrupt of I2C module.
Definition: i2c.c:110
uint32_t I2C_GetData(I2C_T *i2c)
This function returns the data stored in data register of I2C module.
Definition: i2c.c:203
void I2C_EnableWakeup(I2C_T *i2c)
This function enables the wakeup function of I2C module.
Definition: i2c.c:297
void I2C_ClearIntFlag(I2C_T *i2c)
This function clears the interrupt flag of I2C module.
Definition: i2c.c:183
uint32_t I2C_GetIntFlag(I2C_T *i2c)
This function gets the interrupt flag of I2C module.
Definition: i2c.c:173
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:287