Timed bootup of the Rasp Pi with a real-time clock

Lead Image © Joerg Michael Gehrke, 123RF.com

Tick Tock

The Raspberry Pi has many amazing features, but it is missing a clock. We'll show how to add a real-time clock chip to your Pi.

Whether the frugality concerns that played such a central role in designing the Raspberry Pi led to the decision to omit a clock – a built-in feature on most personal computers – is not clear. Whatever the reason, the Rasp Pi has no clock with which to tell time or set an alarm, and this omission has proven inconvenient for many Rasp Pi users.

Many tasks you might want to assign to the Raspberry Pi require an on-board clock. For example, many network services require accurate system time, and some embedded systems require timed remote bootup or intricate synchronization with other devices. All you need to retrofit your Raspberry Pi with a clock is a circuit board you solder yourself, a short C program, and a bit of adjustment to the system.

In this article I describe how to outfit your Raspberry Pi with a Real-Time Clock (RTC) chip. An RTC chip has an integrated quartz oscillator used for incrementing a counter. The computer can then convert the oscillator output to a human-readable time of day and date. In this case, I'll use the MCP79410, an RTC chip form Microchip Technology [1]. The MCP79410 offers two independent alarm settings that you can set for up to one year into the future.

Unlike other RTC chips, the MCP79410 stores time stamps from the last power outage and the subsequent restoration of power. The chip also offers the option of fine tuning the seconds interval in microsecond steps. However, the driver running under Linux only supports the chip in the basic functions of setting the time, reading the current time, and accessing internal SRAM.

Functional Specifications

The functional specifications for carrying out the solution described in this article include the following:

  • The correct time of day after each start of the system, even after a power outage lasting several hours.
  • A watchdog timer to shut down the system in the case of boot failure.
  • Automatic restart after a power outage.
  • Manual restart by button after system stop.
  • Remote time-controlled restart after a system stop.
  • Automatic separation of all power consumers from the power supply after a system stop.

The Rasp Pi already offers two kinds of manual restarts. You can either manually unplug the power supply and then plug it back in, or you can retrofit the Rasp Pi with a reset button on pin P6 and activate this after the system stops. In contrast, a restart following a power outage works just like any system start. Ultimately, it doesn't matter to a PC why the power is back on.

Automatically separating the Rasp Pi from the power source after shutdown is a good idea, especially for systems running on a battery. This prevents the parts that permanently need power, like the 1.5mA power-on LED and other less visible components, from drawing power.

The main goal of the schematic in Figure  1 is to control all of the functions from the specifications entirely via the I2C bus [2] of the Rasp Pi, which can handle multiple devices. This design keeps the precious GPIO pins free and also reduces connections to a minimum. (See the box titled "How It Works.")

Figure 1: The goal of the circuit on the extension board is to control all functions via the I2C bus so that the GPIO pins remain available for other use.

How It Works

The switch contact of the K1 relay functions as the main switch. If it is open, then the Rasp Pi cannot draw power. Only when the contact is closed will the computer boot. Booting will occur independent of whether the power supply runs directly over the pin strip P1 or over the USB socket of the extension board.

The driver transistor T3 for the timer IC3 (NE555) [3] controls the K1 relay. Here, the coil of the relay must be connected in parallel with a flyback diode in the reverse direction. Otherwise, under certain circumstances, a voltage spike could destroy the transistor driver T3 or other components when the relay coil is shut off.

If the trigger input (TRI) of the timer is driven to 0V level, then the relay will be activated. Transistors T1 and T2 maintain this state until a positive edge at the trigger input unblocks the timer function of IC3. Then, after about 35 seconds, as determined by the relationship T = 1.1×R8 ×C7, the timer will lapse and deactivate the relay.

You have three ways to drive this trigger input. First, connecting the 5V USB power supply at the PWR-IN port produces a brief 0V impulse via R5 and C7. Second, this type of impulse can be produced via the push-button switch S1. Both ways trigger the above-mentioned switch-on phase that lasts 35 seconds. The third option is to use the timer chip IC2.

The IC2 – RTC MCP79410 – chip serves as the core component of this circuit. The Rasp Pi directly controls this chip via the I2C bus, which requires IC2 to be operated on the 3.3V voltage rail supplied by IC1. Provided that IC2 is configured correctly, it sets its output (MFP) to a permanent 0V level. This is achieved by either reaching a previously set alarm time point or by resetting the OUT bit and deactivating both alarms in the control register with address 0x07.

If the Rasp Pi is booted up via a brief 0V impulse, the booting operating system must confirm this by resetting the OUT bit within the 35-second interval mentioned above. Otherwise, the timer chip will cut the power supply. This time span is all that is available for starting the OS kernel, mounting the filesystem, and loading the I2C kernel modules.

Even if the 5V power supply is not available for several days, the super capacitor C5 will continue to provide enough power for the timer chip. As soon as power is restored, the system recharges the super capacitor via D1 and R4.

Startup Procedures

Once you have completed the extension circuit board (Figure 2) step by step, you can see how well it starts up and functions. The example used for this article is an updated Raspbian distribution built on Wheezy [4] that you can already easily configure with the attached circuit board (Figure  3); a brand new MCP79410 is configured in such a way that it provides continuous power (Figure  4).

Figure 2: A view of the extension board from underneath with relay to the upper left and super capacitor and quartz timer to the lower right.
Figure 3: A view of the extension board with, from the top down, push-button switches IC2, IC3, and IC1.
Figure 4: An excerpt from the Microchip data sheet showing the register table of the MCP79410.

As a first step, you need to install tools to communicate with the clock chip and set the time. Using the command from the first line of Listing 1, you can install all of the required packages. After that, you can load the kernel module for the operation of both I2C buses and establish access to them via the device files /dev/i2c-0 and /dev/i2c-1 (Listing 1, lines  2 and 3). The module parameter of the first command sets the clock frequency to 400kHz, which is the maximum amount allowable for the MCP79410.

Listing 1

Install Required Packages

$ sudo apt-get install i2c-tools
$ sudo modprobe i2c-bcm2708 baudrate=400000
$ sudo modprobe i2c-dev

Your next step will depend on the revision number of the circuit board of the Rasp Pi. With an old Rev 1 board, you select bus number 0 (/dev/i2c-0); with the newer Rev  2 board, select bus number 1.

The clock chip is tested as shown in Listing 2, where you will see that the MCP79410 reacts to two I2C addresses. These addresses are 0x57 and 0x6f. The EEPROM, a status register and a unique ID register lie behind the first address. At the second address, you will find the actual clock chip and the SRAM.

Listing 2

Test Clock Chip

$ sudo i2cdetect -y <busnumber>
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- 57 -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 6f
70: -- -- -- -- -- -- -- --

As a final step, you can load and configure kernel access to the RTC chip and set the time. Initially, you should set the time to the current system time with reference to UTC, or coordinated universal time. The Linux kernel module that communicates with the RTC timer (rtc-ds1307) supports several chips, so you will need to configure the system to use the MCP79410 RTC chip (as shown in Listing 3).

Listing 3

Configure System for Chip

$ sudo modprobe rtc-ds1307
$ sudo echo "mcp7941x 0x6f" > /sys/bus/i2c/devices/i2c-<bus#>/new_device
$ sudo hwclock --systohc -D --utc

To check whether the configuration was successful, simply read out all of the registers of the chip (Listing 4) and compare their contents to either the date or the time output. Remember to take the indicated time zone into account.

Listing 4

Check Configuration

$ sudo hwclock -r && sudo i2cdump -y -f -r 0x00-0x1f \ <bus#> 0x6f b
Fr 01 Nov 2013 21:54:14 CET  -0.377799 seconds
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00: 94 54 20 2e 01 11 13 00 00 00 00 00 00 00 00 00
10: 01 00 00 00 01 01 01 01 00 00 00 00 00 00 00 00

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content