├── CHANGELOG ├── COPYING ├── README.md └── install-piface-real-time-clock.sh /CHANGELOG: -------------------------------------------------------------------------------- 1 | Change Log 2 | ========== 3 | 4 | v0.2.0 5 | ------ 6 | - Loads RTC with `/etc/init.d/pifacertc` service. 7 | 8 | v0.1.0 9 | ------ 10 | - Inital version, loads RTC with `/etc/rc.local`. 11 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 OpenLX SP Ltd. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PiFace Real Time Clock 2 | PiFace Real Time Clock is a Real Time Clock (RTC) for the Raspberry Pi. 3 | 4 | 5 | ## Install 6 | ### Add the `pifacertc` service 7 | Attach PiFace Clock, [download the install script](https://raw.github.com/piface/PiFace-Real-Time-Clock/master/install-piface-real-time-clock.sh) and copy it to your 8 | SD card. Make the script executable and then run it: 9 | 10 | chmod +x install-piface-real-time-clock.sh 11 | sudo ./install-piface-real-time-clock.sh 12 | 13 | Alternatively, if you have internet access then this one-liner should do the trick: 14 | 15 | wget https://raw.github.com/piface/PiFace-Real-Time-Clock/master/install-piface-real-time-clock.sh && chmod +x install-piface-real-time-clock.sh && sudo ./install-piface-real-time-clock.sh 16 | 17 | 18 | ### Enable I2C 19 | Run: 20 | 21 | sudo raspi-config 22 | 23 | Then navigate to `Advanced Options` > `I2C` and select `yes` to enable the ARM I2C interface. 24 | 25 | ### Set the correct date 26 | Reboot and then set the correct date with `sudo date -s`, for example: 27 | 28 | sudo date -s "14 JAN 2014 10:10:30" 29 | 30 | Replace `14 JAN 2014 10:10:30` with today's date and time. 31 | 32 | ### Set the hardware clock 33 | Finally, save the system clock to the hardware clock with: 34 | 35 | sudo hwclock --systohc 36 | 37 | 38 | ## Starting and Stopping the Service 39 | After installing, PiFace RTC will start as a service (`/etc/init.d/pifacertc`) 40 | on boot. You can start, stop, enable on boot and disable on boot with the 41 | following commands: 42 | 43 | sudo service pifacertc start 44 | sudo service pifacertc stop 45 | sudo service pifacertc defaults # enable on boot 46 | sudo service pifacertc remove # disable on boot 47 | 48 | 49 | ## Old versions 50 | If you installed PiFace RTC using the old script (earlier than 2016-05-04) 51 | then you might need to **remove** the following lines from `/etc/rc.local`: 52 | 53 | modprobe i2c-dev 54 | # Calibrate the clock (default: 0x47). See datasheet for MCP7940N 55 | i2cset -y 1 0x6f 0x08 0x47 56 | modprobe i2c:mcp7941x 57 | echo mcp7941x 0x6f > /sys/class/i2c-dev/i2c-$i/device/new_device 58 | hwclock --hctosys 59 | -------------------------------------------------------------------------------- /install-piface-real-time-clock.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #: Description: Enables the required modules for PiFace Clock. 3 | 4 | #======================================================================= 5 | # NAME: check_for_i2c_tools 6 | # DESCRIPTION: Checks if i2c-tools is installed. 7 | #======================================================================= 8 | check_for_i2c_tools() { 9 | dpkg -s i2c-tools > /dev/null 2>&1 10 | if [[ $? -eq 1 ]]; then 11 | echo "The package `i2c-tools` is not installed. Install it with:" 12 | echo "" 13 | echo " sudo apt-get install i2c-tools" 14 | echo "" 15 | exit 1 16 | fi 17 | } 18 | 19 | #======================================================================= 20 | # NAME: set_revision_var 21 | # DESCRIPTION: Stores the revision number of this Raspberry Pi into 22 | # $RPI_REVISION 23 | #======================================================================= 24 | set_revision_var() { 25 | revision=$(grep "Revision" /proc/cpuinfo | sed -e "s/Revision\t: //") 26 | RPI2_REVISION=$((16#a01041)) 27 | RPI3_REVISION=$((16#a02082)) 28 | RPI4_REVISION=$((16#a03111)) 29 | 30 | if [ "$((16#$revision))" -ge "$RPI4_REVISION" ]; then 31 | RPI_REVISION="4" 32 | elif [ "$((16#$revision))" -ge "$RPI3_REVISION" ]; then 33 | RPI_REVISION="3" 34 | elif [ "$((16#$revision))" -ge "$RPI2_REVISION" ]; then 35 | RPI_REVISION="2" 36 | else 37 | RPI_REVISION="1" 38 | fi 39 | } 40 | 41 | #======================================================================= 42 | # NAME: start_on_boot 43 | # DESCRIPTION: Load the I2C modules and send magic number to RTC, on boot. 44 | #======================================================================= 45 | start_on_boot() { 46 | echo "Create a new pifacertc init script to load time from PiFace RTC." 47 | echo "Adding /etc/init.d/pifacertc ." 48 | 49 | if [[ $RPI_REVISION == "4" ]]; then 50 | i=1 # i2c-1 51 | elif [[ $RPI_REVISION == "3" ]]; then 52 | i=1 # i2c-1 53 | elif [[ $RPI_REVISION == "2" ]]; then 54 | i=1 # i2c-1 55 | else 56 | i=0 # i2c-0 57 | fi 58 | 59 | cat > /etc/init.d/pifacertc << EOF 60 | #!/bin/sh 61 | ### BEGIN INIT INFO 62 | # Provides: pifacertc 63 | # Required-Start: udev mountkernfs \$remote_fs raspi-config 64 | # Required-Stop: 65 | # Default-Start: S 66 | # Default-Stop: 67 | # Short-Description: Add the PiFace RTC 68 | # Description: Add the PiFace RTC 69 | ### END INIT INFO 70 | 71 | . /lib/lsb/init-functions 72 | 73 | case "\$1" in 74 | start) 75 | log_success_msg "Probe the i2c-dev" 76 | modprobe i2c-dev 77 | # Calibrate the clock (default: 0x47). See datasheet for MCP7940N 78 | log_success_msg "Calibrate the clock" 79 | i2cset -y $i 0x6f 0x08 0x47 80 | log_success_msg "Probe the mcp7941x driver" 81 | modprobe i2c:mcp7941x 82 | log_success_msg "Add the mcp7941x device in the sys filesystem" 83 | # https://www.kernel.org/doc/Documentation/i2c/instantiating-devices 84 | echo mcp7941x 0x6f > /sys/class/i2c-dev/i2c-$i/device/new_device 85 | log_success_msg "Synchronise the system clock and hardware RTC" 86 | hwclock --hctosys 87 | ;; 88 | stop) 89 | ;; 90 | restart) 91 | ;; 92 | force-reload) 93 | ;; 94 | *) 95 | echo "Usage: \$0 start" >&2 96 | exit 3 97 | ;; 98 | esac 99 | EOF 100 | chmod +x /etc/init.d/pifacertc 101 | 102 | echo "Install the pifacertc init script" 103 | update-rc.d pifacertc defaults 104 | } 105 | 106 | #======================================================================= 107 | # MAIN 108 | #======================================================================= 109 | # check if the script is being run as root 110 | if [[ $EUID -ne 0 ]] 111 | then 112 | printf 'This script must be run as root.\nExiting..\n' 113 | exit 1 114 | fi 115 | RPI_REVISION="" 116 | check_for_i2c_tools && 117 | set_revision_var && 118 | start_on_boot && 119 | if [[ ! -e /sys/class/i2c-dev/i2c-$i ]]; then 120 | echo "Enable I2C by using:" 121 | echo "" 122 | echo " raspi-config" 123 | echo "" 124 | echo "Then navigate to 'Advanced Options' > 'I2C' and select 'yes' to " 125 | echo "enable the ARM I2C interface. Then *reboot* and set your clock " 126 | echo "with:" 127 | else 128 | echo "Now *reboot* and set your clock with:" 129 | fi 130 | echo "" 131 | echo ' sudo date -s "14 JAN 2014 10:10:30"' 132 | echo " sudo hwclock --systohc" 133 | echo "" 134 | echo "Enable auto-sync on boot by enabling the service" 135 | echo " sudo systemctl enable pifacertc" 136 | echo "" 137 | echo "Check service status" 138 | echo " sudo systemctl status pifacertc" 139 | echo "" 140 | --------------------------------------------------------------------------------