5 * Copyright (c) 2013 Gordon Henderson.
6 ***********************************************************************
7 * This file is part of wiringPi:
8 * https://projects.drogon.net/raspberry-pi/wiringpi/
10 * wiringPi is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * wiringPi is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
22 ***********************************************************************
48 static unsigned int masks [] = { 0x7F, 0x7F, 0x3F, 0x3F, 0x1F, 0x07, 0xFF } ;
54 *********************************************************************************
57 static int bcdToD (unsigned int byte, unsigned int mask)
62 b2 = ((byte >> 4) & 0x0F) * 10 ;
66 static unsigned int dToBcd (unsigned int byte)
68 return ((byte / 10) << 4) + (byte % 10) ;
74 * Simple test of the 31 bytes of RAM inside the DS1302 chip
75 *********************************************************************************
78 static int ramTestValues [] =
79 { 0x00, 0xFF, 0xAA, 0x55, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00, 0xF0, 0x0F, -1 } ;
81 static int ramTest (void)
89 printf ("DS1302 RAM TEST\n") ;
91 testVal = ramTestValues [i] ;
95 for (addr = 0 ; addr < 31 ; ++addr)
96 ds1302ramWrite (addr, testVal) ;
98 for (addr = 0 ; addr < 31 ; ++addr)
99 if ((got = ds1302ramRead (addr)) != testVal)
101 printf ("DS1302 RAM Failure: Address: %2d, Expected: 0x%02X, Got: 0x%02X\n",
102 addr, testVal, got) ;
105 testVal = ramTestValues [++i] ;
108 for (addr = 0 ; addr < 31 ; ++addr)
109 ds1302ramWrite (addr, addr) ;
111 for (addr = 0 ; addr < 31 ; ++addr)
112 if ((got = ds1302ramRead (addr)) != addr)
114 printf ("DS1302 RAM Failure: Address: %2d, Expected: 0x%02X, Got: 0x%02X\n",
120 printf ("-- DS1302 RAM TEST: OK\n") ;
122 printf ("-- DS1302 RAM TEST FAILURE. %d errors.\n", errors) ;
129 * Set the Linux clock from the hardware
130 *********************************************************************************
133 static int setLinuxClock (void)
140 printf ("Setting the Linux Clock from the DS1302... ") ; fflush (stdout) ;
142 ds1302clockRead (clock) ;
144 // [MMDDhhmm[[CC]YY][.ss]]
146 sprintf (dateTime, "%02d%02d%02d%02d%02d%02d.%02d",
147 bcdToD (clock [RTC_MONTH], masks [RTC_MONTH]),
148 bcdToD (clock [RTC_DATE], masks [RTC_DATE]),
149 bcdToD (clock [RTC_HOURS], masks [RTC_HOURS]),
150 bcdToD (clock [RTC_MINS], masks [RTC_MINS]),
152 bcdToD (clock [RTC_YEAR], masks [RTC_YEAR]),
153 bcdToD (clock [RTC_SECS], masks [RTC_SECS])) ;
155 sprintf (command, "/bin/date %s", dateTime) ;
164 * Set the DS1302 block from Linux time
165 *********************************************************************************
168 static int setDSclock (void)
174 printf ("Setting the clock in the DS1302 from Linux time... ") ;
177 gmtime_r (&now, &t) ;
179 clock [ 0] = dToBcd (t.tm_sec) ; // seconds
180 clock [ 1] = dToBcd (t.tm_min) ; // mins
181 clock [ 2] = dToBcd (t.tm_hour) ; // hours
182 clock [ 3] = dToBcd (t.tm_mday) ; // date
183 clock [ 4] = dToBcd (t.tm_mon + 1) ; // months 0-11 --> 1-12
184 clock [ 5] = dToBcd (t.tm_wday + 1) ; // weekdays (sun 0)
185 clock [ 6] = dToBcd (t.tm_year - 100) ; // years
186 clock [ 7] = 0 ; // W-Protect off
188 ds1302clockWrite (clock) ;
198 int main (int argc, char *argv [])
204 ds1302setup (0, 1, 2) ;
208 /**/ if (strcmp (argv [1], "-slc") == 0)
209 return setLinuxClock () ;
210 else if (strcmp (argv [1], "-sdsc") == 0)
211 return setDSclock () ;
212 else if (strcmp (argv [1], "-rtest") == 0)
216 printf ("Usage: ds1302 [-slc | -sdsc | -rtest]\n") ;
217 return EXIT_FAILURE ;
223 printf ("%5d: ", i) ;
225 ds1302clockRead (clock) ;
226 printf (" %2d:%02d:%02d",
227 bcdToD (clock [2], masks [2]), bcdToD (clock [1], masks [1]), bcdToD (clock [0], masks [0])) ;
229 printf (" %2d/%02d/%04d",
230 bcdToD (clock [3], masks [3]), bcdToD (clock [4], masks [4]), bcdToD (clock [6], masks [6]) + 2000) ;