| NUC472_NUC442_BSP V3.04.000 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
53 void 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
86 void I2C_ClearTimeoutFlag(I2C_T *i2c)
87{
88 i2c->TOCTL |= I2C_TOCTL_TOIF_Msk;
89}
90
100 void 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
122 void I2C_DisableInt(I2C_T *i2c)
123{
124 i2c->CTL &= ~I2C_CTL_INTEN_Msk;
125}
126
132 void I2C_EnableInt(I2C_T *i2c)
133{
134 i2c->CTL |= I2C_CTL_INTEN_Msk;
135}
136
142uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
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
204 void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
205{
206 i2c->DAT = 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->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
247 void 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
274 void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout)
275{
276 if(u8LongTimeout)
277 i2c->TOCTL |= I2C_TOCTL_TOCDIV4_Msk;
278 else
279 i2c->TOCTL &= ~I2C_TOCTL_TOCDIV4_Msk;
280
281 i2c->TOCTL |= I2C_TOCTL_TOCEN_Msk;
282}
283
289 void I2C_DisableTimeout(I2C_T *i2c)
290{
291 i2c->TOCTL &= ~I2C_TOCTL_TOCEN_Msk;
292}
293
299 void I2C_EnableWakeup(I2C_T *i2c)
300{
301 i2c->WKCTL |= I2C_WKCTL_WKEN_Msk;
302}
303
309 void I2C_DisableWakeup(I2C_T *i2c)
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_442.h
NUC472/NUC442 peripheral access layer header file. This file contains all the peripheral register's d...
SYS_IPRST1_I2C1RST_Msk
#define SYS_IPRST1_I2C1RST_Msk
Definition: NUC472_442.h:24262
SYS_IPRST1_I2C3RST_Msk
#define SYS_IPRST1_I2C3RST_Msk
Definition: NUC472_442.h:24268
I2C_CTL_INTEN_Msk
#define I2C_CTL_INTEN_Msk
Definition: NUC472_442.h:17103
I2C_TOCTL_TOIF_Msk
#define I2C_TOCTL_TOIF_Msk
Definition: NUC472_442.h:17121
SYS_IPRST1_I2C2RST_Msk
#define SYS_IPRST1_I2C2RST_Msk
Definition: NUC472_442.h:24265
I2C_TOCTL_TOCEN_Msk
#define I2C_TOCTL_TOCEN_Msk
Definition: NUC472_442.h:17127
I2C_WKCTL_WKEN_Msk
#define I2C_WKCTL_WKEN_Msk
Definition: NUC472_442.h:17160
I2C_CTL_I2CEN_Msk
#define I2C_CTL_I2CEN_Msk
Definition: NUC472_442.h:17100
SYS_IPRST1_I2C0RST_Msk
#define SYS_IPRST1_I2C0RST_Msk
Definition: NUC472_442.h:24259
I2C_CTL_SI_Msk
#define I2C_CTL_SI_Msk
Definition: NUC472_442.h:17091
I2C_TOCTL_TOCDIV4_Msk
#define I2C_TOCTL_TOCDIV4_Msk
Definition: NUC472_442.h:17124
CLK_GetPCLKFreq
uint32_t CLK_GetPCLKFreq(void)
This function get PCLK frequency. The frequency unit is Hz.
Definition: clk.c:104
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:247
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:155
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:100
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:274
I2C_ClearTimeoutFlag
void I2C_ClearTimeoutFlag(I2C_T *i2c)
This function clears the time-out flag.
Definition: i2c.c:86
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:142
I2C_EnableInt
void I2C_EnableInt(I2C_T *i2c)
This function enables the interrupt (EI bit) of I2C module.
Definition: i2c.c:132
I2C_DisableWakeup
void I2C_DisableWakeup(I2C_T *i2c)
This function disables the wakeup function of I2C module.
Definition: i2c.c:309
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:122
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:299
I2C_GetIntFlag
uint32_t I2C_GetIntFlag(I2C_T *i2c)
This function gets the interrupt flag of I2C module.
Definition: i2c.c:173
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 time-out function.
Definition: i2c.c:289
SYS
#define SYS
Definition: NUC472_442.h:28797
I2C2_BASE
#define I2C2_BASE
Definition: NUC472_442.h:28753
I2C0_BASE
#define I2C0_BASE
Definition: NUC472_442.h:28752
I2C1_BASE
#define I2C1_BASE
Definition: NUC472_442.h:28779
I2C_T::ADDR3
__IO uint32_t ADDR3
Definition: NUC472_442.h:16983
I2C_T::ADDR0
__IO uint32_t ADDR0
Definition: NUC472_442.h:16870
I2C_T::DAT
__IO uint32_t DAT
Definition: NUC472_442.h:16882
I2C_T::ADDRMSK3
__IO uint32_t ADDRMSK3
Definition: NUC472_442.h:17047
I2C_T::TOCTL
__IO uint32_t TOCTL
Definition: NUC472_442.h:16932
I2C_T::CLKDIV
__IO uint32_t CLKDIV
Definition: NUC472_442.h:16910
I2C_T::STATUS
__I uint32_t STATUS
Definition: NUC472_442.h:16897
I2C_T::WKCTL
__IO uint32_t WKCTL
Definition: NUC472_442.h:17064
I2C_T::ADDRMSK1
__IO uint32_t ADDRMSK1
Definition: NUC472_442.h:17015
I2C_T::ADDR1
__IO uint32_t ADDR1
Definition: NUC472_442.h:16949
I2C_T::ADDR2
__IO uint32_t ADDR2
Definition: NUC472_442.h:16966
I2C_T::CTL
__IO uint32_t CTL
Definition: NUC472_442.h:16853
I2C_T::ADDRMSK0
__IO uint32_t ADDRMSK0
Definition: NUC472_442.h:16999
I2C_T::ADDRMSK2
__IO uint32_t ADDRMSK2
Definition: NUC472_442.h:17031
I2C_T
Definition: NUC472_442.h:16820