guild icon
Toit
#Why is the ADS1115 on I2C so ultra slow ? (solved)
Thread channel in help
kaxori
kaxori 07/03/2023 02:25 PM
Has anyone an idea why?
floitsch
floitsch 07/03/2023 02:47 PM
Can you define what "slow" means?
floitsch
floitsch 07/03/2023 02:48 PM
How many measurements do you see?
floitsch
floitsch 07/03/2023 02:49 PM
I don't have any ads1115 yet, so can't test the code, but the test.toit itself looks good.
kaxori
kaxori 07/03/2023 03:14 PM
slow means 1 measure per second. In the example the led toggles each step without sleep.
floitsch
floitsch 07/03/2023 03:30 PM
That's really slow...
floitsch
floitsch 07/03/2023 03:30 PM
I'm wondering if we are stuck in the sleep loop of the driver (waiting for the value to be ready)(edited)
kaxori
kaxori 07/03/2023 03:31 PM
i changed the ads1115 and measure now ~140ms !
floitsch
floitsch 07/03/2023 03:31 PM
Interesting
kaxori
kaxori 07/03/2023 03:41 PM
the lowest is slowest
floitsch
floitsch 07/03/2023 03:41 PM
Weird
floitsch
floitsch 07/03/2023 03:42 PM
Would be interesting to know whether it's faster with other systems or if Toit does something different
kaxori
kaxori 07/03/2023 03:43 PM
I will check it out
floitsch
floitsch 07/03/2023 03:43 PM
Thanks
kaxori
kaxori 07/03/2023 05:58 PM
=> 34ms with Arduino SW
floitsch
floitsch 07/03/2023 05:59 PM
Even for the 1s one?
kaxori
kaxori 07/03/2023 06:00 PM
yes measure with the slow ADS1115, the faster one does it in 12 ms
floitsch
floitsch 07/03/2023 06:00 PM
Strange.
kaxori
kaxori 07/03/2023 06:01 PM
I guess the used i2c implementation could be improved
floitsch
floitsch 07/03/2023 06:01 PM
Not that much
floitsch
floitsch 07/03/2023 06:01 PM
It's mapped to the hardware I2C. Shouldn't be that slow.
floitsch
floitsch 07/03/2023 06:02 PM
Need to go now, but something that I will need to investigate.
I should get the same sensor soon.
kaxori
kaxori 07/03/2023 06:54 PM
GPIO Speed test (fast DO toggle):
toit: 5,72 碌s, ~ 175kHz
Arduino : 7.6 碌s, ~ 132 kHz
floitsch
floitsch 07/04/2023 08:11 AM
Nice.
floitsch
floitsch 07/04/2023 08:12 AM
There is one thing that Toit currently doesn't do very well: sleep for very short periods. We are using the Freertos timer underneath and that one only has a granularity of 10ms. So the driver's sleep --ms=1 will only come back after 10ms. But that doesn't explain the delays of ~1s.
kaxori
kaxori 07/04/2023 08:32 AM
it | know
works | why
-------+-------
NO | NO : usually
NO | YES ==> knowing what to do
YES | NO : still learning
YES | YES : asymtote
(edited)
kaxori
kaxori 07/04/2023 08:33 AM
so for hw close driver hacking small delays are needed
floitsch
floitsch 07/04/2023 10:00 AM
It would be nice to make sleep work better with less granularity.
It shouldn't be an issue here, though. As I said: I should get an ADS1115 soon, and will investigate then.

Can you confirm that it's the sleep loop that takes so much time?
Would be interesting to count the number of iterations of that loop. Checking how often the device responds with "not ready yet".
kaxori
kaxori 07/04/2023 06:42 PM
it is not the sleep , i removed it (in a temp copy of the driver).
I also checked the register parametrisation unfortunatly without knowlegde
floitsch
floitsch 07/04/2023 06:43 PM
strange.
Maybe add some print Time.monotonic_us in strategic places to figure out what is taking so long?
(edited)
kaxori
kaxori 07/04/2023 07:20 PM
ups again: chips needs about 450 cycles (~1ms, without sleep) to get ready :馃: :馃槵:
floitsch
floitsch 07/04/2023 08:43 PM
The sensor should be able to sample at ~1ms. Weird...
floitsch
floitsch 07/17/2023 10:52 PM
I finally got my Ads1115 sensors.
After a bit of testing I figured out the problem.
The constructor is setting the config register.
It directly ors in the RATES_DR_475SPS_ constant. However, the location of the data rate is bits 5-7. So there is a shift needed:
| RATES_DR_475SPS_ << 5
Alternatively, the RATES_DR_475SPS_ constant should have the bits already shifted. (That would actually be more consistent with the rest of the code).

Once I fixed that, I also changed the sleep --ms=1 to yield.
The driver should probably remember the samples-per-second, and then either just yield (if it's less than 10ms), or sleep otherwise.

With that change the driver is nicely computing new values close to the speed that it should.
floitsch
floitsch 07/17/2023 10:55 PM
@kaxori let me know if you want to do the change yourself, or if you prefer a PR.
kaxori
kaxori 07/18/2023 07:29 AM
:馃憤馃徎: good job, i prefer PR (you has the last look at it)
floitsch
floitsch 07/18/2023 09:29 AM
Sounds good. Don't hesitate to ping me if it looks like I forgot.
kaxori
kaxori 07/18/2023 09:30 AM
I just tried it out :馃ぉ:
馃挭1
40 messages in total