I2C with attny2313/4313 - Anyone ever succeeded ?

Hi Folks,

I'm trying to use an ATTiny4313 as a slave over I2C. The scope of work is pretty simple: ATTiny4313 will receive 1 byte over I2C, then do something with a bunch of LEDs.

Wire library give me the following error:

"error: 'TWBR' was not declared in this scope" + some other info.

TinyWireS & USIwire libraries did not give any better result.

Has anyone ever been able to do I2C with that chip ?

If so, can I have a sample code and library you've used ??

Thanks

Having a look at Chapter 16 of the datasheet may give some insight.
A little over my head to be honest, but it seems that the registers and a description of what you may want is there?

As google will show you, many people have used I2C on that chip.

TinyWireS & USIwire libraries did not give any better result.

Describe the problems if you want help.

jremington: believe me, I have looked on Google. Almost all of them ended up using the ATTiny85 (even if my search mentioned ATTiny2313)

my problem, is that no matter which library I try, it won't compile. I use their example, and with an ATTiny85, it compiles, but as soon as I select the ATTiny2313 (or ATTiny4313) it won't compile.

as soon as I select the ATTiny2313 (or ATTiny4313) it won't compile

You really do not pay attention, do you? Describe the problems if you want help.

Are you using the correct Arduino core?

Librarie used: TinyWireS

trying with the provided example:attiny85_i2c_slave

When compiling with ATTiny85 selected, it compile no problem. If I select the ATTiny2313, I get tons of error:

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c: In function ‘usiTwiSlaveInit’:

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:369:3: error: ‘DDR_USI’ undeclared (first use in this function)

DDR_USI |= ( 1 << PORT_USI_SCL ) | ( 1 << PORT_USI_SDA );

^

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:369:3: note: each undeclared identifier is reported only once for each function it appears in

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:369:21: error: ‘PORT_USI_SCL’ undeclared (first use in this function)

DDR_USI |= ( 1 << PORT_USI_SCL ) | ( 1 << PORT_USI_SDA );

^

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:369:45: error: ‘PORT_USI_SDA’ undeclared (first use in this function)

DDR_USI |= ( 1 << PORT_USI_SCL ) | ( 1 << PORT_USI_SDA );

^

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:372:3: error: ‘PORT_USI’ undeclared (first use in this function)

PORT_USI |= ( 1 << PORT_USI_SCL );

^

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:395:18: error: ‘USI_START_COND_INT’ undeclared (first use in this function)

USISR = ( 1 << USI_START_COND_INT ) | ( 1 << USIOIF ) | ( 1 << USIPF ) | ( 1 << USIDC );

^

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c: In function ‘USI_START_VECTOR’:

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:487:3: error: ‘DDR_USI’ undeclared (first use in this function)

DDR_USI &= ~( 1 << PORT_USI_SDA );

^

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:487:22: error: ‘PORT_USI_SDA’ undeclared (first use in this function)

DDR_USI &= ~( 1 << PORT_USI_SDA );

^

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:496:10: error: ‘PIN_USI’ undeclared (first use in this function)

( PIN_USI & ( 1 << PIN_USI_SCL ) ) &&

^

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:496:27: error: ‘PIN_USI_SCL’ undeclared (first use in this function)

( PIN_USI & ( 1 << PIN_USI_SCL ) ) &&

^

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:498:30: error: ‘PIN_USI_SDA’ undeclared (first use in this function)

!( ( PIN_USI & ( 1 << PIN_USI_SDA ) ) )

^

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:543:15: error: ‘USI_START_COND_INT’ undeclared (first use in this function)

( 1 << USI_START_COND_INT ) | ( 1 << USIOIF ) |

^

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c: In function ‘USI_OVERFLOW_VECTOR’:

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:183:3: error: ‘DDR_USI’ undeclared (first use in this function)

DDR_USI |= ( 1 << PORT_USI_SDA ); \

^

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:583:9: note: in expansion of macro ‘SET_USI_TO_SEND_ACK’

SET_USI_TO_SEND_ACK( );

^

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:183:21: error: ‘PORT_USI_SDA’ undeclared (first use in this function)

DDR_USI |= ( 1 << PORT_USI_SDA ); \

^

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:583:9: note: in expansion of macro ‘SET_USI_TO_SEND_ACK’

SET_USI_TO_SEND_ACK( );

^

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:186:15: error: ‘USI_START_COND_INT’ undeclared (first use in this function)

( 0 << USI_START_COND_INT ) | \

^

I:\arduino-1.8.1 No Install\libraries\TinyWireS_V2\usiTwiSlave.c:583:9: note: in expansion of macro ‘SET_USI_TO_SEND_ACK’

SET_USI_TO_SEND_ACK( );

^

exit status 1
Error compiling for board ATtiny2313/4313.

Library used:USIwire, with provided example "TestSlave

Chip selected: ATTiny85....it won't compile. I get the following:
In file included from I:\arduino-1.8.1 No Install\libraries\USIWire-master\src\USI_TWI_Master/USI_TWI_Master.h:76:0,

from I:\arduino-1.8.1 No Install\libraries\USIWire-master\src\USIWire.cpp:27:

I:\arduino-1.8.1 No Install\libraries\USIWire-master\src\USI_TWI_Master/usi_io.h:1:1: error: expected unqualified-id before '.' token

../USI_TWI_Slave/usi_io.h

^

I:\arduino-1.8.1 No Install\libraries\USIWire-master\src\USIWire.cpp: In member function 'void USIWire::begin()':

I:\arduino-1.8.1 No Install\libraries\USIWire-master\src\USIWire.cpp:55:29: error: 'USI_TWI_Master_Initialise' was not declared in this scope

USI_TWI_Master_Initialise();

^

exit status 1
Error compiling for board ATtiny25/45/85.

TinyWireS doesn't appear to support the ATTiny4313 yet. Since the 4313 and the 2313 are supposed to be identical, bar the memory sizes, you may be able to get it working with a small tweak to the library.

Edit usiTwiSlave.c:

#if defined( __AVR_ATtiny2313__ )
# define DDR_USI DDRB
...

change to:

#if defined( __AVR_ATtiny2313__ ) | \
    defined( __AVR_ATtiny4313__ )
# define DDR_USI DDRB
...

This should work out of the box (with the Wire.h library) with my ATTinyCore - my core supplies a special version of Wire.h that detects which hardware the selected chip has and chooses from compatible implementations using real I2C hardware, USI, or a full software implementation.

This means you don't have to fiddle with libraries that make use of the Wire library.

DrAzzy: I had an older version and the board weren't registered......

Got the latest version, registered the board and tried a sample sketch

It compiled the first time !!! Looks promising !

I will "breadboard" it and let you know !

Thanks

The solution provided by DrAzzy worked fine !

Thanks !