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

i2c.c
Go to the documentation of this file.
1/**************************************************************************/
12#include "Mini55Series.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 SYS->IPRST1 |= SYS_IPRST1_I2C_RST_Msk;
55 SYS->IPRST1 &= ~SYS_IPRST1_I2C_RST_Msk;
56
57 /* Disable I2C */
58 i2c->CTL &= ~I2C_CTL_I2CEN_Msk;
59}
60
66 void I2C_ClearTimeoutFlag(I2C_T *i2c)
67{
68 i2c->TOCTL |= I2C_TOCTL_TOIF_Msk;
69}
70
80 void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack)
81{
82 uint32_t u32Reg = 0;
83
84 if (u8Start)
85 u32Reg |= I2C_STA;
86 if (u8Stop)
87 u32Reg |= I2C_STO;
88 if (u8Si)
89 u32Reg |= I2C_SI;
90 if (u8Ack)
91 u32Reg |= I2C_AA;
92
93 i2c->CTL = (i2c->CTL & ~0x3C) | u32Reg;
94}
95
101 void I2C_DisableInt(I2C_T *i2c)
102{
103 i2c->CTL &= ~I2C_CTL_INTEN_Msk;
104}
105
111 void I2C_EnableInt(I2C_T *i2c)
112{
113 i2c->CTL |= I2C_CTL_INTEN_Msk;
114}
115
121uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
122{
123 uint32_t u32Divider = i2c->CLKDIV;
124
125 return ( SystemCoreClock / ((u32Divider+1)<<2) );
126}
127
134uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
135{
136 uint32_t u32Div;
137
138 u32Div = (uint32_t) (((SystemCoreClock * 10)/(u32BusClock * 4) + 5) / 10 - 1); /* Compute proper divider for I2C clock */
139 i2c->CLKDIV = u32Div;
140
141 return ( SystemCoreClock / ((u32Div+1)<<2) );
142}
143
151uint32_t I2C_GetIntFlag(I2C_T *i2c)
152{
153 return ( (i2c->CTL & I2C_CTL_SI_Msk) == I2C_CTL_SI_Msk ? 1:0 );
154}
155
161uint32_t I2C_GetStatus(I2C_T *i2c)
162{
163 return ( i2c->STATUS );
164}
165
171uint32_t I2C_GetData(I2C_T *i2c)
172{
173 return ( i2c->DAT );
174}
175
182 void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
183{
184 i2c->DAT = u8Data;
185}
186
195 void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode)
196{
197 switch (u8SlaveNo)
198 {
199 case 0:
200 i2c->ADDR0 = (u8SlaveAddr << 1) | u8GCMode;
201 break;
202 case 1:
203 i2c->ADDR1 = (u8SlaveAddr << 1) | u8GCMode;
204 break;
205 case 2:
206 i2c->ADDR2 = (u8SlaveAddr << 1) | u8GCMode;
207 break;
208 case 3:
209 i2c->ADDR3 = (u8SlaveAddr << 1) | u8GCMode;
210 break;
211 default:
212 i2c->ADDR0 = (u8SlaveAddr << 1) | u8GCMode;
213 }
214}
215
223 void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask)
224{
225 switch (u8SlaveNo)
226 {
227 case 0:
228 i2c->ADDRMSK0 = u8SlaveAddrMask << 1;
229 break;
230 case 1:
231 i2c->ADDRMSK1 = u8SlaveAddrMask << 1;
232 break;
233 case 2:
234 i2c->ADDRMSK2 = u8SlaveAddrMask << 1;
235 break;
236 case 3:
237 i2c->ADDRMSK3 = u8SlaveAddrMask << 1;
238 break;
239 default:
240 i2c->ADDRMSK0 = u8SlaveAddrMask << 1;
241 }
242}
243
250 void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout)
251{
252 if(u8LongTimeout)
253 i2c->TOCTL |= I2C_TOCTL_TOCDIV4_Msk;
254 else
255 i2c->TOCTL &= ~I2C_TOCTL_TOCDIV4_Msk;
256
257 i2c->TOCTL |= I2C_TOCTL_TOCEN_Msk;
258}
259
265 void I2C_DisableTimeout(I2C_T *i2c)
266{
267 i2c->TOCTL &= ~I2C_TOCTL_TOCEN_Msk;
268}
269
275 void I2C_EnableWakeup(I2C_T *i2c)
276{
277 i2c->CTL1 |= I2C_CTL1_WKEN_Msk;
278}
279
285 void I2C_DisableWakeup(I2C_T *i2c)
286{
287 i2c->CTL1 &= ~I2C_CTL1_WKEN_Msk;
288}
289 /* end of group MINI55_I2C_EXPORTED_FUNCTIONS */
291 /* end of group MINI55_I2C_Driver */
293 /* end of group MINI55_Device_Driver */
295
296/*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
Mini55Series.h
Mini55 series peripheral access layer header file. This file contains all the peripheral register's d...
I2C_CTL1_WKEN_Msk
#define I2C_CTL1_WKEN_Msk
Definition: Mini55Series.h:2471
I2C_CTL_INTEN_Msk
#define I2C_CTL_INTEN_Msk
Definition: Mini55Series.h:2414
I2C_TOCTL_TOIF_Msk
#define I2C_TOCTL_TOIF_Msk
Definition: Mini55Series.h:2432
I2C_TOCTL_TOCEN_Msk
#define I2C_TOCTL_TOCEN_Msk
Definition: Mini55Series.h:2438
I2C_CTL_I2CEN_Msk
#define I2C_CTL_I2CEN_Msk
Definition: Mini55Series.h:2411
I2C_CTL_SI_Msk
#define I2C_CTL_SI_Msk
Definition: Mini55Series.h:2402
SYS_IPRST1_I2C_RST_Msk
#define SYS_IPRST1_I2C_RST_Msk
Definition: Mini55Series.h:5535
I2C_TOCTL_TOCDIV4_Msk
#define I2C_TOCTL_TOCDIV4_Msk
Definition: Mini55Series.h:2435
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: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
SYS
#define SYS
Pointer to SYS register structure.
Definition: Mini55Series.h:7009
I2C_T::ADDR3
__IO uint32_t ADDR3
Definition: Mini55Series.h:2278
I2C_T::ADDR0
__IO uint32_t ADDR0
Definition: Mini55Series.h:2159
I2C_T::DAT
__IO uint32_t DAT
Definition: Mini55Series.h:2171
I2C_T::ADDRMSK3
__IO uint32_t ADDRMSK3
Definition: Mini55Series.h:2330
I2C_T::TOCTL
__IO uint32_t TOCTL
Definition: Mini55Series.h:2227
I2C_T::CLKDIV
__IO uint32_t CLKDIV
Definition: Mini55Series.h:2205
I2C_T::STATUS
__I uint32_t STATUS
Definition: Mini55Series.h:2192
I2C_T::CTL1
__IO uint32_t CTL1
Definition: Mini55Series.h:2368
I2C_T::ADDRMSK1
__IO uint32_t ADDRMSK1
Definition: Mini55Series.h:2304
I2C_T::ADDR1
__IO uint32_t ADDR1
Definition: Mini55Series.h:2244
I2C_T::ADDR2
__IO uint32_t ADDR2
Definition: Mini55Series.h:2261
I2C_T::CTL
__IO uint32_t CTL
Definition: Mini55Series.h:2142
I2C_T::ADDRMSK0
__IO uint32_t ADDRMSK0
Definition: Mini55Series.h:2291
I2C_T::ADDRMSK2
__IO uint32_t ADDRMSK2
Definition: Mini55Series.h:2317
I2C_T
Definition: Mini55Series.h:2108
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_Mini55Series.c:22