I’m going to focus mostly on some design decisions and how I went about writing an SPI interface using Go on a Raspberry Pi. I assume my readers have a basic understanding of what a Raspberry Pi is, and how basic electronics work. If not, read on anyways and I will be sure to include some valuable resources below.

Why Go?

In a past life I worked on hardware interfacing software, and the first thing I can tell you is that I hate C. Don’t get me wrong, I understand the appeal of having lightning fast code and the ability to manipulate memory and low level functions. I also understand the headache of writing concurrent C code, and anyone familiar with Go knows that this is where it shines.

The project that first got me interested in using Go for embedded applications was one where we decided to use a Raspberry Pi Compute Module 3 to interact with ADC (analog to digital converter) components, and collect data using several of these components.

I quickly threw together a prototype in Python using the standard Python GPIO library, and was satisfied with the initial results. It was apparent however that for a more industrial solution we needed an application that was:

Faster — Able to support more operations per second at lower memory and cpu usage.

— Able to support more operations per second at lower memory and cpu usage. Compiled — We didn’t want anyone to be able to pop out the Compute Module and steal our source code.

— We didn’t want anyone to be able to pop out the Compute Module and steal our source code. Maintainable — We had a very small team, and none of us were very experienced in firmware so we wanted to abstract up a bit if possible.

I considered C++ at first. C++ is about as fast as it gets, and is a compiled language. I’m quite comfortable in C++ and have written many applications using it, but we ultimately decided on Go instead for the simple fact the concurrent programming in Go is as easy as it gets. C++ may run a bit faster, but we knew that if we wrote this particular program in Go, it would likely be about half the size (in lines of code) and we were more confident in our ability to keep the code clean and maintainable.

It is important to note that the program in question was doing a lot more than just the data collection via SPI interface with an ADC component. There were user inputs, data displays, etc. It was to be a highly concurrent program.

What is SPI?

SPI stands for serial peripheral interface. I don’t want to get too far off track, but basically it is just a protocol for a master (your program) to communicate with hardware (like a thermometer or analog to digital converter). If you want to learn more check out the link in the picture’s caption.

My Environment

Device: Rasbperry Pi 3B (We used the compute module for production)

IDE: VS Code

Remote Editing: Check out this tutorial to edit code on the pi using VS Code remotely

Hardware: Breadboard, jumper wires, an ADC that uses an SPI interface

OS: Raspbian

Implementation

First things first, I needed a great GPIO package. For this project, we built the code to be able to use any of the GPIO pins on the Pi. I used Dave Cheney’s library: https://github.com/davecheney/gpio

The code below should build and run. Please keep in mind that if you are going to use this code, you will need to change the pin numbers to match the pins you used to connect your GPIOs to your ADC.

Hope you enjoyed the explanation!

Donate

BTC: bc1qvw0ytfntx6zs0lfsruem6xwj0mewng523ktatp

ETH: 0x3d737324ed6Ac204a809d17bc4807C6D2a05D6eD

BCH: qz5j4ypu4wf2x3960elyyxz4vk0z2p9lagfh52xq59

Nano: xrb_1ipdmx5ck6fwc1hkt3fg631z6twjo9e5m6e9dbenm65sbb3e4hgmd1ygxnq3