Skip to content

Pico 2 W: import mcu (GPIO, ADC, PWM, UART, I2C, SPI, interrupts) - #75

Merged
voodoomods merged 1 commit into
mainfrom
pico2w-hal
Dec 5, 2026
Merged

voodoomods merged 1 commit into
mainfrom
pico2w-hal

Conversation

@voodoomods

Copy link
Copy Markdown
Owner

import mcu; on the Raspberry Pi Pico 2 W: GPIO, ADC, PWM, UART, I2C, SPI, pin and timer interrupts, the chip's temperature and hardware random numbers, in ports/rp2350/src/mg_hal.c. The RP2350 has no DAC or CAN, so those calls return their "none" values, as the S3's DAC calls do. WiFi, and the LED on the WiFi chip, are still to come.

On the board

A check program that needs no wiring ran on the Pico, with every check passing:

  • GPIO: outputs read back; pull-ups and pull-downs; the WiFi chip's pins (23–25, 29) read −1.
  • PWM: duty measured from the pin's own edge events: 124/375 µs high at 2 kHz for 25%/75%, and 1499 µs for a 1500 µs servo pulse. A slice's second pin at another frequency is refused, and so are GPIO n and n + 16 on one channel. 10 Hz and 1 MHz run; 5 Hz is refused.
  • Timers: a 1 ms timer, 998–1000 µs between stamps, reaches the waiting program 3.0 µs after its tick. Four timers run at once, and a fifth is refused.
  • ADC: 12 bits on GPIO26–28. Captures run at 10 kHz (100.1 ms for 1000 readings), at 500 kHz (2.1 ms, paced by the ADC with DMA) and at 200 Hz (timed by the CPU). The chip temperature reads 26–27 °C.
  • UART: 10 × 0x55 at 9600 baud makes exactly 100 edges on the TX pin, GPIO0, and the same on the alternate pins GPIO2 and GPIO26. A read times out at 50 ms, and wrong pin pairs are refused.
  • I2C: both buses open. A scan of an empty bus takes 13 ms and finds nothing, and writes and reads to an absent device fail.
  • SPI: MISO = MOSI reads back in all four modes, and 300 bytes at 75 MHz. The chip select pulses once per call, and an unconnected MISO reads 0xFF.
  • Threads: two workers drive pins while the program takes timer events.

The examples ran on the board too: interrupts, pwm (duty 10–90% measured exact), analog, serial (SPI reads back, I2C scan) and blink. The 22 conformance samples still match the PC.

Found on the board

  • RP2350 erratum E9: a pulled-down input read 1 after being pulled up. pin_input(pin, PULL_DOWN) now turns the pad's input buffer off for 20 µs, so the pull-down brings the pin low first. The docs give the datasheet's fix for a pin that something drives and then lets go: an external pull-down of 8.2 kΩ or less.
  • ADC at 96 cycles: with the divider set to 96 cycles the ADC skipped every other start, giving 250 kHz. The fastest rate now runs it back to back.
  • Temperature sensor: the first reading after it's switched on was 3 °C high, so it now settles for 2 ms first.

Differences from the other boards (in the docs)

  • No one-wire UART: a Pico pin is either a TX or an RX. serial.goose uses GPIO0 → GPIO1 there and says to connect them.
  • i2c_probe reads a byte, because the controller can't send an address alone.
  • MISO = MOSI reads back through the SPI controller's loopback, as on the ESP32s.
  • The pins pick the UART, I2C and SPI port, as on the H7. The hardware API page has the tables.

Elsewhere

  • CI: builds every Pico example except the WiFi ones, instead of five.
  • test_mcu: now checks that the Pico defines every extern fn in lib/mcu.goose.
  • tests/stress/pico_hal.goose: the wire-free check program, to run again on a board.
  • Docs: a Pico 2 W column on the hardware API page, plus Boards, README and changelog updates.

Not checked

  • Wired setups: UART between two pins, I2C with a real device, and an external SPI device.
  • Rate limits: the extreme baud and I2C rates.

🤖 Generated with Claude Code

ports/rp2350/src/mg_hal.c gives the Pico 2 W everything in lib/mcu.goose
the RP2350 has: GPIO on the header's pins, the ADC with captures the ADC
paces itself (DMA, up to 500 kHz), PWM on the 8 slices, UART0/1, I2C0/1,
SPI0/1 (MISO = MOSI through the controller's loopback), pin edges and
timers as events, the chip's temperature and its hardware RNG. No DAC or
CAN on the chip: those calls return their "none" values. The locking,
the waits for interrupts and the event queue follow mg_h7.c.

Checked on the board with no wiring (tests/stress/pico_hal.goose, every
check passes), and the conformance samples still pass 22 of 22. Found
there: erratum E9 held a pulled-down pin high after a pull-up (the input
buffer now goes off for 20 us when a pin becomes pulled down); a 96-cycle
ADC divider skipped every other start (the fastest rate now runs back to
back); the temperature sensor read 3 C high just after it was switched on.

The examples choose Pico pins, CI builds every non-WiFi example for the
Pico, test_mcu covers its HAL, and the docs get a Pico 2 W column.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@voodoomods
voodoomods merged commit c02a049 into main Dec 5, 2026
14 checks passed
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