Raspberry PI and your SPI Real Time Clock (RTC)

This post documents how I got my DS3234 SPI Real Time Clock working with my Raspberry Pi 2.

I have previously used I²C Real Time Clocks on my Raspberry Pi without a problem. Setting  up these I²C RTCs was as simple as attaching the appropriate wires and loading the kernel module. So you can imagine my disappointment when I attached my shiny new DS3234 RTC, loaded the kernel module and found no rtc devices appearing in /dev − even after I had unbound the spidev modules from the SPI bus.

It seems the world of embedded Linux has moved on and now uses Device Tree overlays − or maybe they always were and I just wasn’t paying attention − to configure the various pieces of attached hardware.

DS3232

The Freetronics I²C DS3232 Real Time Clock Module

Now I am not sure if the transition to using device tree overlays was the cause of the issue or SPI devices always worked this way but it was clear that I would have to investigate how to use device tree overlays to configure my DS3234 . I am not sure if this is the best way to do this, but it is how I got my clock to work.

The DS3234 module I purchased from SparkFun

The DS3234 module I purchased from SparkFun

The following resources provide background knowledge for the next section:

Connect the DS3234

First we need attach our clock to the PI, do do this we need to look up the location of the SPI pins and connect them to the appropriate pins on the clock.  The mapping is provided:  

Pin Mapping
Raspberry Pi Pin Raspberry Pi Name DS3234 Module
25 Ground GND
17 3v3 Power VCC
23 SCLK CLK
21 MISO MISO
19 MOSI MOSI
24 CE0 SS

RaspberryPi 2 and a DS3234 RTC

RaspberryPi 2 connected to the DS3234 RTC

OK! now it is time to configure the software.

Enable SPI

By default SPI is disabled on a fresh raspian install, so we need to enable it.

raspi-config - Advanced Options

Select Advanced Options and press Enter

Select A5 SPI and press Enter

Select A5 SPI and press Enter

Select  and press enter

Select <YES> and press enter

Press Enter

Press Enter

Select  and press Enter

Select <Finish> and press Enter

OK SPI is not enabled and by default will provide spidev devices on the next reboot.

Device Tree

We will create a device tree overlay that will disable spidev on the CE0 slave of the SPI bus and configure the DS3234 RTC to run with it instead.

Make a working directory:

 

Create the file ds3234-rpi-overlay.dts with the following contents:

 

Add the following contents to the Makefile:

 

Make and install the overlay:

 

If you get errors on line two you may need to upgrade your version of the device tree compiler. From the Raspberry Pi Device Tree page :

Warning! − you should not download and run a script from the internet without first examining it to ensure that you are happy it is not malicious. Not that I am saying that there is a problem with this script, I just want you to avoid being the low hanging fruit.

Lets get back to it then shall we?

Ensure the following is 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:

And the kernel module loaded:

And finally, lets check the time:

Awesome! my DS3234 RTC is now working!

In my next post I will look at running the DS3234 RTC on a bitbanged SPI master using the Raspberry Pi’s GPIO pins.

Leave a Reply

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