00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <math.h>
00021 #include <stdlib.h>
00022 #include <inttypes.h>
00023 #include <avr/io.h>
00024 #include <avr/interrupt.h>
00025 #include <compat/twi.h>
00026
00027 #ifndef cbi
00028 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
00029 #endif
00030
00031 #ifndef sbi
00032 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
00033 #endif
00034
00035 #include "twi.h"
00036
00037 static volatile uint8_t twi_state;
00038 static uint8_t twi_slarw;
00039
00040 static void (*twi_onSlaveTransmit)(void);
00041 static void (*twi_onSlaveReceive)(uint8_t*, int);
00042
00043 static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
00044 static volatile uint8_t twi_masterBufferIndex;
00045 static uint8_t twi_masterBufferLength;
00046
00047 static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH];
00048 static volatile uint8_t twi_txBufferIndex;
00049 static volatile uint8_t twi_txBufferLength;
00050
00051 static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH];
00052 static volatile uint8_t twi_rxBufferIndex;
00053
00054 static volatile uint8_t twi_error;
00055
00056
00057
00058
00059
00060
00061
00062 void twi_init(void)
00063 {
00064
00065 twi_state = TWI_READY;
00066
00067 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
00068
00069
00070 sbi(PORTC, 4);
00071 sbi(PORTC, 5);
00072 #else
00073
00074
00075 sbi(PORTD, 0);
00076 sbi(PORTD, 1);
00077 #endif
00078
00079
00080 cbi(TWSR, TWPS0);
00081 cbi(TWSR, TWPS1);
00082 TWBR = ((CPU_FREQ / TWI_FREQ) - 16) / 2;
00083
00084
00085
00086
00087
00088
00089
00090 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);
00091 }
00092
00093
00094
00095
00096
00097
00098
00099 void twi_setAddress(uint8_t address)
00100 {
00101
00102 TWAR = address << 1;
00103 }
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length)
00115 {
00116 uint8_t i;
00117
00118
00119 if(TWI_BUFFER_LENGTH < length){
00120 return 0;
00121 }
00122
00123
00124 while(TWI_READY != twi_state){
00125 continue;
00126 }
00127 twi_state = TWI_MRX;
00128
00129 twi_error = 0xFF;
00130
00131
00132 twi_masterBufferIndex = 0;
00133 twi_masterBufferLength = length-1;
00134
00135
00136
00137
00138
00139
00140
00141 twi_slarw = TW_READ;
00142 twi_slarw |= address << 1;
00143
00144
00145 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
00146
00147
00148 while(TWI_MRX == twi_state){
00149 continue;
00150 }
00151
00152 if (twi_masterBufferIndex < length)
00153 length = twi_masterBufferIndex;
00154
00155
00156 for(i = 0; i < length; ++i){
00157 data[i] = twi_masterBuffer[i];
00158 }
00159
00160 return length;
00161 }
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait)
00178 {
00179 uint8_t i;
00180
00181
00182 if(TWI_BUFFER_LENGTH < length){
00183 return 1;
00184 }
00185
00186
00187 while(TWI_READY != twi_state){
00188 continue;
00189 }
00190 twi_state = TWI_MTX;
00191
00192 twi_error = 0xFF;
00193
00194
00195 twi_masterBufferIndex = 0;
00196 twi_masterBufferLength = length;
00197
00198
00199 for(i = 0; i < length; ++i){
00200 twi_masterBuffer[i] = data[i];
00201 }
00202
00203
00204 twi_slarw = TW_WRITE;
00205 twi_slarw |= address << 1;
00206
00207
00208 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
00209
00210
00211 while(wait && (TWI_MTX == twi_state)){
00212 continue;
00213 }
00214
00215 if (twi_error == 0xFF)
00216 return 0;
00217 else if (twi_error == TW_MT_SLA_NACK)
00218 return 2;
00219 else if (twi_error == TW_MT_DATA_NACK)
00220 return 3;
00221 else
00222 return 4;
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 uint8_t twi_transmit(uint8_t* data, uint8_t length)
00236 {
00237 uint8_t i;
00238
00239
00240 if(TWI_BUFFER_LENGTH < length){
00241 return 1;
00242 }
00243
00244
00245 if(TWI_STX != twi_state){
00246 return 2;
00247 }
00248
00249
00250 twi_txBufferLength = length;
00251 for(i = 0; i < length; ++i){
00252 twi_txBuffer[i] = data[i];
00253 }
00254
00255 return 0;
00256 }
00257
00258
00259
00260
00261
00262
00263
00264 void twi_attachSlaveRxEvent( void (*function)(uint8_t*, int) )
00265 {
00266 twi_onSlaveReceive = function;
00267 }
00268
00269
00270
00271
00272
00273
00274
00275 void twi_attachSlaveTxEvent( void (*function)(void) )
00276 {
00277 twi_onSlaveTransmit = function;
00278 }
00279
00280
00281
00282
00283
00284
00285
00286 void twi_reply(uint8_t ack)
00287 {
00288
00289 if(ack){
00290 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA);
00291 }else{
00292 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
00293 }
00294 }
00295
00296
00297
00298
00299
00300
00301
00302 void twi_stop(void)
00303 {
00304
00305 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO);
00306
00307
00308
00309 while(TWCR & _BV(TWSTO)){
00310 continue;
00311 }
00312
00313
00314 twi_state = TWI_READY;
00315 }
00316
00317
00318
00319
00320
00321
00322
00323 void twi_releaseBus(void)
00324 {
00325
00326 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT);
00327
00328
00329 twi_state = TWI_READY;
00330 }
00331
00332 SIGNAL(TWI_vect)
00333 {
00334 switch(TW_STATUS){
00335
00336 case TW_START:
00337 case TW_REP_START:
00338
00339 TWDR = twi_slarw;
00340 twi_reply(1);
00341 break;
00342
00343
00344 case TW_MT_SLA_ACK:
00345 case TW_MT_DATA_ACK:
00346
00347 if(twi_masterBufferIndex < twi_masterBufferLength){
00348
00349 TWDR = twi_masterBuffer[twi_masterBufferIndex++];
00350 twi_reply(1);
00351 }else{
00352 twi_stop();
00353 }
00354 break;
00355 case TW_MT_SLA_NACK:
00356 twi_error = TW_MT_SLA_NACK;
00357 twi_stop();
00358 break;
00359 case TW_MT_DATA_NACK:
00360 twi_error = TW_MT_DATA_NACK;
00361 twi_stop();
00362 break;
00363 case TW_MT_ARB_LOST:
00364 twi_error = TW_MT_ARB_LOST;
00365 twi_releaseBus();
00366 break;
00367
00368
00369 case TW_MR_DATA_ACK:
00370
00371 twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
00372 case TW_MR_SLA_ACK:
00373
00374 if(twi_masterBufferIndex < twi_masterBufferLength){
00375 twi_reply(1);
00376 }else{
00377 twi_reply(0);
00378 }
00379 break;
00380 case TW_MR_DATA_NACK:
00381
00382 twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
00383 case TW_MR_SLA_NACK:
00384 twi_stop();
00385 break;
00386
00387
00388
00389 case TW_SR_SLA_ACK:
00390 case TW_SR_GCALL_ACK:
00391 case TW_SR_ARB_LOST_SLA_ACK:
00392 case TW_SR_ARB_LOST_GCALL_ACK:
00393
00394 twi_state = TWI_SRX;
00395
00396 twi_rxBufferIndex = 0;
00397 twi_reply(1);
00398 break;
00399 case TW_SR_DATA_ACK:
00400 case TW_SR_GCALL_DATA_ACK:
00401
00402 if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
00403
00404 twi_rxBuffer[twi_rxBufferIndex++] = TWDR;
00405 twi_reply(1);
00406 }else{
00407
00408 twi_reply(0);
00409 }
00410 break;
00411 case TW_SR_STOP:
00412
00413 if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
00414 twi_rxBuffer[twi_rxBufferIndex] = '\0';
00415 }
00416
00417 twi_stop();
00418
00419 twi_onSlaveReceive(twi_rxBuffer, twi_rxBufferIndex);
00420
00421 twi_rxBufferIndex = 0;
00422
00423 twi_releaseBus();
00424 break;
00425 case TW_SR_DATA_NACK:
00426 case TW_SR_GCALL_DATA_NACK:
00427
00428 twi_reply(0);
00429 break;
00430
00431
00432 case TW_ST_SLA_ACK:
00433 case TW_ST_ARB_LOST_SLA_ACK:
00434
00435 twi_state = TWI_STX;
00436
00437 twi_txBufferIndex = 0;
00438
00439 twi_txBufferLength = 0;
00440
00441
00442 twi_onSlaveTransmit();
00443
00444 if(0 == twi_txBufferLength){
00445 twi_txBufferLength = 1;
00446 twi_txBuffer[0] = 0x00;
00447 }
00448
00449 case TW_ST_DATA_ACK:
00450
00451 TWDR = twi_txBuffer[twi_txBufferIndex++];
00452
00453 if(twi_txBufferIndex < twi_txBufferLength){
00454 twi_reply(1);
00455 }else{
00456 twi_reply(0);
00457 }
00458 break;
00459 case TW_ST_DATA_NACK:
00460 case TW_ST_LAST_DATA:
00461
00462 twi_reply(1);
00463
00464 twi_state = TWI_READY;
00465 break;
00466
00467
00468 case TW_NO_INFO:
00469 break;
00470 case TW_BUS_ERROR:
00471 twi_error = TW_BUS_ERROR;
00472 twi_stop();
00473 break;
00474 }
00475 }
00476