| Nano100BN Series BSP V3.03.002 The Board Support Package for Nano100BN Series

i2c.c
Go to the documentation of this file.
1 /****************************************************************************/
13 #include "Nano100Series.h"
14
34 uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock)
35 {
36 uint32_t u32Div;
37
38 u32Div = (uint32_t) (((SystemCoreClock * 10)/(u32BusClock * 4) + 5) / 10 - 1); /* Compute proper divider for I2C clock */
39 i2c->DIV = u32Div;
40
41 /* Enable I2C */
42 i2c->CON |= I2C_CON_IPEN_Msk;
43
44 return ( SystemCoreClock / ((u32Div+1)<<2) );
45 }
46
52 void I2C_Close(I2C_T *i2c)
53 {
54 /* Reset I2C */
55 if((uint32_t)i2c == I2C0_BASE)
56 {
57 SYS->IPRST_CTL2 |= SYS_IPRST_CTL2_I2C0_RST_Msk;
58 SYS->IPRST_CTL2 &= ~SYS_IPRST_CTL2_I2C0_RST_Msk;
59 }
60 else
61 {
62 SYS->IPRST_CTL2 |= SYS_IPRST_CTL2_I2C1_RST_Msk;
63 SYS->IPRST_CTL2 &= ~SYS_IPRST_CTL2_I2C1_RST_Msk;
64 }
65
66 /* Disable I2C */
67 i2c->CON &= ~I2C_CON_IPEN_Msk;
68 }
69
75 void I2C_ClearTimeoutFlag(I2C_T *i2c)
76 {
77 i2c->INTSTS |= I2C_INTSTS_TIF_Msk;
78 }
79
89 void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack)
90 {
91 uint32_t u32Reg = 0;
92 uint32_t u32Val = i2c->CON & ~(I2C_STA | I2C_STO | I2C_AA);
93
94 if (u8Start)
95 u32Reg |= I2C_STA;
96 if (u8Stop)
97 u32Reg |= I2C_STO;
98 if (u8Si)
99 u32Reg |= I2C_SI;
100 if (u8Ack)
101 u32Reg |= I2C_AA;
102
103 i2c->CON = u32Val | u32Reg;
104 }
105
111 void I2C_DisableInt(I2C_T *i2c)
112 {
113 i2c->CON &= ~I2C_CON_INTEN_Msk;
114 }
115
121 void I2C_EnableInt(I2C_T *i2c)
122 {
123 i2c->CON |= I2C_CON_INTEN_Msk;
124 }
125
131 uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
132 {
133 uint32_t u32Divider = i2c->DIV;
134
135 return ( SystemCoreClock / ((u32Divider+1)<<2) );
136 }
137
144 uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
145 {
146 uint32_t u32Div;
147
148 u32Div = (uint32_t) (((SystemCoreClock * 10)/(u32BusClock * 4) + 5) / 10 - 1); /* Compute proper divider for I2C clock */
149 i2c->DIV = u32Div;
150
151 return ( SystemCoreClock / ((u32Div+1)<<2) );
152 }
153
161 uint32_t I2C_GetIntFlag(I2C_T *i2c)
162 {
163 return ( (i2c->INTSTS & I2C_INTSTS_INTSTS_Msk) == I2C_INTSTS_INTSTS_Msk ? 1:0 );
164 }
165
171 void I2C_ClearIntFlag(I2C_T *i2c)
172 {
173 i2c->INTSTS |= I2C_INTSTS_INTSTS_Msk;
174 }
175
181 uint32_t I2C_GetStatus(I2C_T *i2c)
182 {
183 return ( i2c->STATUS );
184 }
185
191 uint32_t I2C_GetData(I2C_T *i2c)
192 {
193 return ( i2c->DATA );
194 }
195
202 void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
203 {
204 i2c->DATA = u8Data;
205 }
206
217 void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode)
218 {
219 switch (u8SlaveNo)
220 {
221 case 0:
222 i2c->SADDR0 = (u8SlaveAddr << 1) | u8GCMode;
223 break;
224 case 1:
225 i2c->SADDR1 = (u8SlaveAddr << 1) | u8GCMode;
226 break;
227 }
228 }
229
237 void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask)
238 {
239 switch (u8SlaveNo)
240 {
241 case 0:
242 i2c->SAMASK0 = u8SlaveAddrMask << 1;
243 break;
244 case 1:
245 i2c->SAMASK1 = u8SlaveAddrMask << 1;
246 break;
247 }
248 }
249
256 void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout)
257 {
258 if(u8LongTimeout)
259 i2c->TOUT |= I2C_TOUT_DIV4_Msk;
260 else
261 i2c->TOUT &= ~I2C_TOUT_DIV4_Msk;
262
263 i2c->TOUT |= I2C_TOUT_TOUTEN_Msk;
264 }
265
271 void I2C_DisableTimeout(I2C_T *i2c)
272 {
273 i2c->TOUT &= ~I2C_TOUT_TOUTEN_Msk;
274 }
275
281 void I2C_EnableWakeup(I2C_T *i2c)
282 {
283 i2c->WKUPCON |= I2C_WKUPCON_WKUPEN_Msk;
284 }
285
291 void I2C_DisableWakeup(I2C_T *i2c)
292 {
293 i2c->WKUPCON &= ~I2C_WKUPCON_WKUPEN_Msk;
294 }
295 /* end of group NANO100_I2C_EXPORTED_FUNCTIONS */
297 /* end of group NANO100_I2C_Driver */
299 /* end of group NANO100_Device_Driver */
301
302 /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_Nano100Series.c:20
I2C_SetBusClockFreq
uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
This function sets bus frequency of I2C module.
Definition: i2c.c:144
I2C_ClearTimeoutFlag
void I2C_ClearTimeoutFlag(I2C_T *i2c)
This function clears the timeout flag.
Definition: i2c.c:75
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
I2C_EnableInt
void I2C_EnableInt(I2C_T *i2c)
This function enables the interrupt (EI bit) of I2C module.
Definition: i2c.c:121
I2C_STO
#define I2C_STO
Definition: i2c.h:34
I2C_INTSTS_INTSTS_Msk
#define I2C_INTSTS_INTSTS_Msk
Definition: Nano100Series.h:4476
I2C_SI
#define I2C_SI
Definition: i2c.h:35
I2C_T
Definition: Nano100Series.h:4214
I2C_T::WKUPCON
__IO uint32_t WKUPCON
Definition: Nano100Series.h:4434
I2C_Close
void I2C_Close(I2C_T *i2c)
This function closes the I2C module.
Definition: i2c.c:52
I2C_STA
#define I2C_STA
Definition: i2c.h:33
I2C_DisableTimeout
void I2C_DisableTimeout(I2C_T *i2c)
This function disables timeout function.
Definition: i2c.c:271
I2C_T::STATUS
__I uint32_t STATUS
Definition: Nano100Series.h:4291
Nano100Series.h
Nano100 series peripheral access layer header file. This file contains all the peripheral register's ...
I2C0_BASE
#define I2C0_BASE
I2C0 register base address.
Definition: Nano100Series.h:11614
I2C_GetStatus
uint32_t I2C_GetStatus(I2C_T *i2c)
This function returns the status of I2C module.
Definition: i2c.c:181
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:202
I2C_INTSTS_TIF_Msk
#define I2C_INTSTS_TIF_Msk
Definition: Nano100Series.h:4479
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:237
SYS_IPRST_CTL2_I2C1_RST_Msk
#define SYS_IPRST_CTL2_I2C1_RST_Msk
Definition: Nano100Series.h:3005
I2C_GetData
uint32_t I2C_GetData(I2C_T *i2c)
This function returns the data stored in data register of I2C module.
Definition: i2c.c:191
I2C_T::INTSTS
__IO uint32_t INTSTS
Definition: Nano100Series.h:4269
I2C_TOUT_TOUTEN_Msk
#define I2C_TOUT_TOUTEN_Msk
Definition: Nano100Series.h:4488
I2C_T::SAMASK1
__IO uint32_t SAMASK1
Definition: Nano100Series.h:4419
I2C_T::SADDR0
__IO uint32_t SADDR0
Definition: Nano100Series.h:4364
I2C_GetIntFlag
uint32_t I2C_GetIntFlag(I2C_T *i2c)
This function gets the interrupt flag of I2C module.
Definition: i2c.c:161
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:89
I2C_TOUT_DIV4_Msk
#define I2C_TOUT_DIV4_Msk
Definition: Nano100Series.h:4491
SYS_IPRST_CTL2_I2C0_RST_Msk
#define SYS_IPRST_CTL2_I2C0_RST_Msk
Definition: Nano100Series.h:3002
I2C_CON_IPEN_Msk
#define I2C_CON_IPEN_Msk
Definition: Nano100Series.h:4458
I2C_CON_INTEN_Msk
#define I2C_CON_INTEN_Msk
Definition: Nano100Series.h:4473
I2C_T::DIV
__IO uint32_t DIV
Definition: Nano100Series.h:4304
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:217
I2C_DisableWakeup
void I2C_DisableWakeup(I2C_T *i2c)
This function disables the wakeup function of I2C module.
Definition: i2c.c:291
I2C_AA
#define I2C_AA
Definition: i2c.h:36
I2C_ClearIntFlag
void I2C_ClearIntFlag(I2C_T *i2c)
This function clears the interrupt flag of I2C module.
Definition: i2c.c:171
I2C_T::DATA
__IO uint32_t DATA
Definition: Nano100Series.h:4343
I2C_T::TOUT
__IO uint32_t TOUT
Definition: Nano100Series.h:4323
I2C_DisableInt
void I2C_DisableInt(I2C_T *i2c)
This function disables the interrupt of I2C module.
Definition: i2c.c:111
I2C_T::SAMASK0
__IO uint32_t SAMASK0
Definition: Nano100Series.h:4403
I2C_T::SADDR1
__IO uint32_t SADDR1
Definition: Nano100Series.h:4385
I2C_WKUPCON_WKUPEN_Msk
#define I2C_WKUPCON_WKUPEN_Msk
Definition: Nano100Series.h:4515
I2C_EnableWakeup
void I2C_EnableWakeup(I2C_T *i2c)
This function enables the wakeup function of I2C module.
Definition: i2c.c:281
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:256
I2C_GetBusClockFreq
uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
This function returns the real bus clock of I2C module.
Definition: i2c.c:131
I2C_T::CON
__IO uint32_t CON
Definition: Nano100Series.h:4253
SYS
#define SYS
Pointer to SYS register structure.
Definition: Nano100Series.h:11692