MINI58_BSP V3.01.005
The Board Support Package for Mini58 Series MCU
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
34#define I2C_STA 0x20
35#define I2C_STO 0x10
36#define I2C_SI 0x08
37#define I2C_AA 0x04 /* end of group Mini58_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
67__STATIC_INLINE int32_t I2C_STOP(I2C_T *i2c)
68{
69 int32_t tout = (SystemCoreClock / 10);
70
72 while ((i2c->CTL & I2C_CTL_STO_Msk) && (tout-- > 0)) ;
73 if (i2c->CTL & I2C_CTL_STO_Msk)
74 return -1;
75 return 0;
76}
77
84__STATIC_INLINE int32_t I2C_WAIT_READY(I2C_T *i2c)
85{
86 int32_t tout = (SystemCoreClock / 10);
87
88 while (!(i2c->CTL & I2C_CTL_SI_Msk) && (tout-- > 0));
89 if (!(i2c->CTL & I2C_CTL_SI_Msk))
90 return -1;
91 return 0;
92}
93
94
100#define I2C_DISABLE_FIFO(i2c) ( (i2c)->CTL1 &= ~I2C_CTL1_TWOLVFIFO_Msk )
101
107#define I2C_ENABLE_FIFO(i2c) ( (i2c)->CTL1 |= I2C_CTL1_TWOLVFIFO_Msk )
108
114#define I2C_DISABLE_CLOCK_STRETCH(i2c) ( (i2c)->CTL1 &= ~I2C_CTL1_NSTRETCH_Msk )
115
121#define I2C_ENABLE_CLOCK_STRETCH(i2c) ( (i2c)->CTL1 |= I2C_CTL1_NSTRETCH_Msk )
122
128#define I2C_DISABLE_OVERRUN_INT(i2c) ( (i2c)->CTL1 &= ~I2C_CTL1_OVIEN_Msk )
129
135#define I2C_ENABLE_OVERRUN_INT(i2c) ( (i2c)->CTL1 |= I2C_CTL1_OVIEN_Msk )
136
142#define I2C_ENABLE_UNDERRUN_INT(i2c) ( (i2c)->CTL1 |= I2C_CTL1_URIEN_Msk )
143
149#define I2C_DISABLE_UNDERRUN_INT(i2c) ( (i2c)->CTL1 &= ~I2C_CTL1_URIEN_Msk )
150
156#define I2C_GET_DATA(i2c) ( (i2c)->DAT )
157
164#define I2C_SET_DATA(i2c, u8Data) ( (i2c)->DAT = u8Data )
165
171#define I2C_GET_STATUS(i2c) ( (i2c)->STATUS )
172
180#define I2C_GET_TIMEOUT_FLAG(i2c) ( ((i2c)->TOCTL & I2C_TOCTL_TOIF_Msk) == I2C_TOCTL_TOIF_Msk ? 1:0 )
181
189#define I2C_GET_WAKEUP_FLAG(i2c) ( ((i2c)->STATUS1 & I2C_STATUS1_WKIF_Msk) == I2C_STATUS1_WKIF_Msk ? 1:0 )
190
196#define I2C_CLEAR_WAKEUP_FLAG(i2c) ( (i2c)->STATUS1 = I2C_STATUS1_WKIF_Msk )
197
198uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock);
199void I2C_Close(I2C_T *i2c);
200void I2C_ClearTimeoutFlag(I2C_T *i2c);
201void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack);
202void I2C_DisableInt(I2C_T *i2c);
203void I2C_EnableInt(I2C_T *i2c);
204uint32_t I2C_GetBusClockFreq(I2C_T *i2c);
205uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock);
206uint32_t I2C_GetIntFlag(I2C_T *i2c);
207uint32_t I2C_GetStatus(I2C_T *i2c);
208uint32_t I2C_GetData(I2C_T *i2c);
209void I2C_SetData(I2C_T *i2c, uint8_t u8Data);
210void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode);
211void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask);
212void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout);
213void I2C_DisableTimeout(I2C_T *i2c);
214void I2C_EnableWakeup(I2C_T *i2c);
215void I2C_DisableWakeup(I2C_T *i2c);
216 /* end of group Mini58_I2C_EXPORTED_FUNCTIONS */
218 /* end of group Mini58_I2C_Driver */
220 /* end of group Mini58_Device_Driver */
222
223#ifdef __cplusplus
224}
225#endif
226
227#endif //__I2C_H__
228
229/*** (C) COPYRIGHT 2022 Nuvoton Technology Corp. ***/
#define I2C_CTL_STO_Msk
#define I2C_CTL_SI_Msk
__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:67
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:231
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 enables the interrupt (EI bit) of I2C module.
Definition: i2c.c:142
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:258
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:203
void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
This function writes the data to data register of I2C module.
Definition: i2c.c:190
uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
This function returns the real bus clock of I2C module.
Definition: i2c.c:129
void I2C_EnableInt(I2C_T *i2c)
This function enables the interrupt (EI bit) of I2C module.
Definition: i2c.c:119
void I2C_DisableWakeup(I2C_T *i2c)
This function disables the wakeup function of I2C module.
Definition: i2c.c:294
uint32_t I2C_GetStatus(I2C_T *i2c)
This function returns the status of I2C module.
Definition: i2c.c:169
__STATIC_INLINE int32_t I2C_WAIT_READY(I2C_T *i2c)
This macro will return when I2C module is ready.
Definition: i2c.h:84
void I2C_DisableInt(I2C_T *i2c)
This function disables the interrupt (EI bit) of I2C module.
Definition: i2c.c:109
uint32_t I2C_GetData(I2C_T *i2c)
This function returns the data stored in data register of I2C module.
Definition: i2c.c:179
void I2C_EnableWakeup(I2C_T *i2c)
This function enables the wakeup function of I2C module.
Definition: i2c.c:283
uint32_t I2C_GetIntFlag(I2C_T *i2c)
This function gets the interrupt flag (SI bit) of I2C module.
Definition: i2c.c:159
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
void I2C_DisableTimeout(I2C_T *i2c)
This function disables timeout function.
Definition: i2c.c:273
__IO uint32_t CTL
uint32_t SystemCoreClock