TWI / I2C Detecting what slave was addressed

Hi ,

I was wondering if someone could help me , I can see that you can use the TWAMR register to mask of parts of the TWI / I2C address. Which works as expected , however I can't seem to work out how to know what address was referenced...

It's required as a I need one Arduino to act as 4 different slaves , so I was just masking of the last two bits. to give me an address range of 4-7 for example. There just doesn't seem to be any logic that holds the slave address that was referenced.

Am I missing something?

I got the mask address register from this datasheet Smart | Connected | Secure | Microchip Technology

Section : 21.9.6 TWAMR – TWI (Slave) Address Mask Register

To reply to myself it's stored in the TWDR on the first address call.

Currently the Arduino library doesn't do anything with this , so I'll have to modify it slightly.

Below is my quick test to assign it to the received buffer.

    // Slave Receiver
    case TW_SR_SLA_ACK:   // addressed, returned ack
    case TW_SR_GCALL_ACK: // addressed generally, returned ack
    case TW_SR_ARB_LOST_SLA_ACK:   // lost arbitration, returned ack
    case TW_SR_ARB_LOST_GCALL_ACK: // lost arbitration, returned ack
      // enter slave receiver mode
      twi_state = TWI_SRX;
      // indicate that rx buffer can be overwritten and ack
      twi_rxBufferIndex = 0;
      // Change Start
      twi_rxBuffer[twi_rxBufferIndex++] = TWDR; 
      // Change End
      twi_reply(1);
      break;