| MINI55_BSP V3.02.004 The Board Support Package for Mini55 Series MCU

i2c.h
Go to the documentation of this file.
1/**************************************************************************/
12#ifndef __I2C_H__
13#define __I2C_H__
14
15#ifdef __cplusplus
16 extern "C"
17{
18#endif
19
20
34#define I2C_STA 0x20
35#define I2C_STO 0x10
36#define I2C_SI 0x08
37#define I2C_AA 0x04 /* end of group MINI55_I2C_EXPORTED_CONSTANTS */
40
41
52#define I2C_SET_CONTROL_REG(i2c, u8Ctrl) ( (i2c)->CTL = ((i2c)->CTL & ~0x3c) | u8Ctrl )
53
59#define I2C_START(i2c) ( (i2c)->CTL = ((i2c)->CTL & ~I2C_CTL_SI_Msk) | I2C_CTL_STA_Msk )
60
66#if 0
67#define I2C_STOP(i2c) \
68 do { \
69 (i2c)->CTL |= (I2C_CTL_SI_Msk | I2C_CTL_STO_Msk); \
70 while((i2c)->CTL & I2C_CTL_STO_Msk); \
71} while(0)
72#else
73 static __INLINE void I2C_STOP(I2C_T *i2c)
74{
75 uint32_t u32TimeOutCount = SystemCoreClock; // 1 second timeout
76 (i2c)->CTL |= (I2C_CTL_SI_Msk | I2C_CTL_STO_Msk);
77 while(i2c->CTL & I2C_CTL_STO_Msk)
78 {
79 u32TimeOutCount--;
80 if(u32TimeOutCount == 0) break;
81 }
82}
83#endif
84
90#define I2C_WAIT_READY(i2c) while(!((i2c)->CTL & I2C_CTL_SI_Msk))
91
97#define I2C_DISABLE_FIFO(i2c) ( (i2c)->CTL1 &= ~I2C_CTL1_FIFOEN_Msk )
98
104#define I2C_ENABLE_FIFO(i2c) ( (i2c)->CTL1 |= I2C_CTL1_FIFOEN_Msk )
105
111#define I2C_DISABLE_CLOCK_STRETCH(i2c) ( (i2c)->CTL1 |= I2C_CTL1_NSTRETCH_Msk )
112
118#define I2C_ENABLE_CLOCK_STRETCH(i2c) ( (i2c)->CTL1 &= ~I2C_CTL1_NSTRETCH_Msk )
119
125#define I2C_DISABLE_OVERRUN_INT(i2c) ( (i2c)->CTL1 &= ~I2C_CTL1_OVIEN_Msk )
126
132#define I2C_ENABLE_OVERRUN_INT(i2c) ( (i2c)->CTL1 |= I2C_CTL1_OVIEN_Msk )
133
139#define I2C_ENABLE_UNDERRUN_INT(i2c) ( (i2c)->CTL1 |= I2C_CTL1_URIEN_Msk )
140
146#define I2C_DISABLE_UNDERRUN_INT(i2c) ( (i2c)->CTL1 &= ~I2C_CTL1_URIEN_Msk )
147
153#define I2C_GET_DATA(i2c) ( (i2c)->DAT )
154
161#define I2C_SET_DATA(i2c, u8Data) ( (i2c)->DAT = u8Data )
162
168#define I2C_GET_STATUS(i2c) ( (i2c)->STATUS )
169
177#define I2C_GET_TIMEOUT_FLAG(i2c) ( ((i2c)->TOCTL & I2C_TOCTL_TOIF_Msk) == I2C_TOCTL_TOIF_Msk ? 1:0 )
178
186#define I2C_GET_WAKEUP_FLAG(i2c) ( ((i2c)->STATUS1 & I2C_STATUS1_WKIF_Msk) == I2C_STATUS1_WKIF_Msk ? 1:0 )
187
193#define I2C_CLEAR_WAKEUP_FLAG(i2c) ( (i2c)->STATUS1 = I2C_STATUS1_WKIF_Msk )
194
195uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock);
196 void I2C_Close(I2C_T *i2c);
197 void I2C_ClearTimeoutFlag(I2C_T *i2c);
198 void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack);
199 void I2C_DisableInt(I2C_T *i2c);
200 void I2C_EnableInt(I2C_T *i2c);
201uint32_t I2C_GetBusClockFreq(I2C_T *i2c);
202uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock);
203uint32_t I2C_GetIntFlag(I2C_T *i2c);
204uint32_t I2C_GetStatus(I2C_T *i2c);
205uint32_t I2C_GetData(I2C_T *i2c);
206 void I2C_SetData(I2C_T *i2c, uint8_t u8Data);
207 void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode);
208 void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask);
209 void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout);
210 void I2C_DisableTimeout(I2C_T *i2c);
211 void I2C_EnableWakeup(I2C_T *i2c);
212 void I2C_DisableWakeup(I2C_T *i2c);
213 /* end of group MINI55_I2C_EXPORTED_FUNCTIONS */
215 /* end of group MINI55_I2C_Driver */
217 /* end of group MINI55_Device_Driver */
219
220#ifdef __cplusplus
221}
222#endif
223
224#endif //__I2C_H__
225
226/*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
I2C_CTL_STO_Msk
#define I2C_CTL_STO_Msk
Definition: Mini55Series.h:2405
I2C_CTL_SI_Msk
#define I2C_CTL_SI_Msk
Definition: Mini55Series.h:2402
I2C_STOP
static __INLINE void I2C_STOP(I2C_T *i2c)
This macro only set STOP bit to the control register of I2C module.
Definition: i2c.h:73
I2C_SetSlaveAddrMask
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:223
I2C_Close
void I2C_Close(I2C_T *i2c)
This function closes the I2C module.
Definition: i2c.c:51
I2C_SetBusClockFreq
uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
This function enables the interrupt (EI bit) of I2C module.
Definition: i2c.c:134
I2C_Trigger
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:80
I2C_EnableTimeout
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:250
I2C_ClearTimeoutFlag
void I2C_ClearTimeoutFlag(I2C_T *i2c)
This function clears the timeout flag.
Definition: i2c.c:66
I2C_SetSlaveAddr
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:195
I2C_SetData
void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
This function writes the data to data register of I2C module.
Definition: i2c.c:182
I2C_GetBusClockFreq
uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
This function returns the real bus clock of I2C module.
Definition: i2c.c:121
I2C_EnableInt
void I2C_EnableInt(I2C_T *i2c)
This function enables the interrupt (EI bit) of I2C module.
Definition: i2c.c:111
I2C_DisableWakeup
void I2C_DisableWakeup(I2C_T *i2c)
This function disables the wakeup function of I2C module.
Definition: i2c.c:285
I2C_GetStatus
uint32_t I2C_GetStatus(I2C_T *i2c)
This function returns the status of I2C module.
Definition: i2c.c:161
I2C_DisableInt
void I2C_DisableInt(I2C_T *i2c)
This function disables the interrupt (EI bit) of I2C module.
Definition: i2c.c:101
I2C_GetData
uint32_t I2C_GetData(I2C_T *i2c)
This function returns the data stored in data register of I2C module.
Definition: i2c.c:171
I2C_EnableWakeup
void I2C_EnableWakeup(I2C_T *i2c)
This function enables the wakeup function of I2C module.
Definition: i2c.c:275
I2C_GetIntFlag
uint32_t I2C_GetIntFlag(I2C_T *i2c)
This function gets the interrupt flag (SI bit) of I2C module.
Definition: i2c.c:151
I2C_Open
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:33
I2C_DisableTimeout
void I2C_DisableTimeout(I2C_T *i2c)
This function disables timeout function.
Definition: i2c.c:265
I2C_T::CTL
__IO uint32_t CTL
Definition: Mini55Series.h:2142
I2C_T
Definition: Mini55Series.h:2108
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_Mini55Series.c:22