MINI58_BSP V3.01.005
The Board Support Package for Mini58 Series MCU
i2c.c
Go to the documentation of this file.
1/**************************************************************************/
12#include "Mini58Series.h"
13
33uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock)
34{
35 uint32_t u32Div;
36
37 u32Div = (uint32_t) (((SystemCoreClock * 10)/(u32BusClock * 4) + 5) / 10 - 1); /* Compute proper divider for I2C clock */
38 i2c->CLKDIV = u32Div;
39
40 /* Enable I2C */
41 i2c->CTL |= I2C_CTL_I2CEN_Msk;
42
43 return ( SystemCoreClock / ((u32Div+1)<<2) );
44}
45
51void I2C_Close(I2C_T *i2c)
52{
53 /* Reset SPI */
54 if(i2c == I2C0)
55 {
56 SYS->IPRST1 |= SYS_IPRST1_I2C0RST_Msk;
57 SYS->IPRST1 &= ~SYS_IPRST1_I2C0RST_Msk;
58 }
59 else
60 {
61 SYS->IPRST1 |= SYS_IPRST1_I2C1RST_Msk;
62 SYS->IPRST1 &= ~SYS_IPRST1_I2C1RST_Msk;
63 }
64
65 /* Disable I2C */
66 i2c->CTL &= ~I2C_CTL_I2CEN_Msk;
67}
68
75{
77}
78
88void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack)
89{
90 uint32_t u32Reg = 0;
91
92 if (u8Start)
93 u32Reg |= I2C_STA;
94 if (u8Stop)
95 u32Reg |= I2C_STO;
96 if (u8Si)
97 u32Reg |= I2C_SI;
98 if (u8Ack)
99 u32Reg |= I2C_AA;
100
101 i2c->CTL = (i2c->CTL & ~0x3C) | u32Reg;
102}
103
110{
111 i2c->CTL &= ~I2C_CTL_INTEN_Msk;
112}
113
120{
121 i2c->CTL |= I2C_CTL_INTEN_Msk;
122}
123
130{
131 uint32_t u32Divider = i2c->CLKDIV;
132
133 return ( SystemCoreClock / ((u32Divider+1)<<2) );
134}
135
142uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
143{
144 uint32_t u32Div;
145
146 u32Div = (uint32_t) (((SystemCoreClock * 10)/(u32BusClock * 4) + 5) / 10 - 1); /* Compute proper divider for I2C clock */
147 i2c->CLKDIV = u32Div;
148
149 return ( SystemCoreClock / ((u32Div+1)<<2) );
150}
151
159uint32_t I2C_GetIntFlag(I2C_T *i2c)
160{
161 return ( (i2c->CTL & I2C_CTL_SI_Msk) == I2C_CTL_SI_Msk ? 1:0 );
162}
163
169uint32_t I2C_GetStatus(I2C_T *i2c)
170{
171 return ( i2c->STATUS );
172}
173
179uint32_t I2C_GetData(I2C_T *i2c)
180{
181 return ( i2c->DAT );
182}
183
190void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
191{
192 i2c->DAT = u8Data;
193}
194
203void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode)
204{
205 switch (u8SlaveNo)
206 {
207 case 0:
208 i2c->ADDR0 = (u8SlaveAddr << 1) | u8GCMode;
209 break;
210 case 1:
211 i2c->ADDR1 = (u8SlaveAddr << 1) | u8GCMode;
212 break;
213 case 2:
214 i2c->ADDR2 = (u8SlaveAddr << 1) | u8GCMode;
215 break;
216 case 3:
217 i2c->ADDR3 = (u8SlaveAddr << 1) | u8GCMode;
218 break;
219 default:
220 i2c->ADDR0 = (u8SlaveAddr << 1) | u8GCMode;
221 }
222}
223
231void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask)
232{
233 switch (u8SlaveNo)
234 {
235 case 0:
236 i2c->ADDRMSK0 = u8SlaveAddrMask << 1;
237 break;
238 case 1:
239 i2c->ADDRMSK1 = u8SlaveAddrMask << 1;
240 break;
241 case 2:
242 i2c->ADDRMSK2 = u8SlaveAddrMask << 1;
243 break;
244 case 3:
245 i2c->ADDRMSK3 = u8SlaveAddrMask << 1;
246 break;
247 default:
248 i2c->ADDRMSK0 = u8SlaveAddrMask << 1;
249 }
250}
251
258void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout)
259{
260 if(u8LongTimeout)
262 else
263 i2c->TOCTL &= ~I2C_TOCTL_TOCURIEN_Msk;
264
266}
267
274{
275 i2c->TOCTL &= ~I2C_TOCTL_TOCEN_Msk;
276}
277
284{
285 if(i2c == I2C0) //only support for port0
286 i2c->CTL1 |= I2C_CTL1_WKEN_Msk;
287}
288
295{
296 if(i2c == I2C0) //only support for port0
297 i2c->CTL1 &= ~I2C_CTL1_WKEN_Msk;
298}
299 /* end of group Mini58_I2C_EXPORTED_FUNCTIONS */
301 /* end of group Mini58_I2C_Driver */
303 /* end of group Mini58_Device_Driver */
305
306/*** (C) COPYRIGHT 2022 Nuvoton Technology Corp. ***/
Mini58 series peripheral access layer header file. This file contains all the peripheral register's d...
#define SYS_IPRST1_I2C1RST_Msk
#define I2C_TOCTL_TOCURIEN_Msk
#define I2C_CTL1_WKEN_Msk
#define I2C_CTL_INTEN_Msk
#define I2C_TOCTL_TOIF_Msk
#define I2C_TOCTL_TOCEN_Msk
#define I2C_CTL_I2CEN_Msk
#define SYS_IPRST1_I2C0RST_Msk
#define I2C_CTL_SI_Msk
#define I2C_SI
Definition: i2c.h:36
#define I2C_AA
Definition: i2c.h:37
#define I2C_STO
Definition: i2c.h:35
#define I2C_STA
Definition: i2c.h:34
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
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
#define I2C0
Pointer to I2C0 register structure.
#define SYS
Pointer to SYS register structure.
__IO uint32_t ADDR3
__IO uint32_t ADDR0
__IO uint32_t DAT
__IO uint32_t ADDRMSK3
__IO uint32_t TOCTL
__IO uint32_t CLKDIV
__I uint32_t STATUS
__IO uint32_t CTL1
__IO uint32_t ADDRMSK1
__IO uint32_t ADDR1
__IO uint32_t ADDR2
__IO uint32_t CTL
__IO uint32_t ADDRMSK0
__IO uint32_t ADDRMSK2
uint32_t SystemCoreClock