Skip to content

rp2/rp2_pio: Add support for the origin directive in PIO programs. - #19618

Open
agatti wants to merge 1 commit into
micropython:masterfrom
agatti:rp2-pio-origin
Open

rp2/rp2_pio: Add support for the origin directive in PIO programs.#19618
agatti wants to merge 1 commit into
micropython:masterfrom
agatti:rp2-pio-origin

Conversation

@agatti

@agatti agatti commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR adds a new parameter to the PIO program decorator, to indicate an explicit program origin in the PIO memory area.

There was no possibility to pass an origin position in the PIO constructor, and that would create some problems for PIO programs that use computed jump offsets. By default the state machine program loader does not guarantee a specific start position inside the PIO memory block, which makes things unpredictable for jump tables.

Now the decorator allows for an origin parameter that takes an integer containing the required offset in PIO memory. Without that parameter behaviour is unchanged.

An example showing the usage of this parameter is supplied, examples/rp2/pio_quadencoder.py. That is a direct port from quadrature_encoder.pio in the Raspberry Pi Pico samples repository into MicroPython's own PIO assembler.

Relevant documentation was also updated to keep track of the new decorator signature.

This closes #11531.

Testing

Although I don't have an encoder available (who knows where the rotary switches went...), the clock frequency is high enough that when running the example code on a RP2040 shorting either pin 16 or pin 17 to ground triggers an increment/decrement response, demonstrating that the PIO program origin was actually picked up correctly (plus you can debug your own way through and see the value coming out :)).

Trade-offs and Alternatives

Size increase is minimal, but this might probably be the least amount of code needed to pull this off.

There are a few PRs that are supposed to improve the state of things w.r.t. PIO, but none of those actually implement origin support. If somebody else with a more complete PR wants to take these changes in to merge with theirs, that's fine by me.

Generative AI

I did not use generative AI tools when creating this PR.

This commit adds a new parameter to the PIO program decorator, to
indicate an explicit program origin in the PIO memory area.

There was no possibility to pass an origin position in the PIO
constructor, and that would create some problems for PIO programs that
use computed jump offsets.  By default the state machine program
loader does not guarantee a specific start position inside the PIO
memory block, which makes things unpredictable for jump tables.

Now the decorator allows for an `origin` parameter that takes an integer
containing the required offset in PIO memory.  Without that parameter
behaviour is unchanged.

An example showing the usage of this parameter is supplied,
`examples/rp2/pio_quadencoder.py`.  That is a direct port from
`quadrature_encoder.pio` in the Raspberry Pi Pico samples repository
into MicroPython's own PIO assembler.

Relevant documentation was also updated to keep track of the new
decorator signature.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
@codecov

codecov Bot commented Aug 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.58%. Comparing base (1827631) to head (36cde5a).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #19618   +/-   ##
=======================================
  Coverage   98.58%   98.58%           
=======================================
  Files         182      182           
  Lines       23322    23322           
  Branches        5        5           
=======================================
  Hits        22993    22993           
  Misses        328      328           
  Partials        1        1           
Flag Coverage Δ
unix-coverage-32bit 98.59% <ø> (ø)
unix-coverage-64bit 98.52% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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
Copy Markdown

Code size report:

Reference:  stm32/mpu: Reuse MPU_ATTRIBUTES_NUMBER0 for all non-cacheable uses. [1827631]
Comparison: rp2/rp2_pio: Add support for the origin directive in PIO programs. [merge of 36cde5a]
  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
      esp32:    +0 +0.000% ESP32_GENERIC
     mimxrt:    +0 +0.000% TEENSY40
        rp2:   +32 +0.003% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

@octoprobe-bot

Copy link
Copy Markdown

Octoprobe PR report

Test Tests
passed
Tests
skipped
Tests
xfailed
Tests
failed
format flash 4
run-tests.py 3839 446
run-tests.py --via-mpy --emit native 3795 490
run-tests.py --via-mpy 3839 446
run-perfbench.py 96
run-natmodtests.py 288 34 6
run-mpremote-tests.sh 84 24
run-tests.py --test-dirs=extmod_hardware 18 72 30
run-tests.py --test-dirs=extmod_hardware --emit-native 18 72 30
Total 11981 1560 66 24
Failures

Group: run-mpremote-tests.sh

Test rp2
5334-
RPI_PICO2
rp2
5334-
RPI_PICO2-
RISCV
rp2
552b-
RPI_PICO2_W
rp2
6038-
RPI_PICO_W
test_unicode.sh FAIL FAIL FAIL FAIL FAIL FAIL FAIL FAIL FAIL FAIL FAIL FAIL
test_mount.sh FAIL FAIL FAIL FAIL FAIL FAIL FAIL FAIL FAIL FAIL FAIL FAIL

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support .origin PIO directive

2 participants