Real Time Clock on Raspberry Pi with FreeBSD 11

The Raspberry Pi doesn’t have any ‘Real Time Clock’ (RTC) module, which keeps actual time.

To use external RTC module DS1307 on the Raspberry Pi with FreeBSD 10 I wrote a small program.
But FreeBSD 11 has a new driver for DS1307.

Here is instruction how to use it with FreeBSD 11 on the Raspberry Pi.

Running FreeBSD 11

FreeBSD 11 isn’t released yet, but we can use latest snapshot available here: ftp://ftp.freebsd.org/pub/FreeBSD/snapshots/arm/armv6/ISO-IMAGES/11.0

uname

Connecting DS1307

Let’s connect DS1307 to our Raspberry Pi and check that it works.

pi-ds1307
To check DS1307 module I use my utilities.

i2c-ds1307
My utility detects DS1307 on address 0x68 but DS1307(4) manual page says that default address for it is 0xD0.

This is because my utilities use ‘standard’ 7-bit I2C addresses, but FreeBSD uses 8-bit I2C addresses: (7-bit device address << 1), leaving the room for the read/write bit.

0xD0 = 0x68 << 1

bc

Updating FDT

To use DS1307 in FreeBSD 11 we need to update FDT for our Raspberry Pi.
To do this we need first to get and extract source code.

svn-sys
After that we can modify file /usr/src/sys/boot/fdt/dts/arm/bcm2835.dtsi:

vi-bcm2835.dtsi
We have RTC device with address 0xD0 on the iicbus 1, so we need to add this description to the file bcm2835.dtsi:

bcm2835.dtsi
After that we should to compile and install our new FDT:

make-dtb
After reboot we can check the results.

unknown-card
ofw1
ofw2
We have unknown device on the iicbus 1.

This is because updating FDT is a necessary but insufficient condition to get working RTC module in the FreeBSD 11.

Updating kernel

We should to add device ds1307 to the kernel file /usr/src/sys/arm/conf/RPI-B:

vi-rpi-b
rpi-b

To compile new kernel we needs something more that the /usr/src/sys tree.
We needs Makefile in the /usr/src directory at least.

The simplest way to have this file is get all source code.
We can add /usr/src path to the svn(1) without removing /usr/src/sys path from it.

svn-src

After that we can compile and install our new kernel:

make-buildkernel1
make-buildkernel2
make-installkernel1

After reboot we can check the result.

dmesg-ds1307

We got our DS1307 device.

Checking DS1307

Let’s look to our new device:

sysctl-ds1307

To be sure that this device really works we need to turn off ntpd(8), check time, turn off our Raspberry Pi for the some days, and then turn it on and check the time.

rc.conf
date1
date2

As we see it works!

NTPD and DS1307

If we turn on ntpd(8) with connected DS1307, after some short period of time our system will go to panic:

ntp-rtc
panic

This is known issue. The workaround is:

sysctl machdep.rtc_save_period=0

See here: http://permalink.gmane.org/gmane.os.freebsd.devel.arm/10991

Advertisements

12 thoughts on “Real Time Clock on Raspberry Pi with FreeBSD 11

  1. Some notes about Raspberry Pi 2

    First, you should use files bcm2836.dtsi instead of bcm2835.dtsi, rpi2.dts instead of rpi.dts, and RPI2 instead of RPI-B.

    Also you should add this two strings after string “bsc1 {” into the file bcm2836.dtsi
    #address-cells = <1>;
    #size-cells = <0>;

    So module for bsc1 should looks like this:

           bsc1 {
                    #address-cells = <1>;
                    #size-cells = <0>;
                    compatible = "broadcom,bcm2835-bsc",
                                  "broadcom,bcm2708-bsc";
                    reg = <0x804000 0x20>;
                    interrupts = <61>;
                    interrupt-parent = <&intc>;
    
                    rtc {
                           compatible = "maxim,ds1307";
                           reg = <0xd0>;
                    };
                };
    
  2. Note about last FreeBSD 11 snapshots.

    I have noticed that RTC stop to works in some last FreeBSD 11 snapshots.
    The message was: “cannot read from RTC”.
    But before we can see: “<Maxim DS1307 RTC> at addr 0x1a0 on iicbus1”

    The game rule has changed once again.

    To correct this we should use 0x68 instead of 0xd0 in the file /usr/src/sys/boot/fdt/dts/arm/bcm2835.dtsi

  3. Vadim,
    I appreciate your comments here, and the general discussion about gpio, and I would be following them if I can figure out how to get GPIO 18 to be recognized as the DCD line in conjunction with the UART, or some other method to get ntp to recognize it as the PPS signal. There is a device driver in Linux that presents a gpio pin specified in config.txt as /dev/pps0, but all the searching i have done has not turned up anything showing how one would tie a gpio pin to ntp when using FreeBSD. I have FreeBSD-arm running on a Cubox-I4pro, using usb devices, so I could do that on the Pi, but i would prefer the >10x better jitter characteristics of the gpio if I can get it to work. Unfortunately I don’t believe the numbers coming out of Debian because there is a minimum constant value at 1907ns, which shows up in searches indicating that others are getting the same number. For awhile ntp would report jitter as 0 on Debian when it dropped below some unpublished minimum, but now it appears to provide a non-zero constant value. 2 other FreeBSD systems being fed from that same gps through a relatively sloppy RS232 driver chip are reporting varying values around 1350 and 1590ns jitter, further indicating a false reading when using Debian. http://www.tndh.net/~tony/GPS-PPS-5v-ttl_232-USB-box.pdf Any thoughts on this would be appreciated. If this comment leads to an interest in doing something similar, I have done an Eagle Cad of that box and would be happy to share the board.

    • Hi! Thank you for interest to my blog.
      The topic of your comment is new for me. I have never hear about PPS signal. I don’t think that FreeBSD supports it.
      It’s interesting for me, but I will think about it some time later.

      • I found a driver reference for the BeagleBoard Black in /usr/src/sys/arm/conf/BEAGLEBONE, but that is specific to the AM335x chip series. I don’t know what it would take to port that to the BCM283x chips. As far as FreeBSD in general though, it has had pps support even in the GENERIC images for over a decade. I found that options was turned off in the IMX6 arm default builds, but is easy enough to turn back on with a local kernel build. What appears to be missing is the device driver.

  4. Thanks for your blogpost and software: very useful!
    I bought 3 cheap DS1307 RTC modules on ebay and they all sucks: the crystal isn’t accurate at all and the clock runs about two times too slow 😦 Waiting for 10 real minutes, the RTC has only increased 5 minutes.

    So it can be OK to keep time between reboots (then using NTP, GPS, DCF77,…), but not for extended periods of time.
    But in this case, I wonder if it wouldn’t be better to keep time in a file on the SD card before shutdown and reload it during boot.

    • It is possible to use ntpdate(8) (or ntpd with -q option).
      ntpdate utility sets the local date and time by polling the Network Time Protocol (NTP) server(s) given as the server arguments to determine the correct time.

  5. Hello, Thanks for your help. I want to add rtc to the BPi and the bananapi.dts format looks different. Would it use the bsc1 bus as well??

    How to determine where in the device tree to add the rtc section. There is no i2c bus section there. Very sparse with SOC and hdmi

  6. Where in the device tree for the BananaPi A20 would an rtc} compatible “maxim,ds3231” entry go. I see no i2c section and the bananapi.dts is very sparse with only SOC and HDMi sections. They do link to other GNU/DTS like sun7i-a20-bananapi with an i2c0 and i2c2 sections.
    Would the BananaPi use a bsc0 or bsc1 section like rpi?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s