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