Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
DvdGiessen wants to merge2 commits intomicropython:master
base:master
Choose a base branch
Loading
fromDvdGiessen:machine_mem_slice

Conversation

@DvdGiessen
Copy link
Contributor

@DvdGiessenDvdGiessen commentedOct 24, 2025
edited
Loading

Summary

This implements support for reading/writing slices of memory through themachine.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 theMICROPY_PY_MACHINE_MEMX_SLICE feature flag.

After testing on the ESP32-S3, I encountered an issues where certain registers require word-aligned reads. Thus, trying to usestruct.unpack directly would fail becausemp_binary_get_int reads one byte at a time, but copying the slice usingbytes(machine.mem8[...]) first worked becausememcpy tries to copy entire words at once. To fix this I implemented an extra path inmp_binary_get_int that 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 thetime.ticks_ms() method we can read out the time directly from the LAC timer / systimer peripheral register usingstruct.unpack():

importmachine,struct,time# ESP32:print('ticks_ms:',time.ticks_ms(),', mem8:',struct.unpack('@Q',machine.mem8[0x3FF5F078:0x3FF5F080])[0]//2000)# ESP32-S3:print('ticks_ms:',time.ticks_ms(),', mem8:',sum(v<<sfors,vinzip((32,0),struct.unpack('@II',machine.mem8[0x60023040:0x60023048])))//16000)
ticks_ms: 12769608 , mem8: 12769608

Trade-offs and Alternatives

First tried implementing the buffer protocol for themachine.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 functionmachine.ptr(some_object), which returns the pointer to a MicroPython object. SeeDvdGiessen@d6262c3.

>>>data=0xC0FFEECAFE>>>data,hex(data)(828927560446,'0xc0ffeecafe')>>>machine.mem8[machine.ptr(data)+16:machine.ptr(data)+16+2]=b'\xDE\xC0'>>>data,hex(data)(828927557854,'0xc0ffeec0de')

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.

@codecov
Copy link

codecovbot commentedOct 24, 2025
edited
Loading

Codecov Report

❌ Patch coverage is57.57576% with14 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.32%. Comparing base (10b7dfd) to head (521c7a8).

Files with missing linesPatch %Lines
extmod/machine_mem.c41.66%14 Missing⚠️
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.
📢 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

github-actionsbot commentedOct 24, 2025
edited
Loading

Code size report:

Reference:  top: Include tools/cc1 in ruff search path. [10b7dfd]Comparison: py/binary: Read aligned native endianness integers directly. [merge of 521c7a8]  mpy-cross:    +0 +0.000%    bare-arm:    +0 +0.000% minimal x86:    +0 +0.000%    unix x64:   +80 +0.009% standard      stm32:   +56 +0.014% PYBV10     mimxrt:   +48 +0.013% TEENSY40        rp2:   +72 +0.008% RPI_PICO_W       samd:   +56 +0.021% ADAFRUIT_ITSYBITSY_M4_EXPRESS  qemu rv32:   +42 +0.009% VIRT_RV32

@DvdGiessenDvdGiessenforce-pushed themachine_mem_slice branch 5 times, most recently fromfd6c64a toa4d2276CompareOctober 31, 2025 16:32
@dpgeorgedpgeorge added the extmodRelates to extmod/ directory in source labelNov 3, 2025
@DvdGiessenDvdGiessenforce-pushed themachine_mem_slice branch 3 times, most recently from5cbbafd toe1c4f8aCompareNovember 5, 2025 11:04
Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

extmodRelates to extmod/ directory in source

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@DvdGiessen@dpgeorge

[8]ページ先頭

©2009-2025 Movatter.jp