I recently picked up a new MacBook Air with the latest M2 chip and while I won't be fully transferring away from my x86 MacBook Pro anytime soon (still figuring out the complex world of emulators), I still wanted to start experimenting with how working on an ARM-based host system would be like.
I also dreamed up a Halloween-themed project based on the Raspberry Pi Pico so I figured the best place to start on my new M2 Mac would be to install the necessary tools to compile and upload code to an RPi Pico from the command line. I was also surprised at how much I forget that I can treat my Mac OS host like a Linux OS host. And yes, I know Mac OS has changed up things like their proprietary file system, but for the most part one can treat the approach to Mac OS the same conceptually as approaching a new Linux distribution. Because overall, it still comes down to the basics: figuring out what package manager to use and finding the appropriate source code to build up whatever application or tool you need.
In this instance, to compile and upload code to the RPi Pico, the requirements are the following:
- The GNU ARM toolchain for cross-compilation of RPi Pico application source code.
- A package manager for Mac OS to install requirements such as CMake.
- The RPi Pico SDK with example application source code repository.
At first, I was stumped by how I was going to install the GNU ARM toolchain as it very clearly states that it only supports x86 host machines at the moment. However Apple's translation software, Rosetta 2, pleasantly surprised me here and everything worked just fine when I took a blind shot in the dark by downloading the installer for Mac OS from ARM's website and ran it.
Download the GNU ARM toolchain installer for Mac OS from ARM's website here, and pick the latest version that’s not beta (which was 11.3.Rel1 at time of writing). Launch the installer package, arm-gnu-toolchain-11.3.rel1-darwin-x86_64-arm-none-eabi.pkg
, by double-clicking on it in the Downloads folder. Then simply follow the steps in the installer GUI as prompted.
This results in the ARM toolchain being installed to /Applications/ArmGNUToolchain
. It is important to note that the /Applications
and ~/Applications
directories are NOT the same on Mac OS, so be careful later on when setting environment variables for other things like the RPi Pico SDK.
By default, Mac OS does not have a command line package manager installed. While there are several options out there, I've personally been a fan of MacPorts and Homebrew. I usually have both installed because some code bases have better support for one versus the other. At the moment, I find that Homebrew works a little better with the M2 macs so that's what I'm going with here.
Pull down the source code for Homebrew and run the install script for it using curl
:
whitneyknitter@Whitneys-Air ~ % /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
After the install script runs successfully, it will print out directions for the commands you need to run to set the necessary environment variables in your system in order to use Homebrew. For me, this looked like the following:
whitneyknitter@Whitneys-Air ~ % echo '# Set PATH, MANPATH, etc., for Homebrew.' >> /Users/whitneyknitter/.zprofile
whitneyknitter@Whitneys-Air ~ % echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/whitneyknitter/.zprofile
whitneyknitter@Whitneys-Air ~ % eval "$(/opt/homebrew/bin/brew shellenv)"
Once the necessary environment variables are set for Homebrew, it's a good idea to test it out using something like the help flag:
whitneyknitter@Whitneys-Air ~ % brew help
The last requirement that needs to be satisfied before we can start building C applications for the RPi Pico is to install CMake. Using Homebrew, this is very simple:
whitneyknitter@Whitneys-Air ~ % brew install cmake
Then verify CMake installed appropriately by running it with the help flag:
whitneyknitter@Whitneys-Air ~ % cmake --help
I found the Getting Started with Raspberry Pi Pico guide from the official Raspberry Pi website to be very helpful, so I did use it as a reference for the following steps.
Start by creating a directory somewhere like in your home directory (~
) to clone the RPi Pico repositories into:
whitneyknitter@Whitneys-Air ~ % mkdir -p rpi_pico
whitneyknitter@Whitneys-Air ~ % cd ./rpi_pico
NOTE: you can treat the Home directory (~
) in Mac, just like you would in Linux (~
).
Clone the RPi Pico SDK into the directory and pull down the latest version:
whitneyknitter@Whitneys-Air rpi_pico % git clone -b master https://github.com/raspberrypi/pico-sdk.git
whitneyknitter@Whitneys-Air rpi_pico % cd pico-sdk
whitneyknitter@Whitneys-Air pico-sdk % git submodule update --init
whitneyknitter@Whitneys-Air pico-sdk % cd ../
Install RPi Pico ExamplesAfter changing directories back into the top level directory (that the SDK was cloned into, which is ~/rpi_pico
in my case), clone the RPi Pico examples repository and pull down its latest version as well:
whitneyknitter@Whitneys-Air rpi_pico % git clone -b master https://github.com/raspberrypi/pico-examples.git
whitneyknitter@Whitneys-Air rpi_pico % cd pico-examples
whitneyknitter@Whitneys-Air pico-examples % git submodule update --init
Next, create a directory in the pico-examples
repository called build
:
whitneyknitter@Whitneys-Air pico-examples % mkdir -p build
whitneyknitter@Whitneys-Air pico-examples % cd build
Within the build
directory, set the path variable for where the RPi SDK is located, as well as where the GNU ARM toolchain is located on the host machine:
whitneyknitter@Whitneys-Air build % export PICO_SDK_PATH=../../pico-sdk
whitneyknitter@Whitneys-Air build % export PICO_TOOLCHAIN_PATH=../../../../../Applications/ArmGNUToolchain/11.3.rel1/arm-none-eabi
whitneyknitter@Whitneys-Air build % cmake ../
With the environment variables set for the RPi Pico SDK and the GNU ARM toolchain, change directories into whichever application you want to build and run make (I chose the blink LED here for the sake of the demo):
whitneyknitter@Whitneys-Air build % cd ./blink
whitneyknitter@Whitneys-Air blink % make -j4
This ultimately results in all of the necessary output files such as blink.elf (for the debugger) and blink.uf2 (to flash the RPi Pico with) being generated in this directory.
The easiest way to upload code to the RPi Pico is to mount it as a USB mass storage device and drag & drop the binary data file (.uf2) for the compiled application to it.
To mount the RPi Pico as a USB mass storage device, simply hold down the BOOTSEL button on it as you plug the microUSB connector into the host machine (my M2 MacBook Air in this case).
You'll then see it appear in Finder just like a USB flash drive would. Which is where the binary data file (.uf2) is dragged & dropped to. So in this case, I'm dragging & dropping blink.uf2
from ~/rpi_pico/pico-examples/build/blink
to RPI-RP2
in Finder.
After dragging & dropping the.uf2 file to the RPi Pico in Finder, it will automatically flash and start running the application. This will also cause Mac OS to complain that the USB mass storage device the RPi Pico was mounted as did not eject properly, but you can safely ignore this warning.
Immediately after seeing the "Disc Not Ejected Properly" warning, the LED on the RPi Pico will start blinking, indicating that the blink application is successfully running.
At this point it's pretty straight forward to develop my own C application for my upcoming Halloween project within the pico-examples
directory and build it.
Comments