Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arduino as slave with multiple i2c addresses

I would like to use an Arduino as an i2c slave. But I require that the Arduino acts as multiple devices by registering itself with multiple i2c addresses.

This is probably not something one would normally do, but here is my reason for doing it:

I want to use an Arduino to act as Telemetry sensors for Spektrum Telemetry. The Telemetry receiver has a few i2c plugs which connects to multiple sensors (current 0x02, voltage 0x03, airspeed 0x11, etc) each that have a fixed i2c address which the Telemetry receiver expects.

I would like to use one Arduino to act as all these devices by registering itself with all of the above addresses, and responding appropriately with the readings.

I could use one Arduino per sensor, which seems silly as I can perform all these readings with one Arduino pro-mini.

I know you can register the Arduino using

Wire.begin(0x02); 

But I require something similar to this (pseudo code)

Wire.begin(0x02, 0x03, 0x11);

And when a request is received, I need to know with what address the Arduino was queried.

For example (pseudo code)

void receiveEvent(byte address, int bytesReceived){
  if(address == 0x02){
    // Current reading
  }
  else if(address == 0x03){
    // Voltage reading
  }
  else if(address == 0x11){
    // Airspeed reading
  }
}

Any advice would be appreciated.

like image 266
trojanc Avatar asked Jan 09 '16 08:01

trojanc


3 Answers

It is not possible to make the Arduino listen to to multiple slave addresses by using the Wire library since Wire.begin() only allows to pass a single slave address.


Even the Atmel ATmega microcontroller on which most Arduinos are based only allows its hardware 2-wire serial interface (TWI) to be set to a single 7-bit address via its 2-wire address register TWAR. However, it is possible to work around this limitation by masking one or more address bits using the TWI address mask register TWAMR as documented (somewhat briefly) in e.g. this ATmega datasheet section 22.9.6:

The TWAMR can be loaded with a 7-bit Salve (sic!) Address mask. Each of the bits in TWAMR can mask (disable) the corresponding address bits in the TWI address Register (TWAR). If the mask bit is set to one then the address match logic ignores the compare between the incoming address bit and the corresponding bit in TWAR.

So we would first have to set up the mask bits based on all I2C addresses we want to respond to by OR'ing them and shifting right to match the TWAMR register layout (TWAMR holds mask in bit7:1, bit0 is unused):

TWAMR = (sensor1_addr | sensor2_addr | sensor3_addr) << 1;

The main problem from here on will be to find out which particular I2C address was queried (we only know it was one that matches the address mask). If I interpret section 22.5.3 correctly, stating

The TWDR contains the address or data bytes to be transmitted, or the address or data bytes received.

we should be able to retrieve the unmasked I2C address from the TWDR register.

ATmega TWI operation is interrupt-based, more specifically, it utilizes a single interrupt vector for a plethora of different TWI events indicated by status codes in the TWSR status register. In the TWI interrupt service routine, we'll have to

  1. make sure the reason why we've entered the ISR is because we've been queried. This can be done by checking TWSR for status code 0xA8 (own SLA+R has been received)
  2. decide which sensor data to send back to the master based on what I2C address was actually queried by checking the last byte on the bus in TWDR.

This part of the ISR could look something like this (untested):

if (TWSR == 0xA8) { // read request has been received
  byte i2c_addr = TWDR >> 1; // retrieve address from last byte on the bus
  switch (i2c_addr) {
    case sensor1_addr:
      // send sensor 1 reading
      break;
    case sensor2_addr:
      // send sensor 2 reading
      break;
    case sensor3_addr:
      // send sensor 3 reading
      break;
    default:
      // I2C address does not match any of our sensors', ignore.
      break;
  }
}

Thanks for asking this interesting question!

like image 90
vega8 Avatar answered Nov 08 '22 00:11

vega8


I really do like vega8's answer, but I'd also like to mention that if your I2C master isn't going to clock things incredibly fast, using a software-based implementation of I2C would also be feasible and give you the freedom you want.

You might want to consider that approach if rough calculation shows that the time spent in the TWI ISR is too high and interrupts might start to overlap.

like image 4
Marcus Müller Avatar answered Nov 08 '22 01:11

Marcus Müller


void setup() 
{
    Wire.begin(0x11 | 0x12);       // Adr 11 and 12 are used for Alt and Speed by Spectrum DX
    Wire.onRequest(requestEvent);  // register callback function
    TWAMR = (0x11 | 0x12) << 1;    // set filter for given adr
}
like image 1
user7028160 Avatar answered Nov 08 '22 00:11

user7028160