https://toitlang.org/ logo
Infinite recursion in i2c `read-address` code
# general
k
Would you be able to test a fix before we push it out?
@addshore You could try using
device.write-read address size
instead of
device.read-address
here
<pkg:toit-lightbug>/devices/i2c.toit:112:27
.
a
let me have a look
k
The library code for i2c isn't included in the envelope and it is pulled into your app.
You should be able to just patch it in-place and re-run your code.
a
How about instead of
device.write-address
? also
write-read
?
as that calls read-address internally
k
Is write-address broken too? Let me take a quick look.
I think
write-address
is okay.
a
Writer.write
calls
Device.read-address
k
I very well could be wrong.
a
Copy code
0: Device.read-address.<block> <sdk>/i2c.toit:333:39
  1: Device.with-failure-handling_ <sdk>/i2c.toit:199:12
  2: Device.read-address       <sdk>/i2c.toit:340:12
  3: Device.read-address       <sdk>/i2c.toit:333:12
  4: Writer.try-write_         <pkg:toit-lightbug>/devices/i2c.toit:112:27
  5: Writer.try-write          <sdk>/io/writer.toit:110:16
  6: Writer.write              <sdk>/io/writer.toit:51:14
oh wait, writer.write, not i2c writer 😛
aaah, I'm being an idiot and changing the code in my pkg, but not the code actually loaded as a dependency
k
You can also just patch the SDK libs and not change your own code.
I'm just trying to figure out if using
write-read
is correct here 🙂
a
switching to
write-read
gives me:
Copy code
******************************************************************************
EXCEPTION error.
STACK_OVERFLOW
  0: Device.read-address.<block> <sdk>/i2c.toit:333:39
  1: Device.with-failure-handling_ <sdk>/i2c.toit:199:12
  2: Device.read-address       <sdk>/i2c.toit:340:12
  3: Device.read-address       <sdk>/i2c.toit:333:12
  4: Writer.try-write_         <pkg:toit-lightbug>/devices/i2c.toit:112:27
  5: Writer.try-write          <sdk>/io/writer.toit:110:16
  6: Writer.write              <sdk>/io/writer.toit:51:14
  7: Comms.sendSwitching_      <pkg:toit-lightbug>/services/comms/comms.toit:294:19
  8: Comms.send                <pkg:toit-lightbug>/services/comms/comms.toit:273:5
  9: Comms.sendOpen_           <pkg:toit-lightbug>/services/comms/comms.toit:77:13
 10: Comms.start_              <pkg:toit-lightbug>/services/comms/comms.toit:70:7
 11: Comms                     <pkg:toit-lightbug>/services/comms/comms.toit:58:5
no, i must be being an idiot again
there are more places
k
Maybe just patch it in the SDK instead? Seems easier.
Copy code
/**
  Reads $size bytes from the given $address.
  */
  read-address address/ByteArray size/int -> ByteArray:
    return write-read address size
@floitsch did the reimplementation, so he'll probably want to take a look at this too 🙂
a
changed the sdk, and that still seems to result in
Copy code
******************************************************************************
Decoding by `jag`, device has version <2.0.0-alpha.178>
******************************************************************************
EXCEPTION error.
STACK_OVERFLOW
  0: Device.read-address.<block> <sdk>/i2c.toit:333:39
  1: Device.with-failure-handling_ <sdk>/i2c.toit:199:12
  2: Device.read-address       <sdk>/i2c.toit:340:12
  3: Device.read-address       <sdk>/i2c.toit:333:12
  4: Writer.try-write_         <pkg:toit-lightbug>/devices/i2c.toit:112:27
  5: Writer.try-write          <sdk>/io/writer.toit:110:16
  6: Writer.write              <sdk>/io/writer.toit:51:14
  7: Comms.sendSwitching_      <pkg:toit-lightbug>/services/comms/comms.toit:294:19
  8: Comms.send                <pkg:toit-lightbug>/services/comms/comms.toit:273:5
  9: Comms.sendOpen_           <pkg:toit-lightbug>/services/comms/comms.toit:77:13
 10: Comms.start_              <pkg:toit-lightbug>/services/comms/comms.toit:70:7
 11: Comms                     <pkg:toit-lightbug>/services/comms/comms.toit:58:5

******************************************************************************
so perhaps I am still not doing something right here 😄
k
Yeah, it looks like something didn't quite work. I'm not quite familiar with your build environment, so maybe there is something that makes you use an installed SDK, rather than the one where you've checked out the source code. Are you running
jag run
with
JAG_TOIT_REPO_PATH
set correctly?
a
wait, i probably need to
make
in toit repo too
think i got myself in a bit of a muddle in the middle 😄
k
I think we symlink
toit/build/esp32c6/sdk/lib
to
toit/lib
, so you probably shouldn't need to rebuild. I could be wrong.
a
so, i define
export JAG_TOIT_REPO_PATH=$PWD/../toit
for my jag build
but I changed the code, built toit, built jag, used the built jag to re run my code, but still no joy
k
I think you also need it for
jag run
, because otherwise we'll probably pick up an SDK from somewhere else? (
.cache/jaguar/sdk
).
You could also try to patch
.cache/jaguar/sdk/lib/i2c.toit
and see if that takes effect 🙂
a
that was it! Now I get past that first error
(and get a bunch more) 😉
k
Great! More work for us, but I guess this increases our chances of fixing them in one go 🙂
a
just restarting my devcie and I'll paste the next one
k
Thank you!
a
Copy code
[lb-comms] INFO: Comms starting
E (42221) i2c.master: I2C hardware timeout detected
E (42221) i2c.master: s_i2c_synchronous_transaction(876): I2C transaction failed
E (42221) i2c.master: i2c_master_receive(1121): I2C transaction failed

******************************************************************************
EXCEPTION error.
ESP_ERR_INVALID_STATE
  0: i2c-device-read_          <sdk>/i2c.toit:434:3
  1: Device.read               <sdk>/i2c.toit:285:5
  2: Reader.read_              <pkg:toit-lightbug>/devices/i2c.toit:68:21
  3: Reader.more_              <sdk>/io/reader.toit:82:14
  4: Reader.ensure_            <sdk>/io/reader.toit:69:14
  5: Reader.ensure-buffered    <sdk>/io/reader.toit:103:5
  6: Reader.peek-byte          <sdk>/io/reader.toit:238:5
  7: Comms.processInbound_     <pkg:toit-lightbug>/services/comms/comms.toit:106:21
  8: Comms.start_.<lambda>.<lambda> <pkg:toit-lightbug>/services/comms/comms.toit:63:52
  9: catch-and-restart.<block> <pkg:toit-lightbug>/util/resilience.toit:7:9
 10: catch.<block>             <sdk>/core/exceptions.toit:124:10
 11: catch                     <sdk>/core/exceptions.toit:122:1
 12: catch                     <sdk>/core/exceptions.toit:73:10
 13: catch-and-restart         <pkg:toit-lightbug>/util/resilience.toit:5:10
 14: Comms.start_.<lambda>     <pkg:toit-lightbug>/services/comms/comms.toit:63:12
******************************************************************************
So, all I know is this was working before the new release (could also be bugs on our side affecting it)
f
Definitely. Sorry for the regressions.
Looks to me like this comes from the lower levels. I think the new i2c driver is more picky. At the moment I think it's not a bug in our code, but the peripheral not responding correctly. I can see the is a timeout. Maybe the peripheral stretches the clock for too long, and we run into a timeout? And when trying to restart the bus the driver notices that the clock is pulled low and reports an incorrect state. That's my current guess.
a
Good to know, if you think its possibly the peripherla (which is our hardware) I'll take a deeper look on our side 🙂
f
It would be interesting to see, if the peripheral is stretching the clock for a long time. Unless you have done so already, also check the pull up resistors.
a
So, switching read-address with write-read should be fine though in general? and I can assume that mostly does the same behaviour?
f
Yes. It should. But I haven't looked at the bug yet.
k
The fix to
read-address
will be in SDK v2.0.0-alpha.179 which is cooking on the build bots.
a
very nice, I'm going to look closer at the other I2C stuff today and see if we can figure out what our peripheral might be doing that is bad Writing too it seems to be fine, which is great
So, looks like it was likely the i2c peripheral and or noise on i2c that was causing the finaly issue / itmeouts mentioned. Dropping the frequency seems to have eliminated it for now, but we will also take a look at revising the hardware to fix that issue