00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #include <avr/io.h>
00053 #include <avr/signal.h>
00054 #include <avr/interrupt.h>
00055 #include <avr/twi.h>
00056
00057 #include "i2c.h"
00058
00059 #include "rprintf.h"
00060 #include "uart2.h"
00061
00062
00063
00064
00065
00066
00067
00068
00069 static volatile eI2cStateType I2cState;
00070 static u08 I2cDeviceAddrRW;
00071
00072 static u08 I2cSendData[I2C_SEND_DATA_BUFFER_SIZE];
00073 static u08 I2cSendDataIndex;
00074 static u08 I2cSendDataLength;
00075
00076 static u08 I2cReceiveData[I2C_RECEIVE_DATA_BUFFER_SIZE];
00077 static u08 I2cReceiveDataIndex;
00078 static u08 I2cReceiveDataLength;
00079
00080
00081
00082 // is addressed as a slave for writing
00083 static void (*i2cSlaveReceive)(u08 receiveDataLength, u08* recieveData);
00084
00085
00086 static u08 (*i2cSlaveTransmit)(u08 transmitDataLengthMax, u08* transmitData);
00087
00088
00089 void i2cInit(void)
00090 {
00091
00092 sbi(PORTC, 0);
00093 sbi(PORTC, 1);
00094 sbi(PORTD, 0);
00095 sbi(PORTD, 1);
00096
00097
00098 i2cSlaveReceive = 0;
00099 i2cSlaveTransmit = 0;
00100
00101 i2cSetBitrate(100);
00102
00103 sbi(TWCR, TWEN);
00104
00105 I2cState = I2C_IDLE;
00106
00107 sbi(TWCR, TWIE);
00108 sbi(TWCR, TWEA);
00109
00110
00111 sei();
00112 }
00113
00114 void i2cSetBitrate(u16 bitrateKHz)
00115 {
00116 u08 bitrate_div;
00117
00118
00119 #ifdef TWPS0
00120
00121
00122
00123 cbi(TWSR, TWPS0);
00124 cbi(TWSR, TWPS1);
00125 #endif
00126
00127 bitrate_div = ((F_CPU/1000l)/bitrateKHz);
00128 if(bitrate_div >= 16)
00129 bitrate_div = (bitrate_div-16)/2;
00130 outb(TWBR, bitrate_div);
00131
00132
00133
00134 outb(TWBR, 0xFF);
00135 }
00136
00137 void i2cSetLocalDeviceAddr(u08 deviceAddr, u08 genCallEn)
00138 {
00139
00140 outb(TWAR, ((deviceAddr&0xFE) | (genCallEn?1:0)) );
00141 }
00142
00143 void i2cSetSlaveReceiveHandler(void (*i2cSlaveRx_func)(u08 receiveDataLength, u08* recieveData))
00144 {
00145 i2cSlaveReceive = i2cSlaveRx_func;
00146 }
00147
00148 void i2cSetSlaveTransmitHandler(u08 (*i2cSlaveTx_func)(u08 transmitDataLengthMax, u08* transmitData))
00149 {
00150 i2cSlaveTransmit = i2cSlaveTx_func;
00151 }
00152
00153 inline void i2cSendStart(void)
00154 {
00155
00156 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWSTA));
00157 }
00158
00159 inline void i2cSendStop(void)
00160 {
00161
00162
00163 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA)|BV(TWSTO));
00164 }
00165
00166 inline void i2cWaitForComplete(void)
00167 {
00168
00169 while( !(inb(TWCR) & BV(TWINT)) );
00170 }
00171
00172 inline void i2cSendByte(u08 data)
00173 {
00174
00175 outb(TWDR, data);
00176
00177 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT));
00178 }
00179
00180 inline void i2cReceiveByte(u08 ackFlag)
00181 {
00182
00183 if( ackFlag )
00184 {
00185
00186 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));
00187 }
00188 else
00189 {
00190
00191 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT));
00192 }
00193 }
00194
00195 inline u08 i2cGetReceivedByte(void)
00196 {
00197
00198 return( inb(TWDR) );
00199 }
00200
00201 inline u08 i2cGetStatus(void)
00202 {
00203
00204 return( inb(TWSR) );
00205 }
00206
00207 void i2cMasterSend(u08 deviceAddr, u08 length, u08* data)
00208 {
00209 u08 i;
00210
00211 while(I2cState);
00212
00213 I2cState = I2C_MASTER_TX;
00214
00215 I2cDeviceAddrRW = (deviceAddr & 0xFE);
00216 for(i=0; i<length; i++)
00217 I2cSendData[i] = *data++;
00218 I2cSendDataIndex = 0;
00219 I2cSendDataLength = length;
00220
00221 i2cSendStart();
00222 }
00223
00224 void i2cMasterReceive(u08 deviceAddr, u08 length, u08* data)
00225 {
00226 u08 i;
00227
00228 while(I2cState);
00229
00230 I2cState = I2C_MASTER_RX;
00231
00232 I2cDeviceAddrRW = (deviceAddr|0x01);
00233 I2cReceiveDataIndex = 0;
00234 I2cReceiveDataLength = length;
00235
00236 i2cSendStart();
00237
00238 while(I2cState);
00239
00240 for(i=0; i<length; i++)
00241 *data++ = I2cReceiveData[i];
00242 }
00243
00244 u08 i2cMasterSendNI(u08 deviceAddr, u08 length, u08* data)
00245 {
00246 u08 retval = I2C_OK;
00247
00248
00249 cbi(TWCR, TWIE);
00250
00251
00252 i2cSendStart();
00253 i2cWaitForComplete();
00254
00255
00256 i2cSendByte( deviceAddr & 0xFE );
00257 i2cWaitForComplete();
00258
00259
00260 if( inb(TWSR) == TW_MT_SLA_ACK)
00261 {
00262
00263 while(length)
00264 {
00265 i2cSendByte( *data++ );
00266 i2cWaitForComplete();
00267 length--;
00268 }
00269 }
00270 else
00271 {
00272
00273
00274
00275 retval = I2C_ERROR_NODEV;
00276 }
00277
00278
00279
00280 i2cSendStop();
00281 while( !(inb(TWCR) & BV(TWSTO)) );
00282
00283
00284 sbi(TWCR, TWIE);
00285
00286 return retval;
00287 }
00288
00289 u08 i2cMasterReceiveNI(u08 deviceAddr, u08 length, u08 *data)
00290 {
00291 u08 retval = I2C_OK;
00292
00293
00294 cbi(TWCR, TWIE);
00295
00296
00297 i2cSendStart();
00298 i2cWaitForComplete();
00299
00300
00301 i2cSendByte( deviceAddr | 0x01 );
00302 i2cWaitForComplete();
00303
00304
00305 if( inb(TWSR) == TW_MR_SLA_ACK)
00306 {
00307
00308 while(length > 1)
00309 {
00310 i2cReceiveByte(TRUE);
00311 i2cWaitForComplete();
00312 *data++ = i2cGetReceivedByte();
00313
00314 length--;
00315 }
00316
00317
00318 i2cReceiveByte(FALSE);
00319 i2cWaitForComplete();
00320 *data++ = i2cGetReceivedByte();
00321 }
00322 else
00323 {
00324
00325
00326
00327 retval = I2C_ERROR_NODEV;
00328 }
00329
00330
00331
00332 i2cSendStop();
00333
00334
00335 sbi(TWCR, TWIE);
00336
00337 return retval;
00338 }
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403 SIGNAL(SIG_2WIRE_SERIAL)
00404 {
00405
00406 u08 status = inb(TWSR) & TWSR_STATUS_MASK;
00407
00408 switch(status)
00409 {
00410
00411 case TW_START:
00412 case TW_REP_START:
00413 #ifdef I2C_DEBUG
00414 rprintfInit(uart1AddToTxBuffer);
00415 rprintf("I2C: M->START\r\n");
00416 rprintfInit(uart1SendByte);
00417 #endif
00418
00419 i2cSendByte(I2cDeviceAddrRW);
00420 break;
00421
00422
00423 case TW_MT_SLA_ACK:
00424 case TW_MT_DATA_ACK:
00425 #ifdef I2C_DEBUG
00426 rprintfInit(uart1AddToTxBuffer);
00427 rprintf("I2C: MT->SLA_ACK or DATA_ACK\r\n");
00428 rprintfInit(uart1SendByte);
00429 #endif
00430 if(I2cSendDataIndex < I2cSendDataLength)
00431 {
00432
00433 i2cSendByte( I2cSendData[I2cSendDataIndex++] );
00434 }
00435 else
00436 {
00437
00438 i2cSendStop();
00439
00440 I2cState = I2C_IDLE;
00441 }
00442 break;
00443 case TW_MR_DATA_NACK:
00444 #ifdef I2C_DEBUG
00445 rprintfInit(uart1AddToTxBuffer);
00446 rprintf("I2C: MR->DATA_NACK\r\n");
00447 rprintfInit(uart1SendByte);
00448 #endif
00449
00450 I2cReceiveData[I2cReceiveDataIndex++] = inb(TWDR);
00451
00452 case TW_MR_SLA_NACK:
00453 case TW_MT_SLA_NACK:
00454 case TW_MT_DATA_NACK:
00455 #ifdef I2C_DEBUG
00456 rprintfInit(uart1AddToTxBuffer);
00457 rprintf("I2C: MTR->SLA_NACK or MT->DATA_NACK\r\n");
00458 rprintfInit(uart1SendByte);
00459 #endif
00460
00461 i2cSendStop();
00462
00463 I2cState = I2C_IDLE;
00464 break;
00465 case TW_MT_ARB_LOST:
00466
00467 #ifdef I2C_DEBUG
00468 rprintfInit(uart1AddToTxBuffer);
00469 rprintf("I2C: MT->ARB_LOST\r\n");
00470 rprintfInit(uart1SendByte);
00471 #endif
00472
00473 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT));
00474
00475 I2cState = I2C_IDLE;
00476
00477
00478 break;
00479 case TW_MR_DATA_ACK:
00480 #ifdef I2C_DEBUG
00481 rprintfInit(uart1AddToTxBuffer);
00482 rprintf("I2C: MR->DATA_ACK\r\n");
00483 rprintfInit(uart1SendByte);
00484 #endif
00485
00486 I2cReceiveData[I2cReceiveDataIndex++] = inb(TWDR);
00487
00488 case TW_MR_SLA_ACK:
00489 #ifdef I2C_DEBUG
00490 rprintfInit(uart1AddToTxBuffer);
00491 rprintf("I2C: MR->SLA_ACK\r\n");
00492 rprintfInit(uart1SendByte);
00493 #endif
00494 if(I2cReceiveDataIndex < (I2cReceiveDataLength-1))
00495
00496 i2cReceiveByte(TRUE);
00497 else
00498
00499 i2cReceiveByte(FALSE);
00500 break;
00501
00502
00503 case TW_SR_SLA_ACK:
00504 case TW_SR_ARB_LOST_SLA_ACK:
00505 case TW_SR_GCALL_ACK:
00506 case TW_SR_ARB_LOST_GCALL_ACK:
00507 #ifdef I2C_DEBUG
00508 rprintfInit(uart1AddToTxBuffer);
00509 rprintf("I2C: SR->SLA_ACK\r\n");
00510 rprintfInit(uart1SendByte);
00511 #endif
00512
00513
00514 I2cState = I2C_SLAVE_RX;
00515
00516 I2cReceiveDataIndex = 0;
00517
00518 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));
00519 break;
00520 case TW_SR_DATA_ACK:
00521 case TW_SR_GCALL_DATA_ACK:
00522 #ifdef I2C_DEBUG
00523 rprintfInit(uart1AddToTxBuffer);
00524 rprintf("I2C: SR->DATA_ACK\r\n");
00525 rprintfInit(uart1SendByte);
00526 #endif
00527
00528 I2cReceiveData[I2cReceiveDataIndex++] = inb(TWDR);
00529
00530 if(I2cReceiveDataIndex < I2C_RECEIVE_DATA_BUFFER_SIZE)
00531 {
00532
00533 i2cReceiveByte(TRUE);
00534
00535 }
00536 else
00537 {
00538
00539 i2cReceiveByte(FALSE);
00540
00541 }
00542 break;
00543 case TW_SR_DATA_NACK:
00544 case TW_SR_GCALL_DATA_NACK:
00545 #ifdef I2C_DEBUG
00546 rprintfInit(uart1AddToTxBuffer);
00547 rprintf("I2C: SR->DATA_NACK\r\n");
00548 rprintfInit(uart1SendByte);
00549 #endif
00550
00551 i2cReceiveByte(FALSE);
00552
00553 break;
00554 case TW_SR_STOP:
00555 #ifdef I2C_DEBUG
00556 rprintfInit(uart1AddToTxBuffer);
00557 rprintf("I2C: SR->SR_STOP\r\n");
00558 rprintfInit(uart1SendByte);
00559 #endif
00560
00561 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));
00562
00563 if(i2cSlaveReceive) i2cSlaveReceive(I2cReceiveDataIndex, I2cReceiveData);
00564
00565 I2cState = I2C_IDLE;
00566 break;
00567
00568
00569 case TW_ST_SLA_ACK:
00570 case TW_ST_ARB_LOST_SLA_ACK:
00571 #ifdef I2C_DEBUG
00572 rprintfInit(uart1AddToTxBuffer);
00573 rprintf("I2C: ST->SLA_ACK\r\n");
00574 rprintfInit(uart1SendByte);
00575 #endif
00576
00577
00578 I2cState = I2C_SLAVE_TX;
00579
00580 if(i2cSlaveTransmit) I2cSendDataLength = i2cSlaveTransmit(I2C_SEND_DATA_BUFFER_SIZE, I2cSendData);
00581
00582 I2cSendDataIndex = 0;
00583
00584 case TW_ST_DATA_ACK:
00585 #ifdef I2C_DEBUG
00586 rprintfInit(uart1AddToTxBuffer);
00587 rprintf("I2C: ST->DATA_ACK\r\n");
00588 rprintfInit(uart1SendByte);
00589 #endif
00590
00591 outb(TWDR, I2cSendData[I2cSendDataIndex++]);
00592 if(I2cSendDataIndex < I2cSendDataLength)
00593
00594 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));
00595 else
00596
00597 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT));
00598 break;
00599 case TW_ST_DATA_NACK:
00600 case TW_ST_LAST_DATA:
00601 #ifdef I2C_DEBUG
00602 rprintfInit(uart1AddToTxBuffer);
00603 rprintf("I2C: ST->DATA_NACK or LAST_DATA\r\n");
00604 rprintfInit(uart1SendByte);
00605 #endif
00606
00607
00608 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));
00609
00610 I2cState = I2C_IDLE;
00611 break;
00612
00613
00614 case TW_NO_INFO:
00615
00616 #ifdef I2C_DEBUG
00617 rprintfInit(uart1AddToTxBuffer);
00618 rprintf("I2C: NO_INFO\r\n");
00619 rprintfInit(uart1SendByte);
00620 #endif
00621 break;
00622 case TW_BUS_ERROR:
00623 #ifdef I2C_DEBUG
00624 rprintfInit(uart1AddToTxBuffer);
00625 rprintf("I2C: BUS_ERROR\r\n");
00626 rprintfInit(uart1SendByte);
00627 #endif
00628
00629 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWSTO)|BV(TWEA));
00630
00631 I2cState = I2C_IDLE;
00632 break;
00633 }
00634 }
00635
00636 eI2cStateType i2cGetState(void)
00637 {
00638 return I2cState;
00639 }