9 // Default timeout, in milliseconds for generic read/write
11 #define I2C_DEFAULT_TIMEOUT 100
18 I2CFLAGS_START = 0x0001,
19 I2CFLAGS_REPEATEDSTART = 0x0002,
20 I2CFLAGS_STOP = 0x0004,
21 I2CFLAGS_ADDRESS = 0x0008,
22 I2CFLAGS_WRITEDATA = 0x0010,
23 I2CFLAGS_READDATA = 0x0020,
38 static unsigned int i2cTimeoutInTicks = I2C_DEFAULT_TIMEOUT / portTICK_RATE_MS;
41 // Our PCLK is 48Mhz (12Mhz Xtal, PLL x 4, VBPDIV = /1), we want 100Khz SCLK
45 SCB_PCONP |= SCB_PCONP_PCI2C0;
47 PCB_PINSEL0 = (PCB_PINSEL0 & ~(PCB_PINSEL0_P02_MASK | PCB_PINSEL0_P03_MASK)) | (PCB_PINSEL0_P02_SCL0 | PCB_PINSEL0_P03_SDA0);
49 I2C0_CONCLR = I2C_CONCLR_MASK;
50 I2C0_CONSET = I2C_CONSET_I2EN;
58 static i2cErr_e i2cStatus (void)
62 while (!(I2C0_CONSET & I2C_CONSET_SI))
65 if ((status = I2C0_STAT) == I2CERR_BUSERRORx)
66 return I2CERR_BUSERROR;
74 static i2cErr_e i2cStop (void)
76 I2C0_CONCLR = I2C_CONCLR_SIC;
77 I2C0_CONSET = I2C_CONSET_STO;
79 while (I2C0_CONSET & I2C_CONSET_STO)
88 static i2cErr_e i2cStart (void)
90 I2C0_CONCLR = I2C_CONCLR_SIC;
91 I2C0_CONSET = I2C_CONSET_STA;
97 if (((status = i2cStatus ()) == I2CERR_STARTTX) || (status == I2CERR_REPEATEDSTARTTX))
99 I2C0_CONCLR = I2C_CONCLR_STAC;
102 else if (status != I2CERR_NOINFO)
104 I2C0_CONCLR = I2C_CONCLR_STAC;
108 I2C0_CONCLR = I2C_CONCLR_SIC;
115 static i2cErr_e i2cRepeatedStart (void)
117 while (!(I2C0_CONSET & I2C_CONSET_SI))
120 I2C0_CONCLR = I2C_CONCLR_SIC;
121 I2C0_CONSET = I2C_CONSET_STA;
127 if (((status = i2cStatus ()) == I2CERR_STARTTX) || (status == I2CERR_REPEATEDSTARTTX))
129 I2C0_CONCLR = I2C_CONCLR_STAC;
132 else if (status != I2CERR_NOINFO)
134 I2C0_CONCLR = I2C_CONCLR_STAC;
138 I2C0_CONCLR = I2C_CONCLR_SIC;
145 static i2cErr_e i2cPutByte (U8 data)
147 if (!(I2C0_CONSET & I2C_CONSET_SI))
151 I2C0_CONCLR = I2C_CONCLR_SIC;
159 static i2cErr_e i2cGetByte (i2cMode_e mode, U8 *pData)
164 I2C0_CONCLR = I2C_CONCLR_SIC;
165 I2C0_CONSET = I2C_CONSET_AA;
169 I2C0_CONCLR = (I2C_CONCLR_AAC | I2C_CONCLR_SIC);
174 if (!(I2C0_CONSET & I2C_CONSET_SI))
177 *pData = (U8) I2C0_DAT;
188 static i2cErr_e i2cWriteBufferEx (U8 address, U8 *buffer, U32 bufferLength, i2cFlags_e flags)
193 if (flags & I2CFLAGS_START)
195 if ((status = i2cStart ()) != I2CERR_NONE)
201 else if (flags & I2CFLAGS_REPEATEDSTART)
203 if ((status = i2cRepeatedStart ()) != I2CERR_NONE)
210 if (flags & I2CFLAGS_ADDRESS)
213 if (((status = i2cPutByte (address & ~0x01)) != I2CERR_NONE) && (status != I2CERR_BUSY))
215 while (status == I2CERR_BUSY);
218 if (flags & I2CFLAGS_WRITEDATA)
220 for (i = 0; i < bufferLength; i++, buffer++)
224 if (((status = i2cStatus ()) == I2CERR_SLAWTX_ACKRX) || (status == I2CERR_DATTX_ACKRX))
227 if (((status = i2cPutByte (*buffer)) != I2CERR_NONE) && (status != I2CERR_BUSY))
229 while (status == I2CERR_BUSY);
233 else if (status != I2CERR_NOINFO)
242 if (flags & I2CFLAGS_STOP)
246 if (((status = i2cStatus ()) == I2CERR_SLAWTX_ACKRX) || (status == I2CERR_DATTX_ACKRX))
251 else if (status != I2CERR_NOINFO)
265 static i2cErr_e i2cReadBufferEx (U8 address, U8 *buffer, U32 bufferLength, i2cFlags_e flags)
270 if (flags & I2CFLAGS_START)
272 if ((status = i2cStart ()) != I2CERR_NONE)
278 else if (flags & I2CFLAGS_REPEATEDSTART)
280 if ((status = i2cRepeatedStart ()) != I2CERR_NONE)
287 if (flags & I2CFLAGS_ADDRESS)
290 if (((status = i2cPutByte (address | 0x01)) != I2CERR_NONE) && (status != I2CERR_BUSY))
292 while (status == I2CERR_BUSY);
295 if (flags & I2CFLAGS_READDATA)
297 for (i = 0; i < bufferLength; i++, buffer++)
301 if (((status = i2cStatus ()) == I2CERR_SLARTX_ACKRX) || (status == I2CERR_SLARTX_NACKRX) || (status == I2CERR_DATRX_ACKTX))
303 i2cGetByte ((i != bufferLength - 1) ? I2CMODE_ACK : I2CMODE_NACK, NULL);
306 status = i2cGetByte (I2CMODE_READ, buffer);
307 while (status == I2CERR_EMPTY);
311 else if (status != I2CERR_NOINFO)
320 if (flags & I2CFLAGS_STOP)
329 static int i2cPoll (U8 address)
331 clock_t theFuture = times (NULL) + i2cTimeoutInTicks;
333 while (times (NULL) < theFuture)
335 if ((i2cErrno = i2cStart ()) != I2CERR_NONE)
337 if ((i2cErrno = i2cPutByte (address & ~0x01)) != I2CERR_NONE)
339 if ((i2cErrno = i2cStatus ()) == I2CERR_SLAWTX_ACKRX)
343 if (i2cErrno != I2CERR_SLAWTX_ACKRX)
344 i2cErrno = I2CERR_TIMEOUTACKPOLL;
348 return (i2cErrno == I2CERR_SLAWTX_ACKRX) ? 0 : -1;
354 void i2cSetTimeout (unsigned int timeoutInMilliseconds)
356 if (timeoutInMilliseconds < portTICK_RATE_MS)
357 timeoutInMilliseconds = portTICK_RATE_MS;
359 i2cTimeoutInTicks = timeoutInMilliseconds / portTICK_RATE_MS;
364 printf ("i2cDump not implemented for polled mode\n");
367 int i2cWriteBuffer (U8 address, U8 *buffer, U32 bufferLength)
369 if ((i2cErrno = i2cWriteBufferEx (address, buffer, bufferLength, I2CFLAGS_START | I2CFLAGS_ADDRESS | I2CFLAGS_WRITEDATA | I2CFLAGS_STOP)) != I2CERR_NONE)
375 int i2cReadBuffer (U8 address, U8 *buffer, U32 bufferLength)
377 if ((i2cErrno = i2cReadBufferEx (address, buffer, bufferLength, I2CFLAGS_START | I2CFLAGS_ADDRESS | I2CFLAGS_READDATA | I2CFLAGS_STOP)) != I2CERR_NONE)
383 int i2cWriteReadBuffer (U8 address, U8 *buffer, U32 putLength, U32 getLength)
385 if ((i2cErrno = i2cWriteBufferEx (address, buffer, putLength, I2CFLAGS_START | I2CFLAGS_ADDRESS | I2CFLAGS_WRITEDATA)) != I2CERR_NONE)
388 if ((i2cErrno = i2cReadBufferEx (address, buffer, getLength, I2CFLAGS_REPEATEDSTART | I2CFLAGS_ADDRESS | I2CFLAGS_READDATA | I2CFLAGS_STOP)) != I2CERR_NONE)
394 int i2cWriteBufferPoll (U8 address, U8 *buffer, U32 bufferLength)
398 if (!(r = i2cWriteBuffer (address, buffer, bufferLength)))
399 r = i2cPoll (address);
404 int i2cWriteReadBufferPoll (U8 address, U8 *buffer, U32 putLength, U32 getLength)
408 if (!(r = i2cWriteReadBuffer (address, buffer, putLength, getLength)))
409 r = i2cPoll (address);