Raspberry Pi Pico and Pico W Project Templates
Tuesday, September 5th, 2023
There are somethings in software development that we do not do very often and because we perform the task infrequently we often forget the nuances (well I do). One of these tasks is creating a new project.
In this post we will look at one option for automating this process, GitHub Templates. This work is partially my own work but also a case of bringing together elements of other blog posts and GitHub repositories.
All of the code discussed below can be found in the PicoTemplate repository and this should be used as a reference throughout this post.
Starting a New Project
Several days ago, I was in the process of starting a new project for the Raspberry Pi Pico W using the picotool. For those who are not familiar with this tool is is meant to automate the project creation process for you. You simply tell the tool which features you are going to be using and a few other parameters like board type and it will generate a directory containing all of the necessary code and project files.
Sounds too good to be true. For me it was as no matter which feature list I requested I could not get an application to deploy to the board and talk to the desktop computer over UART or USB.
At this point I decided to take this back to basics and get Blinky up and running. The problem definition became:
- C++ application
- Blink the onboard LED
- Support both Pico and Pico W boards
- Standard IO output over UART or USB
It would also be desirable to automate as much of the process as possible.
Blinky
One complication with the Pico boards is that they both use a different mechanism to access the on board LED. The Pico has the LED wired directly to GPIO 25 whilst the Pico W uses a GPIO from the onboard WiFi / Bluetooth chip (hereafter referred to as just wireless). This means we have different versions of Blinky depending upon which board is selected. This also means that the Pico W board also needs an additional library to support the wireless chip even if we are not using any wireless features.
The simplest way of doing this is to use a template directory to contains the main application code and the project CMakeFileList.txt file for each board. We can then copy the template files for the appropriate board type into the sources directory.
Keeping with the theme of forgetting the nuances, we will add some scripts to perform some common tasks:
- Configure the system (copying the files to the correct locations)
- Build the application
- Flash a board using openocd
Our first task will be to set the system up with the correct main.cpp and CMakeFileList.txt files and put the files in the correct place. Checking the default CMakeFileList.txt file we see that the project is named projectname which is not very informative so the configuration script will also rename the project as well.
The purpose of the build script should be obvious, it will build the application code and also offer the ability to perform a full rebuild if necessary.
The final script will flash the board using openocd using another Raspberry Pi Pico configured as a debug probe. This was discussed a few weeks ago in the post PicoDebugger – Bringing Picoprobe and the PicoW Together. This set up also has the advantage of exposing the default UART to the host computer.
Using the Scripts
Using the scripts is a three step process, one of which is only really performed once.
The first step is to use the configure.sh script to copy the files and rename the project:
./configure.sh -b=picow -n=TestApplication
After running this command the src/main.cpp file will contain the code to blink the LED on a Pico W board and the project will have been renamed TestApplication.
Secondly, we can build the application using the build.sh script:
./build.sh
Finally, the application can be deployed to the board using the flash.sh script.
./flash.sh
The UF2 file (in the case above, TestApplication.uf2) can also be copied to the board if a debug probe is not available.
Scope Creep
The story was supposed to end here but this is not going to be the case.
There are some improvements that we can look at to make the system a little more comprehensive. These include:
- Add a testing framework
- Create a Docker file for build and testing
Both of these items are currently work in progress.
Acknowledgements
This project has been inspired by several others on github:
- [Raspberry Pi Pico Examples](https://github.com/raspberrypi/pico-examples)
- [Raspberry Pi Pico Template](https://github.com/cathiele/raspberrypi-pico-cpp-template)
Conclusion
The initial requirement of creating a template for Pico and Pico W development could have been achieved simply by creating two different templates, one for each board type. This would arguably have been quicker and less complex.
The addition of the testing framework and docker container into the requirement would have resulted in some duplication of work in each template. This made bringing the two templates together in one repository more logical as errors or additions in one project type are automatically part of the other board template.