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 
75 {
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 
111 {
112  i2c->CTL &= ~I2C_CTL_INTEN_Msk;
113 }
114 
121 {
122  i2c->CTL |= I2C_CTL_INTEN_Msk;
123 }
124 
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 
184 {
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)
276  else
277  i2c->TOCTL &= ~I2C_TOCTL_TOCDIV4_Msk;
278 
279  i2c->TOCTL |= I2C_TOCTL_TOCEN_Msk;
280 }
281 
288 {
289  i2c->TOCTL &= ~I2C_TOCTL_TOCEN_Msk;
290 }
291 
298 {
299  i2c->CTL2 |= I2C_CTL2_WKUPEN_Msk;
300 }
301 
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. ***/
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
uint32_t I2C_GetStatus(I2C_T *i2c)
This function returns the status of I2C module.
Definition: i2c.c:193
void I2C_Close(I2C_T *i2c)
This function closes the I2C module.
Definition: i2c.c:51
__IO uint32_t ADDRMSK0
Definition: Nano103.h:22442
__IO uint32_t CTL
Definition: Nano103.h:22431
#define I2C0
Pointer to I2C0 register structure.
Definition: Nano103.h:24868
#define I2C_TOCTL_TOCEN_Msk
Definition: Nano103.h:22491
uint32_t CLK_GetPCLK0Freq(void)
This function get PCLK0 frequency. The frequency unit is Hz.
Definition: clk.c:137
uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
This function sets bus frequency of I2C module.
Definition: i2c.c:146
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
#define I2C_CTL_INTEN_Msk
Definition: Nano103.h:22473
__IO uint32_t ADDR0
Definition: Nano103.h:22437
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
#define SYS_IPRST2_I2C0RST_Msk
Definition: Nano103.h:3299
__IO uint32_t ADDRMSK1
Definition: Nano103.h:22443
#define SYS_IPRST2_I2C1RST_Msk
Definition: Nano103.h:3302
#define I2C_INTSTS_TOIF_Msk
Definition: Nano103.h:22479
#define I2C_AA
Definition: i2c.h:36
#define I2C_STA
Definition: i2c.h:33
uint32_t CLK_GetPCLK1Freq(void)
This function get PCLK1 frequency. The frequency unit is Hz.
Definition: clk.c:151
void I2C_DisableTimeout(I2C_T *i2c)
This function disables timeout function.
Definition: i2c.c:287
void I2C_DisableWakeup(I2C_T *i2c)
This function disables the wakeup function of I2C module.
Definition: i2c.c:307
void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
This function writes the data to data register of I2C module.
Definition: i2c.c:214
#define I2C_CTL2_WKUPEN_Msk
Definition: Nano103.h:22518
void I2C_ClearIntFlag(I2C_T *i2c)
This function clears the interrupt flag of I2C module.
Definition: i2c.c:183
__IO uint32_t INTSTS
Definition: Nano103.h:22432
void I2C_DisableInt(I2C_T *i2c)
This function disables the interrupt of I2C module.
Definition: i2c.c:110
__I uint32_t STATUS
Definition: Nano103.h:22433
__IO uint32_t CTL2
Definition: Nano103.h:22447
uint32_t I2C_GetData(I2C_T *i2c)
This function returns the data stored in data register of I2C module.
Definition: i2c.c:203
#define I2C_INTSTS_INTSTS_Msk
Definition: Nano103.h:22476
__IO uint32_t TOCTL
Definition: Nano103.h:22435
__IO uint32_t ADDR1
Definition: Nano103.h:22438
#define I2C0_BASE
I2C0 register base address.
Definition: Nano103.h:24815
#define I2C_STO
Definition: i2c.h:34
#define I2C_SI
Definition: i2c.h:35
__IO uint32_t CLKDIV
Definition: Nano103.h:22434
NANO103 peripheral access layer header file. This file contains all the peripheral register's definit...
void I2C_EnableWakeup(I2C_T *i2c)
This function enables the wakeup function of I2C module.
Definition: i2c.c:297
uint32_t I2C_GetIntFlag(I2C_T *i2c)
This function gets the interrupt flag of I2C module.
Definition: i2c.c:173
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
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
#define I2C_CTL_I2CEN_Msk
Definition: Nano103.h:22458
#define I2C_TOCTL_TOCDIV4_Msk
Definition: Nano103.h:22494
uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
This function returns the real bus clock of I2C module.
Definition: i2c.c:130
void I2C_EnableInt(I2C_T *i2c)
This function enables the interrupt (EI bit) of I2C module.
Definition: i2c.c:120
#define SYS
Pointer to SYS register structure.
Definition: Nano103.h:24883
void I2C_ClearTimeoutFlag(I2C_T *i2c)
This function clears the timeout flag.
Definition: i2c.c:74
__IO uint32_t DAT
Definition: Nano103.h:22436