Mini51 BSP  V3.02.002
The Board Support Package for Mini51 Series
i2c.c
Go to the documentation of this file.
1 /**************************************************************************/
12 #include "Mini51Series.h"
13 
33 uint32_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 
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 
102 {
103  i2c->I2CON &= ~I2C_I2CON_EI_Msk;
104 }
105 
112 {
113  i2c->I2CON |= I2C_I2CON_EI_Msk;
114 }
115 
122 {
123  uint32_t u32Divider = i2c->I2CLK;
124 
125  return ( SystemCoreClock / ((u32Divider+1)<<2) );
126 }
127 
134 uint32_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 
151 uint32_t I2C_GetIntFlag(I2C_T *i2c)
152 {
153  return ( (i2c->I2CON & I2C_I2CON_SI_Msk) == I2C_I2CON_SI_Msk ? 1:0 );
154 }
155 
161 uint32_t I2C_GetStatus(I2C_T *i2c)
162 {
163  return ( i2c->I2CSTATUS );
164 }
165 
171 uint32_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 
266 {
267  i2c->I2CTOC &= ~I2C_I2CTOC_ENTI_Msk;
268 }
269 
276 {
278 }
279 
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. ***/
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
__IO uint32_t I2CDAT
#define I2C_I2CTOC_TIF_Msk
uint32_t I2C_GetStatus(I2C_T *i2c)
This function returns the status of I2C module.
Definition: i2c.c:161
#define I2C_AA
Definition: i2c.h:37
__IO uint32_t I2CADM2
Mini51 series peripheral access layer header file. This file contains all the peripheral register's d...
__IO uint32_t I2CADM3
#define I2C_I2CON_EI_Msk
__IO uint32_t I2CON2
void I2C_ClearTimeoutFlag(I2C_T *i2c)
This function clears the timeout flag.
Definition: i2c.c:66
#define I2C_STO
Definition: i2c.h:35
void I2C_DisableWakeup(I2C_T *i2c)
This function disables the wakeup function of I2C module.
Definition: i2c.c:285
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
__IO uint32_t I2CADDR3
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
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
uint32_t I2C_GetIntFlag(I2C_T *i2c)
This function gets the interrupt flag (SI bit) of I2C module.
Definition: i2c.c:151
#define I2C_I2CON_SI_Msk
void I2C_DisableTimeout(I2C_T *i2c)
This function disables timeout function.
Definition: i2c.c:265
#define SYS
Pointer to SYS register structure.
void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
This function writes the data to data register of I2C module.
Definition: i2c.c:182
uint32_t I2C_GetData(I2C_T *i2c)
This function returns the data stored in data register of I2C module.
Definition: i2c.c:171
uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
This function enables the interrupt (EI bit) of I2C module.
Definition: i2c.c:134
__IO uint32_t I2CTOC
void I2C_EnableInt(I2C_T *i2c)
This function enables the interrupt (EI bit) of I2C module.
Definition: i2c.c:111
__IO uint32_t I2CADDR1
__IO uint32_t I2CLK
void I2C_DisableInt(I2C_T *i2c)
This function disables the interrupt (EI bit) of I2C module.
Definition: i2c.c:101
#define I2C_SI
Definition: i2c.h:36
__I uint32_t I2CSTATUS
void I2C_EnableWakeup(I2C_T *i2c)
This function enables the wakeup function of I2C module.
Definition: i2c.c:275
__IO uint32_t I2CADDR2
#define SYS_IPRSTC2_I2C_RST_Msk
void I2C_Close(I2C_T *i2c)
This function closes the I2C module.
Definition: i2c.c:51
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
#define I2C_I2CTOC_DIV4_Msk
#define I2C_I2CTOC_ENTI_Msk
__IO uint32_t I2CON
#define I2C_I2CON_ENSI_Msk
__IO uint32_t I2CADDR0
uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
This function returns the real bus clock of I2C module.
Definition: i2c.c:121
#define I2C_STA
Definition: i2c.h:34
uint32_t SystemCoreClock
__IO uint32_t I2CADM1
__IO uint32_t I2CADM0
#define I2C_I2CON2_WKUPEN_Msk