I’m trying to understand the wire library code in order to modify it for my project
Unfortunately, I couldn’t get what many variables do in the twi.c file such as : TWAR, TWCR, TWDR, twi_slarw…
I want to connect my arduino to a master using I2C and each time the master asks a slave (using the slave address), the arduino which sees the data flow, extracts the address (that the master is asking) and assign it to itself.
To do that, I need first to understand how the wire lib works and particuarly how does the arduino responds to an I2C master? I mean if I Have many slave Arduinos connected to an I2C master, when the master asks an address how does the concerned arduino know that it has to answer.
There should be a test somewhere to compare the arduino address with the address that the master sent. But I can’t find where is that coded in the wire library.
Any help (suggestion, documentation, anything ) would be appreciated.
You will have to be more precise, i can´t understand what you want to do and why.
When a master (which device, doing what ???) tries to talk to a slave (which arduino you use ???) via I²C the slave address has to be known before the transmission. You say that you want to listen for incoming data but you don´t know the slave address and it´s changing every transmit ???
I guess you explained yourself wrong, normally you don´t need to change the variables used by the twi class.
Ok I'll explain, I want to connect a Raspberry Pi (master) to Arduino (slave) using I2C. Each period of time (let's say 1 second), the RPi sends data to an address. The arduino which is connected to the bus listens to the data flow. According to the I2C protocol , the RPi sends first the slave address and waits for an acknowledgement. At this step, I want that the arduino reads the address and assign that address to itself. I should be using some Twi functions to that. I need to do that for simulation purpose. I want to simulate many sensors with my arduino so it has to change its address each time according to the RPi demand. It is obvious that the master should know the address of the slave before transmitting. What I'd like to do is that, when the arduino checks if the address transmitted by the RPi is the same as his or not, it affects that address to him instead and become able to communicate to the RPi. I hope I made myself clear.
I still don´t quite get the sense of doing this. If you have many sensors with fixed addresses, then you should use them directly or use your arduino to speak to them. In the latter case your arduino has to be master and slave.
I´m not saying what you want to do is not possible but is it worth the effort ? What exactly is the advantage of simulating sensors you don´t have ?
What you want to do is not possible using the ATmega328p hardware I2C. If you want to do this, use a software implementation of the I2C protocol (p.e. in the playground: http://playground.arduino.cc/Main/SoftwareI2CLibrary).
Before you try to change that library get familiar with I2C and all it’s details because otherwise you may break the whole bus.
smithy : First I on't have the sensors, Simulating sensors using arduino is part of a project I'm working and it is not a choice. The arduino should be slave because it will only receive data. pylon : Thank you I already read I2C specifications but I need to get more in details. Can you please explain why the ATmega328p hardware I2C would prevent me from doing what I described above?
Sorry, misread your post.
Can you please explain why the ATmega328p hardware I2C would prevent me from doing what I described above?
Because the TWI hardware in the ATmega328p, in slave mode, checks for the slave address itself, you write the address to react on into the TWAR register. You can use the TWAMR register to mask some or all bits of the address. This way you're able to react on some ranges or even all addresses sent to the bus but you don't get access to the sent address, so you cannot behave differently depending on the address sent. You can use an ATtiny with a USI interface because there the software checks for the address and reacts accordingly, the hardware there is basically only a special shift register.
smithy : no problem thank you anyway :) pylon : that's convincing !! I'll see what I can do with an ATtiny.