1 | #include "lpc17xx.h"
|
2 | #include "lpc_types.h"
|
3 | #include "i2c.h"
|
4 |
|
5 | volatile uint32_t I2CMasterState[I2C_PORT_NUM] = {I2C_IDLE,I2C_IDLE,I2C_IDLE};
|
6 | volatile uint32_t timeout[I2C_PORT_NUM] = {0, 0, 0};
|
7 |
|
8 | volatile uint8_t I2CMasterBuffer[I2C_PORT_NUM][BUFSIZE];
|
9 | volatile uint8_t I2CSlaveBuffer[I2C_PORT_NUM][BUFSIZE];
|
10 | volatile uint32_t I2CCount[I2C_PORT_NUM] = {0, 0, 0};
|
11 | volatile uint32_t I2CReadLength[I2C_PORT_NUM];
|
12 | volatile uint32_t I2CWriteLength[I2C_PORT_NUM];
|
13 |
|
14 | volatile uint32_t RdIndex0 = 0, RdIndex1 = 0, RdIndex2 = 0;
|
15 | volatile uint32_t WrIndex0 = 0, WrIndex1 = 0, WrIndex2 = 0;
|
16 |
|
17 |
|
18 | /*
|
19 | From device to device, the I2C communication protocol may vary,
|
20 | in the example below, the protocol uses repeated start to read data from or
|
21 | write to the device:
|
22 | For master read: the sequence is: STA,Addr(W),offset,RE-STA,Addr(r),data...STO
|
23 | for master write: the sequence is: STA,Addr(W),offset,RE-STA,Addr(w),data...STO
|
24 | Thus, in state 8, the address is always WRITE. in state 10, the address could
|
25 | be READ or WRITE depending on the I2C command.
|
26 | */
|
27 |
|
28 | /*****************************************************************************
|
29 | ** Function name: I2C_IRQHandler
|
30 | **
|
31 | ** Descriptions: I2C interrupt handler, deal with master mode only.
|
32 | **
|
33 | ** parameters: None
|
34 | ** Returned value: None
|
35 | **
|
36 | *****************************************************************************/
|
37 | void I2C0_IRQHandler(void)
|
38 | {
|
39 | uint8_t StatValue;
|
40 |
|
41 | timeout[0] = 0;
|
42 | /* this handler deals with master read and master write only */
|
43 | StatValue = LPC_I2C0->I2STAT;
|
44 | switch ( StatValue )
|
45 | {
|
46 | case 0x08: /* A Start condition is issued. */
|
47 | WrIndex0 = 0;
|
48 | LPC_I2C0->I2DAT = I2CMasterBuffer[0][WrIndex0++];
|
49 | LPC_I2C0->I2CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
|
50 | break;
|
51 |
|
52 | case 0x10: /* A repeated started is issued */
|
53 | RdIndex0 = 0;
|
54 | /* Send SLA with R bit set, */
|
55 | LPC_I2C0->I2DAT = I2CMasterBuffer[0][WrIndex0++];
|
56 | LPC_I2C0->I2CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
|
57 | break;
|
58 |
|
59 | case 0x18: /* Regardless, it's a ACK */
|
60 | if ( I2CWriteLength[0] == 1 )
|
61 | {
|
62 | LPC_I2C0->I2CONSET = I2CONSET_STO; /* Set Stop flag */
|
63 | I2CMasterState[0] = I2C_NO_DATA;
|
64 | }
|
65 | else
|
66 | {
|
67 | LPC_I2C0->I2DAT = I2CMasterBuffer[0][WrIndex0++];
|
68 | }
|
69 | LPC_I2C0->I2CONCLR = I2CONCLR_SIC;
|
70 | break;
|
71 |
|
72 | case 0x28: /* Data byte has been transmitted, regardless ACK or NACK */
|
73 | if ( WrIndex0 < I2CWriteLength[0] )
|
74 | {
|
75 | LPC_I2C0->I2DAT = I2CMasterBuffer[0][WrIndex0++]; /* this should be the last one */
|
76 | }
|
77 | else
|
78 | {
|
79 | if ( I2CReadLength[0] != 0 )
|
80 | {
|
81 | LPC_I2C0->I2CONSET = I2CONSET_STA; /* Set Repeated-start flag */
|
82 | }
|
83 | else
|
84 | {
|
85 | LPC_I2C0->I2CONSET = I2CONSET_STO; /* Set Stop flag */
|
86 | I2CMasterState[0] = I2C_OK;
|
87 | }
|
88 | }
|
89 | LPC_I2C0->I2CONCLR = I2CONCLR_SIC;
|
90 | break;
|
91 |
|
92 | case 0x30:
|
93 | LPC_I2C0->I2CONSET = I2CONSET_STO; /* Set Stop flag */
|
94 | I2CMasterState[0] = I2C_NACK_ON_DATA;
|
95 | LPC_I2C0->I2CONCLR = I2CONCLR_SIC;
|
96 | break;
|
97 |
|
98 | case 0x40: /* Master Receive, SLA_R has been sent */
|
99 | if ( (RdIndex0 + 1) < I2CReadLength[0] )
|
100 | {
|
101 | /* Will go to State 0x50 */
|
102 | LPC_I2C0->I2CONSET = I2CONSET_AA; /* assert ACK after data is received */
|
103 | }
|
104 | else
|
105 | {
|
106 | /* Will go to State 0x58 */
|
107 | LPC_I2C0->I2CONCLR = I2CONCLR_AAC; /* assert NACK after data is received */
|
108 | }
|
109 | LPC_I2C0->I2CONCLR = I2CONCLR_SIC;
|
110 | break;
|
111 |
|
112 | case 0x50: /* Data byte has been received, regardless following ACK or NACK */
|
113 | I2CSlaveBuffer[0][RdIndex0++] = LPC_I2C0->I2DAT;
|
114 | if ( (RdIndex0 + 1) < I2CReadLength[0] )
|
115 | {
|
116 | LPC_I2C0->I2CONSET = I2CONSET_AA; /* assert ACK after data is received */
|
117 | }
|
118 | else
|
119 | {
|
120 | LPC_I2C0->I2CONCLR = I2CONCLR_AAC; /* assert NACK on last byte */
|
121 | }
|
122 | LPC_I2C0->I2CONCLR = I2CONCLR_SIC;
|
123 | break;
|
124 |
|
125 | case 0x58:
|
126 | I2CSlaveBuffer[0][RdIndex0++] = LPC_I2C0->I2DAT;
|
127 | I2CMasterState[0] = I2C_OK;
|
128 | LPC_I2C0->I2CONSET = I2CONSET_STO; /* Set Stop flag */
|
129 | LPC_I2C0->I2CONCLR = I2CONCLR_SIC; /* Clear SI flag */
|
130 | break;
|
131 |
|
132 | case 0x20: /* regardless, it's a NACK */
|
133 | case 0x48:
|
134 | LPC_I2C0->I2CONSET = I2CONSET_STO; /* Set Stop flag */
|
135 | I2CMasterState[0] = I2C_NACK_ON_ADDRESS;
|
136 | LPC_I2C0->I2CONCLR = I2CONCLR_SIC;
|
137 | break;
|
138 |
|
139 | case 0x38: /* Arbitration lost, in this example, we don't
|
140 | deal with multiple master situation */
|
141 | default:
|
142 | I2CMasterState[0] = I2C_ARBITRATION_LOST;
|
143 | LPC_I2C0->I2CONCLR = I2CONCLR_SIC;
|
144 | break;
|
145 | }
|
146 | return;
|
147 | }
|
148 | /*****************************************************************************
|
149 | ** Function name: I2CStart
|
150 | **
|
151 | ** Descriptions: Create I2C start condition, a timeout
|
152 | ** value is set if the I2C never gets started,
|
153 | ** and timed out. It's a fatal error.
|
154 | **
|
155 | ** parameters: None
|
156 | ** Returned value: true or false, return false if timed out
|
157 | **
|
158 | *****************************************************************************/
|
159 | uint32_t I2CStart( uint32_t portNum )
|
160 | {
|
161 | uint32_t retVal = FALSE;
|
162 |
|
163 | timeout[portNum] = 0;
|
164 | /*--- Issue a start condition ---*/
|
165 | LPC_I2C[portNum]->I2CONSET = I2CONSET_STA; /* Set Start flag */
|
166 |
|
167 | /*--- Wait until START transmitted ---*/
|
168 | while( 1 )
|
169 | {
|
170 | if ( I2CMasterState[portNum] == I2C_STARTED )
|
171 | {
|
172 | retVal = TRUE;
|
173 | break;
|
174 | }
|
175 | if ( timeout[portNum] >= MAX_TIMEOUT )
|
176 | {
|
177 | retVal = FALSE;
|
178 | break;
|
179 | }
|
180 | timeout[portNum]++;
|
181 | }
|
182 | return( retVal );
|
183 | }
|
184 |
|
185 | /*****************************************************************************
|
186 | ** Function name: I2CStop
|
187 | **
|
188 | ** Descriptions: Set the I2C stop condition, if the routine
|
189 | ** never exit, it's a fatal bus error.
|
190 | **
|
191 | ** parameters: None
|
192 | ** Returned value: true or never return
|
193 | **
|
194 | *****************************************************************************/
|
195 | uint32_t I2CStop( uint32_t portNum )
|
196 | {
|
197 | LPC_I2C[portNum]->I2CONSET = I2CONSET_STO; /* Set Stop flag */
|
198 | LPC_I2C[portNum]->I2CONCLR = I2CONCLR_SIC; /* Clear SI flag */
|
199 |
|
200 | /*--- Wait for STOP detected ---*/
|
201 | while( LPC_I2C[portNum]->I2CONSET & I2CONSET_STO );
|
202 | return TRUE;
|
203 | }
|
204 |
|
205 | /*****************************************************************************
|
206 | ** Function name: I2CInit
|
207 | **
|
208 | ** Descriptions: Initialize I2C controller as a master
|
209 | **
|
210 | ** parameters: None
|
211 | ** Returned value: None
|
212 | **
|
213 | *****************************************************************************/
|
214 | void I2C0Init( void )
|
215 | {
|
216 | LPC_SC->PCONP |= (1 << 7);
|
217 |
|
218 | /* set PIO0.27 and PIO0.28 to I2C0 SDA and SCL */
|
219 | /* function to 01 on both SDA and SCL. */
|
220 | LPC_PINCON->PINSEL1 &= ~((0x03<<22)|(0x03<<24));
|
221 | LPC_PINCON->PINSEL1 |= ((0x01<<22)|(0x01<<24));
|
222 |
|
223 | /*--- Clear flags ---*/
|
224 | LPC_I2C0->I2CONCLR = I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC | I2CONCLR_I2ENC;
|
225 |
|
226 | /*--- Reset registers ---*/
|
227 | #if FAST_MODE_PLUS
|
228 | LPC_PINCON->I2CPADCFG |= ((0x1<<0)|(0x1<<2));
|
229 | LPC_I2C0->SCLL = I2SCLL_HS_SCLL;
|
230 | LPC_I2C0->SCLH = I2SCLH_HS_SCLH;
|
231 | #else
|
232 | LPC_PINCON->I2CPADCFG &= ~((0x1<<0)|(0x1<<2));
|
233 | LPC_I2C0->I2SCLL = I2SCLL_SCLL;
|
234 | LPC_I2C0->I2SCLH = I2SCLH_SCLH;
|
235 | #endif
|
236 |
|
237 | /* Install interrupt handler */
|
238 | NVIC_EnableIRQ(I2C0_IRQn);
|
239 |
|
240 | LPC_I2C0->I2CONSET = I2CONSET_I2EN;
|
241 | return;
|
242 | }
|
243 | /*****************************************************************************
|
244 | ** Function name: I2CEngine
|
245 | **
|
246 | ** Descriptions: The routine to complete a I2C transaction
|
247 | ** from start to stop. All the intermitten
|
248 | ** steps are handled in the interrupt handler.
|
249 | ** Before this routine is called, the read
|
250 | ** length, write length, I2C master buffer,
|
251 | ** and I2C command fields need to be filled.
|
252 | ** see i2cmst.c for more details.
|
253 | **
|
254 | ** parameters: I2C port number
|
255 | ** Returned value: master state of current I2C port.
|
256 | **
|
257 | *****************************************************************************/
|
258 | uint32_t I2CEngine( uint32_t portNum )
|
259 | {
|
260 | /*--- Issue a start condition ---*/
|
261 | LPC_I2C[portNum]->I2CONSET = I2CONSET_STA; /* Set Start flag */
|
262 |
|
263 | I2CMasterState[portNum] = I2C_BUSY;
|
264 |
|
265 | while ( I2CMasterState[portNum] == I2C_BUSY )
|
266 | {
|
267 | if ( timeout[portNum] >= MAX_TIMEOUT )
|
268 | {
|
269 | I2CMasterState[portNum] = I2C_TIME_OUT;
|
270 | break;
|
271 | }
|
272 | timeout[portNum]++;
|
273 | }
|
274 | LPC_I2C[portNum]->I2CONCLR = I2CONCLR_STAC;
|
275 |
|
276 | return ( I2CMasterState[portNum] );
|
277 | }
|