Skip to content

RP2040 (Raspberry Pi Pico) ​

Board id: rp2040-pico · 6 step channels · PIO-timed STEP/DIR, one state machine per axis · flashed via UF2 (BOOTSEL) or probe-rs.

Status

Implemented, compiles and host-tested; not yet validated on hardware. Flashing, motor movement and pulse timing on a real Pico are unverified until the hardware validation has been carried out.

Architecture ​

The RP2040 runs the same shared firmware as the STM32F411 and the virtual MCU. What differs is how pulses leave the chip.

  • Scheduling (shared). A tick interrupt (TIMER_IRQ_0, every 200 µs) runs the shared stepper code, which tracks position, checks soft limits and endstops before every step, detects queue underrun and late segments, and handles homing and controlled stop. It calls Hal::step once for each step that became due.
  • Pulse execution (RP2040-specific). Hal::step only records the step in a per-axis buffer (at most 4 direction runs per tick). At the end of the tick, Hal::flush_pulses turns each buffer into PIO commands that spread those steps evenly over the next 200 µs window. Overdue steps never go out as a burst.

Everything runs on core 0: the tick interrupt and the main loop (USB, frame decoding, status, watchdog). Core 1 is parked. The PIO does the time-critical part, so the CPU only has to produce one command word per axis per 200 µs. If six-axis measurements show the tick interrupt using more than about half its window, moving it to core 1 is the next step.

Pin map ​

Fixed by the firmware. The resolver checks every profile against it.

ChannelSTEPDIRENABLE (active low)ENDSTOPPIO block / SM
0GP0GP1GP12GP18PIO0 / SM0
1GP2GP3GP13GP19PIO0 / SM1
2GP4GP5GP14GP20PIO0 / SM2
3GP6GP7GP15GP21PIO0 / SM3
4GP8GP9GP16GP22PIO1 / SM0
5GP10GP11GP17GP26PIO1 / SM1
SignalPinNotes
Safety interlockGP27Input with pull-up; must be pulled to GND for drives to enable
Status LEDGP25On while any drive is enabled
Host linkUSB (micro-USB)CDC-ACM, VID:PID 16c0:27dd, product "MotionCore MCU (rp2040)"

Electrical ​

  • RP2040 GPIO is 3.3 V and not 5 V tolerant. Use drivers that accept 3.3 V logic (most TMC, DRV8825 and A4988 boards do), or level shifters.
  • Pin drive strength defaults to 4 mA. For long cables or opto-isolated driver inputs, add a buffer such as a 74HCT245 powered at 5 V.
  • ENABLE needs an external pull-up to the driver's logic supply, so drivers stay disabled during reset, in BOOTSEL mode, during a watchdog reset, and with no firmware. The firmware drives ENABLE high (disabled) before configuring anything else.
  • The Pico must never power motors.

Endstops, E-stop and the interlock ​

  • Endstops: normally-open switch between the ENDSTOP pin and GND; pressed = low. In noisy environments add an RC filter (e.g. 1 kΩ / 100 nF), or use normally-closed switches with invert = true.
  • E-stop (mandatory): a hardware circuit that removes driver power (contactor or safety relay), independent of all software.
  • Interlock monitor (GP27): wire an auxiliary contact of the E-stop circuit — closed when it is safe to run — between GP27 and GND. While GP27 is open, the firmware latches an emergency stop and disables every drive, and refuses both Enable and ResetFault.

An unconnected GP27 counts as open, the safe default. On a bench without an E-stop circuit, use a jumper to GND — and only with motors that cannot cause harm. The interlock only monitors the E-stop; it does not replace it.

Build and flash ​

bash
motioncore firmware build --board rp2040-pico          # reference profile → build/firmware/rp2040-pico/
motioncore firmware build --config my-robot.toml       # your profile → build/firmware/<mcu>/

The UF2 is generated from the ELF and validated block by block: magic numbers, RP2040 family id 0xe48bff56, block numbering, and every block inside the 2 MiB flash window starting with the boot2 stage at 0x10000000.

Via BOOTSEL (UF2):

  1. motioncore disable and remove driver power.
  2. Hold BOOTSEL while plugging in USB. A drive named RPI-RP2 appears.
  3. Flash:
    bash
    motioncore firmware flash --board rp2040-pico --yes
    motioncore firmware flash --board rp2040-pico --uf2-volume /media/$USER/RPI-RP2 --yes   # if not found
  4. Confirm what is running — only this proves the flash succeeded:
    bash
    motioncore firmware verify --board rp2040-pico --device /dev/ttyACM0    # COMx on Windows

Via SWD (optional): with a debug probe such as a second Pico running debugprobe, probe-rs download --chip RP2040 build/firmware/rp2040-pico/firmware.elf. cargo run inside boards/rp2040 uses the same runner.

Only the Pico's W25Q080 (2 MiB) boot2 is included. Boards with other flash chips need a different boot2.

Configuration example ​

robot-profiles/six-axis-rp2040-pico.toml is the reference profile: six revolute axes with endstops and homing. One axis:

toml
[mcu.main]
board = "rp2040-pico"
transport = "serial:///dev/ttyACM0"
watchdog_ms = 500

[[axis]]
id = "j2"
name = "Shoulder"
kind = "revolute"
unit = "deg"
mcu = "main"
channel = 1
steps_per_unit = 44.444444
min = -90.0
max = 90.0
max_velocity = 45.0          # 2000 steps/s, below the 50 kHz board limit
max_acceleration = 180.0

[axis.driver]
actuator = "org.motioncore.actuator.step_dir"
step_pin = "GP2"             # must match the pin map
dir_pin = "GP3"
enable_pin = "GP13"

[axis.endstop]
pin = "GP19"

[axis.homing]
direction = "negative"
speed = 15.0
position = -90.0
max_travel = 190.0

PIO design ​

Allocation ​

One state machine per axis: axes 0–3 on PIO0 SM0–3, axes 4–5 on PIO1 SM0–1; PIO1 SM2–3 stay free. The 8-instruction program is loaded at offset 0 in both blocks (16 of 64 instruction slots used). Each state machine drives STEP by side-set and DIR by out pins, so the two pins need not be adjacent.

Program ​

text
0  pull block          side 0         ; wait for a command
1  out pins, 1         side 0 [S]     ; DIR, then setup delay
2  out x, 15           side 0         ; pulse count - 1
3  out isr, 16         side 0         ; low-time loop count L
4  pulse: nop          side 1 [H]     ; STEP high for H+1 cycles
5  mov y, isr          side 0
6  low: jmp y-- low    side 0         ; L+1 cycles
7  jmp x-- pulse       side 0

Command word: bit 0 = DIR level, bits 1–15 = pulses − 1, bits 16–31 = L.

Timing ​

State-machine clock: 125 MHz / 25 = 5 MHz (200 ns per cycle, integer divider, no fractional jitter).

QuantityCyclesTimeSet by
STEP highH + 1 = 102.0 µsTIMING.high_cycles
STEP low (min)L + 3 ≥ 10≥ 2.0 µsTIMING.min_low_cycles
DIR setup before first STEP edgeS + 3 = 102.0 µsTIMING.dir_setup_cycles
DIR hold after last STEP edgeL + 4 ≥ 11≥ 2.2 µsfollows from minimum low
Fastest period204 µs (250 kHz)high + min low

All values live in boards/rp2040/src/board.rs (TIMING). Check them against your driver's datasheet before changing anything.

Windows, buffering and failure behaviour ​

  • Pulses may use 90 % of each 200 µs window; the rest absorbs command overhead and interrupt jitter.
  • At this timing, at most 44 steps per axis fit a window (220 kHz). The board advertises a conservative 50 kHz per axis until hardware measurements justify more.
  • The TX FIFO is joined (8 words); one window needs at most 4 words per axis.
  • Before queuing a new window, the previous one must have been taken by the state machine. Otherwise output is lagging behind the tick and the firmware faults with StepRateExceeded, rather than letting the lag grow. The same fault is raised if a window's steps don't fit the timing.
  • A tick gap longer than 10 windows (2 ms) during motion faults with SegmentLate.
  • On disable, fault or E-stop: ENABLE goes high first, then the state machine is stopped, STEP forced low, both FIFOs cleared and the program parked at pull. No queued pulse is emitted afterwards.
  • Endstops are checked before every step, but the step reaches the pin up to one window (200 µs) later. Worst-case overtravel is one window of steps: 1 step at 5 kHz, 10 steps at 50 kHz.

Synchronisation between axes ​

The same tick interrupt queues every axis's commands for a window, one after another, so windows of different axes start within a few microseconds of each other. Segment start times are absolute MCU timestamps. This is channel-level synchronisation only; coordinated multi-axis trajectories are a host planner feature that is not yet wired up.

Hardware validation ​

None of this has been carried out yet. Record results (date, board, driver, firmware build hash) when you do.

Equipment: a logic analyser with ≥ 24 MHz sample rate on 8 channels (or an oscilloscope), a flashed Pico, step/dir drivers without motor power for the timing tests. Use a temporary debug build that toggles a spare GPIO (e.g. GP28) at the start and end of TIMER_IRQ_0 to measure interrupt timing.

#TestProcedurePass criterion
1Safe startPower up with and without firmware, in BOOTSEL, during flashingAll ENABLE lines high throughout
2Identitymotioncore firmware verifyBoard rp2040-pico, build hash matches
3STEP high timeJog one axis at 1, 10 and 50 kHz2.0 µs ± 0.1 µs
4STEP low timeSame captures, shortest low phase≥ 2.0 µs at every rate
5DIR setup / holdReversing move: DIR edge to next STEP rise; last STEP fall to DIR edge≥ 2.0 µs / ≥ 2.2 µs
6Frequency accuracyConstant-velocity jog; count edges over 1 sWithin 0.1 % of commanded; exact total step count
7JitterHistogram of STEP periods at constant velocitySpread ≤ one window-boundary effect; no period below minimum
8Max sustainable rateRaise rate until StepRateExceededRecord; keep advertised limit ≤ 50 % of it
9Six axes at onceAll six channels at 50 kHzNo fault; tick ISR (GP28) < 100 µs; inter-axis start skew recorded
10Stop latencyestop during motion: USB command to last STEP edge and ENABLE highENABLE high and STEP stopped within 1 ms of frame receipt
11WatchdogUnplug USB while enabledENABLE high within 500 ms
12HangHalt core 0 with a debugger (watchdog pause on debug off)Reset within ~100 ms, ENABLE high
13InterlockOpen GP27 while movingEmergencyStop fault, drives disabled, reset refused until closed
14EndstopHome each axis at low speedTrigger detected; overtravel ≤ 1 step at homing speed

Only after tests 1–14 pass: connect motors with low current limits and no load, then jog ±1°, home, stop mid-move and E-stop.

Known limitations ​

  • Not yet run on hardware: flashing, USB enumeration, pulse timing, six-axis CPU budget and stop latency are unverified.
  • Step timing resolution is one 200 µs window. Steps are evenly spaced within a window, not placed at their exact ideal times (deviation < 1 window).
  • Endstop and fault reaction can trail the commanded position by one window.
  • Advertised step rate is a conservative 50 kHz per axis until measured.
  • One MCU per robot; no coordinated multi-axis trajectories from the host yet.

Apache-2.0 OR MIT. Not a certified safety system — always use an independent hardware E-stop.