NUC472_NUC442_BSP V3.03.005
The Board Support Package for NUC472/NUC442
i2c.c
Go to the documentation of this file.
1/****************************************************************************/
13#include "NUC472_442.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->CLKDIV = u32Div;
41
42 /* Enable I2C */
43 i2c->CTL |= I2C_CTL_I2CEN_Msk;
44
45 return ( u32Pclk / ((u32Div+1)<<2) );
46}
47
53void I2C_Close(I2C_T *i2c)
54{
55 /* Reset SPI */
56 if((uint32_t)i2c == I2C0_BASE)
57 {
58 SYS->IPRST1 |= SYS_IPRST1_I2C0RST_Msk;
59 SYS->IPRST1 &= ~SYS_IPRST1_I2C0RST_Msk;
60 }
61 else if((uint32_t)i2c == I2C1_BASE)
62 {
63 SYS->IPRST1 |= SYS_IPRST1_I2C1RST_Msk;
64 SYS->IPRST1 &= ~SYS_IPRST1_I2C1RST_Msk;
65 }
66 else if((uint32_t)i2c == I2C2_BASE)
67 {
68 SYS->IPRST1 |= SYS_IPRST1_I2C2RST_Msk;
69 SYS->IPRST1 &= ~SYS_IPRST1_I2C2RST_Msk;
70 }
71 else
72 {
73 SYS->IPRST1 |= SYS_IPRST1_I2C3RST_Msk;
74 SYS->IPRST1 &= ~SYS_IPRST1_I2C3RST_Msk;
75 }
76
77 /* Disable I2C */
78 i2c->CTL &= ~I2C_CTL_I2CEN_Msk;
79}
80
87{
89}
90
100void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack)
101{
102 uint32_t u32Reg = 0;
103 uint32_t u32Val = i2c->CTL & ~(I2C_STA | I2C_STO | I2C_AA);
104
105 if (u8Start)
106 u32Reg |= I2C_STA;
107 if (u8Stop)
108 u32Reg |= I2C_STO;
109 if (u8Si)
110 u32Reg |= I2C_SI;
111 if (u8Ack)
112 u32Reg |= I2C_AA;
113
114 i2c->CTL = u32Val | u32Reg;
115}
116
123{
124 i2c->CTL &= ~I2C_CTL_INTEN_Msk;
125}
126
133{
134 i2c->CTL |= I2C_CTL_INTEN_Msk;
135}
136
143{
144 uint32_t u32Divider = i2c->CLKDIV;
145
146 return ( CLK_GetPCLKFreq() / ((u32Divider+1)<<2) );
147}
148
155uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
156{
157 uint32_t u32Div;
158 uint32_t u32Pclk = CLK_GetPCLKFreq();
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
173uint32_t I2C_GetIntFlag(I2C_T *i2c)
174{
175 return ( (i2c->CTL & I2C_CTL_SI_Msk) == I2C_CTL_SI_Msk ? 1:0 );
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->DAT );
196}
197
204void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
205{
206 i2c->DAT = 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->ADDR0 = (u8SlaveAddr << 1) | u8GCMode;
225 break;
226 case 1:
227 i2c->ADDR1 = (u8SlaveAddr << 1) | u8GCMode;
228 break;
229 case 2:
230 i2c->ADDR2 = (u8SlaveAddr << 1) | u8GCMode;
231 break;
232 case 3:
233 i2c->ADDR3 = (u8SlaveAddr << 1) | u8GCMode;
234 break;
235 default:
236 i2c->ADDR0 = (u8SlaveAddr << 1) | u8GCMode;
237 }
238}
239
247void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask)
248{
249 switch (u8SlaveNo)
250 {
251 case 0:
252 i2c->ADDRMSK0 = u8SlaveAddrMask << 1;
253 break;
254 case 1:
255 i2c->ADDRMSK1 = u8SlaveAddrMask << 1;
256 break;
257 case 2:
258 i2c->ADDRMSK2 = u8SlaveAddrMask << 1;
259 break;
260 case 3:
261 i2c->ADDRMSK3 = u8SlaveAddrMask << 1;
262 break;
263 default:
264 i2c->ADDRMSK0 = u8SlaveAddrMask << 1;
265 }
266}
267
274void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout)
275{
276 if(u8LongTimeout)
278 else
279 i2c->TOCTL &= ~I2C_TOCTL_TOCDIV4_Msk;
280
282}
283
290{
291 i2c->TOCTL &= ~I2C_TOCTL_TOCEN_Msk;
292}
293
300{
302}
303
310{
311 i2c->WKCTL &= ~I2C_WKCTL_WKEN_Msk;
312}
313 /* end of group NUC472_442_I2C_EXPORTED_FUNCTIONS */
315 /* end of group NUC472_442_I2C_Driver */
317 /* end of group NUC472_442_Device_Driver */
319
320/*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
NUC472/NUC442 peripheral access layer header file. This file contains all the peripheral register's d...
#define SYS_IPRST1_I2C1RST_Msk
Definition: NUC472_442.h:24262
#define SYS_IPRST1_I2C3RST_Msk
Definition: NUC472_442.h:24268
#define I2C_CTL_INTEN_Msk
Definition: NUC472_442.h:17103
#define I2C_TOCTL_TOIF_Msk
Definition: NUC472_442.h:17121
#define SYS_IPRST1_I2C2RST_Msk
Definition: NUC472_442.h:24265
#define I2C_TOCTL_TOCEN_Msk
Definition: NUC472_442.h:17127
#define I2C_WKCTL_WKEN_Msk
Definition: NUC472_442.h:17160
#define I2C_CTL_I2CEN_Msk
Definition: NUC472_442.h:17100
#define SYS_IPRST1_I2C0RST_Msk
Definition: NUC472_442.h:24259
#define I2C_CTL_SI_Msk
Definition: NUC472_442.h:17091
#define I2C_TOCTL_TOCDIV4_Msk
Definition: NUC472_442.h:17124
uint32_t CLK_GetPCLKFreq(void)
This function get PCLK frequency. The frequency unit is Hz.
Definition: clk.c:104
#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:247
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:155
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:100
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:274
void I2C_ClearTimeoutFlag(I2C_T *i2c)
This function clears the time-out flag.
Definition: i2c.c:86
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:142
void I2C_EnableInt(I2C_T *i2c)
This function enables the interrupt (EI bit) of I2C module.
Definition: i2c.c:132
void I2C_DisableWakeup(I2C_T *i2c)
This function disables the wakeup function of I2C module.
Definition: i2c.c:309
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:122
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:299
uint32_t I2C_GetIntFlag(I2C_T *i2c)
This function gets the interrupt flag of I2C module.
Definition: i2c.c:173
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 time-out function.
Definition: i2c.c:289
#define SYS
Definition: NUC472_442.h:28797
#define I2C2_BASE
Definition: NUC472_442.h:28753
#define I2C0_BASE
Definition: NUC472_442.h:28752
#define I2C1_BASE
Definition: NUC472_442.h:28779
__IO uint32_t ADDR3
Definition: NUC472_442.h:16983
__IO uint32_t ADDR0
Definition: NUC472_442.h:16870
__IO uint32_t DAT
Definition: NUC472_442.h:16882
__IO uint32_t ADDRMSK3
Definition: NUC472_442.h:17047
__IO uint32_t TOCTL
Definition: NUC472_442.h:16932
__IO uint32_t CLKDIV
Definition: NUC472_442.h:16910
__I uint32_t STATUS
Definition: NUC472_442.h:16897
__IO uint32_t WKCTL
Definition: NUC472_442.h:17064
__IO uint32_t ADDRMSK1
Definition: NUC472_442.h:17015
__IO uint32_t ADDR1
Definition: NUC472_442.h:16949
__IO uint32_t ADDR2
Definition: NUC472_442.h:16966
__IO uint32_t CTL
Definition: NUC472_442.h:16853
__IO uint32_t ADDRMSK0
Definition: NUC472_442.h:16999
__IO uint32_t ADDRMSK2
Definition: NUC472_442.h:17031