Skip to content

Commit

Permalink
add Wire that supports both normalWire and TinyWireM
Browse files Browse the repository at this point in the history
  • Loading branch information
ladyada committed Jan 15, 2016
1 parent f9d19ed commit c437230
Show file tree
Hide file tree
Showing 17 changed files with 1,958 additions and 0 deletions.
8 changes: 8 additions & 0 deletions hardware/adafruit/avr/libraries/Wire/README.md
Original file line number Diff line number Diff line change
@@ -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).
124 changes: 124 additions & 0 deletions hardware/adafruit/avr/libraries/Wire/TinyWireM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
TinyWireM.cpp - a wrapper class for TWI/I2C Master library for the ATtiny on Arduino
1/21/2011 BroHogan - brohoganx10 at gmail dot com

**** See TinyWireM.h for Credits and Usage information ****

This library is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 2.1 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/

#ifdef __AVR_ATtiny85__

extern "C" {
//#include "USI_TWI_Master.h"
//#include <USI_TWI_Master.h>
//#include <USI_TWI_Master\USI_TWI_Master.h>
//#include <USI_TWI_Master/USI_TWI_Master.h>
}

#include "USI_TWI_Master.h"
#include "TinyWireM.h"


// Initialize Class Variables //////////////////////////////////////////////////
uint8_t TinyM_USI_TWI::USI_Buf[TinyM_USI_BUF_SIZE]; // holds I2C send and receive data
uint8_t TinyM_USI_TWI::USI_BufIdx = 0; // current number of bytes in the send buff
uint8_t TinyM_USI_TWI::USI_LastRead = 0; // number of bytes read so far
uint8_t TinyM_USI_TWI::USI_BytesAvail = 0; // number of bytes requested but not read

// Constructors ////////////////////////////////////////////////////////////////

TinyM_USI_TWI::TinyM_USI_TWI(){
}

// Public Methods //////////////////////////////////////////////////////////////

//int TinyM_USI_TWI::peek(){}
//void TinyM_USI_TWI::flush(){}

void TinyM_USI_TWI::begin(){ // initialize I2C lib
TinyM_USI_TWI_Master_Initialise();
}

void TinyM_USI_TWI::beginTransmission(uint8_t slaveAddr){ // setup address & write bit
USI_BufIdx = 0;
USI_Buf[USI_BufIdx] = (slaveAddr<<TWI_ADR_BITS) | TinyM_USI_SEND;
}

size_t TinyM_USI_TWI::write(uint8_t data){ // buffers up data to send
if (USI_BufIdx >= TinyM_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 TinyM_USI_TWI::endTransmission() {
endTransmission(1);
}

uint8_t TinyM_USI_TWI::endTransmission(uint8_t stop){ // actually sends the buffer
bool xferOK = false;
uint8_t errorCode = 0;
xferOK = TinyM_USI_TWI_Start_Read_Write(USI_Buf,USI_BufIdx+1); // core func that does the work
USI_BufIdx = 0;
if (xferOK) {
if (stop) {
errorCode = TinyM_USI_TWI_Master_Stop();
if (errorCode == 0) {
errorCode = TinyM_USI_TWI_Get_State_Info();
return errorCode;
}
}
return 0;
}
else { // there was an error
errorCode = TinyM_USI_TWI_Get_State_Info(); // this function returns the error number
return errorCode;
}
}

uint8_t TinyM_USI_TWI::requestFrom(uint8_t slaveAddr, uint8_t numBytes){ // setup for receiving from slave
bool xferOK = false;
uint8_t errorCode = 0;
USI_LastRead = 0;
USI_BytesAvail = numBytes; // save this off in a global
numBytes++; // add extra byte to transmit header
USI_Buf[0] = (slaveAddr<<TWI_ADR_BITS) | TinyM_USI_RCVE; // setup address & Rcve bit
xferOK = TinyM_USI_TWI_Start_Read_Write(USI_Buf,numBytes); // core func that does the work
// USI_Buf now holds the data read
if (xferOK) {
errorCode = TinyM_USI_TWI_Master_Stop();
if (errorCode == 0) {
errorCode = TinyM_USI_TWI_Get_State_Info();
return errorCode;
}
return 0;
}
else { // there was an error
errorCode = TinyM_USI_TWI_Get_State_Info(); // this function returns the error number
return errorCode;
}
}

int TinyM_USI_TWI::read(){ // returns the bytes received one at a time
USI_LastRead++; // inc first since first uint8_t read is in USI_Buf[1]
return USI_Buf[USI_LastRead];
}

int TinyM_USI_TWI::available(){ // the bytes available that haven't been read yet
return USI_BytesAvail - (USI_LastRead);
}


// Preinstantiate Objects //////////////////////////////////////////////////////

TinyM_USI_TWI Wire = TinyM_USI_TWI();


#endif //attiny85
88 changes: 88 additions & 0 deletions hardware/adafruit/avr/libraries/Wire/TinyWireM.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
TinyWireM.h - a wrapper(+) class for TWI/I2C Master library for the ATtiny on Arduino
1/21/2011 BroHogan - brohoganx10 at gmail dot com

Thanks to 'jkl' for the gcc version of Atmel's USI_TWI_Master code
http://www.cs.cmu.edu/~dst/ARTSI/Create/PC%20Comm/
I added Atmel's original Device dependant defines section back into USI_TWI_Master.h


NOTE! - It's very important to use pullups on the SDA & SCL lines! More so than with the Wire lib.

USAGE is modeled after the standard Wire library . . .
Put in setup():
TinyWireM.begin(){ // initialize I2C lib
To Send:
TinyWireM.beginTransmission(uint8_t slaveAddr){ // setup slave's address (7 bit address - same as Wire)
TinyWireM.send(uint8_t data){ // buffer up bytes to send - can be called multiple times
someByte = TinyWireM.endTransmission(){ // actually send the bytes in the buffer
// returns (optional) 0 = sucess or see USI_TWI_Master.h for error codes
To Receive:
someByte = TinyWireM.requestFrom(uint8_t slaveAddr, uint8_t numBytes){ // reads 'numBytes' from slave's address
// (usage optional) returns 0= success or see USI_TWI_Master.h for error codes
someByte = TinyWireM.receive(){ // returns the next byte in the received buffer - called multiple times
someByte = TinyWireM.available(){ // returns the number of unread bytes in the received buffer

TODO: (by others!)
- merge this class with TinyWireS for master & slave support in one library

This library is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 2.1 of the License, or any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/

#ifdef __AVR_ATtiny85__

#ifndef TinyWireM_h
#define TinyWireM_h

#include <inttypes.h>
#include "Arduino.h"
#define TinyM_USI_SEND 0 // indicates sending to TWI
#define TinyM_USI_RCVE 1 // indicates receiving from TWI
#define TinyM_USI_BUF_SIZE 18 // bytes in message buffer

//class USI_TWI : public Stream
class TinyM_USI_TWI
{
private:
static uint8_t USI_Buf[]; // holds I2C send and receive data
static uint8_t USI_BufIdx; // current number of bytes in the send buff
static uint8_t USI_LastRead; // number of bytes read so far
static uint8_t USI_BytesAvail; // number of bytes requested but not read

public:
TinyM_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);
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 TinyM_USI_TWI Wire;

#endif

#endif //attiny85
Loading

0 comments on commit c437230

Please sign in to comment.