mirror of
https://github.com/digistump/DigistumpArduino.git
synced 2025-04-27 23:29:01 -07:00
updated to latest adafruit version
This commit is contained in:
parent
68ad726f9b
commit
4e2f6ffa1c
8
digistump-avr/libraries/TinyWireM/README.md
Normal file
8
digistump-avr/libraries/TinyWireM/README.md
Normal file
@ -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).
|
@ -37,6 +37,9 @@ USI_TWI::USI_TWI(){
|
|||||||
|
|
||||||
// Public Methods //////////////////////////////////////////////////////////////
|
// Public Methods //////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
//int USI_TWI::peek(){}
|
||||||
|
//void USI_TWI::flush(){}
|
||||||
|
|
||||||
void USI_TWI::begin(){ // initialize I2C lib
|
void USI_TWI::begin(){ // initialize I2C lib
|
||||||
USI_TWI_Master_Initialise();
|
USI_TWI_Master_Initialise();
|
||||||
}
|
}
|
||||||
@ -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;
|
USI_Buf[USI_BufIdx] = (slaveAddr<<TWI_ADR_BITS) | USI_SEND;
|
||||||
}
|
}
|
||||||
|
|
||||||
void USI_TWI::send(uint8_t data){ // buffers up data to send
|
size_t USI_TWI::write(uint8_t data){ // buffers up data to send
|
||||||
if (USI_BufIdx >= USI_BUF_SIZE) return; // dont blow out the buffer
|
if (USI_BufIdx >= USI_BUF_SIZE) return 0; // dont blow out the buffer
|
||||||
USI_BufIdx++; // inc for next byte in buffer
|
USI_BufIdx++; // inc for next byte in buffer
|
||||||
USI_Buf[USI_BufIdx] = data;
|
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;
|
bool xferOK = false;
|
||||||
uint8_t errorCode = 0;
|
uint8_t errorCode = 0;
|
||||||
xferOK = USI_TWI_Start_Read_Write(USI_Buf,USI_BufIdx+1); // core func that does the work
|
xferOK = USI_TWI_Start_Read_Write(USI_Buf,USI_BufIdx+1); // core func that does the work
|
||||||
USI_BufIdx = 0;
|
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
|
else { // there was an error
|
||||||
errorCode = USI_TWI_Get_State_Info(); // this function returns the error number
|
errorCode = USI_TWI_Get_State_Info(); // this function returns the error number
|
||||||
return errorCode;
|
return errorCode;
|
||||||
@ -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
|
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
|
xferOK = USI_TWI_Start_Read_Write(USI_Buf,numBytes); // core func that does the work
|
||||||
// USI_Buf now holds the data read
|
// 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
|
else { // there was an error
|
||||||
errorCode = USI_TWI_Get_State_Info(); // this function returns the error number
|
errorCode = USI_TWI_Get_State_Info(); // this function returns the error number
|
||||||
return errorCode;
|
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]
|
USI_LastRead++; // inc first since first uint8_t read is in USI_Buf[1]
|
||||||
return USI_Buf[USI_LastRead];
|
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);
|
return USI_BytesAvail - (USI_LastRead);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,10 +38,12 @@
|
|||||||
#define TinyWireM_h
|
#define TinyWireM_h
|
||||||
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include "Arduino.h"
|
||||||
#define USI_SEND 0 // indicates sending to TWI
|
#define USI_SEND 0 // indicates sending to TWI
|
||||||
#define USI_RCVE 1 // indicates receiving from 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
|
class USI_TWI
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
@ -54,11 +56,27 @@ class USI_TWI
|
|||||||
USI_TWI();
|
USI_TWI();
|
||||||
void begin();
|
void begin();
|
||||||
void beginTransmission(uint8_t);
|
void beginTransmission(uint8_t);
|
||||||
void send(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 endTransmission(uint8_t);
|
||||||
uint8_t requestFrom(uint8_t, uint8_t);
|
uint8_t requestFrom(uint8_t, uint8_t);
|
||||||
uint8_t receive();
|
int read();
|
||||||
uint8_t available();
|
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;
|
extern USI_TWI TinyWireM;
|
||||||
|
@ -20,20 +20,12 @@
|
|||||||
* length should be these two bytes plus the number of bytes to read.
|
* length should be these two bytes plus the number of bytes to read.
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
#include <avr/interrupt.h>
|
#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 <util/delay.h>
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include "USI_TWI_Master.h"
|
#include "USI_TWI_Master.h"
|
||||||
|
|
||||||
unsigned char USI_TWI_Start_Transceiver_With_Data( unsigned char * , unsigned char );
|
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_Transfer( unsigned char );
|
||||||
unsigned char USI_TWI_Master_Stop( void );
|
|
||||||
unsigned char USI_TWI_Master_Start( void );
|
unsigned char USI_TWI_Master_Start( void );
|
||||||
|
|
||||||
union USI_TWI_state
|
union USI_TWI_state
|
||||||
@ -255,10 +247,7 @@ unsigned char USI_TWI_Start_Transceiver_With_Data( unsigned char *msg, unsigned
|
|||||||
}
|
}
|
||||||
}while( --msgSize) ; // Until all data sent/received.
|
}while( --msgSize) ; // Until all data sent/received.
|
||||||
|
|
||||||
if (!USI_TWI_Master_Stop())
|
// usually a stop condition is sent here, but TinyWireM needs to choose whether or not to send it
|
||||||
{
|
|
||||||
return (FALSE); // Send a STOP condition on the TWI bus.
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Transmission successfully completed*/
|
/* Transmission successfully completed*/
|
||||||
return (TRUE);
|
return (TRUE);
|
||||||
|
@ -21,14 +21,6 @@
|
|||||||
//********** Defines **********//
|
//********** Defines **********//
|
||||||
|
|
||||||
// Defines controlling timing limits - SCL <= 100KHz.
|
// 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()
|
// For use with _delay_us()
|
||||||
#define T2_TWI 5 // >4,7us
|
#define T2_TWI 5 // >4,7us
|
||||||
@ -88,6 +80,16 @@
|
|||||||
#define PIN_USI_SCL PINB2
|
#define PIN_USI_SCL PINB2
|
||||||
#endif
|
#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__)
|
#if defined(__AVR_AT90Tiny2313__) | defined(__AVR_ATtiny2313__)
|
||||||
#define DDR_USI DDRB
|
#define DDR_USI DDRB
|
||||||
#define PORT_USI PORTB
|
#define PORT_USI PORTB
|
||||||
@ -119,4 +121,5 @@
|
|||||||
void USI_TWI_Master_Initialise( void );
|
void USI_TWI_Master_Initialise( void );
|
||||||
unsigned char USI_TWI_Start_Random_Read( unsigned char * , unsigned char );
|
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_Start_Read_Write( unsigned char * , unsigned char );
|
||||||
|
unsigned char USI_TWI_Master_Stop( void );
|
||||||
unsigned char USI_TWI_Get_State_Info( void );
|
unsigned char USI_TWI_Get_State_Info( void );
|
||||||
|
@ -14,8 +14,8 @@ begin KEYWORD2
|
|||||||
beginTransmission KEYWORD2
|
beginTransmission KEYWORD2
|
||||||
endTransmission KEYWORD2
|
endTransmission KEYWORD2
|
||||||
requestFrom KEYWORD2
|
requestFrom KEYWORD2
|
||||||
send KEYWORD2
|
write KEYWORD2
|
||||||
receive KEYWORD2
|
read KEYWORD2
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
# Instances (KEYWORD2)
|
# Instances (KEYWORD2)
|
||||||
|
9
digistump-avr/libraries/TinyWireM/library.properties
Normal file
9
digistump-avr/libraries/TinyWireM/library.properties
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
name=TinyWireM
|
||||||
|
version=1.0.0
|
||||||
|
author=Adafruit
|
||||||
|
maintainer=Adafruit <info@adafruit.com>
|
||||||
|
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=*
|
Loading…
x
Reference in New Issue
Block a user