NANO102/112 BSP V3.03.003
The Board Support Package for Nano102/112 Series
i2c.c
Go to the documentation of this file.
1/****************************************************************************/
13#include "Nano1X2Series.h"
14
34uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock)
35{
36 uint32_t u32Div;
37 uint32_t u32Pclk = CLK_GetPCLKFreq();
38
39 u32Div = (uint32_t) (((u32Pclk * 10)/(u32BusClock * 4) + 5) / 10 - 1); /* Compute proper divider for I2C clock */
40 i2c->DIV = u32Div;
41
42 /* Ensable I2C */
43 i2c->CON |= I2C_CON_IPEN_Msk;
44
45 return ( u32Pclk / ((u32Div+1)<<2) );
46}
47
53void I2C_Close(I2C_T *i2c)
54{
55 /* Reset I2C */
56 if((uint32_t)i2c == I2C0_BASE)
57 {
58 SYS->IPRST_CTL2 |= SYS_IPRST_CTL2_I2C0_RST_Msk;
59 SYS->IPRST_CTL2 &= ~SYS_IPRST_CTL2_I2C0_RST_Msk;
60 }
61 else
62 {
63 SYS->IPRST_CTL2 |= SYS_IPRST_CTL2_I2C1_RST_Msk;
64 SYS->IPRST_CTL2 &= ~SYS_IPRST_CTL2_I2C1_RST_Msk;
65 }
66
67 /* Disable I2C */
68 i2c->CON &= ~I2C_CON_IPEN_Msk;
69}
70
77{
79}
80
90void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack)
91{
92 uint32_t u32Reg = 0;
93 uint32_t u32Val = i2c->CON & ~(I2C_STA | I2C_STO | I2C_AA);
94
95 if (u8Start)
96 u32Reg |= I2C_STA;
97 if (u8Stop)
98 u32Reg |= I2C_STO;
99 if (u8Si)
100 u32Reg |= I2C_SI;
101 if (u8Ack)
102 u32Reg |= I2C_AA;
103
104 i2c->CON = u32Val | u32Reg;
105}
106
113{
114 i2c->CON &= ~I2C_CON_INTEN_Msk;
115}
116
123{
124 i2c->CON |= I2C_CON_INTEN_Msk;
125}
126
133{
134 uint32_t u32Divider = i2c->DIV;
135
136 return ( CLK_GetPCLKFreq() / ((u32Divider+1)<<2) );
137}
138
145uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
146{
147 uint32_t u32Div;
148 uint32_t u32Pclk = CLK_GetPCLKFreq();
149
150 u32Div = (uint32_t) (((u32Pclk * 10)/(u32BusClock * 4) + 5) / 10 - 1); /* Compute proper divider for I2C clock */
151 i2c->DIV = u32Div;
152
153 return ( u32Pclk / ((u32Div+1)<<2) );
154}
155
163uint32_t I2C_GetIntFlag(I2C_T *i2c)
164{
165 return ( (i2c->INTSTS & I2C_INTSTS_INTSTS_Msk) == I2C_INTSTS_INTSTS_Msk ? 1:0 );
166}
167
174{
176}
177
183uint32_t I2C_GetStatus(I2C_T *i2c)
184{
185 return ( i2c->STATUS );
186}
187
193uint32_t I2C_GetData(I2C_T *i2c)
194{
195 return ( i2c->DATA );
196}
197
204void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
205{
206 i2c->DATA = u8Data;
207}
208
219void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode)
220{
221 switch (u8SlaveNo)
222 {
223 case 0:
224 i2c->SADDR0 = (u8SlaveAddr << 1) | u8GCMode;
225 break;
226 case 1:
227 i2c->SADDR1 = (u8SlaveAddr << 1) | u8GCMode;
228 break;
229 default:
230 i2c->SADDR0 = (u8SlaveAddr << 1) | u8GCMode;
231 }
232}
233
241void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask)
242{
243 switch (u8SlaveNo)
244 {
245 case 0:
246 i2c->SAMASK0 = u8SlaveAddrMask << 1;
247 break;
248 case 1:
249 i2c->SAMASK1 = u8SlaveAddrMask << 1;
250 break;
251 default:
252 i2c->SAMASK0 = u8SlaveAddrMask << 1;
253 }
254}
255
262void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout)
263{
264 if(u8LongTimeout)
265 i2c->TOUT |= I2C_TOUT_DIV4_Msk;
266 else
267 i2c->TOUT &= ~I2C_TOUT_DIV4_Msk;
268
270}
271
278{
279 i2c->TOUT &= ~I2C_TOUT_TOUTEN_Msk;
280}
281
288{
290}
291
298{
299 i2c->CON2 &= ~I2C_CON2_WKUPEN_Msk;
300}
301 /* end of group NANO1X2_I2C_EXPORTED_FUNCTIONS */
303 /* end of group NANO1X2_I2C_Driver */
305 /* end of group NANO1X2_Device_Driver */
307
308/*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
Nano102/112 peripheral access layer header file. This file contains all the peripheral register's def...
#define I2C_CON_INTEN_Msk
#define I2C_INTSTS_INTSTS_Msk
#define I2C_TOUT_TOUTEN_Msk
#define I2C_CON2_WKUPEN_Msk
#define I2C_CON_IPEN_Msk
#define SYS_IPRST_CTL2_I2C1_RST_Msk
#define I2C_INTSTS_TIF_Msk
#define I2C_TOUT_DIV4_Msk
#define SYS_IPRST_CTL2_I2C0_RST_Msk
uint32_t CLK_GetPCLKFreq(void)
This function get PCLK frequency. The frequency unit is Hz.
Definition: clk.c:203
#define I2C_SI
Definition: i2c.h:35
#define I2C_AA
Definition: i2c.h:36
#define I2C_STO
Definition: i2c.h:34
#define I2C_STA
Definition: i2c.h:33
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:241
void I2C_Close(I2C_T *i2c)
This function closes the I2C module.
Definition: i2c.c:53
uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
This function sets bus frequency of I2C module.
Definition: i2c.c:145
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:90
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:262
void I2C_ClearTimeoutFlag(I2C_T *i2c)
This function clears the timeout flag.
Definition: i2c.c:76
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:219
void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
This function writes the data to data register of I2C module.
Definition: i2c.c:204
uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
This function returns the real bus clock of I2C module.
Definition: i2c.c:132
void I2C_EnableInt(I2C_T *i2c)
This function enables the interrupt (EI bit) of I2C module.
Definition: i2c.c:122
void I2C_DisableWakeup(I2C_T *i2c)
This function disables the wakeup function of I2C module.
Definition: i2c.c:297
uint32_t I2C_GetStatus(I2C_T *i2c)
This function returns the status of I2C module.
Definition: i2c.c:183
void I2C_DisableInt(I2C_T *i2c)
This function disables the interrupt of I2C module.
Definition: i2c.c:112
uint32_t I2C_GetData(I2C_T *i2c)
This function returns the data stored in data register of I2C module.
Definition: i2c.c:193
void I2C_EnableWakeup(I2C_T *i2c)
This function enables the wakeup function of I2C module.
Definition: i2c.c:287
void I2C_ClearIntFlag(I2C_T *i2c)
This function clears the interrupt flag of I2C module.
Definition: i2c.c:173
uint32_t I2C_GetIntFlag(I2C_T *i2c)
This function gets the interrupt flag of I2C module.
Definition: i2c.c:163
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_DisableTimeout(I2C_T *i2c)
This function disables timeout function.
Definition: i2c.c:277
#define SYS
Pointer to SYS register structure.
#define I2C0_BASE
I2C0 register base address.
__IO uint32_t SAMASK1
__IO uint32_t INTSTS
__IO uint32_t CON2
__IO uint32_t CON
__IO uint32_t TOUT
__I uint32_t STATUS
__IO uint32_t SADDR1
__IO uint32_t SADDR0
__IO uint32_t DIV
__IO uint32_t DATA
__IO uint32_t SAMASK0