Page 1 of 1

time.monotonic() Using Micropython and Circuitpython at the same time

Posted: Tue Oct 05, 2021 12:45 pm
by neveregret
I found something that use Blinka under Micropython on a Raspberry Pi Pico. I am trying to do with Circuitpython's esp32spi library.The first problem is simple The esp32spi library uses time.monotonic() but time.monotonic() is not provided by the micropython time module. So I replaced time.monotic() to time.ticks_ms() / 1000 but It didn't worked. https://githubmemory.com/repo/adafruit/ ... 480?page=1 here same issue as me.
I am getting these errors when I change(time.monotic() to time.ticks_ms()/1000):

Code: Select all

Raspberry Pi RP2040 - ESP32SPI hardware test
Traceback (most recent call last):
  File "<stdin>", line 17, in <module>
  File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 290, in status
  File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 280, in _send_command_get_response
  File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 197, in _send_command
  File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 161, in _wait_for_ready
RuntimeError: ESP32 not responding
How can I add it (if it solves the problem) ? because there are a lot of libraries with time.monotic() on Circuitpython.

Re: time.monotonic() Using Micropython and Circuitpython at the same time

Posted: Fri Oct 15, 2021 4:32 am
by mattyt
It's surprisingly tricky.

I've discussed an implementation with Damien and Jim but any implementation is a compromise. The issue is that using single precision floats are too inaccurate to be generally useful. Double precision floats are inefficient on most micros (on some, horribly inefficient).

One idea that seems surprisingly promising is to return an int from monotonic - it would be different to CPython but, as long as the required resolution was less than one second, ought to work well.

We could then also implement monotonic_ns and encourage its use for those scenarios where greater resolution is required.

Does anyone have any feedback or thoughts about an alternative implementation?

Re: time.monotonic() Using Micropython and Circuitpython at the same time

Posted: Fri Oct 15, 2021 6:27 am
by neveregret
https://forums.raspberrypi.com/viewtopi ... 4&t=320989 I asked on this forum too. @hippy wrote some codes.

Code: Select all

STATIC mp_obj_t time_monotonic(void) {
    uint32_t ms = mp_hal_ticks_ms() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1);
    return mp_obj_new_float((float)ms / 1000);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_monotonic_obj, time_monotonic);
Now, I can use MicroPython and CircuitPython at the same time without problems but @hippy says that time.monotonic() implementation returns a 32-bit float value by default there are two problems which arise once a program has been running long enough, which means code will behave differently depending on how long the program has been running for.
I asked Dahn on discord, he said " You can swich to time.monotonic_ns(), which will not have the precision loss problem. "
Now, I am waiting , I want to try time.monotic() with uint32_t ms code, I will try what happen after 298 hours later :)

Re: time.monotonic() Using Micropython and Circuitpython at the same time

Posted: Fri Oct 15, 2021 4:41 pm
by dhylands
IIRC the linux kernel sets up the monotonic timer (i might be misremembering exactly which timer is setup this way) such that it will wrap after a few minutes after booting just because of this type of situation. This ensures that the wrapping code gets well tested.

Re: time.monotonic() Using Micropython and Circuitpython at the same time

Posted: Sun Oct 17, 2021 9:04 pm
by hippy