| NANO102/112 BSP V3.04.000 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
53 void 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
76 void I2C_ClearTimeoutFlag(I2C_T *i2c)
77{
78 i2c->INTSTS |= I2C_INTSTS_TIF_Msk;
79}
80
90 void 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
112 void I2C_DisableInt(I2C_T *i2c)
113{
114 i2c->CON &= ~I2C_CON_INTEN_Msk;
115}
116
122 void I2C_EnableInt(I2C_T *i2c)
123{
124 i2c->CON |= I2C_CON_INTEN_Msk;
125}
126
132uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
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
173 void I2C_ClearIntFlag(I2C_T *i2c)
174{
175 i2c->INTSTS |= I2C_INTSTS_INTSTS_Msk;
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
204 void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
205{
206 i2c->DATA = u8Data;
207}
208
219 void 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
241 void 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
262 void 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
269 i2c->TOUT |= I2C_TOUT_TOUTEN_Msk;
270}
271
277 void I2C_DisableTimeout(I2C_T *i2c)
278{
279 i2c->TOUT &= ~I2C_TOUT_TOUTEN_Msk;
280}
281
287 void I2C_EnableWakeup(I2C_T *i2c)
288{
289 i2c->CON2 |= I2C_CON2_WKUPEN_Msk;
290}
291
297 void I2C_DisableWakeup(I2C_T *i2c)
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. ***/
Nano1X2Series.h
Nano102/112 peripheral access layer header file. This file contains all the peripheral register's def...
I2C_CON_INTEN_Msk
#define I2C_CON_INTEN_Msk
Definition: Nano1X2Series.h:5151
I2C_INTSTS_INTSTS_Msk
#define I2C_INTSTS_INTSTS_Msk
Definition: Nano1X2Series.h:5154
I2C_TOUT_TOUTEN_Msk
#define I2C_TOUT_TOUTEN_Msk
Definition: Nano1X2Series.h:5169
I2C_CON2_WKUPEN_Msk
#define I2C_CON2_WKUPEN_Msk
Definition: Nano1X2Series.h:5196
I2C_CON_IPEN_Msk
#define I2C_CON_IPEN_Msk
Definition: Nano1X2Series.h:5136
SYS_IPRST_CTL2_I2C1_RST_Msk
#define SYS_IPRST_CTL2_I2C1_RST_Msk
Definition: Nano1X2Series.h:3669
I2C_INTSTS_TIF_Msk
#define I2C_INTSTS_TIF_Msk
Definition: Nano1X2Series.h:5157
I2C_TOUT_DIV4_Msk
#define I2C_TOUT_DIV4_Msk
Definition: Nano1X2Series.h:5172
SYS_IPRST_CTL2_I2C0_RST_Msk
#define SYS_IPRST_CTL2_I2C0_RST_Msk
Definition: Nano1X2Series.h:3666
CLK_GetPCLKFreq
uint32_t CLK_GetPCLKFreq(void)
This function get PCLK frequency. The frequency unit is Hz.
Definition: clk.c:203
I2C_SI
#define I2C_SI
Definition: i2c.h:35
I2C_AA
#define I2C_AA
Definition: i2c.h:36
I2C_STO
#define I2C_STO
Definition: i2c.h:34
I2C_STA
#define I2C_STA
Definition: i2c.h:33
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:241
I2C_Close
void I2C_Close(I2C_T *i2c)
This function closes the I2C module.
Definition: i2c.c:53
I2C_SetBusClockFreq
uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
This function sets bus frequency of I2C module.
Definition: i2c.c:145
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:90
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:262
I2C_ClearTimeoutFlag
void I2C_ClearTimeoutFlag(I2C_T *i2c)
This function clears the timeout flag.
Definition: i2c.c:76
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:219
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:204
I2C_GetBusClockFreq
uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
This function returns the real bus clock of I2C module.
Definition: i2c.c:132
I2C_EnableInt
void I2C_EnableInt(I2C_T *i2c)
This function enables the interrupt (EI bit) of I2C module.
Definition: i2c.c:122
I2C_DisableWakeup
void I2C_DisableWakeup(I2C_T *i2c)
This function disables the wakeup function of I2C module.
Definition: i2c.c:297
I2C_GetStatus
uint32_t I2C_GetStatus(I2C_T *i2c)
This function returns the status of I2C module.
Definition: i2c.c:183
I2C_DisableInt
void I2C_DisableInt(I2C_T *i2c)
This function disables the interrupt of I2C module.
Definition: i2c.c:112
I2C_GetData
uint32_t I2C_GetData(I2C_T *i2c)
This function returns the data stored in data register of I2C module.
Definition: i2c.c:193
I2C_EnableWakeup
void I2C_EnableWakeup(I2C_T *i2c)
This function enables the wakeup function of I2C module.
Definition: i2c.c:287
I2C_ClearIntFlag
void I2C_ClearIntFlag(I2C_T *i2c)
This function clears the interrupt flag of I2C module.
Definition: i2c.c:173
I2C_GetIntFlag
uint32_t I2C_GetIntFlag(I2C_T *i2c)
This function gets the interrupt flag of I2C module.
Definition: i2c.c:163
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:34
I2C_DisableTimeout
void I2C_DisableTimeout(I2C_T *i2c)
This function disables timeout function.
Definition: i2c.c:277
SYS
#define SYS
Pointer to SYS register structure.
Definition: Nano1X2Series.h:10650
I2C0_BASE
#define I2C0_BASE
I2C0 register base address.
Definition: Nano1X2Series.h:10587
I2C_T::SAMASK1
__IO uint32_t SAMASK1
Definition: Nano1X2Series.h:5066
I2C_T::INTSTS
__IO uint32_t INTSTS
Definition: Nano1X2Series.h:4947
I2C_T::CON2
__IO uint32_t CON2
Definition: Nano1X2Series.h:5093
I2C_T::CON
__IO uint32_t CON
Definition: Nano1X2Series.h:4928
I2C_T::TOUT
__IO uint32_t TOUT
Definition: Nano1X2Series.h:4992
I2C_T::STATUS
__I uint32_t STATUS
Definition: Nano1X2Series.h:4960
I2C_T::SADDR1
__IO uint32_t SADDR1
Definition: Nano1X2Series.h:5038
I2C_T::SADDR0
__IO uint32_t SADDR0
Definition: Nano1X2Series.h:5021
I2C_T::DIV
__IO uint32_t DIV
Definition: Nano1X2Series.h:4973
I2C_T::DATA
__IO uint32_t DATA
Definition: Nano1X2Series.h:5004
I2C_T::SAMASK0
__IO uint32_t SAMASK0
Definition: Nano1X2Series.h:5053
I2C_T
Definition: Nano1X2Series.h:4891