| MINI58_BSP V3.01.003 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
51 void 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
74 void I2C_ClearTimeoutFlag(I2C_T *i2c)
75{
76 i2c->TOCTL |= I2C_TOCTL_TOIF_Msk;
77}
78
88 void 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
109 void I2C_DisableInt(I2C_T *i2c)
110{
111 i2c->CTL &= ~I2C_CTL_INTEN_Msk;
112}
113
119 void I2C_EnableInt(I2C_T *i2c)
120{
121 i2c->CTL |= I2C_CTL_INTEN_Msk;
122}
123
129uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
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
190 void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
191{
192 i2c->DAT = u8Data;
193}
194
203 void 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
231 void 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
258 void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout)
259{
260 if(u8LongTimeout)
261 i2c->TOCTL |= I2C_TOCTL_TOCURIEN_Msk;
262 else
263 i2c->TOCTL &= ~I2C_TOCTL_TOCURIEN_Msk;
264
265 i2c->TOCTL |= I2C_TOCTL_TOCEN_Msk;
266}
267
273 void I2C_DisableTimeout(I2C_T *i2c)
274{
275 i2c->TOCTL &= ~I2C_TOCTL_TOCEN_Msk;
276}
277
283 void I2C_EnableWakeup(I2C_T *i2c)
284{
285 if(i2c == I2C0) //only support for port0
286 i2c->CTL1 |= I2C_CTL1_WKEN_Msk;
287}
288
294 void I2C_DisableWakeup(I2C_T *i2c)
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. ***/
Mini58Series.h
Mini58 series peripheral access layer header file. This file contains all the peripheral register's d...
SYS_IPRST1_I2C1RST_Msk
#define SYS_IPRST1_I2C1RST_Msk
Definition: Mini58Series.h:10107
I2C_TOCTL_TOCURIEN_Msk
#define I2C_TOCTL_TOCURIEN_Msk
Definition: Mini58Series.h:4880
I2C_CTL1_WKEN_Msk
#define I2C_CTL1_WKEN_Msk
Definition: Mini58Series.h:4916
I2C_CTL_INTEN_Msk
#define I2C_CTL_INTEN_Msk
Definition: Mini58Series.h:4859
I2C_TOCTL_TOIF_Msk
#define I2C_TOCTL_TOIF_Msk
Definition: Mini58Series.h:4877
I2C_TOCTL_TOCEN_Msk
#define I2C_TOCTL_TOCEN_Msk
Definition: Mini58Series.h:4883
I2C_CTL_I2CEN_Msk
#define I2C_CTL_I2CEN_Msk
Definition: Mini58Series.h:4856
SYS_IPRST1_I2C0RST_Msk
#define SYS_IPRST1_I2C0RST_Msk
Definition: Mini58Series.h:10104
I2C_CTL_SI_Msk
#define I2C_CTL_SI_Msk
Definition: Mini58Series.h:4847
I2C_SI
#define I2C_SI
Definition: i2c.h:36
I2C_AA
#define I2C_AA
Definition: i2c.h:37
I2C_STO
#define I2C_STO
Definition: i2c.h:35
I2C_STA
#define I2C_STA
Definition: i2c.h:34
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:231
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:142
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:88
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:258
I2C_ClearTimeoutFlag
void I2C_ClearTimeoutFlag(I2C_T *i2c)
This function clears the timeout flag.
Definition: i2c.c:74
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:203
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:190
I2C_GetBusClockFreq
uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
This function returns the real bus clock of I2C module.
Definition: i2c.c:129
I2C_EnableInt
void I2C_EnableInt(I2C_T *i2c)
This function enables the interrupt (EI bit) of I2C module.
Definition: i2c.c:119
I2C_DisableWakeup
void I2C_DisableWakeup(I2C_T *i2c)
This function disables the wakeup function of I2C module.
Definition: i2c.c:294
I2C_GetStatus
uint32_t I2C_GetStatus(I2C_T *i2c)
This function returns the status of I2C module.
Definition: i2c.c:169
I2C_DisableInt
void I2C_DisableInt(I2C_T *i2c)
This function disables the interrupt (EI bit) of I2C module.
Definition: i2c.c:109
I2C_GetData
uint32_t I2C_GetData(I2C_T *i2c)
This function returns the data stored in data register of I2C module.
Definition: i2c.c:179
I2C_EnableWakeup
void I2C_EnableWakeup(I2C_T *i2c)
This function enables the wakeup function of I2C module.
Definition: i2c.c:283
I2C_GetIntFlag
uint32_t I2C_GetIntFlag(I2C_T *i2c)
This function gets the interrupt flag (SI bit) of I2C module.
Definition: i2c.c:159
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:273
I2C0
#define I2C0
Pointer to I2C0 register structure.
Definition: Mini58Series.h:11718
SYS
#define SYS
Pointer to SYS register structure.
Definition: Mini58Series.h:11729
I2C_T::ADDR3
__IO uint32_t ADDR3
Definition: Mini58Series.h:4722
I2C_T::ADDR0
__IO uint32_t ADDR0
Definition: Mini58Series.h:4603
I2C_T::DAT
__IO uint32_t DAT
Definition: Mini58Series.h:4615
I2C_T::ADDRMSK3
__IO uint32_t ADDRMSK3
Definition: Mini58Series.h:4774
I2C_T::TOCTL
__IO uint32_t TOCTL
Definition: Mini58Series.h:4671
I2C_T::CLKDIV
__IO uint32_t CLKDIV
Definition: Mini58Series.h:4649
I2C_T::STATUS
__I uint32_t STATUS
Definition: Mini58Series.h:4636
I2C_T::CTL1
__IO uint32_t CTL1
Definition: Mini58Series.h:4812
I2C_T::ADDRMSK1
__IO uint32_t ADDRMSK1
Definition: Mini58Series.h:4748
I2C_T::ADDR1
__IO uint32_t ADDR1
Definition: Mini58Series.h:4688
I2C_T::ADDR2
__IO uint32_t ADDR2
Definition: Mini58Series.h:4705
I2C_T::CTL
__IO uint32_t CTL
Definition: Mini58Series.h:4586
I2C_T::ADDRMSK0
__IO uint32_t ADDRMSK0
Definition: Mini58Series.h:4735
I2C_T::ADDRMSK2
__IO uint32_t ADDRMSK2
Definition: Mini58Series.h:4761
I2C_T
Definition: Mini58Series.h:4552
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_Mini58Series.c:20