whitecatboard / Lua-RTOS-ESP32

Lua RTOS for ESP32
Other
1.2k stars 221 forks source link

I2C Unexpected byte on the bus #296

Closed Mynogs closed 4 years ago

Mynogs commented 5 years ago

I have an ESP32 (master) connected to an ATmega88 (slave) via I2C. I write this program on the ESP32:

  block.d = i2c.attach(i2c.I2C0, i2c.MASTER, 100000)
...
  block.d:start()
  block.d:address(0x08, false)
  block.d:write(123)
  block.d:start()
  block.d:address(0x08, true)
  local read1, read2, read3 = block.d:read(), block.d:read(), block.d:read()
  block.d:stop()
  print(read1, ' ', read2, ' ', read3)

When I trace the bus with the logic analyzer I see this:

grafik

Each time a byte is fetched from the slave, the value is incremented. You can see that four bytes are fetched, but in the program there are only three blocks.d:read(). Why is the reason for this behavior? It looks like the fourth byte is send after block.d:stop().

the0ne commented 5 years ago

Confirmed. Fixed by #312 Please note that you will need to modify your lua code as well:

  block.d = i2c.attach(i2c.I2C0, i2c.MASTER, 100000)
...
  block.d:start()
  block.d:address(0x08, false)
  block.d:write(123)
  block.d:start()
  block.d:address(0x08, true)
  local read1, read2, read3 = block.d:read(3)
  block.d:stop()
  print(read1, ' ', read2, ' ', read3)