Adafruit: RTC DS1307
Inputs and Outputs
The RTC must be powered from the BeagleBone's 5V supply (note it must be powered from the DC adapter, or else I2C bus will hang, as there is not enough current provided by the usb bus to power the RTC). The RTC uses an I2C interface to communicate with the BBB.
On boot BBB is connected power from the DC power adapter, therefore it loads the correct /dev/rtc modules.
P9 pin # (BeagleBone pin) -> Panel pin (wire color)
1 (GND) -> GND
5 (VDD_5V) -> VCC
5 (VDD_5V) -> SQW (through 10k pullup resistor)
19 (I2C2_SCL) -> SCL
20 (I2C2_SCL) -> SDA
interacts with the RTC: if the host is out of date -- reads from the RTC, and overwrites the system clock if the host is not out of date -- updates systemclock from ntp, and overwrites the RTC from systemclock so as to account for adding +/- 2 seconds daily.
NOTE: set "ln -sf /usr/share/zoneinfo/*/* /etc/localtime" as needed for your local timezone
#!/bin/bash a=$(date +%Y) if [[ $a < 2013 ]]; then echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device echo "Retrieving time from rtc" hwclock -s -f /dev/rtc1 hwclock -w echo "Date has been read from RTC, and copied to system clock" else # Assumes if date is correct on reboot, that you are connected to network echo "Fetching time from ntp server" $(/usr/bin/ntpdate -b -s u pool.ntp.org) if [ -e /dev/rtc1 ]; then echo "Writing system clock to RTC" hwclock -w -f /dev/rtc1 else echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device echo "Writing system clock to RTC" hwclock -w -f /dev/rtc1 fi fi # Sets time zone to Eastern (GMT -5) $( ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime ) echo "The new date is:" $(date)
This file manipulates the rtc-ds1307.service to run from a paramaterized directory. It copies over clock_init.sh, and rtc-ds1307.service to the given directory, and manipulates the variables inside of the service file to react accordingly. It then enables the service, so that it will run at startup.
In order to manually start the service execute: systemctl start rtc-ds1306.service and stop: systemctl stop rtc-ds1306.service
#!/bin/bash # By Alex Alvarez, email@example.com # This script creates a new directory if passed as the parameter, and places the rtc script and service files in those given directories. It also manipulates the service file such that the working directories change with the given directory parameter. # If no paramter is given the working directory is used as the service location if [ "$1" ]; then DIR=$2 $(mkdir $2) else DIR=$(pwd) fi SED_ARG="-e 's@WorkingDirectory=.*@WorkingDirectory=$DIR@' rtc-ds1307.service" eval sed "$SED_ARG" > tmp.txt mv tmp.txt rtc-ds1307.service cp rtc-ds1307.service $DIR cp clock_init.sh $DIR systemctl enable $DIR/rtc-ds1306.service
[Unit] Description=DS1307 RTC Service [Service] Type=simple WorkingDirectory=/usr/share/rtc_ds1307 ExecStart=/bin/bash clock_init.sh SyslogIdentifier=rtc_ds1307 [Install] WantedBy=multi-user.target