8 #ifndef TWI_NONBLOCK_H_ 9 #define TWI_NONBLOCK_H_ 12 #if !(defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO)) // hide all code from Teensy 3.1 17 #include <compat/twi.h> 19 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) 20 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) 24 #define TWI_FREQ 100000L 27 #ifndef TWI_BUFFER_LENGTH 28 #define TWI_BUFFER_LENGTH 32 39 static volatile uint8_t twi_state;
40 static volatile uint8_t twi_oldstate;
42 static uint8_t twi_slarw;
44 static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
45 static volatile uint8_t twi_masterBufferIndex;
46 static uint8_t twi_masterBufferLength;
48 static volatile uint8_t twi_error;
50 #define BUFFER_LENGTH 32 51 static uint8_t rxBuffer[BUFFER_LENGTH];
52 static uint8_t rxBufferIndex = 0;
53 static uint8_t rxBufferLength = 0;
55 static uint8_t txAddress = 0;
56 static uint8_t txBuffer[BUFFER_LENGTH];
57 static uint8_t txBufferIndex = 0;
58 static uint8_t txBufferLength = 0;
60 static uint8_t transmitting;
63 void initialize_twi_nonblock();
65 uint8_t twowire_requestFrom(uint8_t address, uint8_t quantity);
66 void twowire_beginTransmission( uint8_t address );
67 void twowire_send( uint8_t data );
68 uint8_t twowire_endTransmission(
void);
71 uint8_t twi_initiateReadFrom(uint8_t address, uint8_t length);
72 void twi_continueReadFrom();
74 uint8_t twi_readMasterBuffer( uint8_t* data, uint8_t length );
76 uint8_t twi_initiateWriteTo(uint8_t address, uint8_t* data, uint8_t length );
77 void twi_continueWriteTo();
80 void twi_reply(uint8_t ack);
82 void twi_releaseBus(
void);
85 uint8_t twi_readFromBlocking(uint8_t address, uint8_t* data, uint8_t length);
86 uint8_t twi_writeToBlocking(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait);