Nano100AN Series BSP  V3.02.002
The Board Support Package for Nano100AN 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 
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 
112 {
113  i2c->CON &= ~I2C_CON_INTEN_Msk;
114 }
115 
122 {
123  i2c->CON |= I2C_CON_INTEN_Msk;
124 }
125 
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 
172 {
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 
272 {
273  i2c->TOUT &= ~I2C_TOUT_TOUTEN_Msk;
274 }
275  /* end of group NANO100_I2C_EXPORTED_FUNCTIONS */
277  /* end of group NANO100_I2C_Driver */
279  /* end of group NANO100_Device_Driver */
281 
282 /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
uint32_t SystemCoreClock
uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
This function sets bus frequency of I2C module.
Definition: i2c.c:144
void I2C_ClearTimeoutFlag(I2C_T *i2c)
This function clears the timeout flag.
Definition: i2c.c:75
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
void I2C_EnableInt(I2C_T *i2c)
This function enables the interrupt (EI bit) of I2C module.
Definition: i2c.c:121
#define I2C_STO
Definition: i2c.h:34
#define I2C_INTSTS_INTSTS_Msk
#define I2C_SI
Definition: i2c.h:35
void I2C_Close(I2C_T *i2c)
This function closes the I2C module.
Definition: i2c.c:52
#define I2C_STA
Definition: i2c.h:33
void I2C_DisableTimeout(I2C_T *i2c)
This function disables timeout function.
Definition: i2c.c:271
__I uint32_t STATUS
Nano100 series peripheral access layer header file. This file contains all the peripheral register's ...
#define I2C0_BASE
I2C0 register base address.
uint32_t I2C_GetStatus(I2C_T *i2c)
This function returns the status of I2C module.
Definition: i2c.c:181
void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
This function writes the data to data register of I2C module.
Definition: i2c.c:202
#define I2C_INTSTS_TIF_Msk
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
#define SYS_IPRST_CTL2_I2C1_RST_Msk
uint32_t I2C_GetData(I2C_T *i2c)
This function returns the data stored in data register of I2C module.
Definition: i2c.c:191
__IO uint32_t INTSTS
#define I2C_TOUT_TOUTEN_Msk
__IO uint32_t SAMASK1
__IO uint32_t SADDR0
uint32_t I2C_GetIntFlag(I2C_T *i2c)
This function gets the interrupt flag of I2C module.
Definition: i2c.c:161
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
#define I2C_TOUT_DIV4_Msk
#define SYS_IPRST_CTL2_I2C0_RST_Msk
#define I2C_CON_IPEN_Msk
#define I2C_CON_INTEN_Msk
__IO uint32_t DIV
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
#define I2C_AA
Definition: i2c.h:36
void I2C_ClearIntFlag(I2C_T *i2c)
This function clears the interrupt flag of I2C module.
Definition: i2c.c:171
__IO uint32_t DATA
__IO uint32_t TOUT
void I2C_DisableInt(I2C_T *i2c)
This function disables the interrupt of I2C module.
Definition: i2c.c:111
__IO uint32_t SAMASK0
__IO uint32_t SADDR1
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
uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
This function returns the real bus clock of I2C module.
Definition: i2c.c:131
__IO uint32_t CON
#define SYS
Pointer to SYS register structure.