| Nano103 BSP V3.01.002 The Board Support Package for Nano103 Series

i2c.c
Go to the documentation of this file.
1 /****************************************************************************/
13 #include "Nano103.h"
14
34 uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock)
35 {
36 uint32_t u32Clk;
37
38 u32Clk = I2C_SetBusClockFreq(i2c, u32BusClock);
39
40 /* Ensable I2C */
41 i2c->CTL |= I2C_CTL_I2CEN_Msk;
42
43 return ( u32Clk );
44 }
45
51 void I2C_Close(I2C_T *i2c)
52 {
53 /* Reset I2C */
54 if((uint32_t)i2c == I2C0_BASE)
55 {
56 SYS->IPRST2 |= SYS_IPRST2_I2C0RST_Msk;
57 SYS->IPRST2 &= ~SYS_IPRST2_I2C0RST_Msk;
58 }
59 else
60 {
61 SYS->IPRST2 |= SYS_IPRST2_I2C1RST_Msk;
62 SYS->IPRST2 &= ~SYS_IPRST2_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->INTSTS |= I2C_INTSTS_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 uint32_t u32Val = i2c->CTL & ~(I2C_STA | I2C_STO | I2C_AA);
92
93 if (u8Start)
94 u32Reg |= I2C_STA;
95 if (u8Stop)
96 u32Reg |= I2C_STO;
97 if (u8Si)
98 u32Reg |= I2C_SI;
99 if (u8Ack)
100 u32Reg |= I2C_AA;
101
102 i2c->CTL = u32Val | u32Reg;
103 }
104
110 void I2C_DisableInt(I2C_T *i2c)
111 {
112 i2c->CTL &= ~I2C_CTL_INTEN_Msk;
113 }
114
120 void I2C_EnableInt(I2C_T *i2c)
121 {
122 i2c->CTL |= I2C_CTL_INTEN_Msk;
123 }
124
130 uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
131 {
132 uint32_t u32Divider = i2c->CLKDIV;
133
134 if(i2c == I2C0)
135 return ( CLK_GetPCLK0Freq() / ((u32Divider+1)<<2) );
136 else
137 return ( CLK_GetPCLK1Freq() / ((u32Divider+1)<<2) );
138 }
139
146 uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
147 {
148 uint32_t u32Div;
149 uint32_t volatile u32Pclk;
150
151 if((uint32_t)i2c == I2C0_BASE)
152 {
153 u32Pclk = CLK_GetPCLK0Freq();
154 }
155 else
156 {
157 u32Pclk = CLK_GetPCLK1Freq();
158 }
159
160 u32Div = (uint32_t) (((u32Pclk * 10)/(u32BusClock * 4) + 5) / 10 - 1); /* Compute proper divider for I2C clock */
161 i2c->CLKDIV = u32Div;
162
163 return ( u32Pclk / ((u32Div+1)<<2) );
164 }
165
173 uint32_t I2C_GetIntFlag(I2C_T *i2c)
174 {
175 return ( (i2c->INTSTS & I2C_INTSTS_INTSTS_Msk) == I2C_INTSTS_INTSTS_Msk ? 1:0 );
176 }
177
183 void I2C_ClearIntFlag(I2C_T *i2c)
184 {
185 i2c->INTSTS |= I2C_INTSTS_INTSTS_Msk;
186 }
187
193 uint32_t I2C_GetStatus(I2C_T *i2c)
194 {
195 return ( i2c->STATUS );
196 }
197
203 uint32_t I2C_GetData(I2C_T *i2c)
204 {
205 return ( i2c->DAT );
206 }
207
214 void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
215 {
216 i2c->DAT = u8Data;
217 }
218
229 void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode)
230 {
231 switch (u8SlaveNo)
232 {
233 case 0:
234 i2c->ADDR0 = (u8SlaveAddr << 1) | u8GCMode;
235 break;
236 case 1:
237 i2c->ADDR1 = (u8SlaveAddr << 1) | u8GCMode;
238 break;
239 default:
240 i2c->ADDR0 = (u8SlaveAddr << 1) | u8GCMode;
241 }
242 }
243
251 void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask)
252 {
253 switch (u8SlaveNo)
254 {
255 case 0:
256 i2c->ADDRMSK0 = u8SlaveAddrMask << 1;
257 break;
258 case 1:
259 i2c->ADDRMSK1 = u8SlaveAddrMask << 1;
260 break;
261 default:
262 i2c->ADDRMSK0 = u8SlaveAddrMask << 1;
263 }
264 }
265
272 void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout)
273 {
274 if(u8LongTimeout)
275 i2c->TOCTL |= I2C_TOCTL_TOCDIV4_Msk;
276 else
277 i2c->TOCTL &= ~I2C_TOCTL_TOCDIV4_Msk;
278
279 i2c->TOCTL |= I2C_TOCTL_TOCEN_Msk;
280 }
281
287 void I2C_DisableTimeout(I2C_T *i2c)
288 {
289 i2c->TOCTL &= ~I2C_TOCTL_TOCEN_Msk;
290 }
291
297 void I2C_EnableWakeup(I2C_T *i2c)
298 {
299 i2c->CTL2 |= I2C_CTL2_WKUPEN_Msk;
300 }
301
307 void I2C_DisableWakeup(I2C_T *i2c)
308 {
309 i2c->CTL2 &= ~I2C_CTL2_WKUPEN_Msk;
310 }
311 /* end of group NANO103_I2C_EXPORTED_FUNCTIONS */
313 /* end of group NANO103_I2C_Driver */
315 /* end of group NANO103_Device_Driver */
317
318 /*** (C) COPYRIGHT 2015 Nuvoton Technology Corp. ***/
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:272
I2C_GetStatus
uint32_t I2C_GetStatus(I2C_T *i2c)
This function returns the status of I2C module.
Definition: i2c.c:193
I2C_Close
void I2C_Close(I2C_T *i2c)
This function closes the I2C module.
Definition: i2c.c:51
I2C_T::ADDRMSK0
__IO uint32_t ADDRMSK0
Definition: Nano103.h:22442
I2C_T::CTL
__IO uint32_t CTL
Definition: Nano103.h:22431
I2C0
#define I2C0
Pointer to I2C0 register structure.
Definition: Nano103.h:24868
I2C_TOCTL_TOCEN_Msk
#define I2C_TOCTL_TOCEN_Msk
Definition: Nano103.h:22491
I2C_T
Definition: Nano103.h:21966
CLK_GetPCLK0Freq
uint32_t CLK_GetPCLK0Freq(void)
This function get PCLK0 frequency. The frequency unit is Hz.
Definition: clk.c:137
I2C_SetBusClockFreq
uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
This function sets bus frequency of I2C module.
Definition: i2c.c:146
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:251
I2C_CTL_INTEN_Msk
#define I2C_CTL_INTEN_Msk
Definition: Nano103.h:22473
I2C_T::ADDR0
__IO uint32_t ADDR0
Definition: Nano103.h:22437
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:34
SYS_IPRST2_I2C0RST_Msk
#define SYS_IPRST2_I2C0RST_Msk
Definition: Nano103.h:3299
I2C_T::ADDRMSK1
__IO uint32_t ADDRMSK1
Definition: Nano103.h:22443
SYS_IPRST2_I2C1RST_Msk
#define SYS_IPRST2_I2C1RST_Msk
Definition: Nano103.h:3302
I2C_INTSTS_TOIF_Msk
#define I2C_INTSTS_TOIF_Msk
Definition: Nano103.h:22479
I2C_AA
#define I2C_AA
Definition: i2c.h:36
I2C_STA
#define I2C_STA
Definition: i2c.h:33
CLK_GetPCLK1Freq
uint32_t CLK_GetPCLK1Freq(void)
This function get PCLK1 frequency. The frequency unit is Hz.
Definition: clk.c:151
I2C_DisableTimeout
void I2C_DisableTimeout(I2C_T *i2c)
This function disables timeout function.
Definition: i2c.c:287
I2C_DisableWakeup
void I2C_DisableWakeup(I2C_T *i2c)
This function disables the wakeup function of I2C module.
Definition: i2c.c:307
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:214
I2C_CTL2_WKUPEN_Msk
#define I2C_CTL2_WKUPEN_Msk
Definition: Nano103.h:22518
I2C_ClearIntFlag
void I2C_ClearIntFlag(I2C_T *i2c)
This function clears the interrupt flag of I2C module.
Definition: i2c.c:183
I2C_T::INTSTS
__IO uint32_t INTSTS
Definition: Nano103.h:22432
I2C_DisableInt
void I2C_DisableInt(I2C_T *i2c)
This function disables the interrupt of I2C module.
Definition: i2c.c:110
I2C_T::STATUS
__I uint32_t STATUS
Definition: Nano103.h:22433
I2C_T::CTL2
__IO uint32_t CTL2
Definition: Nano103.h:22447
I2C_GetData
uint32_t I2C_GetData(I2C_T *i2c)
This function returns the data stored in data register of I2C module.
Definition: i2c.c:203
I2C_INTSTS_INTSTS_Msk
#define I2C_INTSTS_INTSTS_Msk
Definition: Nano103.h:22476
I2C_T::TOCTL
__IO uint32_t TOCTL
Definition: Nano103.h:22435
I2C_T::ADDR1
__IO uint32_t ADDR1
Definition: Nano103.h:22438
I2C0_BASE
#define I2C0_BASE
I2C0 register base address.
Definition: Nano103.h:24815
I2C_STO
#define I2C_STO
Definition: i2c.h:34
I2C_SI
#define I2C_SI
Definition: i2c.h:35
I2C_T::CLKDIV
__IO uint32_t CLKDIV
Definition: Nano103.h:22434
Nano103.h
NANO103 peripheral access layer header file. This file contains all the peripheral register's definit...
I2C_EnableWakeup
void I2C_EnableWakeup(I2C_T *i2c)
This function enables the wakeup function of I2C module.
Definition: i2c.c:297
I2C_GetIntFlag
uint32_t I2C_GetIntFlag(I2C_T *i2c)
This function gets the interrupt flag of I2C module.
Definition: i2c.c:173
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:229
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_CTL_I2CEN_Msk
#define I2C_CTL_I2CEN_Msk
Definition: Nano103.h:22458
I2C_TOCTL_TOCDIV4_Msk
#define I2C_TOCTL_TOCDIV4_Msk
Definition: Nano103.h:22494
I2C_GetBusClockFreq
uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
This function returns the real bus clock of I2C module.
Definition: i2c.c:130
I2C_EnableInt
void I2C_EnableInt(I2C_T *i2c)
This function enables the interrupt (EI bit) of I2C module.
Definition: i2c.c:120
SYS
#define SYS
Pointer to SYS register structure.
Definition: Nano103.h:24883
I2C_ClearTimeoutFlag
void I2C_ClearTimeoutFlag(I2C_T *i2c)
This function clears the timeout flag.
Definition: i2c.c:74
I2C_T::DAT
__IO uint32_t DAT
Definition: Nano103.h:22436