diff --git a/digistump-avr/libraries/TinyWireM/README.md b/digistump-avr/libraries/TinyWireM/README.md new file mode 100644 index 0000000..cbf740e --- /dev/null +++ b/digistump-avr/libraries/TinyWireM/README.md @@ -0,0 +1,8 @@ +TinyWireM +========= + +ATtiny (e.g. Adafruit Trinket, Gemma) I2C library, adapted from BroHogan's code on Arduino Playground: http://playground.arduino.cc/Code/USIi2c + +Minor changes for consistency with the Arduino 1.0 Wire library (e.g. uses write() instead of send()). Buffer size slightly increased for Adafruit_LEDBackpack use. + +On the Trinket boards, pin #0 is SDA (I2C data), pin #2 is SCK (I2C clock). diff --git a/digistump-avr/libraries/TinyWireM/TinyWireM.cpp b/digistump-avr/libraries/TinyWireM/TinyWireM.cpp index 24ae065..d0d2c30 100644 --- a/digistump-avr/libraries/TinyWireM/TinyWireM.cpp +++ b/digistump-avr/libraries/TinyWireM/TinyWireM.cpp @@ -37,6 +37,9 @@ USI_TWI::USI_TWI(){ // Public Methods ////////////////////////////////////////////////////////////// +//int USI_TWI::peek(){} +//void USI_TWI::flush(){} + void USI_TWI::begin(){ // initialize I2C lib USI_TWI_Master_Initialise(); } @@ -46,18 +49,32 @@ void USI_TWI::beginTransmission(uint8_t slaveAddr){ // setup address & write bit USI_Buf[USI_BufIdx] = (slaveAddr<= USI_BUF_SIZE) return; // dont blow out the buffer +size_t USI_TWI::write(uint8_t data){ // buffers up data to send + if (USI_BufIdx >= USI_BUF_SIZE) return 0; // dont blow out the buffer USI_BufIdx++; // inc for next byte in buffer USI_Buf[USI_BufIdx] = data; + return 1; } -uint8_t USI_TWI::endTransmission(){ // actually sends the buffer +uint8_t USI_TWI::endTransmission() { + endTransmission(1); +} + +uint8_t USI_TWI::endTransmission(uint8_t stop){ // actually sends the buffer bool xferOK = false; uint8_t errorCode = 0; xferOK = USI_TWI_Start_Read_Write(USI_Buf,USI_BufIdx+1); // core func that does the work USI_BufIdx = 0; - if (xferOK) return 0; + if (xferOK) { + if (stop) { + errorCode = USI_TWI_Master_Stop(); + if (errorCode == 0) { + errorCode = USI_TWI_Get_State_Info(); + return errorCode; + } + } + return 0; + } else { // there was an error errorCode = USI_TWI_Get_State_Info(); // this function returns the error number return errorCode; @@ -73,19 +90,26 @@ uint8_t USI_TWI::requestFrom(uint8_t slaveAddr, uint8_t numBytes){ // setup for USI_Buf[0] = (slaveAddr< +#include "Arduino.h" #define USI_SEND 0 // indicates sending to TWI #define USI_RCVE 1 // indicates receiving from TWI -#define USI_BUF_SIZE 16 // bytes in message buffer +#define USI_BUF_SIZE 18 // bytes in message buffer +//class USI_TWI : public Stream class USI_TWI { private: @@ -51,14 +53,30 @@ class USI_TWI static uint8_t USI_BytesAvail; // number of bytes requested but not read public: - USI_TWI(); - void begin(); - void beginTransmission(uint8_t); - void send(uint8_t); + USI_TWI(); + void begin(); + void beginTransmission(uint8_t); + size_t write(uint8_t); + inline size_t write(uint8_t* d, uint8_t n) { uint16_t i; for (i = 0; i < n; i++) write(d[i]); return (size_t)n; } + inline size_t write(unsigned long n) { return write((uint8_t)n); } + inline size_t write(long n) { return write((uint8_t)n); } + inline size_t write(unsigned int n) { return write((uint8_t)n); } + inline size_t write(int n) { return write((uint8_t)n); } + void send(uint8_t b) { write(b); } + void send(uint8_t *d, uint8_t n) { write(d, n); } + void send(int n) { write((uint8_t)n); } uint8_t endTransmission(); + uint8_t endTransmission(uint8_t); uint8_t requestFrom(uint8_t, uint8_t); - uint8_t receive(); - uint8_t available(); + int read(); + int available(); + int peek(void); + void flush(void); + uint8_t receive(void) { + int c = read(); + if (c < 0) return 0; + return c; + } }; extern USI_TWI TinyWireM; diff --git a/digistump-avr/libraries/TinyWireM/USI_TWI_Master.cpp b/digistump-avr/libraries/TinyWireM/USI_TWI_Master.cpp index eea99cb..9109ed0 100644 --- a/digistump-avr/libraries/TinyWireM/USI_TWI_Master.cpp +++ b/digistump-avr/libraries/TinyWireM/USI_TWI_Master.cpp @@ -20,20 +20,12 @@ * length should be these two bytes plus the number of bytes to read. ****************************************************************************/ #include -#if defined (__AVR_ATtiny45__) || defined (__AVR_ATtiny85__) -#define F_CPU 16500000UL - -#elif defined (__AVR_ATtiny87__) || defined (__AVR_ATtiny167__) -#define F_CPU 16000000UL // Sets up the default speed for delay.h -#endif - #include #include #include "USI_TWI_Master.h" unsigned char USI_TWI_Start_Transceiver_With_Data( unsigned char * , unsigned char ); unsigned char USI_TWI_Master_Transfer( unsigned char ); -unsigned char USI_TWI_Master_Stop( void ); unsigned char USI_TWI_Master_Start( void ); union USI_TWI_state @@ -254,11 +246,8 @@ unsigned char USI_TWI_Start_Transceiver_With_Data( unsigned char *msg, unsigned USI_TWI_Master_Transfer( tempUSISR_1bit ); // Generate ACK/NACK. } }while( --msgSize) ; // Until all data sent/received. - - if (!USI_TWI_Master_Stop()) - { - return (FALSE); // Send a STOP condition on the TWI bus. - } + + // usually a stop condition is sent here, but TinyWireM needs to choose whether or not to send it /* Transmission successfully completed*/ return (TRUE); diff --git a/digistump-avr/libraries/TinyWireM/USI_TWI_Master.h b/digistump-avr/libraries/TinyWireM/USI_TWI_Master.h index 4a48180..eb7edbb 100644 --- a/digistump-avr/libraries/TinyWireM/USI_TWI_Master.h +++ b/digistump-avr/libraries/TinyWireM/USI_TWI_Master.h @@ -21,14 +21,6 @@ //********** Defines **********// // Defines controlling timing limits - SCL <= 100KHz. -#if defined (__AVR_ATtiny45__) || defined (__AVR_ATtiny85__) -#define SYS_CLK 16500.0 // [kHz] Default for ATtiny2313 - -#elif defined (__AVR_ATtiny87__) || defined (__AVR_ATtiny167__) -#define SYS_CLK 16000.0 // [kHz] Default for ATtiny2313 -#endif - - // For use with _delay_us() #define T2_TWI 5 // >4,7us @@ -88,6 +80,16 @@ #define PIN_USI_SCL PINB2 #endif +#if defined(__AVR_ATtiny84__) | defined(__AVR_ATtiny44__) + # define DDR_USI DDRA + # define PORT_USI PORTA + # define PIN_USI PINA + # define PORT_USI_SDA PORTA6 + # define PORT_USI_SCL PORTA4 + # define PIN_USI_SDA PINA6 + # define PIN_USI_SCL PINA4 +#endif + #if defined(__AVR_AT90Tiny2313__) | defined(__AVR_ATtiny2313__) #define DDR_USI DDRB #define PORT_USI PORTB @@ -119,4 +121,5 @@ void USI_TWI_Master_Initialise( void ); unsigned char USI_TWI_Start_Random_Read( unsigned char * , unsigned char ); unsigned char USI_TWI_Start_Read_Write( unsigned char * , unsigned char ); +unsigned char USI_TWI_Master_Stop( void ); unsigned char USI_TWI_Get_State_Info( void ); diff --git a/digistump-avr/libraries/TinyWireM/keywords.txt b/digistump-avr/libraries/TinyWireM/keywords.txt index b6b61c3..ead4b70 100644 --- a/digistump-avr/libraries/TinyWireM/keywords.txt +++ b/digistump-avr/libraries/TinyWireM/keywords.txt @@ -14,8 +14,8 @@ begin KEYWORD2 beginTransmission KEYWORD2 endTransmission KEYWORD2 requestFrom KEYWORD2 -send KEYWORD2 -receive KEYWORD2 +write KEYWORD2 +read KEYWORD2 ####################################### # Instances (KEYWORD2) diff --git a/digistump-avr/libraries/TinyWireM/library.properties b/digistump-avr/libraries/TinyWireM/library.properties new file mode 100644 index 0000000..2c9d5d5 --- /dev/null +++ b/digistump-avr/libraries/TinyWireM/library.properties @@ -0,0 +1,9 @@ +name=TinyWireM +version=1.0.0 +author=Adafruit +maintainer=Adafruit +sentence=I2C library for Trinket and Gemma, adapted from BroHogan's code on Arduino Playground +paragraph=I2C library for Trinket and Gemma, adapted from BroHogan's code on Arduino Playground +category=Signal Input/Output +url=https://github.com/adafruit/TinyWireM +architectures=*