MC2023 Chapter 1: Hello World and blinking LEDs

Created: 2023-01-01
Updated: 2023-01-13

Welcome to the first "chapter" of my Year of the Microcontroller!

2021-01-01

Not only am I starting this year off with a bang on Day 1, I even prepared yesterday by getting out everything I figured I would need.

Here’s the setup as seen on the landing page:

A photo of my cardboard raspberry pi computer connected to a pi pico microcontroller on a breadboard with some other parts and accessories

(Scroll down to see what this looks like now!)

Today was about getting my feet wet as quickly as possible. I’d already learned how to develop for the RP2040 with MicroPython when I made my Multitimer in 2021. So this time, I wanted to start with the C SDK.

I’m working from the official Getting started with Raspberry Pi Pico PDF:

I really like it so far. Since I’ve chosen the route of least friction by using a Raspberry Pi 4 as my "host computer", I was able to use the pico_setup.sh script without modification and everything just worked. I’m glad, too, because I can see that it would have been a bit of a chore to setup everything manually.

"Doing everything manually" is my usual mode of operation for hobby projects, but I’m just really not interested in the software ecosystem deep-dive yet. I want to get into programming the target architecture ASAP.

By the way, I’m using the "Lite" version of the Raspberry Pi OS because I prefer to work at the terminal on relatively low-powered machines (it boots faster and I feel more "in control" with less "magic" at the command line).

Today was all about making sure I had all the right cables and everything worked. I hadn’t even installed my favorite baseline tools like vim or ag (The Silver Searcher) yet.

Tomorrow, I’ll actually try to program the Pico!

Blog Note: I debated dating these entries and just calling them "Entry 1" or "Day 1" or whatever. There will be gaps. But I decided dates would be easier in the long run and I’m not afraid of hiding the fact that there’s no way I’ll be able to get to this every single day and I have nothing to hide. As I said, there will be gaps.

2021-01-02

Today I learned how to use picotool to talk to the dev board and load compiled binaries.

Oh, right, the binaries. So I went through Chapters 1 through 3 of the aforementioned PDF. Chapter 3 has you compiling an LED blinking program, the "Hello World" of the microcontroller universe.

I’ve done the cmake and make process before as an "end user", but I’m not a C dev by any stretch, so cmake is still pretty opaque to me. I know Makefiles, I just can’t write them. (I mean, I can and I have, but I always have to look everything up and I kinda hate them - and this is coming from someone who has no problem with Bash scripts.)

The compilation process creates several output files including blink.elf and blink.uf2. The UF2 format is interesting. It’s a format created by the evil Microsoft corporation specifically for flashing microcontrollers over USB:

Back to picotool, the process for getting a .uf2 file on the Pico via USB is:

  1. Connect the Pico to the host computer via USB while holding down the BOOTSEL button

  2. Check that it looks good with picotool info, then load the compiled program with picotool load foo.uf2, then restart the RP2040 to run the new program with picotool reboot.

The LED blinks!

The compiled binary is upwards of 40Kb which is…​a lot for blinking an LED. I guess most of that is the SDK’s standard library?

Well, this has been a great second day. I’m really hitting the ground running.

Next will be Chapter 4 in the PDF, which is a real "Hello World" over a serial connection, which is the other Hello World of the microcontroller universe. :-)

2021-01-03

Hello world! Serial communication via USB.

A screenshot of my raspberry pi computer terminal showing Hello World! message thrice

2021-01-04

Soldered pins to the SWD header so I can use my handy little connector cables to connect it to the host computer’s GPIO. Showed the kids the soldering process.

Here’s the wired connection:

photo of three wire connection from pi pico swd header

(By the way, the cable/wires here are from a cheap pack I purchased from Amazon containing multiple ribbons of 40 connectors in a rainbow ribbon that you can tear off, as I have with this trio. The set came with male-to-female, male-to-male, and female-to-female. Super duper handy. Search for "Dupont ribbon" and you ought to find something similar.)

"SWD" is the ARM processor’s JTAG implementation:

The big success today was finding my dang cables and other equipment in the house after not seeing some of them for over a year.

2021-01-05

Today was actually testing the SWD connection. Made it through Chapters 5 and 6 of the Getting Started PDF.

It is very cool to be able to load new programs onto the Pico without having to phsyically disconnect the USB cable and reconnect it while holding down the BOOTSEL button!

Also very cool to be able to connect with GDB to debug a program remotely!

photo of raspberry pi terminal screen showing gdb remote session

Both of these things use OpenOCD (Open On-Chip Debugger): https://openocd.org/

I’ve been using GDB for a while now (see Assembly Nights Season One), but I barely understand what I’m seeing here. Still, it is very, very cool.

2021-01-06

Today I connected the UART connection. UART is an amazingly long-standing serial connection standard created by Gordon Bell of DEC for the PDP-1 circa 1959:

The openocd load command line to load an .elf file onto the RP2040 is so crazy long that I made an alias for it called pooload ("Pico OpenOcd LOAD") to go along with the other p- aliases I’ve created for the Pico so far. The advantage of such a juvenile name for the alias is that I memorize it instantly.

$ pooload blink.elf # LOL

Using both the SWD connection from yesterday and the UART serial connection via GPIO on both Raspberry Pi devices ("Model 4 B" computer and "Pico" RP2040 microcontroller) to flash a serial Hello World program and then read its output is super sweet and satisfying.

On the host computer side, you gotta enable UART serial communication via GPIO through the pi setup script. Then you can talk with ye olde minicom:

$ minicom /dev/serial0 # Command not correct and we also specify baud rate

photo of raspberry pi pico connected via swd and uart pins to a raspberry pi 4 computer gpio

And one point, I "fixed" SWD communication errors by rebooting the host computer. (Reconnecting and restarting the Pi Pico mcu itself had no effect.)

Info : Read incorrect DLIPDR 00000000 (possibly CTRL/STAT value) when selecting coreid 0
Info : DAP init failed

This was almost certainly due to wiggly wires being moved too much and not making a great connection. Not a software issue.

Here’s an identical issue posted on the SDK repo: https://github.com/raspberrypi/pico-sdk/issues/327

Gotta fix those wiggly wires! Right now, my setup is super silly, with the Pi 4 hanging off the back of my cardboard setup!

2021-01-07

New "cardboard computer" setup! I kinda figured all along that I would turn my Raspberry Pi "luggable" into an all-in-one microcontroller workstation and that’s exactly what I did tonight.

Here’s the new look from the front:

photo described below

I really like this side-by-side combination with the screen on the left and breadboard on the right. Now the entire setup has a tiny footprint (which is great for something I plan to have out all year long!) but still leaves everything extremely accessible.

It’s wild that even this truncated "60%" keyboard is still wider than both my display and full-size breadboard combined. Microcontroller stuff is tiny and I love it.

And here’s the back:

photo described below

In this image, you can see the Pi 4 computer tucked under the screen but still right next to the breadboard. This placement is entirely due to wire length. Some of my cables are too long and some are a bit too short, but it works. As you can see, the entire back of the computer remains open for change. Hardly any of the original cardboard even remains!

I also have my "off switch" visible here. It’s a stand-alone power switch for standard US outlets. Much handier than actually unplugging the Raspberry Pi USB power transformer from my extension cord each time I want to power down!

Not shown is a bead of hot glue I ran along the edges of the SWD and UART connections on the Pi computer’s GPIO. This (plus the more rigid placement of the two devices) seems to have solved my flaky connection issues as expected.

After putting this together, I also gave it a test run by completing the Getting Started PDF by doing "Chapter 8. Creating your own Project". (Later chapters are about building on other environments or using tools other than The One True Text Editor, which is, of course, heresy and madness to even consider.)

Setting up that cmake stuff is somewhat unpleasant, especially if you’re just going through the motions like I am at this stage. The important thing I always have to remind myself with things like this is that you’re only going to have to do it once for the life of the project. Which amortizes to nothing for a real project.

What I like about Chapter 8 is that you end up making your own Hello World program from scratch that not only prints the "Hello World" phrase to serial but also blinks an LED for the ultimate hello world combo. It also has you debug it via SWD and everything. Chapter 8 is a great summation of everything you’ve learned so far.

And this is why I’m going with the Raspberry Pi ecosystem for this stage of my microcontroller learning: excellent beginner documentation.

2021-01-08

The official pico-project-generator (Python) tool sounded interesting, but even though it has a CLI interface, it won’t run on Lite because it still attempts to use X libraries. Oh well.

Played with the blinking LED to get more practice using OpenOCD to flash and reboot the Pico. It seems most of the family can see the LED blink down to about 20ms. Interesting.

I think this is a good place to close "Chapter 1". I’ve got the base system for my learning and experimentation working. The next thing will be to actually start a real project using the C SDK.