10 #if !(defined(__MK20DX128__) || defined(__MK20DX256__))
12 #include "twi_nonblock.h"
14 #include <avr/interrupt.h>
16 uint8_t twi_writeAddress;
17 uint8_t * twi_writeData;
18 uint8_t twi_writeLength;
20 uint8_t twi_readAddress;
22 uint8_t twi_readLength;
30 void initialize_twi_nonblock(){
38 twi_state = TWI_READY;
40 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
55 TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
63 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);
67 uint8_t twowire_requestFromBlocking(uint8_t address, uint8_t quantity)
70 if(quantity > BUFFER_LENGTH){
71 quantity = BUFFER_LENGTH;
74 uint8_t read = twi_readFromBlocking(address, rxBuffer, quantity);
77 rxBufferLength = read;
82 void twowire_beginTransmission( uint8_t address ){
92 void twowire_send( uint8_t data ){
96 if(txBufferLength >= BUFFER_LENGTH){
100 txBuffer[txBufferIndex] = data;
103 txBufferLength = txBufferIndex;
107 uint8_t twowire_endTransmission(
void)
110 int8_t ret = twi_writeToBlocking(txAddress, txBuffer, txBufferLength, 1);
129 uint8_t twi_readFromBlocking(uint8_t address, uint8_t* data, uint8_t length)
134 if(TWI_BUFFER_LENGTH < length){
139 while(TWI_READY != twi_state){
148 twi_masterBufferIndex = 0;
149 twi_masterBufferLength = length-1;
158 twi_slarw |= address << 1;
161 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
164 while(TWI_MRX == twi_state){
168 if (twi_masterBufferIndex < length)
169 length = twi_masterBufferIndex;
172 for(i = 0; i < length; ++i){
173 data[i] = twi_masterBuffer[i];
184 uint8_t twi_initiateReadFrom(uint8_t address, uint8_t length)
188 if(TWI_BUFFER_LENGTH < length){
192 twi_readLength = length;
193 twi_readAddress = address;
195 if ( TWI_READY == twi_state ){
196 twi_continueReadFrom();
198 twi_state = TWI_PRE_MRX;
200 if (twi_error == 0xFF)
202 else if (twi_error == TW_MT_SLA_NACK)
204 else if (twi_error == TW_MT_DATA_NACK)
212 void twi_continueReadFrom(){
219 twi_masterBufferIndex = 0;
220 twi_masterBufferLength = twi_readLength-1;
229 twi_slarw |= twi_readAddress << 1;
232 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
237 uint8_t twi_readMasterBuffer( uint8_t* data, uint8_t length ){
239 if (twi_masterBufferIndex < length)
240 length = twi_masterBufferIndex;
243 for(i = 0; i < length; ++i){
244 data[i] = twi_masterBuffer[i];
270 uint8_t twi_writeToBlocking(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait)
275 if(TWI_BUFFER_LENGTH < length){
280 while(TWI_READY != twi_state){
289 twi_masterBufferIndex = 0;
290 twi_masterBufferLength = length;
293 for(i = 0; i < length; ++i){
294 twi_masterBuffer[i] = data[i];
298 twi_slarw = TW_WRITE;
299 twi_slarw |= address << 1;
302 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
305 while(wait && (TWI_MTX == twi_state)){
309 if (twi_error == 0xFF)
311 else if (twi_error == TW_MT_SLA_NACK)
313 else if (twi_error == TW_MT_DATA_NACK)
324 uint8_t twi_initiateWriteTo(uint8_t address, uint8_t* data, uint8_t length )
327 if(TWI_BUFFER_LENGTH < length){
330 twi_writeAddress = address;
331 twi_writeData = data;
332 twi_writeLength = length;
334 if ( TWI_READY == twi_state ){
335 twi_continueWriteTo();
337 twi_state = TWI_PRE_MTX;
339 if (twi_error == 0xFF)
341 else if (twi_error == TW_MT_SLA_NACK)
343 else if (twi_error == TW_MT_DATA_NACK)
351 void twi_continueWriteTo(){
363 twi_masterBufferIndex = 0;
364 twi_masterBufferLength = twi_writeLength;
367 for(i = 0; i < twi_writeLength; ++i){
368 twi_masterBuffer[i] = twi_writeData[i];
372 twi_slarw = TW_WRITE;
373 twi_slarw |= twi_writeAddress << 1;
376 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
389 void twi_reply(uint8_t ack)
393 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA);
395 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
410 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO);
414 while(TWCR & _BV(TWSTO)){
418 twi_oldstate = twi_state;
420 twi_state = TWI_READY;
421 if ( twi_oldstate == TWI_PRE_MTX ){
422 twi_continueWriteTo();
423 }
else if ( twi_oldstate == TWI_PRE_MRX ){
424 twi_continueReadFrom();
436 void twi_releaseBus(
void)
439 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT);
441 twi_oldstate = twi_state;
443 twi_state = TWI_READY;
444 if ( twi_oldstate == TWI_PRE_MTX ){
445 twi_continueWriteTo();
446 }
else if ( twi_oldstate == TWI_PRE_MRX ){
447 twi_continueReadFrom();
468 if(twi_masterBufferIndex < twi_masterBufferLength){
470 TWDR = twi_masterBuffer[twi_masterBufferIndex++];
477 twi_error = TW_MT_SLA_NACK;
480 case TW_MT_DATA_NACK:
481 twi_error = TW_MT_DATA_NACK;
485 twi_error = TW_MT_ARB_LOST;
492 twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
495 if(twi_masterBufferIndex < twi_masterBufferLength){
501 case TW_MR_DATA_NACK:
503 twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
592 twi_error = TW_BUS_ERROR;