Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@iabdalkader
Copy link
Contributor

@iabdalkader iabdalkader commented Nov 15, 2025

Summary

This PR introduces a multi-level initialization and deinitialization system for embedded MicroPython ports. The system uses GNU linker sections to automatically collect and order init/deinit function pointers, eliminating the need to manually maintain initialization sequences in main.c.

Example usage:

MP_INIT_REGISTER(driver, 00, timer_init0);
MP_DEINIT_REGISTER(driver, 00, timer_deinit);

...
// Calls all your init functions
mp_init_run_level(MP_INIT_DRIVER);

Key Benefits

  • Makes init order dependencies (or lack thereof) explicit and consistent across ports.
  • Optional via config: Ports can keep their old init/deinit behavior.
  • Allows ordering init/deinit functions within a level, so can easily replicate exact current behavior.
  • Modules/drivers can register their init/deinit. Alternatively, we could extern & register all init/deinit functions in a port-specific file (e.g., stm32/mpinit_register.c), allowing ports even more flexibility and customization, but this kinda defeats the consistency point.
  • Improves readability: replace 100s of duplicate lines with a few calls.
  • New ports get correct initialization automatically.
  • New driver/service/etc.. won't require updating main.c in every port that uses it.

Testing

Didn't do any actual testing, since this is just an RFC/PoC, but here's what one section looks like:

grep "\.mp_init_driver\." build-*/firmware.map

 *(SORT_BY_NAME(.mp_init_driver.*))
 .mp_init_driver.00.readline_init0
 .mp_init_driver.01.pin_init0
 .mp_init_driver.02.extint_init0
 .mp_init_driver.03.timer_init0
 .mp_init_driver.05.pyb_usb_init0
 .mp_init_driver.09.servo_init

Challenges and risks

  • There's no common linker script between boards, let alone ports, so to enable this in stm32 port, for example, we'd have to go through every board and make sure its linker script includes the common script.
  • Very few init functions accept args, we could either make their state/arg extern/global, add wrappers or just call them manually.
  • This is a big change that may break things, however, not every port needs to enable it. So we could start small, then migrate more and more ports.

Introduces a multi-level initialization and deinitialization system
for embedded MicroPython ports.
Uses linker sections to automatically collect and order init/deinit
function pointers, eliminating the need to manually maintain
initialization sequences in main.c.

Signed-off-by: iabdalkader <[email protected]>
@codecov
Copy link

codecov bot commented Nov 15, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.38%. Comparing base (27544a2) to head (1daa79e).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #18424   +/-   ##
=======================================
  Coverage   98.38%   98.38%           
=======================================
  Files         171      171           
  Lines       22294    22294           
=======================================
  Hits        21933    21933           
  Misses        361      361           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
Copy link

Code size report:

Reference:  esp32/boards: Add Silicognition ManT1S board definition. [27544a2]
Comparison: stm32: Add common init linker script. [merge of 1daa79e]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant