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

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
neveregret
Posts: 28
Joined: Sun Aug 08, 2021 5:48 pm

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

Post by neveregret » Tue Oct 05, 2021 12:45 pm

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.

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

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

Post by mattyt » Fri Oct 15, 2021 4:32 am

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?

neveregret
Posts: 28
Joined: Sun Aug 08, 2021 5:48 pm

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

Post by neveregret » Fri Oct 15, 2021 6:27 am

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 :)

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

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

Post by dhylands » Fri Oct 15, 2021 4:41 pm

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.

hippy
Posts: 130
Joined: Sat Feb 20, 2021 2:46 pm
Location: UK

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

Post by hippy » Sun Oct 17, 2021 9:04 pm


Post Reply