Raspberry Pi RP2040

From Wildsong
Jump to navigationJump to search

Raspberry Pi Pico documentation

Inventory

These are RP2040 boards that I have right now.

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

Raspberry Pi Debug Probe

Port "U" is a serial port. Port "D" is the debugger port.

The Debug Probe 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

This is working as of 2024-Sept-15, yay!

Using a Pi as the development platform is described here, RPI "Getting Started" pdf doc

Another useful tutorial at Electronics Hub

Install tools

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 for some reason, so I did.

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

Using the Electronics Hub instructions to build openocd myself goes like this, (installs into /usr/local/bin) I think this is better because it uses the rp2040 branch, so I am trying it instead of the above. It is perhaps an older version? .11 vs .12 Config files install into /usr/local/share/openocd/scripts

git clone https://github.com/raspberrypi/openocd.git --recursive --branch rp2040 --depth=1
cd openocd
./bootstrap
./configure --enable-ftdi --enable-sysfsgpio --enable-bcm2835gpio

OpenOCD configuration summary
--------------------------------------------------
MPSSE mode of FTDI based devices        yes
ST-Link Programmer                      yes (auto)
TI ICDI JTAG Programmer                 yes (auto)
Keil ULINK JTAG Programmer              yes (auto)
ANGIE Adapter                           yes (auto)
Altera USB-Blaster II Compatible        yes (auto)
Bitbang mode of FT232R based devices    yes (auto)
Versaloon-Link JTAG Programmer          yes (auto)
TI XDS110 Debug Probe                   yes (auto)
CMSIS-DAP v2 Compliant Debugger         yes (auto)
OSBDM (JTAG only) Programmer            yes (auto)
eStick/opendous JTAG Programmer         yes (auto)
Olimex ARM-JTAG-EW Programmer           yes (auto)
Raisonance RLink JTAG Programmer        yes (auto)
USBProg JTAG Programmer                 yes (auto)
Espressif JTAG Programmer               yes (auto)
CMSIS-DAP Compliant Debugger            no
Nu-Link Programmer                      no
Cypress KitProg Programmer              no
Altera USB-Blaster Compatible           yes (auto)
ASIX Presto Adapter                     yes (auto)
OpenJTAG Adapter                        yes (auto)
Linux GPIO bitbang through libgpiod     no
SEGGER J-Link Programmer                no
Bus Pirate                              yes (auto)
Use Capstone disassembly framework      no

make clean
make -j4
sudo make install
openocd
Open On-Chip Debugger 0.12.0+dev-gebec950 (2024-09-15-19:37)
Licensed under GNU GPL v2
For bug reports, read

http://openocd.org/doc/doxygen/bugs.html

embedded:startup.tcl:28: Error: Can't find openocd.cfg in procedure 'script' 
at file "embedded:startup.tcl", line 28
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Error: Debug Adapter has to be specified, see "adapter driver" command
embedded:startup.tcl:28: Error: 
in procedure 'script' 
at file "embedded:startup.tcl", line 28

Build a binary

cd ~/Documents/pico/pico-examples
cd build/hello_world/serial
make -j4

If the build succeeded there should be a hello_serial.elf file.

Run a binary

So, now I can try sending a binary to the Pico that's plugged in to the debug probe. You start openocd then you run gdb and it talks to the board through the debug probe.

If you set permissions properly you could ditch the 'sudo'; see the README for openocd. "Permissions delegation".

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

If you see this then the freaking connector on the SWD port is loose! LEDs should be lit up on the debug probe.

Error: Failed to connect multidrop rp2040.dap0

Once openocd is running, open another window, and run the program.

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

Using VS Code

This means running VS Code on Murre and running openocd on the Pi 5. I tried with Pearl and gave up. Using Windows as an embedded dev platform is just a pain.

In VS Code,
Pico Profile
Pico Profile

Install the pico profile from https://github.com/raspberrypi/pico-vscode/blob/HEAD/scripts/Pico.code-profile and the extensions you need will be installed into it for you. It's slow. When it's done, the left sidebar should have a Pico icon at the bottom for the Pico extension when the profile is active.

It launched a new window automatically after the profile was created.

I still could not get openocd running on Murre because there is no cmsis-dap driver. Oh well. There's still an 'external mode' mode for openocd in the launch.json. That means I have a Pi 5 used ONLY to run openocd. Oh well.

Install the build toolchain on Murre

This page has lots of gory details, kudos to them! Windows instructions but I press on using Murre anyway. https://www.circuitstate.com/tutorials/debugging-rp2040-pico-c-cpp-sdk-projects-using-raspberry-pi-debug-probe-and-vs-code/

See also, https://learn.arm.com/install-guides/gcc/cross/

Ultimate guide, https://github.com/raspberrypi/pico-sdk

apt install gcc-arm-none-eabi gcc-aarch64-linux-gnu
arm-none-eabi-gcc --version
apt install gdb-multiarch binutils-multiarch
# hack for Debian, https://forums.raspberrypi.com/viewtopic.php?t=333146
cd /usr/bin
sudo ln -s /usr/bin/objdump objdump-multiarch
sudo ln -s /usr/bin/nm nm-multiarch

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), and 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