Trouble setting ESP32 as I2C Slave, Linux Master

nmingott
Posts: 1
Joined: Sun Sep 29, 2024 12:38 am

Trouble setting ESP32 as I2C Slave, Linux Master

Postby nmingott » Sun Sep 29, 2024 1:05 am

Hi,

It is my first question in this forum, i don't know where to ask and i am out of ideas, i hope you can give me a some hints.

Taking from reference an example in the book "Exploring BeagleBone" edition, 2, pg.483, by Derek Molloy, i am making an ESP32
act like an I2C slave, while the master is a RPI3b+.

The code I load into the ESP32 is below. I get the same results with a Dymore ESP32 WROOM-32 and an Heltec wifi kit 32.

. I can see the board from Linux (it has address 0x44)
$> i2cdetect -y -r 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- 44 -- -- -- -- -- -- -- -- -- -- --

. By the program, when I query a register value i should receive back just its address,
But what happens is that OnRequest answers before OnReceive, so it answers
before the data coming from the master has been red.

$> i2cdump -y 1 0x44 b
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
00: 05 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e ..??????????????
10: 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e ????????????????
.....
as you see the first value is 5, but it should instead be zero !
then all other values are delayed by one. second value should be 01,
third 02 and so on.

. I put 1 kOhm resistors pull up to SDA and SCL
. I put the RPi and the ESP32 in common ground
. I'm compiling with the latest ArduinoIDE appimage, 2.3.3
. The boards model i am using in ArduinoIDE are DOIT ESP32 DEVKIT V1 for the WROOM and
Heltec wifi kit 32(v3) for the Heltec board.
. I tried to reduce the speed from 100_000 -> nothing changed.

please let me know if you have any idea

bye
Nicola

====== ESP32 code ========

Code: Select all

#include <Wire.h>  

const byte slaveAddr = 0x44; 
byte inRegAddr = 5;

void myOnReceive(int x) { 
  inRegAddr = Wire.read();
}

void myOnRequest() {
  Wire.write(inRegAddr); 
}
 
void setup() {

  // i2c slave 
  Wire.begin((uint8_t)slaveAddr);
  Wire.setClock(1000L);
  Wire.onReceive(myOnReceive);
  Wire.onRequest(myOnRequest);
}

void loop() {
  delay(2000);
}
========================

Who is online

Users browsing this forum: No registered users and 35 guests