Skip to content

A simple but fully-functional Striking Clock firmware for the Raspberry Pi Pico W, written in Zig, using Arduino-Pico and PlatformIO.

Notifications You must be signed in to change notification settings

debrisapron/zig-arduino-pico-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zig-arduino-pico-example

A simple but fully-functional Striking Clock firmware for the Raspberry Pi Pico W, based on the earlephilhower Arduino-Pico core, using PlatformIO, with the core logic written in Zig.

Hardware

If you want to actually make the thing, wire Pico GPIO 0 to one terminal of a small speaker (I used this one), and Pico ground to the other terminal1. Plug the Pico into a power source. That's it.

To build & install

Please note it must be a Pico W, this firmware uses NTP!

  • Install Zig
  • Install PlatformIO
  • Set the SSID, PASSPHRASE, and TIMEZONE consts in src/core.zig
  • Plug in the Pico W
  • ./build-core.sh && pio run -t upload

Why?

Zig is a small, highly rigorous & extremely performant language for close-to-the-metal programming, ideal for embedded systems. The earlephilhower Arduino-Pico core is a powerful omakase-style framework, exposing APIs for almost the entire Pico hardware stack. Furthermore, the Arduino ecosystem has hundreds (thousands?) of fantastically useful libraries for almost anything you might want to attach to an MCU. However Arduino-Pico and other Arduino libraries are generally written in C++ and as such cannot be used from Zig.

How?

  • We have a main.cpp with the standard Arduino setup and loop entry points. These call into equivalent entry points in our Zig library.
  • We have a HAL (Hardware Abstraction Layer) written in C and C++ containing all the API functions exposed to Zig. This is split into a standard C header file & a C++ implementation file.
  • We have a Zig source file which exports its own setup & loop entry points.
  • The same file also imports our HAL header file using Zig's C translation capability.
  • We have a build-core.sh script which uses zig build-lib to compile a binary library file, making sure to point it to the location of the HAL header file.
  • Going the other way, we have a plaformio.ini file which contains linker flags, so when we run pio run the linker can find the Zig-generated binaries.

Note that this design also makes it easy to test core.zig, since we can simply replace the HAL with a mock implementation.

Areas for improvement

  • It would be good to do the time and date stuff in Zig, however the docs around this are rather sparse at the moment.
  • I tried to use the Zig build system to compile the object files & kick off pio run but I ran into terrible problems trying to get the target flags correct. I'm not sure why this is so hard, I'm probably just being dense.

Footnotes

  1. I don't think it matters which way round they go but I also can't be bothered to look it up right now, so caveat emptor.

About

A simple but fully-functional Striking Clock firmware for the Raspberry Pi Pico W, written in Zig, using Arduino-Pico and PlatformIO.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published