10 // Default timeout, in milliseconds for generic read/write
12 #define I2C_DEFAULT_TIMEOUT 100
28 static volatile int i2cBusInUse;
30 static U8 *i2cDataBuffer;
31 static int i2cDataLenWrite;
32 static int i2cDataLenRead;
33 static unsigned int i2cTimeoutInTicks = I2C_DEFAULT_TIMEOUT / portTICK_RATE_MS;
34 static int i2cDataCnt;
35 static ackPoll_e i2cAckPoll = ACKPOLL_NO;
41 static U8 i2cTransactions [64];
42 static int i2cTransactionsIndex;
43 #define i2cDebug(x) do { if (i2cAckPoll && (i2cTransactionsIndex < (int) sizeof (i2cTransactions))) i2cTransactions [i2cTransactionsIndex++] = x; } while (0)
44 #define i2cDebugReset() do { i2cTransactionsIndex = 0; } while (0)
46 #define i2cDebug(x) do { } while (0)
47 #define i2cDebugReset() do { } while (0)
50 static void i2cISR (void) __attribute__ ((interrupt ("IRQ")));
51 static void i2cISR (void)
53 i2cErrno = (I2C0_STAT & I2C_STAT_STATMASK);
59 // Transmit conditions
61 case I2CERR_BUSERRORx : // 0x00
63 I2C0_CONSET = I2C_CONSET_STO | I2C_CONSET_AA;
64 i2cAckPoll = ACKPOLL_NO;
69 case I2CERR_STARTTX : // 0x08
70 case I2CERR_REPEATEDSTARTTX : // 0x10
72 i2cDebug (i2cAddress);
73 I2C0_DAT = i2cAddress;
77 case I2CERR_SLAWTX_ACKRX : // 0x18
78 case I2CERR_DATTX_ACKRX : // 0x28
80 i2cAckPoll = ACKPOLL_NO;
82 if (i2cDataLenWrite && (i2cDataCnt < i2cDataLenWrite))
84 i2cDebug (i2cDataBuffer [i2cDataCnt]);
85 I2C0_DAT = i2cDataBuffer [i2cDataCnt++];
86 I2C0_CONCLR = I2C_CONCLR_STAC;
93 I2C0_CONCLR = I2C_CONCLR_STAC;
94 I2C0_CONSET = I2C_CONSET_STO;
95 i2cErrno = I2CERR_NONE;
103 I2C0_CONSET = I2C_CONSET_STA;
109 case I2CERR_DATRX_NACKTX : // 0x58
111 i2cDataBuffer [i2cDataCnt++] = I2C0_DAT;
112 i2cDebug (i2cDataBuffer [i2cDataCnt - 1]);
113 i2cErrno = I2CERR_NONE;
116 case I2CERR_SLAWTX_NACKRX : // 0x20
117 case I2CERR_SLARTX_NACKRX : // 0x48
123 case I2CERR_DATTX_NACKRX : // 0x30
125 I2C0_CONCLR = I2C_CONCLR_STAC;
126 I2C0_CONSET = I2C_CONSET_STO;
131 case I2CERR_ARBLOST : // 0x38
133 I2C0_CONSET = I2C_CONSET_STA;
138 // Receive byte conditions (fall through is intentional)
140 case I2CERR_DATRX_ACKTX : // 0x50
142 i2cDataBuffer [i2cDataCnt++] = I2C0_DAT;
143 i2cDebug (i2cDataBuffer [i2cDataCnt - 1]);
146 case I2CERR_SLARTX_ACKRX : // 0x40
148 if (i2cDataCnt < i2cDataLenRead - 1)
150 I2C0_CONCLR = I2C_CONCLR_STAC;
151 I2C0_CONSET = I2C_CONSET_AA;
154 I2C0_CONCLR = I2C_CONCLR_STAC | I2C_CONCLR_AAC;
163 I2C0_CONCLR = I2C_CONCLR_I2ENC;
164 i2cAckPoll = ACKPOLL_NO;
170 I2C0_CONCLR = I2C_CONSET_SI;
180 i2cAckPoll = ACKPOLL_NO;
182 SCB_PCONP |= SCB_PCONP_PCI2C0;
184 PCB_PINSEL0 = (PCB_PINSEL0 & ~(PCB_PINSEL0_P02_MASK | PCB_PINSEL0_P03_MASK)) | (PCB_PINSEL0_P02_SCL0 | PCB_PINSEL0_P03_SDA0);
186 I2C0_CONCLR = I2C_CONCLR_MASK;
187 I2C0_CONSET = I2C_CONSET_I2EN;
189 // the Microchip 24FC1025 can handle a 1 Mhz clock
190 // PCLK / (i2c_clock * 2), with 48 Mhz PCLK that's 24...
191 // for AltusMetrum, 14.7456 Mhz xtal, it's 29.4912, use 30.
200 // Initialize the interrupt vector
202 VIC_IntSelect &= ~VIC_IntSelect_I2C0;
203 VIC_VectCntl7 = VIC_VectCntl_ENABLE | VIC_Channel_I2C0;
204 VIC_VectAddr7 = (int) i2cISR;
205 VIC_IntEnable = VIC_IntEnable_I2C0;
211 static int i2cWaitComplete (int ticks)
217 for (theFuture = times (NULL) + ticks; i2cBusInUse; )
219 if (times (NULL) > theFuture)
221 I2C0_CONCLR = I2C_CONCLR_I2ENC;
222 i2cErrno = I2CERR_TIMEOUTWC;
228 return (i2cErrno == I2CERR_NONE) ? 0 : -1;
231 static int i2cPoll (U8 address)
233 clock_t theFuture = times (NULL) + 10;
235 VIC_IntEnClr = VIC_IntEnClr_I2C0;
237 while (times (NULL) < theFuture)
239 I2C0_CONCLR = I2C_CONSET_SI;
240 I2C0_CONSET = I2C_CONSET_STA;
242 while (!(I2C0_CONSET & I2C_CONSET_SI))
245 I2C0_CONCLR = I2C_CONCLR_STAC | I2C_CONCLR_SIC;
246 I2C0_DAT = address & ~0x01;
248 while (!(I2C0_CONSET & I2C_CONSET_SI))
251 if ((i2cErrno = I2C0_STAT) == I2CERR_SLAWTX_ACKRX)
255 if (i2cErrno != I2CERR_SLAWTX_ACKRX)
256 i2cErrno = I2CERR_TIMEOUTACKPOLL;
258 I2C0_CONCLR = I2C_CONCLR_SIC;
259 I2C0_CONSET = I2C_CONSET_STO | I2C_CONSET_AA;
261 while (I2C0_CONSET & I2C_CONSET_STO)
264 VIC_IntEnable = VIC_IntEnable_I2C0;
266 return (i2cErrno == I2CERR_SLAWTX_ACKRX) ? 0 : -1;
272 static int i2cKickOff (U8 address, U8 *buffer, int bufferLenWrite, int bufferLenRead, ackPoll_e ackPoll)
275 // Determine if our first operation will be a write or read. If both, the
276 // write always occurs first.
280 else if (bufferLenRead)
286 // Wait until last I2C operation has finished.
288 if (i2cBusInUse && i2cWaitComplete (i2cTimeoutInTicks))
290 i2cErrno = I2CERR_TIMEOUT;
295 // Mark bus as in use, save the address, buffer and length
298 i2cAddress = address;
299 i2cDataBuffer = buffer;
300 i2cDataLenWrite = bufferLenWrite;
301 i2cDataLenRead = bufferLenRead;
303 i2cAckPoll = ackPoll;
307 // Clear any outstanding bits, enable the interface, and send a start bit.
308 // Once the start bit goes out, the interrupt drive state machine begins.
310 I2C0_CONCLR = I2C_CONCLR_MASK;
311 I2C0_CONSET = I2C_CONSET_I2EN;
312 I2C0_CONSET = I2C_CONSET_STA;
320 static int i2cWriteBufferEx (U8 address, U8 *buffer, U32 bufferLength, int milliseconds)
324 if (!(r = i2cKickOff (address, buffer, bufferLength, 0, ACKPOLL_NO)))
325 r = i2cWaitComplete (milliseconds / portTICK_RATE_MS);
330 static int i2cReadBufferEx (U8 address, U8 *buffer, U32 bufferLength, int milliseconds)
334 if (!(r = i2cKickOff (address, buffer, 0, bufferLength, ACKPOLL_NO)))
335 r = i2cWaitComplete (milliseconds / portTICK_RATE_MS);
340 static int i2cWriteReadBufferEx (U8 address, U8 *buffer, U32 putLength, U32 getLength, int milliseconds)
344 if (!(r = i2cKickOff (address, buffer, putLength, getLength, ACKPOLL_NO)))
345 r = i2cWaitComplete (milliseconds / portTICK_RATE_MS);
353 void i2cSetTimeout (unsigned int timeoutInMilliseconds)
355 if (timeoutInMilliseconds < portTICK_RATE_MS)
356 timeoutInMilliseconds = portTICK_RATE_MS;
358 i2cTimeoutInTicks = timeoutInMilliseconds / portTICK_RATE_MS;
366 for (i = 0; i < i2cTransactionsIndex; i++)
367 printf ("0x%02x ", i2cTransactions [i]);
371 printf ("Not compiled for i2c debugging\n");
376 // DANGER, WILL ROBINSON! The callers buffer must persist until we're done
378 int i2cWriteBuffer (U8 address, U8 *buffer, U32 bufferLength)
380 return i2cWriteBufferEx (address, buffer, bufferLength, i2cTimeoutInTicks);
383 int i2cReadBuffer (U8 address, U8 *buffer, U32 bufferLength)
385 return i2cReadBufferEx (address, buffer, bufferLength, i2cTimeoutInTicks);
388 int i2cWriteReadBuffer (U8 address, U8 *buffer, U32 putLength, U32 getLength)
390 return i2cWriteReadBufferEx (address, buffer, putLength, getLength, i2cTimeoutInTicks);
393 int i2cWriteBufferPoll (U8 address, U8 *buffer, U32 bufferLength)
397 if (!(r = i2cKickOff (address, buffer, bufferLength, 0, ACKPOLL_NO)))
398 if (!(r = i2cWaitComplete (i2cTimeoutInTicks)))
399 r = i2cPoll (address);
404 int i2cWriteReadBufferPoll (U8 address, U8 *buffer, U32 putLength, U32 getLength)
408 if (!(r = i2cKickOff (address, buffer, putLength, getLength, ACKPOLL_NO)))
409 if (!(r = i2cWaitComplete (i2cTimeoutInTicks)))
410 r = i2cPoll (address);