| MINI51DE_BSP V3.02.004 The Board Support Package for Mini51DE Series MCU

i2c.c
Go to the documentation of this file.
1/**************************************************************************/
12#include "Mini51Series.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 MINI51_I2C_EXPORTED_FUNCTIONS */
291 /* end of group MINI51_I2C_Driver */
293 /* end of group MINI51_Device_Driver */
295
296/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
Mini51Series.h
Mini51 series peripheral access layer header file. This file contains all the peripheral register's d...
I2C_I2CON_ENSI_Msk
#define I2C_I2CON_ENSI_Msk
Definition: Mini51Series.h:2450
I2C_I2CON_SI_Msk
#define I2C_I2CON_SI_Msk
Definition: Mini51Series.h:2441
I2C_I2CTOC_TIF_Msk
#define I2C_I2CTOC_TIF_Msk
Definition: Mini51Series.h:2471
SYS_IPRSTC2_I2C_RST_Msk
#define SYS_IPRSTC2_I2C_RST_Msk
Definition: Mini51Series.h:1578
I2C_I2CTOC_DIV4_Msk
#define I2C_I2CTOC_DIV4_Msk
Definition: Mini51Series.h:2474
I2C_I2CON2_WKUPEN_Msk
#define I2C_I2CON2_WKUPEN_Msk
Definition: Mini51Series.h:2483
I2C_I2CON_EI_Msk
#define I2C_I2CON_EI_Msk
Definition: Mini51Series.h:2453
I2C_I2CTOC_ENTI_Msk
#define I2C_I2CTOC_ENTI_Msk
Definition: Mini51Series.h:2477
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: Mini51Series.h:5735
I2C_T::I2CSTATUS
__I uint32_t I2CSTATUS
Definition: Mini51Series.h:2412
I2C_T::I2CLK
__IO uint32_t I2CLK
Definition: Mini51Series.h:2413
I2C_T::I2CADDR2
__IO uint32_t I2CADDR2
Definition: Mini51Series.h:2416
I2C_T::I2CADDR0
__IO uint32_t I2CADDR0
Definition: Mini51Series.h:2410
I2C_T::I2CTOC
__IO uint32_t I2CTOC
Definition: Mini51Series.h:2414
I2C_T::I2CON2
__IO uint32_t I2CON2
Definition: Mini51Series.h:2425
I2C_T::I2CADM3
__IO uint32_t I2CADM3
Definition: Mini51Series.h:2421
I2C_T::I2CON
__IO uint32_t I2CON
Definition: Mini51Series.h:2409
I2C_T::I2CADM0
__IO uint32_t I2CADM0
Definition: Mini51Series.h:2418
I2C_T::I2CDAT
__IO uint32_t I2CDAT
Definition: Mini51Series.h:2411
I2C_T::I2CADM2
__IO uint32_t I2CADM2
Definition: Mini51Series.h:2420
I2C_T::I2CADDR3
__IO uint32_t I2CADDR3
Definition: Mini51Series.h:2417
I2C_T::I2CADM1
__IO uint32_t I2CADM1
Definition: Mini51Series.h:2419
I2C_T::I2CADDR1
__IO uint32_t I2CADDR1
Definition: Mini51Series.h:2415
I2C_T
Definition: Mini51Series.h:2203
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_Mini51Series.c:20