13 #include "twi_nonblock.h" 16 #include <avr/interrupt.h> 18 uint8_t twi_writeAddress;
19 uint8_t * twi_writeData;
20 uint8_t twi_writeLength;
22 uint8_t twi_readAddress;
24 uint8_t twi_readLength;
32 void initialize_twi_nonblock(){
40 twi_state = TWI_READY;
42 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__) 57 TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
65 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);
69 uint8_t twowire_requestFromBlocking(uint8_t address, uint8_t quantity)
72 if(quantity > BUFFER_LENGTH){
73 quantity = BUFFER_LENGTH;
76 uint8_t read = twi_readFromBlocking(address, rxBuffer, quantity);
79 rxBufferLength = read;
84 void twowire_beginTransmission( uint8_t address ){
94 void twowire_send( uint8_t data ){
98 if(txBufferLength >= BUFFER_LENGTH){
102 txBuffer[txBufferIndex] = data;
105 txBufferLength = txBufferIndex;
109 uint8_t twowire_endTransmission(
void)
112 int8_t ret = twi_writeToBlocking(txAddress, txBuffer, txBufferLength, 1);
131 uint8_t twi_readFromBlocking(uint8_t address, uint8_t* data, uint8_t length)
136 if(TWI_BUFFER_LENGTH < length){
141 while(TWI_READY != twi_state){
150 twi_masterBufferIndex = 0;
151 twi_masterBufferLength = length-1;
160 twi_slarw |= address << 1;
163 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
166 while(TWI_MRX == twi_state){
170 if (twi_masterBufferIndex < length)
171 length = twi_masterBufferIndex;
174 for(i = 0; i < length; ++i){
175 data[i] = twi_masterBuffer[i];
186 uint8_t twi_initiateReadFrom(uint8_t address, uint8_t length)
190 if(TWI_BUFFER_LENGTH < length){
194 twi_readLength = length;
195 twi_readAddress = address;
197 if ( TWI_READY == twi_state ){
198 twi_continueReadFrom();
200 twi_state = TWI_PRE_MRX;
202 if (twi_error == 0xFF)
204 else if (twi_error == TW_MT_SLA_NACK)
206 else if (twi_error == TW_MT_DATA_NACK)
214 void twi_continueReadFrom(){
221 twi_masterBufferIndex = 0;
222 twi_masterBufferLength = twi_readLength-1;
231 twi_slarw |= twi_readAddress << 1;
234 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
239 uint8_t twi_readMasterBuffer( uint8_t* data, uint8_t length ){
241 if (twi_masterBufferIndex < length)
242 length = twi_masterBufferIndex;
245 for(i = 0; i < length; ++i){
246 data[i] = twi_masterBuffer[i];
272 uint8_t twi_writeToBlocking(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait)
277 if(TWI_BUFFER_LENGTH < length){
282 while(TWI_READY != twi_state){
291 twi_masterBufferIndex = 0;
292 twi_masterBufferLength = length;
295 for(i = 0; i < length; ++i){
296 twi_masterBuffer[i] = data[i];
300 twi_slarw = TW_WRITE;
301 twi_slarw |= address << 1;
304 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
307 while(wait && (TWI_MTX == twi_state)){
311 if (twi_error == 0xFF)
313 else if (twi_error == TW_MT_SLA_NACK)
315 else if (twi_error == TW_MT_DATA_NACK)
326 uint8_t twi_initiateWriteTo(uint8_t address, uint8_t* data, uint8_t length )
329 if(TWI_BUFFER_LENGTH < length){
332 twi_writeAddress = address;
333 twi_writeData = data;
334 twi_writeLength = length;
336 if ( TWI_READY == twi_state ){
337 twi_continueWriteTo();
339 twi_state = TWI_PRE_MTX;
341 if (twi_error == 0xFF)
343 else if (twi_error == TW_MT_SLA_NACK)
345 else if (twi_error == TW_MT_DATA_NACK)
353 void twi_continueWriteTo(){
365 twi_masterBufferIndex = 0;
366 twi_masterBufferLength = twi_writeLength;
369 for(i = 0; i < twi_writeLength; ++i){
370 twi_masterBuffer[i] = twi_writeData[i];
374 twi_slarw = TW_WRITE;
375 twi_slarw |= twi_writeAddress << 1;
378 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
391 void twi_reply(uint8_t ack)
395 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA);
397 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
412 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO);
416 while(TWCR & _BV(TWSTO)){
420 twi_oldstate = twi_state;
422 twi_state = TWI_READY;
423 if ( twi_oldstate == TWI_PRE_MTX ){
424 twi_continueWriteTo();
425 }
else if ( twi_oldstate == TWI_PRE_MRX ){
426 twi_continueReadFrom();
438 void twi_releaseBus(
void)
441 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT);
443 twi_oldstate = twi_state;
445 twi_state = TWI_READY;
446 if ( twi_oldstate == TWI_PRE_MTX ){
447 twi_continueWriteTo();
448 }
else if ( twi_oldstate == TWI_PRE_MRX ){
449 twi_continueReadFrom();
470 if(twi_masterBufferIndex < twi_masterBufferLength){
472 TWDR = twi_masterBuffer[twi_masterBufferIndex++];
479 twi_error = TW_MT_SLA_NACK;
482 case TW_MT_DATA_NACK:
483 twi_error = TW_MT_DATA_NACK;
487 twi_error = TW_MT_ARB_LOST;
494 twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
497 if(twi_masterBufferIndex < twi_masterBufferLength){
503 case TW_MR_DATA_NACK:
505 twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
594 twi_error = TW_BUS_ERROR;