ds1302.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * ds1302.c:
  3. * Real Time clock
  4. *
  5. * Copyright (c) 2013 Gordon Henderson.
  6. ***********************************************************************
  7. * This file is part of wiringPi:
  8. * https://projects.drogon.net/raspberry-pi/wiringpi/
  9. *
  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.
  14. *
  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.
  19. *
  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. ***********************************************************************
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdint.h>
  27. #include <string.h>
  28. #include <time.h>
  29. #include <wiringPi.h>
  30. #include <ds1302.h>
  31. // Register defines
  32. #define RTC_SECS 0
  33. #define RTC_MINS 1
  34. #define RTC_HOURS 2
  35. #define RTC_DATE 3
  36. #define RTC_MONTH 4
  37. #define RTC_DAY 5
  38. #define RTC_YEAR 6
  39. #define RTC_WP 7
  40. #define RTC_TC 8
  41. #define RTC_BM 31
  42. static unsigned int masks [] = { 0x7F, 0x7F, 0x3F, 0x3F, 0x1F, 0x07, 0xFF } ;
  43. /*
  44. * bcdToD: dToBCD:
  45. * BCD decode/encode
  46. *********************************************************************************
  47. */
  48. static int bcdToD (unsigned int byte, unsigned int mask)
  49. {
  50. unsigned int b1, b2 ;
  51. byte &= mask ;
  52. b1 = byte & 0x0F ;
  53. b2 = ((byte >> 4) & 0x0F) * 10 ;
  54. return b1 + b2 ;
  55. }
  56. static unsigned int dToBcd (unsigned int byte)
  57. {
  58. return ((byte / 10) << 4) + (byte % 10) ;
  59. }
  60. /*
  61. * ramTest:
  62. * Simple test of the 31 bytes of RAM inside the DS1302 chip
  63. *********************************************************************************
  64. */
  65. static int ramTestValues [] =
  66. { 0x00, 0xFF, 0xAA, 0x55, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00, 0xF0, 0x0F, -1 } ;
  67. static int ramTest (void)
  68. {
  69. int addr ;
  70. int got ;
  71. int i = 0 ;
  72. int errors = 0 ;
  73. int testVal ;
  74. printf ("DS1302 RAM TEST\n") ;
  75. testVal = ramTestValues [i] ;
  76. while (testVal != -1)
  77. {
  78. for (addr = 0 ; addr < 31 ; ++addr)
  79. ds1302ramWrite (addr, testVal) ;
  80. for (addr = 0 ; addr < 31 ; ++addr)
  81. if ((got = ds1302ramRead (addr)) != testVal)
  82. {
  83. printf ("DS1302 RAM Failure: Address: %2d, Expected: 0x%02X, Got: 0x%02X\n",
  84. addr, testVal, got) ;
  85. ++errors ;
  86. }
  87. testVal = ramTestValues [++i] ;
  88. }
  89. for (addr = 0 ; addr < 31 ; ++addr)
  90. ds1302ramWrite (addr, addr) ;
  91. for (addr = 0 ; addr < 31 ; ++addr)
  92. if ((got = ds1302ramRead (addr)) != addr)
  93. {
  94. printf ("DS1302 RAM Failure: Address: %2d, Expected: 0x%02X, Got: 0x%02X\n",
  95. addr, addr, got) ;
  96. ++errors ;
  97. }
  98. if (errors == 0)
  99. printf ("-- DS1302 RAM TEST: OK\n") ;
  100. else
  101. printf ("-- DS1302 RAM TEST FAILURE. %d errors.\n", errors) ;
  102. return 0 ;
  103. }
  104. /*
  105. * setLinuxClock:
  106. * Set the Linux clock from the hardware
  107. *********************************************************************************
  108. */
  109. static int setLinuxClock (void)
  110. {
  111. char dateTime [20] ;
  112. char command [64] ;
  113. int clock [8] ;
  114. printf ("Setting the Linux Clock from the DS1302... ") ; fflush (stdout) ;
  115. ds1302clockRead (clock) ;
  116. // [MMDDhhmm[[CC]YY][.ss]]
  117. sprintf (dateTime, "%02d%02d%02d%02d%02d%02d.%02d",
  118. bcdToD (clock [RTC_MONTH], masks [RTC_MONTH]),
  119. bcdToD (clock [RTC_DATE], masks [RTC_DATE]),
  120. bcdToD (clock [RTC_HOURS], masks [RTC_HOURS]),
  121. bcdToD (clock [RTC_MINS], masks [RTC_MINS]),
  122. 20,
  123. bcdToD (clock [RTC_YEAR], masks [RTC_YEAR]),
  124. bcdToD (clock [RTC_SECS], masks [RTC_SECS])) ;
  125. sprintf (command, "/bin/date %s", dateTime) ;
  126. system (command) ;
  127. return 0 ;
  128. }
  129. /*
  130. * setDSclock:
  131. * Set the DS1302 block from Linux time
  132. *********************************************************************************
  133. */
  134. static int setDSclock (void)
  135. {
  136. struct tm t ;
  137. time_t now ;
  138. int clock [8] ;
  139. printf ("Setting the clock in the DS1302 from Linux time... ") ;
  140. now = time (NULL) ;
  141. gmtime_r (&now, &t) ;
  142. clock [ 0] = dToBcd (t.tm_sec) ; // seconds
  143. clock [ 1] = dToBcd (t.tm_min) ; // mins
  144. clock [ 2] = dToBcd (t.tm_hour) ; // hours
  145. clock [ 3] = dToBcd (t.tm_mday) ; // date
  146. clock [ 4] = dToBcd (t.tm_mon + 1) ; // months 0-11 --> 1-12
  147. clock [ 5] = dToBcd (t.tm_wday + 1) ; // weekdays (sun 0)
  148. clock [ 6] = dToBcd (t.tm_year - 100) ; // years
  149. clock [ 7] = 0 ; // W-Protect off
  150. ds1302clockWrite (clock) ;
  151. printf ("OK\n") ;
  152. return 0 ;
  153. }
  154. int main (int argc, char *argv [])
  155. {
  156. int i ;
  157. int clock [8] ;
  158. wiringPiSetup () ;
  159. ds1302setup (0, 1, 2) ;
  160. if (argc == 2)
  161. {
  162. /**/ if (strcmp (argv [1], "-slc") == 0)
  163. return setLinuxClock () ;
  164. else if (strcmp (argv [1], "-sdsc") == 0)
  165. return setDSclock () ;
  166. else if (strcmp (argv [1], "-rtest") == 0)
  167. return ramTest () ;
  168. else
  169. {
  170. printf ("Usage: ds1302 [-slc | -sdsc | -rtest]\n") ;
  171. return EXIT_FAILURE ;
  172. }
  173. }
  174. for (i = 0 ;; ++i)
  175. {
  176. printf ("%5d: ", i) ;
  177. ds1302clockRead (clock) ;
  178. printf (" %2d:%02d:%02d",
  179. bcdToD (clock [2], masks [2]), bcdToD (clock [1], masks [1]), bcdToD (clock [0], masks [0])) ;
  180. printf (" %2d/%02d/%04d",
  181. bcdToD (clock [3], masks [3]), bcdToD (clock [4], masks [4]), bcdToD (clock [6], masks [6]) + 2000) ;
  182. printf ("\n") ;
  183. delay (200) ;
  184. }
  185. return 0 ;
  186. }