Repository navigation
Conversation
627c41d to
9e80f03
Compare
Displays and display buses are now allocated in two parts. First, the core state struct is allocated outside the VM heap so that it works when the VM is not running. Second, a proxy allocation is done within the VM to tie the display's lifetime to the VM. The proxy also holds a second _vm_t state struct that holds VM pointers so they get collected. Some signal lines like DC and backlight used to be provided as native pins. Now they also take in DigitalInOut and PWMOut where appropriate so that they can be shared across multiple displays and buses. deinit()ing the display or bus will not deinit the shared objects. It is assumed that garbage collection will do it when appropriate. One display can be designated as the primary via `supervisor.runtime.display`. This applies a primary designation to the display bus it uses as well. These are then preserved when the VM stops using `stop_vm`. During `stop_vm`, some objects like PWMOut for backlight will be moved internal to the display so they keep working. When the VM runs again, `start_vm` is called. This allows the preserved display and bus to move the internal objects like PWMOut back to the VM heap so it is shareable with other displays. Board initiated displays are also not deinit() when the VM stops. They do not move objects because they are constructed fully outside of the VM. `release_displays()` still exists but is deprecated. It will deinit all board displays and the primary display. It will not deinit user created displays that are not the primary. Code should be changed to explicitly deinit the display it wants to. For example, `board.DISPLAY.deinit()` will allow one to re-init the board's display. Fixes micropython#1760. Related to micropython#8675, micropython#10791, micropython#6395 and micropython#7472.
This reverts the epaper sequence changes and adds const display ops tables. It also reverts the nlr guards that were overly cautious.
f98f4b4 to
b6185ad
Compare
|
Ok @dhalbert. I think this is ready for review. The CI passes. I'm sure there are bugs but this gets us pretty far and bug fixes will be easier to review as separate PRs. I tested on T keyboard display with only one board.DISPLAY set and other three init at runtime. One of them is then set as primary. |
If a primary display can't live without the VM it will be deinit when the vm is stopped. This allows primary displays when doing "simple" lifetime while the VM is active.
dhalbert
left a comment
There was a problem hiding this comment.
Massive amount of work, thanks. Fortunately the board stuff is mostly boilerplate.
Most of my comments are about terminology, but a few code questions.
I have yet to test on hardware, and will do that.
| void reset_port_early(void) { | ||
| // Deinit the camera now, before reset_board_buses() deinits board.I2C(). | ||
| // | ||
| // The camera shares its I2C bus with busio.I2C and keeps a device | ||
| // registered on that bus for as long as the camera is alive. ESP-IDF | ||
| // refuses to delete a bus that still has devices, so if the camera is | ||
| // still alive when the bus is deinited, the bus is never freed. Its I2C | ||
| // peripheral then stays in use until a hard reset. | ||
| // | ||
| // Waiting for the camera's finaliser doesn't work: finalisers run after | ||
| // reset_board_buses(). | ||
| #if CIRCUITPY_ESPCAMERA | ||
| espcamera_reset(); | ||
| #endif | ||
| } | ||
|
|
There was a problem hiding this comment.
Why is this no longer necessary? I would think there is still an issue with the esp-camera thinking it owns the I2C bus. Will need to test. This was a very recent fix, in #11951.
There was a problem hiding this comment.
reset_board_buses no longer deinits board.I2C devices. So they'll be live when the camera is deinit.
|
This looks quite good. Tested with smitka's cp-desktop-lab simulator 22 boards with this PR and compared it to Each board creates two ST7789s and an SSD1306 at runtime next to
Boards: clue, funhouse, macropad, magtag, pybadge, pygamer, pyportal, circuitplayground, circuitplayground_bluefruit, feather, fruitjam, matrixportal, metro, metro_m4_airlift, metro_rp2350, pico, picopad, propmaker, qtpy, trellis_m4, trinket Two things:
|
Yes, that is intended. We now want displays to be explicitly opted into being the primary. I changed this because some folks have complained about the repl showing on their screen when they don't want it to.
Ok, I'll double check this. I was still messing with it a bit in the most recent commit. |
|
Ok, please take another look. I did a bunch of renaming and thinking about the deinit state of displays. They no longer pretend to be None. Instead, they eval as False when deinit. |
There was a problem hiding this comment.
Retested 9c3c554 on 22 board files: all pass, shared D/C included.
You will need to resolve the zephyr CI issues.
nit: setting a deinited display says it isn't AnyDisplay.
| Check | This PR | main |
|---|---|---|
board.DISPLAY exists and is primary at start |
22/22 | 22/22 |
| 3 displays created at runtime | 22/22 | 22/22 |
ST7789 subclass set as supervisor.runtime.display |
22/22 | 0/22 |
| That display survives reload as primary | 22/22 | 0/22 |
| Non-primary display released at reload | 22/22 | 0/22 |
board.DISPLAY alive and can be primary again |
22/22 | 22/22 |
Holding off until the zephyr display tests pass.
| if (self->bus == &self->inline_bus) { | ||
| busio_spi_obj_t *heap_bus = mp_obj_malloc_with_finaliser(busio_spi_obj_t, &busio_spi_type); | ||
| if (!common_hal_busio_spi_move(self->bus, heap_bus)) { | ||
| m_free(heap_bus); | ||
| common_hal_fourwire_fourwire_deinit(self); | ||
| return NULL; | ||
| } | ||
| self->bus = heap_bus; | ||
| } |
There was a problem hiding this comment.
After a reload this moves the SPI into a heap object that deinit() never releases, so its pins stay claimed (All SPI peripherals are in use). Keeping the inline bus lets the existing inline_bus check in deinit() release it.
| if (self->bus == &self->inline_bus) { | |
| busio_spi_obj_t *heap_bus = mp_obj_malloc_with_finaliser(busio_spi_obj_t, &busio_spi_type); | |
| if (!common_hal_busio_spi_move(self->bus, heap_bus)) { | |
| m_free(heap_bus); | |
| common_hal_fourwire_fourwire_deinit(self); | |
| return NULL; | |
| } | |
| self->bus = heap_bus; | |
| } |
There was a problem hiding this comment.
This is intentional. The SPI bus should be released by a gc.collect() in case it was shared. I wonder if we should call gc.collect internally and try again on in-use pins.
| if (self->bus == &self->inline_bus) { | ||
| busio_i2c_obj_t *heap_bus = mp_obj_malloc_with_finaliser(busio_i2c_obj_t, &busio_i2c_type); | ||
| if (!common_hal_busio_i2c_move(self->bus, heap_bus)) { | ||
| m_free(heap_bus); | ||
| common_hal_i2cdisplaybus_i2cdisplaybus_deinit(self); | ||
| return NULL; | ||
| } | ||
| self->bus = heap_bus; | ||
| } |
There was a problem hiding this comment.
Same as FourWire.
| if (self->bus == &self->inline_bus) { | |
| busio_i2c_obj_t *heap_bus = mp_obj_malloc_with_finaliser(busio_i2c_obj_t, &busio_i2c_type); | |
| if (!common_hal_busio_i2c_move(self->bus, heap_bus)) { | |
| m_free(heap_bus); | |
| common_hal_i2cdisplaybus_i2cdisplaybus_deinit(self); | |
| return NULL; | |
| } | |
| self->bus = heap_bus; | |
| } |
| mp_raise_TypeError_varg(MP_ERROR_TEXT("expected %q"), type->name); | ||
| } | ||
| mp_obj_t native = unwrap_native_base(base, type); | ||
| if (native == MP_OBJ_NULL) { |
There was a problem hiding this comment.
board.DISPLAY isn't a proxy, so after release_displays() it still answers .width, and so does an EPaperDisplay. This gives every type the check BusDisplay has.
| if (native == MP_OBJ_NULL) { | |
| if (native == MP_OBJ_NULL || displayio_native_get_link(native)->deinited) { |
There was a problem hiding this comment.
I don't think deinited objects should fail every attribute read. It shouldn't be able to do the things it needs resources (aka peripherals) for. I ran into this with a code.py that was reading board.DISPLAY.backlight. It's easier to handle backlight being None than raising an exception.
dhalbert
left a comment
There was a problem hiding this comment.
Thanks for the changes. This is a small batch of comments, based on my own follow-ups. I will work on a larger one with things that Claude noticed.
I marked some renaming things, and one thing about the camera code.
A new more general issue Claude noticed while testing the camera code is that invoking board.I2C() in one VM run, and then inovking busio.I2C(board.SCL, board.SDA) after a soft reload, in the next VM, fails with ValueError: SCL in use. On 10.3.2 and on main this does not fail. This is true on plain old boards liked Metro ESP32-S3, not just a camera board. Same kind of problem with board.SPI() and board.UART() -- they live past the end of the VM.
This was a deliberate change to simplify/remove the code around resetting the board buses. (It used to be that we had to look at all possible stuff that would want to keep using the bus to know if it was safe to deinit or not.) Now that we can call .deinit() on it then that should be done when switching. Calling |
Displays and display buses are now allocated in two parts. First, the core state struct is allocated outside the VM heap so that it works when the VM is not running. Second, a proxy allocation is done within the VM to tie the display's lifetime to the VM. The proxy also holds a second _vm_t state struct that holds VM pointers so they get collected.
Some signal lines like DC and backlight used to be provided as native pins. Now they also take in DigitalInOut and PWMOut where appropriate so that they can be shared across multiple displays and buses. deinit()ing the display or bus will not deinit the shared objects. It is assumed that garbage collection will do it when appropriate.
One display can be designated as the primary via
supervisor.runtime.display. This applies a primary designation to the display bus it uses as well. These are then (usually) preserved when the VM stops usingstop_vm. Duringstop_vm, some objects like PWMOut for backlight will be moved internal to the display so they keep working. When the VM runs again,start_vmis called. This allows the preserved display and bus to move the internal objects like PWMOut back to the VM heap so it is shareable with other displays.Small builds have a "simple" lifetime where user constructed displays are not kept outside the VM and the proxy mechanics are not compiled in. This saves space but still allows for multiple displays while the VM is running. board displays are still outlive the VM and can be used as the primary.
Board initiated displays are also not deinit() when the VM stops. They do not move objects because they are constructed fully outside of the VM.
release_displays()still exists but is deprecated. It will deinit all board displays and the primary display. It will not deinit user created displays that are not the primary. Code should be changed to explicitly deinit the display it wants to. For example,board.DISPLAY.deinit()will allow one to re-init the board's display.Fixes #1760. Related to #8675, #10791, #6395 and #7472.