Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.5k
extmod/machine_mem: Support slices for reading/writing memory.#18322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Conversation
3b7ae16 toc3ffbacComparecodecovbot commentedOct 24, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@## master #18322 +/- ##==========================================- Coverage 98.38% 98.32% -0.07%========================================== Files 171 171 Lines 22294 22325 +31 ==========================================+ Hits 21933 21950 +17- Misses 361 375 +14 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
github-actionsbot commentedOct 24, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Code size report: |
fd6c64a toa4d2276Compare5cbbafd toe1c4f8aCompareSigned-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
e1c4f8a to521c7a8Compare
Uh oh!
There was an error while loading.Please reload this page.
Summary
This implements support for reading/writing slices of memory through the
machine.mem{8,16,32}objects. Reading is implemented by returning a memoryview.After seeing the code size report, I concluded the slice support was not worth adding unconditionally. It is thus gated by the
MICROPY_PY_MACHINE_MEMX_SLICEfeature flag.After testing on the ESP32-S3, I encountered an issues where certain registers require word-aligned reads. Thus, trying to use
struct.unpackdirectly would fail becausemp_binary_get_intreads one byte at a time, but copying the slice usingbytes(machine.mem8[...])first worked becausememcpytries to copy entire words at once. To fix this I implemented an extra path inmp_binary_get_intthat tries reading entire words when the alignment and endianness work out. This is the cause for most of the code size increase (as it isn't gated behind a feature flag).Testing
Tested on a few different ESP32 boards.
For example, instead of using the
time.ticks_ms()method we can read out the time directly from the LAC timer / systimer peripheral register usingstruct.unpack():Trade-offs and Alternatives
First tried implementing the buffer protocol for the
machine.mem{8,16,32}objects; however did not include that here due to limitations in memoryview (length and offset fields have limited size, thus only allowing access to low memory addresses).I also implemented a helper function
machine.ptr(some_object), which returns the pointer to a MicroPython object. SeeDvdGiessen@d6262c3.Fun for a bit of poking around in the MicroPython internals interactively, but that does not seem useful enough for actual users, so I've left it out of this PR.