Electronic – arduino – ATTiny25/45/85 as I2C master AND slave

arduinoattinyi2c

I'm still trying to wrap my head around I2C as well as the ATTiny. I've found plenty of tutorials on how to use the tiny as an I2C slave as well as master, but is it possible to use it as both at the same time?

I basically want to create a device similar to the BlinkM smart LED but that also had a connected EEPROM for some extra storage. Maybe there's another way to connect the EEPROM but I didn't figure I'd have enough pins (only talking about the 8 pin ATTiny variety)….

Any thoughts of how to do this?

Clarification: Note, that ONLY the ATTiny needs to access the external EEPROM or should access it. I definitely want it to be on a separate bus. So I realize that I will need two pins for ATMega -> ATTiny and 2 more for ATTiny to EEPROM (hopefully leaving me 2). My major concern here is if only 2 pins on the ATTiny can be used for i2c or if you can configure ANY 2 pins for that?

Best Answer

Toby says you'll need two I2C buses, but that's not necessarily so. In this answer I point at the information in the LPC930 datasheet how to switch between the two modes on the same bus.

I guess by "at the same time" you mean in one and the same application, and that's not so hard. If you want to write or read on the bus as master you wait until the bus is free, switch to master mode and start clocking. After the message is sent/received switch back to slave mode and listen for messages from other masters.

Electrically collisions are not a problem, since the bus is a wired-OR. Protocol-wise you may have a problem when two masters would claim the bus simultaneously, but then the message will be so messed up that you won't get an acknowledge, so you know you have to send it again. Most protocols will retry after a random time, which should be different for each master, so that the collisions don't keep repeating.

Alternatively, you can make a token-passing protocol of it, where the controller which has the token is master, and after doing its business, can pass the token to the other master. It then switches to slave mode, and waits until it receives the token back. Only then it can switch to master mode again.