Skip to content

Commit

Permalink
updated to latest adafruit version
Browse files Browse the repository at this point in the history
  • Loading branch information
ekettenburg committed Jun 23, 2015
1 parent 68ad726 commit 4e2f6ff
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 37 deletions.
8 changes: 8 additions & 0 deletions digistump-avr/libraries/TinyWireM/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).
38 changes: 31 additions & 7 deletions digistump-avr/libraries/TinyWireM/TinyWireM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -46,18 +49,32 @@ void USI_TWI::beginTransmission(uint8_t slaveAddr){ // setup address & write bit
USI_Buf[USI_BufIdx] = (slaveAddr<<TWI_ADR_BITS) | USI_SEND;
}

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

uint8_t USI_TWI::endTransmission(){ // actually sends the buffer
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;
Expand All @@ -73,19 +90,26 @@ uint8_t USI_TWI::requestFrom(uint8_t slaveAddr, uint8_t numBytes){ // setup for
USI_Buf[0] = (slaveAddr<<TWI_ADR_BITS) | USI_RCVE; // setup address & Rcve bit
xferOK = USI_TWI_Start_Read_Write(USI_Buf,numBytes); // core func that does the work
// USI_Buf now holds the data read
if (xferOK) return 0;
if (xferOK) {
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;
}
}

uint8_t USI_TWI::receive(){ // returns the bytes received one at a time
int 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];
}

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

Expand Down
32 changes: 25 additions & 7 deletions digistump-avr/libraries/TinyWireM/TinyWireM.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
#define TinyWireM_h

#include <inttypes.h>
#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:
Expand All @@ -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;
Expand Down
15 changes: 2 additions & 13 deletions digistump-avr/libraries/TinyWireM/USI_TWI_Master.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,12 @@
* length should be these two bytes plus the number of bytes to read.
****************************************************************************/
#include <avr/interrupt.h>
#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 <util/delay.h>
#include <avr/io.h>
#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
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 11 additions & 8 deletions digistump-avr/libraries/TinyWireM/USI_TWI_Master.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 );
4 changes: 2 additions & 2 deletions digistump-avr/libraries/TinyWireM/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ begin KEYWORD2
beginTransmission KEYWORD2
endTransmission KEYWORD2
requestFrom KEYWORD2
send KEYWORD2
receive KEYWORD2
write KEYWORD2
read KEYWORD2

#######################################
# Instances (KEYWORD2)
Expand Down
9 changes: 9 additions & 0 deletions digistump-avr/libraries/TinyWireM/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=TinyWireM
version=1.0.0
author=Adafruit
maintainer=Adafruit <[email protected]>
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=*

0 comments on commit 4e2f6ff

Please sign in to comment.