Working with Raspberry Pi

Abate: Given your interest in the Raspberry Pi, you must have home handy RPi-based projects or applications running at home or at work. Can you tell us about any RPi-based projects that stand out?
 
Gay:
The PySpy program written for the book Exploring the Raspberry Pi 2 with C++ was perhaps the most interesting thing I have written for the Pi so far. This involved developing a kernel loadable module that would give the command line program access to the Linux internals. The objective was to use the kernel facilities to allocate a DMA channel so that all GPIO inputs could be read quickly by the DMA controller. The GPIO hardware itself was limited to an overall effective sampling rate of about 1 MHz. A remaining challenge was to safely deallocate the DMA resources if the PySpy command was interrupted for any reason. Otherwise, a failure could eventually result in disaster, with the kernel exhausting its DMA channels. 
 
RPi 4 project
RPi 4 project
Since then, I have been chipping away at squeezing a headless Raspberry Pi 4 into a small repurposed audio/video rack mount case, with a built-in switching power supply and 5V fan. This fits nicely underneath my second desktop monitor. The photo shows the edge of the Pi 4 sticking out at the bottom right. A black USB cable connecting the internal SSD pokes out of the front panel to connect to a USB 3 port. The upper switch controls a CPU fan, while the lower one controls power. The BNC connectors were intended to be connected to various GPIOs, though this has not been wired yet. The banana jacks provide +5V (red) and ground (black).

FreeRTOS for ESP32-Arduino

Abate: Your new book, FreeRTOS for ESP32-Arduino: Practical Multitasking Fundamentals (Elektor 2020), offers readers interested in embedded programming an accessible introduction to FreeRTOS and the ESP32 Arduino environment. Why FreeRTOS? Which problems does it solve?
 
Gay:
In the Arduino context, everyone drives events from the loop() function. The loop() function is called repeatedly so that your code can poll each device that you service. Is there some serial input received? If so, then take some action on it. Is a button pressed? Take some other action. In effect, your application becomes one big polling loop, invoking actions when polled events are detected. The larger the set of polled events however, the state management becomes more problematic. Excessive polling and complex state management leads to programming errors and thus more time spent debugging. 
 
Interested in embedded programming? Check out FreeRTOS for ESP32-Arduino
FreeRTOS for ESP32-Arduino (Elektor, 2020)
FreeRTOS allows you to split that into simpler dedicated tasks. One task would be focused on the incoming serial data, while another task debounces push buttons and so forth. Each task is written as if it is the only program in the system. This keeps the code clean and simple, which significantly eases the software maintenance. In many cases the individual tasks can block on the I/O operation involved eliminating the need to poll.  Another alternative is to apply interrupts to detect asynchronous events like button presses. FreeRTOS then provides safe event notification mechanisms from the Interrupt Service Routine (ISR) to the servicing task.
 
Finally, FreeRTOS also has task priority baked into it. The highest-priority task will get first crack at reading or writing a message queue, for example. The Arduino loop() on the other hand, runs everything at the same priority.

Arduino projects

Abate: Are you new to Arduino, or are you a long-time user?
 
Gay:
I’ve used Arduino almost from its beginning. As an advanced user, I only use it when it is convenient because I use emacs and make from the command line. For the ESP32, I’ll usually create an ESP-IDF project using a Makefile. However, the user contributed Arduino libraries do provide a compelling reason to use the Arduino framework.
 
The Arduino environment is very good for those who are learning and for those who don’t want to manage a Makefile. It removes the many pain points that students would otherwise stumble over at the beginning. Starting with the simple software download and install, it promotes early success.
 
Abate: Any interesting Arduino projects on your personal workbench?
 
Gay:
While not Arduino-based, I did a breadboard of an ESP32 SNTP clock so that it would auto connect to our Wi-Fi and download the atomic clock time from the University of Toronto time server pool. After that initial startup, it uses the ESP provided SNTP protocol keep the clock synchronized and displays the time on 14-segment LEDs. This project uses the ESP-IDF framework, which of course includes FreeRTOS. Once per minute, at a random time, the current date is displayed without the year, in the form of “WedJul22”, for example. I found it useful to be reminded of the day of the week, when I get buried in work! 
 
ESP32_SNTP
The beadboard of an ESP32 SNTP clock.
The plan is to eventually add an OLED or TFT display to show some local weather information using a service like openweathermap.org. The 14-segment LED units I used were sale priced because of the mixed red+yellow digit pairs. But I have grown so accustom to this arrangement that I plan to use them in the final build. The color differences highlights the hour, minute, and second columns.