Esp8266 i2c problem

Hello guys . Iam confused for months spent trying to read i2c registers from 10 bit device using the esp8266 with wire library and really i need help ... can any one guide me what to do

What is this 10 bit device?

Eeprom memory ... and i used twi on arduino mega to read or write on it

Which EEprom memory.
Can we see your code please.

1 Like

So which is it - ESP or Mega ?

Does it support 10-bit addresses?

Only 7-bit is mentioned:

Now iam using esp ..
And the way i used to work on mega ...

We use 10 bits I2C address but Atmega is only compatible with 7 bits address. That's not a problem.
We just have to trick a little bit because 10 bits addressing is completely fine with the standard I2C physical protocol

Let's say address = [9876543210] (10 bits)

Here is how to write communication to a 10bits addressed slave : (A = ACK)
[START] [0b1111 098W]A [76543210]A [WRITE_1]A [WRITE_2]A ... [WRITE_N]A [STOP]

We begin with the template '1111 0' in order to talk to 10-bits addressed slaves, followed by 2 address bits, and W.
Then the first byte we transmit are the 8 following bits of the address, in order to finish the 10 bits addressing.
Nothing else changes. In order to know if the slave is missing, it's the second ACK that should be checked.

Read communication (NA = NACK)
[START] [0b1111 098W]A [76543210]A [RESTART] [0b1111 098R]A [ANSWER_1]A [ANSWER_2]A ... [ANSWER_N]NA [STOP]
We begin as a standard 10 bits addressed write in order to ask our question to the slave, then we do a restart in order to get the answer

After the restart, only one byte can be placed (with Read Bit at the end) before reading, so we cant put the full 10 bits address. We put only but the begining of the address.
The fully adressed component just before the restart will be the only one to answer.

This setup is working perfect on mega..... but i wonder if wire can do this on esp8266 now

Sure

This is apart from old sketch i used for reading

''''''''''''
bool read_TI046B1_register(uint16_t TenBits_slave_address, uint8_t firstRegisterByte, uint8_t secondRegisterByte, uint8_t thirdRegisterByte, uint8_t * destinationBuffer, uint16_t readSize)
{
uint16_t slave_address_LSB = TenBits_slave_address & 0x0FF; //0076543210 //8 LSB
uint16_t slave_address_MSB = TenBits_slave_address & 0x300; //9800000000 //2 MSB

//Put the MSB bits to the Left :
slave_address_MSB = slave_address_MSB >> 8; //For example, 0x300 becomes 0x003

//7 bits address equivalent for the begining :
uint8_t SevenBits_compat_address = 0x78 | slave_address_MSB; //TWI library will put send those 7 bits followed by read/write bit.

//Preparation of the write buffer : 8LSB of the slave's address, then 3 bytes writes to call a register read.
uint8_t txBuffer[4];
txBuffer[0] = slave_address_LSB;
txBuffer[1] = firstRegisterByte;
txBuffer[2] = secondRegisterByte;
txBuffer[3] = thirdRegisterByte;

//Execution of the 4 bytes write
uint16_t nRet = twi_writeTo(SevenBits_compat_address, txBuffer, 4, 1, false); //4 bytes, wait, and no stop, since we will do a restart in order to read just after
if (nRet == 1)
{
//it seems, reading TWI code, that the stop is sent anyway when there is a NACK. No need to call twi_stop().
Serial.println("Error occured wile trying to write to the slave : TWI class's buffer is too small");
return false;
}

else if (nRet == 2)
{
//it seems, reading TWI code, that the stop is sent anyway when there is a NACK. No need to call twi_stop().
Serial.println("Error occured wile trying to write to the slave : Slave's address not acknowldged (begning only) - is it connected ?");
return false;
}

else if (nRet == 3)
{
//it seems, reading TWI code, that the stop is sent anyway when there is a NACK. No need to call twi_stop().
Serial.println("Error occured wile trying to write to the slave :");
Serial.println("Slaves's begining of address acknowledged but NACK encountered during the following writes - are all of the 10 bits of slave's address correct ? Are the writes value are meaningful to the slave ?");

return false;

}

else if (nRet == 4)
{
//it seems, reading TWI code, that the stop is sent anyway when there is a NACK. No need to call twi_stop().
Serial.println("I2C protocol error occured wile trying to write to the slave. Is the wiring correct ? TWI Initialisation ?");
return false;
}

//else :
nRet = twi_readFrom(SevenBits_compat_address, destinationBuffer, readSize, true); //xxx bytes read with a Stop at the end.
if (nRet == 0x00)
{
//Incomplete reading : twi_stop() already done anyway when there is a NACK on Addr + read.
Serial.println("Error occured wile trying to read from the slave.");
return false;
}

if (nRet != readSize)
{
//Incomplete reading : twi_stop() already done anyway when there is a NACK on Addr + read.
Serial.print("Unable to read all the bytes from the slave : ");
Serial.print(nRet, DEC);
Serial.println(" bytes read");

return false;

}

//else :

Serial.print("uint8_t destinationBuffer[");
Serial.print(readSize);
Serial.print("] = {");
for (uint16_t i=0; i<readSize; i++)
{
char bufferHex[5];
sprintf(bufferHex, "0x%02X", destinationBuffer[i]);
Serial.write(bufferHex);
if (i != readSize-1) Serial.print(", ");
else Serial.print("};");
}
Serial.println(""); //prints a new line

//Uncomment this to print as ASCII.
// It doesn't print very well, prefer using a c computer program, fopen and fwrite, and
// recopy previous results (looking like a C/C++ buffer declaration with the content included)
// Then you can see it with the editor of your choice (wich is, of course, notepad++).
/*Serial.print("destinationBuffer = ");
for (uint16_t i=0; i<readSize; i++)
{
Serial.write(destinationBuffer[i]);
}
Serial.println(""); //prints a new line
*/
return true;
}

`type or paste code here`

Sorry, never used the 8266, only the ESP32

1 Like

Very thanks for your care

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.