I2C software implementation for Arduino

Hi guys,
Can anyone help me find a software imlementation of I2C slave for arduino? I need one like this Stepper-Motor-Controller/usi_i2c_slave.c at master · CalcProgrammer1/Stepper-Motor-Controller · GitHub which is for ATtiny and for the slave it checks the address sent by the master first to decide if the slave should answer or no. The wire library doesn't offer that functionnality so I need to know if there's another library independent of the Atmega I2C hardware for arduino that can act this way.
Many thanks...

Start with Nick Gammon's page on I2C

Thank you for the link! It's very instructive.. However, I'm gonna need a library that doesn't rely on the I2C hardware of the arduino ATmega. I want to manipulate the check of the slave address sent by the master when initiating the communication.. I even wonder if that is possible on an Arduino board.

If you don't rely on the hardware of the arduino then you need to hardware - the devices all need open drain drivers. The Master controls the clock, sends out data, the slave must acknowledge that data using the Master's clock, and then return data, also using the master's clock.
I suppose you could do a software version where you switch a pin from input to output a lot, and do it in sync with incoming SCL. Or use one input pin and one output and put an open drain buffer on the output pin so it could only pull low.
Try looking at Software Serial library - you'd need the same kind of code. I2C is generally 100K or 400K, could be tough to implement those kinds of speeds.

I wonder how that library takes of getting off the bus when receiving. Don't have time to dig into its code tho, especially if written in assembler.

Can anyone help me find a software imlementation of I2C slave for arduino? I need one like this Stepper-Motor-Controller/UnipolarStepperDriver/usi_i2c_slave.c at master · CalcProgrammer1/Stepper-Motor-Controller · GitHub which is for ATtiny and for the slave it checks the address sent by the master first to decide if the slave should answer or no. The wire library doesn't offer that functionnality so I need to know if there's another library independent of the Atmega I2C hardware for arduino that can act this way.

Why don't you just use an ATtiny (there are other tinys than the 8-pin ATtiny85)?
Your problem is so special that probably no one else had to solve it before. You either have to write that library yourself or change the hardware. I don't think that a software implementation is that hard as CrossRoads seems to think but it's quite a lot of work. I would not use the SoftwareSerial library as an example because there the author switches off interrupts for longer times, stopping all activity for the duration of the transmission (or reception). You could use the TinyWireS library as an example of how to implement it, just use the external interrupt (pins 2 or 3 on the UNO) to get an interrupt for every clock signal the master sends, the take the state of the data signal and act accordingly. If coded correctly you could even live without external hardware because you can switch between input and output/LOW directly. Just be cautious and test with another Arduino first, otherwise you may blow your I2C master device with a faulty implementation.

Another possibility is to use for example an ATtiny1634 which offers the USI hardware as well as a UART. So you can have the ATtiny be the I2C slave which then talks to an Arduino (or PC) over the serial interface.

Thank you all :slight_smile:
I also think that using an ATtiny would be simpler than developping a library especially that I don't have much time to achieve my work. I was trying to find a solution with arduino because it's more efficient and easy to use when it comes to connectivity things (ethernet, usb ..).
As a first step I'm gonna use an ATtiny and we'll see how it goes.

Do you please know any commercial ATtiny based board? All I can find are standalone ATtiny processors or Tutos on how to make the borad yourself.. An arduino-like board with an ATtiny processor would be great !

As you didn't specify which processor you're looking for: http://imall.iteadstudio.com/im130615003.html

Another, currently non-commercial, project (forum is in German): An Error Has Occurred!

The ITeaduino Tiny seems to be good.
Gracias

Finally, I couldn't use an ATtiny as it wasn't as powerful as my simulation needs it to be. I should be working on high frequencies.
So I will end up developping some bit banging I2C slave driver for Arduino or other since it will be hardware independant.
Can anyone tell me from where to start and if there are some examples or samples to begin with? I found only bit banging drivers for masters or for slaves written in asm.. I should write sth in C.

Finally, I couldn't use an ATtiny as it wasn't as powerful as my simulation needs it to be. I should be working on high frequencies.

The ATtiny family works with the same frequencies as the ATmega family of microcontrollers. Please define what kind of power you're missing.

So I will end up developping some bit banging I2C slave driver for Arduino or other since it will be hardware independant.

I see much more risks that this solution isn't powerful enough than using an ATtiny.

Can anyone tell me from where to start and if there are some examples or samples to begin with? I found only bit banging drivers for masters or for slaves written in asm.. I should write sth in C.

Some posts ago I gave you points to start with. You won't find finished code, you'll only find code that shows you how to use the hardware features you will need.

The ATtiny family works with the same frequencies as the ATmega family of microcontrollers.

You're absolutely right! Actually I'd like to develop a bit banged I2C slave driver (hardware independant) to use it on a processor that have a higher frequency than 500 MHz. I haven't defined that processor yet.

you'll only find code that shows you how to use the hardware features you will need.

Yes that's what I need :slight_smile: I never worked on a microcontroller before so developping a driver seems tough to me..
Thanks I'll re-check your previous answers :slight_smile:

Yes that's what I need smiley I never worked on a microcontroller before so developping a driver seems tough to me..
Thanks I'll re-check your previous answers

My previous post was related to an ATmega processor. The information therein is directly usable on a completely different platform. I don't see why you will need a 500MHz processor for an I2C bus which is specified to a maximum of 400kHz.

Hi again,
I found a solution to my problem so I'd like to share it if s.o else need it :slight_smile:
I found a software implementation of the TWI module of arduino in the atmel avr site. This driver emulates the hardware TWI module of ATmega. It uses two pins for SDA and SCL that you can modifiy and the control over slave address transmitted by the master is transparent. This driver allows using I2C on low cost devices that doesn't have TWI neither USI modules or on sophisticated microcontrollers when you need to put your hand over details, control everything and be hardware independant (which is my case).
Here is the link to the application note Atmel AVR154: Software Emulation of TWI Slave Hardware Module http://www.atmel.com/Images/doc8478.pdf
The soft is available here http://www.atmel.com/devices/atxmega64a1u.aspx?tab=documents.