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