ds3231.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * (C) Copyright 2006
  3. * Markus Klotzbuecher, mk@denx.de
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Date & Time support (no alarms) for Dallas Semiconductor (now Maxim)
  25. * Extremly Accurate DS3231 Real Time Clock (RTC).
  26. *
  27. * copied from ds1337.c
  28. */
  29. #include <common.h>
  30. #include <command.h>
  31. #include <rtc.h>
  32. #include <i2c.h>
  33. #if defined(CONFIG_RTC_DS3231) && defined(CONFIG_CMD_DATE)
  34. /*---------------------------------------------------------------------*/
  35. #undef DEBUG_RTC
  36. #ifdef DEBUG_RTC
  37. #define DEBUGR(fmt,args...) printf(fmt ,##args)
  38. #else
  39. #define DEBUGR(fmt,args...)
  40. #endif
  41. /*---------------------------------------------------------------------*/
  42. /*
  43. * RTC register addresses
  44. */
  45. #define RTC_SEC_REG_ADDR 0x0
  46. #define RTC_MIN_REG_ADDR 0x1
  47. #define RTC_HR_REG_ADDR 0x2
  48. #define RTC_DAY_REG_ADDR 0x3
  49. #define RTC_DATE_REG_ADDR 0x4
  50. #define RTC_MON_REG_ADDR 0x5
  51. #define RTC_YR_REG_ADDR 0x6
  52. #define RTC_CTL_REG_ADDR 0x0e
  53. #define RTC_STAT_REG_ADDR 0x0f
  54. /*
  55. * RTC control register bits
  56. */
  57. #define RTC_CTL_BIT_A1IE 0x1 /* Alarm 1 interrupt enable */
  58. #define RTC_CTL_BIT_A2IE 0x2 /* Alarm 2 interrupt enable */
  59. #define RTC_CTL_BIT_INTCN 0x4 /* Interrupt control */
  60. #define RTC_CTL_BIT_RS1 0x8 /* Rate select 1 */
  61. #define RTC_CTL_BIT_RS2 0x10 /* Rate select 2 */
  62. #define RTC_CTL_BIT_DOSC 0x80 /* Disable Oscillator */
  63. /*
  64. * RTC status register bits
  65. */
  66. #define RTC_STAT_BIT_A1F 0x1 /* Alarm 1 flag */
  67. #define RTC_STAT_BIT_A2F 0x2 /* Alarm 2 flag */
  68. #define RTC_STAT_BIT_OSF 0x80 /* Oscillator stop flag */
  69. static uchar rtc_read (uchar reg);
  70. static void rtc_write (uchar reg, uchar val);
  71. static uchar bin2bcd (unsigned int n);
  72. static unsigned bcd2bin (uchar c);
  73. /*
  74. * Get the current time from the RTC
  75. */
  76. int rtc_get (struct rtc_time *tmp)
  77. {
  78. int rel = 0;
  79. uchar sec, min, hour, mday, wday, mon_cent, year, control, status;
  80. control = rtc_read (RTC_CTL_REG_ADDR);
  81. status = rtc_read (RTC_STAT_REG_ADDR);
  82. sec = rtc_read (RTC_SEC_REG_ADDR);
  83. min = rtc_read (RTC_MIN_REG_ADDR);
  84. hour = rtc_read (RTC_HR_REG_ADDR);
  85. wday = rtc_read (RTC_DAY_REG_ADDR);
  86. mday = rtc_read (RTC_DATE_REG_ADDR);
  87. mon_cent = rtc_read (RTC_MON_REG_ADDR);
  88. year = rtc_read (RTC_YR_REG_ADDR);
  89. DEBUGR ("Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
  90. "hr: %02x min: %02x sec: %02x control: %02x status: %02x\n",
  91. year, mon_cent, mday, wday, hour, min, sec, control, status);
  92. if (status & RTC_STAT_BIT_OSF) {
  93. printf ("### Warning: RTC oscillator has stopped\n");
  94. /* clear the OSF flag */
  95. rtc_write (RTC_STAT_REG_ADDR,
  96. rtc_read (RTC_STAT_REG_ADDR) & ~RTC_STAT_BIT_OSF);
  97. rel = -1;
  98. }
  99. tmp->tm_sec = bcd2bin (sec & 0x7F);
  100. tmp->tm_min = bcd2bin (min & 0x7F);
  101. tmp->tm_hour = bcd2bin (hour & 0x3F);
  102. tmp->tm_mday = bcd2bin (mday & 0x3F);
  103. tmp->tm_mon = bcd2bin (mon_cent & 0x1F);
  104. tmp->tm_year = bcd2bin (year) + ((mon_cent & 0x80) ? 2000 : 1900);
  105. tmp->tm_wday = bcd2bin ((wday - 1) & 0x07);
  106. tmp->tm_yday = 0;
  107. tmp->tm_isdst= 0;
  108. DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  109. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  110. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  111. return rel;
  112. }
  113. /*
  114. * Set the RTC
  115. */
  116. void rtc_set (struct rtc_time *tmp)
  117. {
  118. uchar century;
  119. DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  120. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  121. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  122. rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
  123. century = (tmp->tm_year >= 2000) ? 0x80 : 0;
  124. rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon) | century);
  125. rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday + 1));
  126. rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
  127. rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour));
  128. rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
  129. rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
  130. }
  131. /*
  132. * Reset the RTC. We also enable the oscillator output on the
  133. * SQW/INTB* pin and program it for 32,768 Hz output. Note that
  134. * according to the datasheet, turning on the square wave output
  135. * increases the current drain on the backup battery from about
  136. * 600 nA to 2uA.
  137. */
  138. void rtc_reset (void)
  139. {
  140. rtc_write (RTC_CTL_REG_ADDR, RTC_CTL_BIT_RS1 | RTC_CTL_BIT_RS2);
  141. }
  142. /*
  143. * Helper functions
  144. */
  145. static
  146. uchar rtc_read (uchar reg)
  147. {
  148. return (i2c_reg_read (CFG_I2C_RTC_ADDR, reg));
  149. }
  150. static void rtc_write (uchar reg, uchar val)
  151. {
  152. i2c_reg_write (CFG_I2C_RTC_ADDR, reg, val);
  153. }
  154. static unsigned bcd2bin (uchar n)
  155. {
  156. return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
  157. }
  158. static unsigned char bin2bcd (unsigned int n)
  159. {
  160. return (((n / 10) << 4) | (n % 10));
  161. }
  162. #endif