Raspberry Pi RP2040

From Wildsong
Jump to navigationJump to search

Raspberry Pi Pico documentation

Inventory

These are RP2040 boards that I have right now.

  • 2x RPi PicoEdit
  • Adafruit Feather RP2040
  • Seeed Studio SenseCAP Indicator
  • Wiznet EVB hat for a Pico (ethernet)
  • Wiznet Pico WS5100S which is the Pico and ethernet on one board
  • Raspberry Pi Debug Module

Other boards

There are many more than i could list. Like ones from Seeed Studio.

This is a neat redesign of the Pico board from Hackaday. https://dmytroengineering.com/content/projects/pro-pico It fixes problems with the original; it has a low noise voltage regulator, a better reference for the ADC, a USB-C connector.

Development

Debug Module

The Debug Module is an RP2040 in a cute little plastic box with a set of cables. Right now I have mine plugged into a Pico board that's plugged into a Grove board with 10 (!) Grove ports on it. I soldered the 3 pin header into the Pico and used the female cable to connect. I put the UART cable to UART0 on the Pico and confirmed serial port messages are showing up properly. The Pico has CircuitPython on it right now. I note there is also a compatible JST connector marked UART on Pi5. I will have to try using that as a console connection sometime.

Next up, I need to see the SWD port do something. I am going to set up Pi5 and use it for development as mentioned above.

Pi 5 for dev

Using a Pi as the development platform is described here, RPI "Getting Started" pdf doc Setting up on Windows is not the same. See the "C/C++ in Windows" section below.

This script installs tools and downloads and builds some tools and sample programs. I put it in Documents/ and it created the pico/ folder and told me to reboot. So I did.

wget https://raw.githubusercontent.com/raspberrypi/pico-setup/master/pico_setup.sh

So, now I can try sending a program to the Pico that's plugged in to the debug module. How to do that? It's Chapter 6 of the Getting Started doc.

The command to use if you wire the Pi 5 directly to the Pico using GPIO pins would be

openocd -f interface/raspberrypi-swd.cfg -f target/rp2040.cfg

This is wrong for me, because I have the Debug Module plugged in to /dev/ttyACM0. (The Pico UART is on /dev/ttyACM1.) The "sudo" is very important. I made an alias for this, "ocd".

sudo openocd -f interface/cmsis-dap.cfg -f target/rp2040.cfg -c "adapter speed 5000"

Then in another window,

gdb hello_world.elf
target remote localhost:3333
monitor reset init
continue

Debian WSL2 for dev

I was able to build executables on the Pi (after all, the instructions are very detailed.) But I don't run a desktop on the Pi so I wanted to do builds on Pearl, my Windows laptop.

I am installing the toolchain into a Debian WSL virtual machine and trying out using Visual Studio Code on there. The instructions are the same as for the Pi.

apt-get install python3

Arduino

Seeed Studio suggests using Arduino to program the RP2040 in the Sensecap D1, but, meh, not an Arduino fan so I have not tried it yet.

C/C++ in Windows + Visual Studio Code

Tracking the official instructions, I ran their installer for Windows.

The examples end up in Documents/Pico*. Examples for the Pico board are in Github.

FreeRTOS

Since FreeRTOS can be used on the ESP32 (on the SenseCap D1), I should look at it for the RP2040 too.

Python

Micropython

See also the Micropython page for now.

CircuitPython

Download from CircuitPython -- make sure you download the correct version, for example, the Rpi Pico version does not work on the Feather (but does work on SenseCap). Adafruit recommends you use the latest beta image, not the stable one.

General notes on VSCode. At one point I gave up on using VSCode with Micropython but last time I tried, it was fine. Maybe I can get Pymakr extension going now that I have figured out CircuitPython.

When you fire up a CP board (any, regardless of who made it, if it runs CP), it will mount a drive and set up a serial port. If you edit files it will automatically restart. Micropython uses a REPL instead of a drive so it's not as easy.

  1. Install CircuitPython on the board. (Power cycle with BOOT button held down then drag the UF2 file onto the drive.) It will reboot itself.
  2. Start Visual Studio Code.
  3. Select the board. There should be a way to do that from the bottom bar.
  4. Open a serial console, also in the bottom bar. You should see it's CircuitPython in the window.
  5. Open the folder.
  6. Edit the file code.py and save it. Saving will force reload the script and that should be visible in the console.
GPS AT6558

This is an M5 Grove module. The GPS receiver chip is the AT6558 (You can tell it's M5 because of the fine little enclosure, Seeed boards tend to be naked.) I put the Pico on a Grove interface board and plugged in the GPS to the UART0 port. There is a crossbar so any port can do anything on the RP2040 so I have to initialize the port, it's marked UART0 on the board so I went with that (GP0,GP1)

Get the adafruit circuit python for your version of CP. Unzip it and put adafruit_gps.mpy in lib/ on the device.

import board
import busio
import time
import digitalio
import adafruit_gps

uart = busio.UART(tx=board.GP0, rx=board.GP1, baudrate=9600, timeout=10)
gps = adafruit_gps.GPS(uart, debug=False)  # Use UART/pyserial
t = time.monotonic()

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
while True:
    gps.update()
    now = time.monotonic()
    if now - t < 1.0:
        continue
    if not gps.has_fix:
        print("no fix")
        time.sleep(.5)
        led.value = True
        continue
    led.value = True
    #print(gps.datetime)
    print("{0:.6f}".format(gps.latitude),"{0:.6f}".format(gps.longitude), gps.altitude_m)
    print(gps.satellites, gps.pdop, gps.hdop)
    t = now
    led.value = False

Crash recovery

I wrote a CircuitPython script that my SenseCap did not like, and it stopped mounting the storage device so I could not edit it. I reflashed it with "flash_nuke.uf2".

(Hold down the hidden reset button + power up to get a "root" storage device then drag and drop). This will put it back in a pristine state, wiping out your code.py file and forcing you to reinstall CircuitPython. Flash_nuke.uf2 has been added to my tool kit.

The errant program was one that tries to play an MP3 piano sample on the internal buzzer. It almost works. Lots of buzzing and clicking though.

Projects

Power management

The idea for this project is to manage a power strip or other interface that can control other equipment including load shedding and watchdog restarts. I want to define an API and talk to it exclusively via MQTT pub/sub messages.

  • Track the Ubiquiti link, and reset the link if the remote sites go offline. Can the UBNT do that on its own?
  • Bring up power to the other components and shut down when not needed to save power.
  • Real time clock calibrated via SNTP
  • Use hardware WDT to reset the Pico if it gets stuck.
  • Monitor a UPS over the USB port.
  • Monitor and reset the onboard ethernet hardware if it gets stuck.
  • Configure IP settings in flash. Initial address is 192.168.100.1.
  • OTA flash updates

The "other components" under control include a FlexRadio and a Windows computer. Incidentally -- both the full Raspberry Pi and the Intel NUC have built in hardware WDT.

Power - Need a regulated clean supply to feed it 3v3 from 12v power.

Resources

Wiznet

https://www.hackster.io/news/wiznet-s-w5100s-evb-pico-clones-the-raspberry-pi-pico-but-adds-a-handy-ethernet-port-too-11125e568928

Wiznet did a WiFi + Pico EVB, too, but I can't find it for sale anywhere. https://github.com/Wiznet/WizFi360-EVB-Pico-C/blob/main/getting_started.md

The CircuitPython page for the Wiznet Pico is here https://circuitpython.org/board/wiznet_w5100s_evb_pico/

Sample code: https://github.com/Wiznet/RP2040-HAT-CircuitPython