1 /*****************************************************************************
2 * i2c.c: I2C C file for NXP LPC11xx/13xx Family Microprocessors
4 * Copyright(C) 2008, NXP Semiconductor
5 * Parts of this code are (C) 2010, MyVoice CAD/CAM Services
9 * 2009.12.07 ver 1.00 Preliminary version, first Release
10 * 2010.07.19 ver 1.10 Rob Jansen - MyVoice CAD/CAM Services:
11 * Major cleaning and a rewrite of some functions
12 * - adding ACK/NACK handling to the state machine
13 * - adding a return result to the I2CEngine()
14 * 2011.02.19 ver 1.20 KTownsend - microBuilder.eu
15 * - Added slave mode status values to
18 *****************************************************************************/
21 volatile uint32_t I2CMasterState
= I2CSTATE_IDLE
;
22 volatile uint32_t I2CSlaveState
= I2CSTATE_IDLE
;
24 volatile uint8_t I2CMasterBuffer
[I2C_BUFSIZE
]; // Master Mode
25 volatile uint8_t I2CSlaveBuffer
[I2C_BUFSIZE
]; // Master Mode
27 //volatile uint8_t I2CWrBuffer[I2C_BUFSIZE]; // Slave Mode
28 //volatile uint8_t I2CRdBuffer[I2C_BUFSIZE]; // Slave Mode
30 volatile uint32_t I2CReadLength
;
31 volatile uint32_t I2CWriteLength
;
33 volatile uint32_t RdIndex
;
34 volatile uint32_t WrIndex
;
36 volatile uint32_t _I2cMode
; // I2CMASTER or I2CSLAVE
38 /*****************************************************************************
39 ** Function name: I2C_IRQHandler
41 ** Descriptions: I2C interrupt handler, deal with master mode only.
44 ** Returned value: None
46 *****************************************************************************/
47 void I2C_IRQHandler(void)
51 /* this handler deals with master read and master write only */
52 StatValue
= I2C_I2CSTAT
;
57 * A START condition has been transmitted.
58 * We now send the slave address and initialize
60 * (we always start with a write after START+SLA)
63 I2C_I2CDAT
= I2CMasterBuffer
[WrIndex
++];
64 I2C_I2CCONCLR
= (I2CONCLR_SIC
| I2CONCLR_STAC
);
65 I2CMasterState
= I2CSTATE_PENDING
;
70 * A repeated START condition has been transmitted.
71 * Now a second, read, transaction follows so we
72 * initialize the read buffer.
75 /* Send SLA with R bit set, */
76 I2C_I2CDAT
= I2CMasterBuffer
[WrIndex
++];
77 I2C_I2CCONCLR
= (I2CONCLR_SIC
| I2CONCLR_STAC
);
82 * SLA+W has been transmitted; ACK has been received.
83 * We now start writing bytes.
85 I2C_I2CDAT
= I2CMasterBuffer
[WrIndex
++];
86 I2C_I2CCONCLR
= I2CONCLR_SIC
;
91 * SLA+W has been transmitted; NOT ACK has been received.
92 * Send a stop condition to terminate the transaction
93 * and signal I2CEngine the transaction is aborted.
95 I2C_I2CCONSET
= I2CONSET_STO
;
96 I2C_I2CCONCLR
= I2CONCLR_SIC
;
97 I2CMasterState
= I2CSTATE_SLA_NACK
;
102 * Data in I2DAT has been transmitted; ACK has been received.
103 * Continue sending more bytes as long as there are bytes to send
104 * and after this check if a read transaction should follow.
106 if ( WrIndex
< I2CWriteLength
)
108 /* Keep writing as long as bytes avail */
109 I2C_I2CDAT
= I2CMasterBuffer
[WrIndex
++];
113 if ( I2CReadLength
!= 0 )
115 /* Send a Repeated START to initialize a read transaction */
116 /* (handled in state 0x10) */
117 I2C_I2CCONSET
= I2CONSET_STA
; /* Set Repeated-start flag */
121 I2CMasterState
= I2CSTATE_ACK
;
122 I2C_I2CCONSET
= I2CONSET_STO
; /* Set Stop flag */
125 I2C_I2CCONCLR
= I2CONCLR_SIC
;
130 * Data byte in I2DAT has been transmitted; NOT ACK has been received
131 * Send a STOP condition to terminate the transaction and inform the
132 * I2CEngine that the transaction failed.
134 I2C_I2CCONSET
= I2CONSET_STO
;
135 I2C_I2CCONCLR
= I2CONCLR_SIC
;
136 I2CMasterState
= I2CSTATE_NACK
;
141 * Arbitration loss in SLA+R/W or Data bytes.
142 * This is a fatal condition, the transaction did not complete due
143 * to external reasons (e.g. hardware system failure).
144 * Inform the I2CEngine of this and cancel the transaction
145 * (this is automatically done by the I2C hardware)
147 I2CMasterState
= I2CSTATE_ARB_LOSS
;
148 I2C_I2CCONCLR
= I2CONCLR_SIC
;
153 * SLA+R has been transmitted; ACK has been received.
155 * Since a NOT ACK is sent after reading the last byte,
156 * we need to prepare a NOT ACK in case we only read 1 byte.
158 if ( I2CReadLength
== 1 )
160 /* last (and only) byte: send a NACK after data is received */
161 I2C_I2CCONCLR
= I2CONCLR_AAC
;
165 /* more bytes to follow: send an ACK after data is received */
166 I2C_I2CCONSET
= I2CONSET_AA
;
168 I2C_I2CCONCLR
= I2CONCLR_SIC
;
173 * SLA+R has been transmitted; NOT ACK has been received.
174 * Send a stop condition to terminate the transaction
175 * and signal I2CEngine the transaction is aborted.
177 I2C_I2CCONSET
= I2CONSET_STO
;
178 I2C_I2CCONCLR
= I2CONCLR_SIC
;
179 I2CMasterState
= I2CSTATE_SLA_NACK
;
184 * Data byte has been received; ACK has been returned.
185 * Read the byte and check for more bytes to read.
186 * Send a NOT ACK after the last byte is received
188 I2CSlaveBuffer
[RdIndex
++] = I2C_I2CDAT
;
189 if ( RdIndex
< (I2CReadLength
-1) )
191 /* lmore bytes to follow: send an ACK after data is received */
192 I2C_I2CCONSET
= I2CONSET_AA
;
196 /* last byte: send a NACK after data is received */
197 I2C_I2CCONCLR
= I2CONCLR_AAC
;
199 I2C_I2CCONCLR
= I2CONCLR_SIC
;
204 * Data byte has been received; NOT ACK has been returned.
205 * This is the last byte to read.
206 * Generate a STOP condition and flag the I2CEngine that the
207 * transaction is finished.
209 I2CSlaveBuffer
[RdIndex
++] = I2C_I2CDAT
;
210 I2CMasterState
= I2CSTATE_ACK
;
211 I2C_I2CCONSET
= I2CONSET_STO
; /* Set Stop flag */
212 I2C_I2CCONCLR
= I2CONCLR_SIC
; /* Clear SI flag */
218 // case 0x60: /* An own SLA_W has been received. */
221 // I2C_I2CCONSET = I2CONSET_AA; /* assert ACK after SLV_W is received */
222 // I2C_I2CCONCLR = I2CONCLR_SIC;
223 // I2CSlaveState = I2C_WR_STARTED;
226 // case 0x80: /* data receive */
228 // if ( I2CSlaveState == I2C_WR_STARTED )
230 // I2CRdBuffer[RdIndex++] = I2C_I2CDAT;
231 // I2C_I2CCONSET = I2CONSET_AA; /* assert ACK after data is received */
235 // I2C_I2CCONCLR = I2CONCLR_AAC; /* assert NACK */
237 // // ToDo: Set a flag to indicate data is ready and process it somewhere
238 // I2C_I2CCONCLR = I2CONCLR_SIC;
241 // case 0xA8: /* An own SLA_R has been received. */
244 // WrIndex = I2CRdBuffer[0]; /* The 1st byte is the index. */
245 // I2C_I2CDAT = I2CRdBuffer[WrIndex+1]; /* write the same data back to master */
246 // WrIndex++; /* Need to skip the index byte in RdBuffer */
247 // I2C_I2CCONSET = I2CONSET_AA; /* assert ACK after SLV_R is received */
248 // I2C_I2CCONCLR = I2CONCLR_SIC;
249 // I2CSlaveState = I2C_RD_STARTED;
252 // case 0xB8: /* Data byte has been transmitted */
254 // if ( I2CSlaveState == I2C_RD_STARTED )
256 // I2C_I2CDAT = I2CRdBuffer[WrIndex+1]; /* write the same data back to master */
257 // WrIndex++; /* Need to skip the index byte in RdBuffer */
258 // I2C_I2CCONSET = I2CONSET_AA; /* assert ACK */
262 // I2C_I2CCONCLR = I2CONCLR_AAC; /* assert NACK */
264 // I2C_I2CCONCLR = I2CONCLR_SIC;
267 // case 0xC0: /* Data byte has been transmitted, NACK */
268 // I2C_I2CCONCLR = I2CONCLR_AAC; /* assert NACK */
269 // I2C_I2CCONCLR = I2CONCLR_SIC;
270 // I2CSlaveState = DATA_NACK;
273 // case 0xA0: /* Stop condition or repeated start has */
274 // I2C_I2CCONSET = I2CONSET_AA; /* been received, assert ACK. */
275 // I2C_I2CCONCLR = I2CONCLR_SIC;
276 // I2CSlaveState = I2C_IDLE;
280 I2C_I2CCONCLR
= I2CONCLR_SIC
;
281 // if (_I2cMode = I2CSLAVE)
283 // I2C_I2CCONSET = I2CONSET_I2EN | I2CONSET_SI;
290 /*****************************************************************************
291 ** Function name: I2CStart
293 ** Descriptions: Create I2C start condition, a timeout
294 ** value is set if the I2C never gets started,
295 ** and timed out. It's a fatal error.
298 ** Returned value: true or false, return false if timed out
300 *****************************************************************************/
301 static uint32_t I2CStart( void )
303 uint32_t timeout
= 0;
305 /*--- Issue a start condition ---*/
306 I2C_I2CCONSET
= I2CONSET_STA
; /* Set Start flag */
308 while((I2CMasterState
!= I2CSTATE_PENDING
) && (timeout
< MAX_TIMEOUT
))
313 return (timeout
< MAX_TIMEOUT
);
316 /*****************************************************************************
317 ** Function name: I2CStop
319 ** Descriptions: Set the I2C stop condition
322 ** Returned value: true or never return
324 *****************************************************************************/
325 static uint32_t I2CStop( void )
327 uint32_t timeout
= 0;
329 I2C_I2CCONSET
= I2CONSET_STO
; /* Set Stop flag */
330 I2C_I2CCONCLR
= I2CONCLR_SIC
; /* Clear SI flag */
332 /*--- Wait for STOP detected ---*/
333 while((I2C_I2CCONSET
& I2CONSET_STO
) && (timeout
< MAX_TIMEOUT
))
337 return (timeout
>= MAX_TIMEOUT
);
340 /*****************************************************************************
341 ** Function name: I2CInit
343 ** Descriptions: Initialize I2C controller
345 ** parameters: I2c mode is either MASTER or SLAVE
346 ** Returned value: true or false, return false if the I2C
347 ** interrupt handler was not installed correctly
349 *****************************************************************************/
350 uint32_t i2cInit( uint32_t I2cMode
)
354 SCB_PRESETCTRL
|= (0x1<<1);
357 SCB_SYSAHBCLKCTRL
|= (SCB_SYSAHBCLKCTRL_I2C
);
359 // Configure pin 0.4 for SCL
360 IOCON_PIO0_4
&= ~(IOCON_PIO0_4_FUNC_MASK
| IOCON_PIO0_4_I2CMODE_MASK
);
361 IOCON_PIO0_4
|= (IOCON_PIO0_4_FUNC_I2CSCL
);
363 // Configure pin 0.5 for SDA
364 IOCON_PIO0_5
&= ~(IOCON_PIO0_5_FUNC_MASK
| IOCON_PIO0_5_I2CMODE_MASK
);
365 IOCON_PIO0_5
|= IOCON_PIO0_5_FUNC_I2CSDA
;
368 I2C_I2CCONCLR
= I2C_I2CCONCLR_AAC
|
373 // See p.128 for appropriate values for SCLL and SCLH
374 #if I2C_FAST_MODE_PLUS
375 IOCON_PIO0_4
|= (IOCON_PIO0_4_I2CMODE_FASTPLUSI2C
);
376 IOCON_PIO0_5
|= (IOCON_PIO0_5_I2CMODE_FASTPLUSI2C
);
377 I2C_I2CSCLL
= I2C_SCLL_HS_SCLL
;
378 I2C_I2CSCLH
= I2C_SCLH_HS_SCLH
;
380 I2C_I2CSCLL
= I2SCLL_SCLL
;
381 I2C_I2CSCLH
= I2SCLH_SCLH
;
384 if ( _I2cMode
== I2CSLAVE
)
386 I2C_I2CADR0
= SLAVE_ADDR
;
387 I2CSlaveState
= I2C_IDLE
;
390 /* Enable the I2C Interrupt */
391 NVIC_EnableIRQ(I2C_IRQn
);
392 if ( _I2cMode
== I2CMASTER
)
394 I2C_I2CCONSET
= I2C_I2CCONSET_I2EN
;
398 // I2C_I2CCONSET = I2C_I2CCONSET_I2EN | I2C_I2CCONSET_SI;
404 /*****************************************************************************
405 ** Function name: I2CEngine
407 ** Descriptions: The routine to complete a I2C transaction
408 ** from start to stop. All the intermitten
409 ** steps are handled in the interrupt handler.
410 ** Before this routine is called, the read
411 ** length, write length and I2C master buffer
412 ** need to be filled.
415 ** Returned value: Any of the I2CSTATE_... values. See i2c.h
417 *****************************************************************************/
418 uint32_t i2cEngine( void )
420 I2CMasterState
= I2CSTATE_IDLE
;
423 if ( I2CStart() != TRUE
)
429 /* wait until the state is a terminal state */
430 while (I2CMasterState
< 0x100);
432 return ( I2CMasterState
);
435 /******************************************************************************
437 ******************************************************************************/