| NUC029FAE_BSP V3.02.000 The Board Support Package for NUC029FAE MCU

i2c.c
Go to the documentation of this file.
1/**************************************************************************/
12#include "NUC029FAE.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->I2CLK = u32Div;
39
40 /* Enable I2C */
41 i2c->I2CON |= I2C_I2CON_ENSI_Msk;
42
43 return ( SystemCoreClock / ((u32Div+1)<<2) );
44}
45
51 void I2C_Close(I2C_T *i2c)
52{
53 /* Reset SPI */
54 SYS->IPRSTC2 |= SYS_IPRSTC2_I2C_RST_Msk;
55 SYS->IPRSTC2 &= ~SYS_IPRSTC2_I2C_RST_Msk;
56
57 /* Disable I2C */
58 i2c->I2CON &= ~I2C_I2CON_ENSI_Msk;
59}
60
66 void I2C_ClearTimeoutFlag(I2C_T *i2c)
67{
68 i2c->I2CTOC |= I2C_I2CTOC_TIF_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->I2CON = (i2c->I2CON & ~0x3C) | u32Reg;
94}
95
101 void I2C_DisableInt(I2C_T *i2c)
102{
103 i2c->I2CON &= ~I2C_I2CON_EI_Msk;
104}
105
111 void I2C_EnableInt(I2C_T *i2c)
112{
113 i2c->I2CON |= I2C_I2CON_EI_Msk;
114}
115
121uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
122{
123 uint32_t u32Divider = I2C->I2CLK;
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->I2CLK = u32Div;
140
141 return ( SystemCoreClock / ((u32Div+1)<<2) );
142}
143
151uint32_t I2C_GetIntFlag(I2C_T *i2c)
152{
153 return ( (i2c->I2CON & I2C_I2CON_SI_Msk) == I2C_I2CON_SI_Msk ? 1:0 );
154}
155
161uint32_t I2C_GetStatus(I2C_T *i2c)
162{
163 return ( i2c->I2CSTATUS );
164}
165
171uint32_t I2C_GetData(I2C_T *i2c)
172{
173 return ( i2c->I2CDAT );
174}
175
182 void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
183{
184 i2c->I2CDAT = 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->I2CADDR0 = (u8SlaveAddr << 1) | u8GCMode;
201 break;
202 case 1:
203 i2c->I2CADDR1 = (u8SlaveAddr << 1) | u8GCMode;
204 break;
205 case 2:
206 i2c->I2CADDR2 = (u8SlaveAddr << 1) | u8GCMode;
207 break;
208 case 3:
209 i2c->I2CADDR3 = (u8SlaveAddr << 1) | u8GCMode;
210 break;
211 default:
212 i2c->I2CADDR0 = (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->I2CADM0 = u8SlaveAddrMask << 1;
229 break;
230 case 1:
231 i2c->I2CADM1 = u8SlaveAddrMask << 1;
232 break;
233 case 2:
234 i2c->I2CADM2 = u8SlaveAddrMask << 1;
235 break;
236 case 3:
237 i2c->I2CADM3 = u8SlaveAddrMask << 1;
238 break;
239 default:
240 i2c->I2CADM0 = u8SlaveAddrMask << 1;
241 }
242}
243
250 void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout)
251{
252 if(u8LongTimeout)
253 i2c->I2CTOC |= I2C_I2CTOC_DIV4_Msk;
254 else
255 i2c->I2CTOC &= ~I2C_I2CTOC_DIV4_Msk;
256
257 i2c->I2CTOC |= I2C_I2CTOC_ENTI_Msk;
258}
259
265 void I2C_DisableTimeout(I2C_T *i2c)
266{
267 i2c->I2CTOC &= ~I2C_I2CTOC_ENTI_Msk;
268}
269
275 void I2C_EnableWakeup(I2C_T *i2c)
276{
277 i2c->I2CON2 |= I2C_I2CON2_WKUPEN_Msk;
278}
279
285 void I2C_DisableWakeup(I2C_T *i2c)
286{
287 i2c->I2CON2 &= ~I2C_I2CON2_WKUPEN_Msk;
288}
289 /* end of group NUC029FAE_I2C_EXPORTED_FUNCTIONS */
291 /* end of group NUC029FAE_I2C_Driver */
293 /* end of group NUC029FAE_Device_Driver */
295
296/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
NUC029FAE.h
NUC029FAE peripheral access layer header file. This file contains all the peripheral register's defin...
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
I2C
#define I2C
Pointer to I2C register structure.
Definition: NUC029FAE.h:3220
SYS
#define SYS
Pointer to SYS register structure.
Definition: NUC029FAE.h:3227
I2C_T::I2CADDR3
__IO uint32_t I2CADDR3
Definition: NUC029FAE.h:1050
I2C_T::I2CLK
__IO uint32_t I2CLK
Definition: NUC029FAE.h:1046
I2C_T::I2CSTATUS
__IO uint32_t I2CSTATUS
Definition: NUC029FAE.h:1045
I2C_T::I2CADDR2
__IO uint32_t I2CADDR2
Definition: NUC029FAE.h:1049
I2C_T::I2CADDR1
__IO uint32_t I2CADDR1
Definition: NUC029FAE.h:1048
I2C_T::I2CADM1
__IO uint32_t I2CADM1
Definition: NUC029FAE.h:1052
I2C_T::I2CON
__IO uint32_t I2CON
Definition: NUC029FAE.h:1042
I2C_T::I2CADDR0
__IO uint32_t I2CADDR0
Definition: NUC029FAE.h:1043
I2C_T::I2CADM3
__IO uint32_t I2CADM3
Definition: NUC029FAE.h:1054
I2C_T::I2CTOC
__IO uint32_t I2CTOC
Definition: NUC029FAE.h:1047
I2C_T::I2CDAT
__IO uint32_t I2CDAT
Definition: NUC029FAE.h:1044
I2C_T::I2CON2
__IO uint32_t I2CON2
Definition: NUC029FAE.h:1057
I2C_T::I2CADM0
__IO uint32_t I2CADM0
Definition: NUC029FAE.h:1051
I2C_T::I2CADM2
__IO uint32_t I2CADM2
Definition: NUC029FAE.h:1053
I2C_T
Definition: NUC029FAE.h:1041
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_NUC029FAE.c:20