This semester I’m helping with some undergraduates studying a Hardware-Software interfaces course. The software is C and assembly, and the hardware is the raspberry pi. For a long time I’ve wanted to try out using a raspberry pi, so this seemed like a good opportunity to jump in and try it out.
This course just bypasses all of the easy-to-use python programming (which seems to be the oft-recommended entry point) in favour of low-level programming. I’m not a massive fan of this, largely due to how difficult it is to debug everything. VS Code, my go-to IDE doesn’t seem to like playing with C development on ARM. So all I have to help me are compiler warnings and print statements. But as a learning tool, I begrudgingly accept that it is useful and worth experiencing at least once for new programmers.
I bought for myself a Raspberry Pi 4B. If you’re going to get a mini computer for embedded technology and efficient small programs, of course you go for the model with the highest specs 😎. I got a bunch of wires, LEDs and a 16×2 LCD display. I bought from The Pi Hut and CPC. The latter likes to wrap everything in a lot of plastic and seems more suited to bulk purchases, but the former is more expensive. But both have good selections of hardware to buy.
I didn’t bother buying a power supply because goodness knows I have enough of them around the house already. No sense in creating yet more electronic waste. The Pi 4 needs a USB-C connection supplying 5v, which is conveniently met by the OnePlus charger, which I have from my phone.
Setup
The setup of the Raspberry Pi 4 is very simple. I bought an SD card, unzipped the appropriate OS files onto it, plugged it into the Pi, and was on my way. Note if you’re using the case for the Pi 4: Put the SD card in after you put the board in the case, otherwise the SD card won’t fit and you might break something trying to fit it in the case.
Out-of-box, it did need a keyboard and mouse to fully set it up. This posed an issue as all I had to hand was a spare mouse, and the Pi doesn’t have a built-in on-screen keyboard application. There’s a feature for the Raspbian OS team to include. I did a bit of jiggling with a USB drive to copy and paste my Wi-Fi info over in a text file, turned on SSH n the config settings, and then got started using SSH.
Programming
The course I’m assisting with offers a good introduction to how you map the Pi’s GPIO pins into memory, and how you write the appropriate commands into the right registers when controlling them. This is where I fell at a bit of a stumbling block. The Pi 2/3 has a manual which talks you through all you need to know for this kind of use of the BCM283x chipset (PDF, Chapter 6). However, the Pi 4B uses a different chipset (BCM2711). I tried to find the manual for the Pi 4, but the Raspberry Pi foundation doesn’t seem to have published one yet 🤦♀️.
After much internet scouring and experimentation I eventually figured out that the GPIO pins and registers follow the same rules as given in the manual for earlier Pi models, so it can still be used. The only difference is the offset required to access the GPIO when memory-mapping it. Where BCM283x lists it as 0x3F200000, the Pi 4 with BCM2711 should instead use 0xF3200000. For future reference, and just so this knowledge exists elsewhere than one single forum, run the following command to figure it out:
cat /proc/iomem | grep gpio@
Now that I figured out how to actually map the memory, the rest was easy. I got a breadboard wired up, lights blinking and buttons responding to being pressed. It’s fun when you see something physical result from your programming!
I now need to figure out how to use the LCD display, which seems a lot more involved, and according to the course materials I’m following, requires hacking in assembly 😷.