Bitbanging your SPI RTC on your Raspberry PI

In my previous post I described the steps I followed to get my DS3234 Real Time Clock working on my Raspberry PI 2. It turns out that the audio board we were using on the pinger project had already claimed the SPI port and both Chip Enable (CE) pins so I went searching for an alternative solution.

The DS3234 module I purchased from SparkFun

The DS3234 module we will use on the Bitbanged SPI Bus

That solution turned out to be utilizing the Kernel’s SPI Bitbang capabilities. The following resources provide background knowledge for the next section:

Compiling the Kernel and Kernel Modules

We are going to be using the spi-gpio kernel module to create a new SPI master on the Raspberry PI’s GPIO pins. Since the spi-gpio module is not installed by default we are going to have to build it ourselves. Now for whatever reason I can never seem to get my modules loaded into a stock kernel, so I end up compiling my own kernel and installing it. To do this we first need to install the following packages:

Then grab the kernel source, clone the existing kernel config, and reconfigure it to enable the spi-gpio module:

kernel_config_step_1

Select Device Drivers and press Enter

kernel_config_step_2

Select SPI support and press Enter

Select GPIO-based bitbanged SPI Master and press m

Select GPIO-based bitbanged SPI Master and press m

Then select <Exit> and press Enter, repeat this until you see this screen:

kernel_config_step_4

Select <Yes> and press Enter

With our kernel configuration completed it is time to compile our new kernel, this will take quite some time.

To enable the Device Tree we need to grab some Raspberry PI tools and run the mkknlimg tool on our freshly compiled kernel:

To use this kernel we need to edit /boot/config.txt and set the kernel parameter to our new kernel location (for more information on the /boot/config.txt please read the Raspberry PI config.txt Documentation):

Checking the Kernel

We could just copy the zImage directly to /boot/bitbang_kernel.img but by doing so it seems the kernel does not load the device tree.  We can see this in the logs if we were to reboot and run the following:

Opposed to :

So our Kernel is running and loading the Device Tree, lets check that the kernel modules are loading:

 

Sweet!  We have installed our own kernel and it can load the spi-gpio module, now it is time to create our overlay.

Creating the Device Tree Overlay

Make a working directory

To use the gpio pins we need to understand how to reference them in our overlay, for this we look to the kernel documentation Documentation/devicetree/bindings/gpio/gpio.txt, specifically the following lines:

So if we want to have an active low pin for CE then we need to set the third position to be 1 instead of 0 for active high.

Using the Raspberry PI Pinout I selected the GPIO pins that I wanted to use and then retrieved the Broadcom pin number.

These values can be seen on lines 15 to 19 of the fileds3234-bitbang-rpi-overlay.dts below
SPI Pin Name Raspberry PI GPIO Pin Broadcom pin number Active High Node Specification
MOSI 29 5 0 gpio-mosi = <&gpio 5 0>;
MISO 31 6 0 gpio-miso = <&gpio 6 0>;
CLK 33 13 0 gpio-sck = <&gpio 13 0>;
CE 32 12 1 cs-gpios = <&gpio 12 1>;

With the pins selected we can create the file ds3234-bitbang-rpi-overlay.dts with the following contents:

and connect the RTC to the Raspberry PI

Raspberry PI and a DS3234 ready to use the bitbanged SPI Master

Raspberry PI and a DS3234 ready to use the bitbanged SPI Master

Then we create the file Makefile with the following contents:

 

Compile and install the Device Tree Overlay:

 

Ensure that SPI is enabled in /boot/config.txt:

And enable our overlay by adding the following to /boot/config.txt

Testing the Clock

Now we are ready to test our DS3234 RTC, first we will need to reboot the PI so that the SPI bus and our overlay can be enabled.

After the reboot we can check if our overlay was loaded:

That the kernel modules are loaded:

And finally, lets check the time:

Awesome! my DS3234 RTC is now working on a SPI Bitbang Bus!

 

One thought on “Bitbanging your SPI RTC on your Raspberry PI

  1. M.

    Hello,
    Thanks for this post! It really made the difference while I was trying to suss out spi-gpio. I’m writing a blog post about my experiences and I was wondering if it would be okay if I included snippets of your device tree overlay source and Makefile?
    Once again, thanks for the post!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *