Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ATTiny1634 #3

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# STOP - IF USING MY CORE, DO NOT USE THIS LIBRARY #
## MY CORE NOW HAS UNIVERSAL Wire.h LIBRARY. Just use Wire.h and unmodified I2C device libraries ##

TinyWireM
=========

Expand Down
2 changes: 1 addition & 1 deletion TinyWireM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ size_t USI_TWI::write(uint8_t data){ // buffers up data to send
}

uint8_t USI_TWI::endTransmission() {
endTransmission(1);
return endTransmission(1);
}

uint8_t USI_TWI::endTransmission(uint8_t stop){ // actually sends the buffer
Expand Down
2 changes: 1 addition & 1 deletion TinyWireM.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include "Arduino.h"
#define USI_SEND 0 // indicates sending to TWI
#define USI_RCVE 1 // indicates receiving from TWI
#define USI_BUF_SIZE 18 // bytes in message buffer
#define USI_BUF_SIZE 19 // bytes in message buffer

//class USI_TWI : public Stream
class USI_TWI
Expand Down
18 changes: 9 additions & 9 deletions USI_TWI_Master.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ union USI_TWI_state
void USI_TWI_Master_Initialise( void )
{
PORT_USI |= (1<<PIN_USI_SDA); // Enable pullup on SDA, to set high as released state.
PORT_USI |= (1<<PIN_USI_SCL); // Enable pullup on SCL, to set high as released state.
PORT_USI_CL |= (1<<PIN_USI_SCL); // Enable pullup on SCL, to set high as released state.

DDR_USI |= (1<<PIN_USI_SCL); // Enable SCL as output.
DDR_USI_CL |= (1<<PIN_USI_SCL); // Enable SCL as output.
DDR_USI |= (1<<PIN_USI_SDA); // Enable SDA as output.

USIDR = 0xFF; // Preload dataregister with "released level" data.
Expand Down Expand Up @@ -193,7 +193,7 @@ unsigned char USI_TWI_Start_Transceiver_With_Data( unsigned char *msg, unsigned
if (USI_TWI_state.addressMode || USI_TWI_state.masterWriteDataMode)
{
/* Write a byte */
PORT_USI &= ~(1<<PIN_USI_SCL); // Pull SCL LOW.
PORT_USI_CL &= ~(1<<PIN_USI_SCL); // Pull SCL LOW.
USIDR = *(msg++); // Setup data.
USI_TWI_Master_Transfer( tempUSISR_8bit ); // Send 8 bits on bus.

Expand Down Expand Up @@ -270,7 +270,7 @@ unsigned char USI_TWI_Master_Transfer( unsigned char temp )
{
_delay_us(T2_TWI);
USICR = temp; // Generate positve SCL edge.
while( !(PIN_USI & (1<<PIN_USI_SCL)) );// Wait for SCL to go high.
while( !(PIN_USI_CL & (1<<PIN_USI_SCL)) );// Wait for SCL to go high.
_delay_us(T4_TWI);
USICR = temp; // Generate negative SCL edge.
}while( !(USISR & (1<<USIOIF)) ); // Check for transfer complete.
Expand All @@ -288,14 +288,14 @@ unsigned char USI_TWI_Master_Transfer( unsigned char temp )
unsigned char USI_TWI_Master_Start( void )
{
/* Release SCL to ensure that (repeated) Start can be performed */
PORT_USI |= (1<<PIN_USI_SCL); // Release SCL.
while( !(PORT_USI & (1<<PIN_USI_SCL)) ); // Verify that SCL becomes high.
PORT_USI_CL |= (1<<PIN_USI_SCL); // Release SCL.
while( !(PORT_USI_CL & (1<<PIN_USI_SCL)) ); // Verify that SCL becomes high.
_delay_us(T2_TWI);

/* Generate Start Condition */
PORT_USI &= ~(1<<PIN_USI_SDA); // Force SDA LOW.
_delay_us(T4_TWI);
PORT_USI &= ~(1<<PIN_USI_SCL); // Pull SCL LOW.
PORT_USI_CL &= ~(1<<PIN_USI_SCL); // Pull SCL LOW.
PORT_USI |= (1<<PIN_USI_SDA); // Release SDA.

#ifdef SIGNAL_VERIFY
Expand All @@ -314,8 +314,8 @@ unsigned char USI_TWI_Master_Start( void )
unsigned char USI_TWI_Master_Stop( void )
{
PORT_USI &= ~(1<<PIN_USI_SDA); // Pull SDA low.
PORT_USI |= (1<<PIN_USI_SCL); // Release SCL.
while( !(PIN_USI & (1<<PIN_USI_SCL)) ); // Wait for SCL to go high.
PORT_USI_CL |= (1<<PIN_USI_SCL); // Release SCL.
while( !(PIN_USI_CL & (1<<PIN_USI_SCL)) ); // Wait for SCL to go high.
_delay_us(T4_TWI);
PORT_USI |= (1<<PIN_USI_SDA); // Release SDA.
_delay_us(T2_TWI);
Expand Down
31 changes: 31 additions & 0 deletions USI_TWI_Master.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@
# define PIN_USI_SCL PINA4
#endif

#if defined(__AVR_ATtiny167__) | defined(__AVR_ATtiny87__)
# define DDR_USI DDRB
# define PORT_USI PORTB
# define PIN_USI PINB
# define PORT_USI_SDA PORTB0
# define PORT_USI_SCL PORTB2
# define PIN_USI_SDA PINB0
# define PIN_USI_SCL PINB2


#endif

#if defined(__AVR_ATtiny1634__)
#define DDR_USI DDRB
#define PORT_USI PORTB
#define PIN_USI PINB
#define DDR_USI_CL DDRC
#define PORT_USI_CL PORTC
#define PIN_USI_CL PINC
#define PORT_USI_SDA PORTB1
#define PORT_USI_SCL PORTC1
#define PIN_USI_SDA PINB1
#define PIN_USI_SCL PINC1
#endif

#if defined(__AVR_AT90Tiny2313__) | defined(__AVR_ATtiny2313__)
#define DDR_USI DDRB
#define PORT_USI PORTB
Expand All @@ -115,6 +140,12 @@
#define TRUE 1
#define FALSE 0

#ifndef DDR_USI_CL
#define DDR_USI_CL DDR_USI
#define PORT_USI_CL PORT_USI
# define PIN_USI_CL PIN_USI
#endif

//********** Prototypes **********//

void USI_TWI_Master_Initialise( void );
Expand Down
36 changes: 36 additions & 0 deletions examples/AT24_EEPROM/AT24_EEPROM.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <TinyWireM.h>
//Demo of reading AT24 EEPROM, and logging the results to serial. Reads the first 256 bytes.
//#define DoWrite //uncomment to write consecutive integers to first 256 bytes while sketch runs
//comment out Serial statements for boards without hardware serial.


byte i=0;


void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //comment out if you're using a chip w/out serial.
TinyWireM.begin(); // initialize I2C lib
Serial.println("Tinywire begun"); //comment out if you're using a chip w/out serial.
}

void loop() {
// put your main code here, to run repeatedly:

#ifdef DoWrite
TinyWireM.beginTransmission(0x50);
TinyWireM.send(0x00);
TinyWireM.send(i);
TinyWireM.send(i);
TinyWireM.endTransmission(); // Send to the slave
delay(500);
#endif
TinyWireM.beginTransmission(0x50);
TinyWireM.send(0x00);
TinyWireM.send(i);
TinyWireM.endTransmission();
TinyWireM.requestFrom(0x50,1);
Serial.println(TinyWireM.receive()); //comment out if you're using a chip w/out serial.
delay(500);
i++;
}