Skip to content

Commit 4e2f6ff

Browse files
committed
updated to latest adafruit version
1 parent 68ad726 commit 4e2f6ff

File tree

7 files changed

+88
-37
lines changed

7 files changed

+88
-37
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
TinyWireM
2+
=========
3+
4+
ATtiny (e.g. Adafruit Trinket, Gemma) I2C library, adapted from BroHogan's code on Arduino Playground: http://playground.arduino.cc/Code/USIi2c
5+
6+
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.
7+
8+
On the Trinket boards, pin #0 is SDA (I2C data), pin #2 is SCK (I2C clock).

digistump-avr/libraries/TinyWireM/TinyWireM.cpp

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ USI_TWI::USI_TWI(){
3737

3838
// Public Methods //////////////////////////////////////////////////////////////
3939

40+
//int USI_TWI::peek(){}
41+
//void USI_TWI::flush(){}
42+
4043
void USI_TWI::begin(){ // initialize I2C lib
4144
USI_TWI_Master_Initialise();
4245
}
@@ -46,18 +49,32 @@ void USI_TWI::beginTransmission(uint8_t slaveAddr){ // setup address & write bit
4649
USI_Buf[USI_BufIdx] = (slaveAddr<<TWI_ADR_BITS) | USI_SEND;
4750
}
4851

49-
void USI_TWI::send(uint8_t data){ // buffers up data to send
50-
if (USI_BufIdx >= USI_BUF_SIZE) return; // dont blow out the buffer
52+
size_t USI_TWI::write(uint8_t data){ // buffers up data to send
53+
if (USI_BufIdx >= USI_BUF_SIZE) return 0; // dont blow out the buffer
5154
USI_BufIdx++; // inc for next byte in buffer
5255
USI_Buf[USI_BufIdx] = data;
56+
return 1;
57+
}
58+
59+
uint8_t USI_TWI::endTransmission() {
60+
endTransmission(1);
5361
}
5462

55-
uint8_t USI_TWI::endTransmission(){ // actually sends the buffer
63+
uint8_t USI_TWI::endTransmission(uint8_t stop){ // actually sends the buffer
5664
bool xferOK = false;
5765
uint8_t errorCode = 0;
5866
xferOK = USI_TWI_Start_Read_Write(USI_Buf,USI_BufIdx+1); // core func that does the work
5967
USI_BufIdx = 0;
60-
if (xferOK) return 0;
68+
if (xferOK) {
69+
if (stop) {
70+
errorCode = USI_TWI_Master_Stop();
71+
if (errorCode == 0) {
72+
errorCode = USI_TWI_Get_State_Info();
73+
return errorCode;
74+
}
75+
}
76+
return 0;
77+
}
6178
else { // there was an error
6279
errorCode = USI_TWI_Get_State_Info(); // this function returns the error number
6380
return errorCode;
@@ -73,19 +90,26 @@ uint8_t USI_TWI::requestFrom(uint8_t slaveAddr, uint8_t numBytes){ // setup for
7390
USI_Buf[0] = (slaveAddr<<TWI_ADR_BITS) | USI_RCVE; // setup address & Rcve bit
7491
xferOK = USI_TWI_Start_Read_Write(USI_Buf,numBytes); // core func that does the work
7592
// USI_Buf now holds the data read
76-
if (xferOK) return 0;
93+
if (xferOK) {
94+
errorCode = USI_TWI_Master_Stop();
95+
if (errorCode == 0) {
96+
errorCode = USI_TWI_Get_State_Info();
97+
return errorCode;
98+
}
99+
return 0;
100+
}
77101
else { // there was an error
78102
errorCode = USI_TWI_Get_State_Info(); // this function returns the error number
79103
return errorCode;
80104
}
81105
}
82106

83-
uint8_t USI_TWI::receive(){ // returns the bytes received one at a time
107+
int USI_TWI::read(){ // returns the bytes received one at a time
84108
USI_LastRead++; // inc first since first uint8_t read is in USI_Buf[1]
85109
return USI_Buf[USI_LastRead];
86110
}
87111

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

digistump-avr/libraries/TinyWireM/TinyWireM.h

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@
3838
#define TinyWireM_h
3939

4040
#include <inttypes.h>
41+
#include "Arduino.h"
4142
#define USI_SEND 0 // indicates sending to TWI
4243
#define USI_RCVE 1 // indicates receiving from TWI
43-
#define USI_BUF_SIZE 16 // bytes in message buffer
44+
#define USI_BUF_SIZE 18 // bytes in message buffer
4445

46+
//class USI_TWI : public Stream
4547
class USI_TWI
4648
{
4749
private:
@@ -51,14 +53,30 @@ class USI_TWI
5153
static uint8_t USI_BytesAvail; // number of bytes requested but not read
5254

5355
public:
54-
USI_TWI();
55-
void begin();
56-
void beginTransmission(uint8_t);
57-
void send(uint8_t);
56+
USI_TWI();
57+
void begin();
58+
void beginTransmission(uint8_t);
59+
size_t write(uint8_t);
60+
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; }
61+
inline size_t write(unsigned long n) { return write((uint8_t)n); }
62+
inline size_t write(long n) { return write((uint8_t)n); }
63+
inline size_t write(unsigned int n) { return write((uint8_t)n); }
64+
inline size_t write(int n) { return write((uint8_t)n); }
65+
void send(uint8_t b) { write(b); }
66+
void send(uint8_t *d, uint8_t n) { write(d, n); }
67+
void send(int n) { write((uint8_t)n); }
5868
uint8_t endTransmission();
69+
uint8_t endTransmission(uint8_t);
5970
uint8_t requestFrom(uint8_t, uint8_t);
60-
uint8_t receive();
61-
uint8_t available();
71+
int read();
72+
int available();
73+
int peek(void);
74+
void flush(void);
75+
uint8_t receive(void) {
76+
int c = read();
77+
if (c < 0) return 0;
78+
return c;
79+
}
6280
};
6381

6482
extern USI_TWI TinyWireM;

digistump-avr/libraries/TinyWireM/USI_TWI_Master.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,12 @@
2020
* length should be these two bytes plus the number of bytes to read.
2121
****************************************************************************/
2222
#include <avr/interrupt.h>
23-
#if defined (__AVR_ATtiny45__) || defined (__AVR_ATtiny85__)
24-
#define F_CPU 16500000UL
25-
26-
#elif defined (__AVR_ATtiny87__) || defined (__AVR_ATtiny167__)
27-
#define F_CPU 16000000UL // Sets up the default speed for delay.h
28-
#endif
29-
3023
#include <util/delay.h>
3124
#include <avr/io.h>
3225
#include "USI_TWI_Master.h"
3326

3427
unsigned char USI_TWI_Start_Transceiver_With_Data( unsigned char * , unsigned char );
3528
unsigned char USI_TWI_Master_Transfer( unsigned char );
36-
unsigned char USI_TWI_Master_Stop( void );
3729
unsigned char USI_TWI_Master_Start( void );
3830

3931
union USI_TWI_state
@@ -254,11 +246,8 @@ unsigned char USI_TWI_Start_Transceiver_With_Data( unsigned char *msg, unsigned
254246
USI_TWI_Master_Transfer( tempUSISR_1bit ); // Generate ACK/NACK.
255247
}
256248
}while( --msgSize) ; // Until all data sent/received.
257-
258-
if (!USI_TWI_Master_Stop())
259-
{
260-
return (FALSE); // Send a STOP condition on the TWI bus.
261-
}
249+
250+
// usually a stop condition is sent here, but TinyWireM needs to choose whether or not to send it
262251

263252
/* Transmission successfully completed*/
264253
return (TRUE);

digistump-avr/libraries/TinyWireM/USI_TWI_Master.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@
2121
//********** Defines **********//
2222

2323
// Defines controlling timing limits - SCL <= 100KHz.
24-
#if defined (__AVR_ATtiny45__) || defined (__AVR_ATtiny85__)
25-
#define SYS_CLK 16500.0 // [kHz] Default for ATtiny2313
26-
27-
#elif defined (__AVR_ATtiny87__) || defined (__AVR_ATtiny167__)
28-
#define SYS_CLK 16000.0 // [kHz] Default for ATtiny2313
29-
#endif
30-
31-
3224

3325
// For use with _delay_us()
3426
#define T2_TWI 5 // >4,7us
@@ -88,6 +80,16 @@
8880
#define PIN_USI_SCL PINB2
8981
#endif
9082

83+
#if defined(__AVR_ATtiny84__) | defined(__AVR_ATtiny44__)
84+
# define DDR_USI DDRA
85+
# define PORT_USI PORTA
86+
# define PIN_USI PINA
87+
# define PORT_USI_SDA PORTA6
88+
# define PORT_USI_SCL PORTA4
89+
# define PIN_USI_SDA PINA6
90+
# define PIN_USI_SCL PINA4
91+
#endif
92+
9193
#if defined(__AVR_AT90Tiny2313__) | defined(__AVR_ATtiny2313__)
9294
#define DDR_USI DDRB
9395
#define PORT_USI PORTB
@@ -119,4 +121,5 @@
119121
void USI_TWI_Master_Initialise( void );
120122
unsigned char USI_TWI_Start_Random_Read( unsigned char * , unsigned char );
121123
unsigned char USI_TWI_Start_Read_Write( unsigned char * , unsigned char );
124+
unsigned char USI_TWI_Master_Stop( void );
122125
unsigned char USI_TWI_Get_State_Info( void );

digistump-avr/libraries/TinyWireM/keywords.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ begin KEYWORD2
1414
beginTransmission KEYWORD2
1515
endTransmission KEYWORD2
1616
requestFrom KEYWORD2
17-
send KEYWORD2
18-
receive KEYWORD2
17+
write KEYWORD2
18+
read KEYWORD2
1919

2020
#######################################
2121
# Instances (KEYWORD2)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=TinyWireM
2+
version=1.0.0
3+
author=Adafruit
4+
maintainer=Adafruit <[email protected]>
5+
sentence=I2C library for Trinket and Gemma, adapted from BroHogan's code on Arduino Playground
6+
paragraph=I2C library for Trinket and Gemma, adapted from BroHogan's code on Arduino Playground
7+
category=Signal Input/Output
8+
url=https://github.com/adafruit/TinyWireM
9+
architectures=*

0 commit comments

Comments
 (0)