20 #include <avr/interrupt.h>
22 uint8_t twi_writeAddress;
23 uint8_t * twi_writeData;
24 uint8_t twi_writeLength;
26 uint8_t twi_readAddress;
28 uint8_t twi_readLength;
36 void initialize_twi_nonblock(){
44 twi_state = TWI_READY;
46 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
61 TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
69 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);
73 uint8_t twowire_requestFromBlocking(uint8_t address, uint8_t quantity)
76 if(quantity > BUFFER_LENGTH){
77 quantity = BUFFER_LENGTH;
80 uint8_t read = twi_readFromBlocking(address, rxBuffer, quantity);
83 rxBufferLength = read;
88 void twowire_beginTransmission( uint8_t address ){
98 void twowire_send( uint8_t data ){
102 if(txBufferLength >= BUFFER_LENGTH){
106 txBuffer[txBufferIndex] = data;
109 txBufferLength = txBufferIndex;
113 uint8_t twowire_endTransmission(
void)
116 int8_t ret = twi_writeToBlocking(txAddress, txBuffer, txBufferLength, 1);
135 uint8_t twi_readFromBlocking(uint8_t address, uint8_t* data, uint8_t length)
140 if(TWI_BUFFER_LENGTH < length){
145 while(TWI_READY != twi_state){
154 twi_masterBufferIndex = 0;
155 twi_masterBufferLength = length-1;
164 twi_slarw |= address << 1;
167 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
170 while(TWI_MRX == twi_state){
174 if (twi_masterBufferIndex < length)
175 length = twi_masterBufferIndex;
178 for(i = 0; i < length; ++i){
179 data[i] = twi_masterBuffer[i];
190 uint8_t twi_initiateReadFrom(uint8_t address, uint8_t length)
194 if(TWI_BUFFER_LENGTH < length){
198 twi_readLength = length;
199 twi_readAddress = address;
201 if ( TWI_READY == twi_state ){
202 twi_continueReadFrom();
204 twi_state = TWI_PRE_MRX;
206 if (twi_error == 0xFF)
208 else if (twi_error == TW_MT_SLA_NACK)
210 else if (twi_error == TW_MT_DATA_NACK)
218 void twi_continueReadFrom(){
225 twi_masterBufferIndex = 0;
226 twi_masterBufferLength = twi_readLength-1;
235 twi_slarw |= twi_readAddress << 1;
238 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
243 uint8_t twi_readMasterBuffer( uint8_t* data, uint8_t length ){
245 if (twi_masterBufferIndex < length)
246 length = twi_masterBufferIndex;
249 for(i = 0; i < length; ++i){
250 data[i] = twi_masterBuffer[i];
276 uint8_t twi_writeToBlocking(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait)
281 if(TWI_BUFFER_LENGTH < length){
286 while(TWI_READY != twi_state){
295 twi_masterBufferIndex = 0;
296 twi_masterBufferLength = length;
299 for(i = 0; i < length; ++i){
300 twi_masterBuffer[i] = data[i];
304 twi_slarw = TW_WRITE;
305 twi_slarw |= address << 1;
308 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
311 while(wait && (TWI_MTX == twi_state)){
315 if (twi_error == 0xFF)
317 else if (twi_error == TW_MT_SLA_NACK)
319 else if (twi_error == TW_MT_DATA_NACK)
330 uint8_t twi_initiateWriteTo(uint8_t address, uint8_t* data, uint8_t length )
333 if(TWI_BUFFER_LENGTH < length){
336 twi_writeAddress = address;
337 twi_writeData = data;
338 twi_writeLength = length;
340 if ( TWI_READY == twi_state ){
341 twi_continueWriteTo();
343 twi_state = TWI_PRE_MTX;
345 if (twi_error == 0xFF)
347 else if (twi_error == TW_MT_SLA_NACK)
349 else if (twi_error == TW_MT_DATA_NACK)
357 void twi_continueWriteTo(){
369 twi_masterBufferIndex = 0;
370 twi_masterBufferLength = twi_writeLength;
373 for(i = 0; i < twi_writeLength; ++i){
374 twi_masterBuffer[i] = twi_writeData[i];
378 twi_slarw = TW_WRITE;
379 twi_slarw |= twi_writeAddress << 1;
382 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
395 void twi_reply(uint8_t ack)
399 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA);
401 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
416 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO);
420 while(TWCR & _BV(TWSTO)){
424 twi_oldstate = twi_state;
426 twi_state = TWI_READY;
427 if ( twi_oldstate == TWI_PRE_MTX ){
428 twi_continueWriteTo();
429 }
else if ( twi_oldstate == TWI_PRE_MRX ){
430 twi_continueReadFrom();
442 void twi_releaseBus(
void)
445 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT);
447 twi_oldstate = twi_state;
449 twi_state = TWI_READY;
450 if ( twi_oldstate == TWI_PRE_MTX ){
451 twi_continueWriteTo();
452 }
else if ( twi_oldstate == TWI_PRE_MRX ){
453 twi_continueReadFrom();
474 if(twi_masterBufferIndex < twi_masterBufferLength){
476 TWDR = twi_masterBuffer[twi_masterBufferIndex++];
483 twi_error = TW_MT_SLA_NACK;
486 case TW_MT_DATA_NACK:
487 twi_error = TW_MT_DATA_NACK;
491 twi_error = TW_MT_ARB_LOST;
498 twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
501 if(twi_masterBufferIndex < twi_masterBufferLength){
507 case TW_MR_DATA_NACK:
509 twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
598 twi_error = TW_BUS_ERROR;