pcf8563.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (C) 2006 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*!
  34. * \file dev/pcf8563.c
  35. * \brief RTC for Philips PCF8563 clock chip.
  36. *
  37. * \verbatim
  38. *
  39. * $Log$
  40. * Revision 1.4 2008/08/11 06:59:42 haraldkipp
  41. * BSD types replaced by stdint types (feature request #1282721).
  42. *
  43. * Revision 1.3 2006/10/05 17:18:49 haraldkipp
  44. * Hardware independant RTC layer added.
  45. *
  46. * Revision 1.2 2006/06/28 17:23:47 haraldkipp
  47. * Bugfix. PcfRtcGetClock() returned wrong century.
  48. *
  49. * Revision 1.1 2006/04/07 13:54:17 haraldkipp
  50. * PCF8563 RTC driver added.
  51. *
  52. *
  53. * \endverbatim
  54. */
  55. #include <cfg/os.h>
  56. #include <dev/twif.h>
  57. #include <sys/event.h>
  58. #include <sys/timer.h>
  59. #include <time.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include <dev/pcf8563.h>
  63. #ifndef I2C_SLA_RTC
  64. #define I2C_SLA_RTC 0x51
  65. #endif
  66. static uint32_t rtc_status;
  67. /*!
  68. * \brief Read RTC registers.
  69. *
  70. * \param reg The first register to read.
  71. * \param buff Pointer to a buffer that receives the register contents.
  72. * \param cnt The number of registers to read.
  73. *
  74. * \return 0 on success or -1 in case of an error.
  75. */
  76. int PcfRtcReadRegs(uint8_t reg, uint8_t *buff, size_t cnt)
  77. {
  78. int rc = -1;
  79. if (TwMasterTransact(I2C_SLA_RTC, &reg, 1, buff, cnt, NUT_WAIT_INFINITE) == cnt) {
  80. rc = 0;
  81. }
  82. return rc;
  83. }
  84. /*!
  85. * \brief Write to RTC registers.
  86. *
  87. * \param nv Must be set to 1 when writing to non-volatile registers.
  88. * In this case the routine will poll for write cycle
  89. * completion before returning to the caller. Set to zero
  90. * if writing to volatile registers.
  91. * \param buff This buffer must contain all bytes to be transfered to
  92. * the RTC chip, including the register address.
  93. * \param cnt Number of valid bytes in the buffer.
  94. *
  95. * \return 0 on success or -1 in case of an error.
  96. */
  97. int PcfRtcWrite(int nv, const uint8_t *buff, size_t cnt)
  98. {
  99. return TwMasterTransact(I2C_SLA_RTC, buff, cnt, 0, 0, NUT_WAIT_INFINITE);
  100. }
  101. /*!
  102. * \brief Get date and time from an PCF8563 hardware clock.
  103. *
  104. * \param tm Points to a structure that receives the date and time
  105. * information.
  106. *
  107. * \return 0 on success or -1 in case of an error.
  108. */
  109. int PcfRtcGetClock(NUTRTC *rtc, struct _tm *tm)
  110. {
  111. int rc;
  112. uint8_t data[7];
  113. if ((rc = PcfRtcReadRegs(0x02, data, 7)) == 0) {
  114. tm->tm_sec = BCD2BIN(data[0] & 0x7F);
  115. tm->tm_min = BCD2BIN(data[1] & 0x7F);
  116. tm->tm_hour = BCD2BIN(data[2] & 0x3F);
  117. tm->tm_mday = BCD2BIN(data[3] & 0x3F);
  118. tm->tm_mon = BCD2BIN(data[5] & 0x1F) - 1;
  119. tm->tm_year = BCD2BIN(data[6]);
  120. if (data[5] & 0x80) {
  121. tm->tm_year += 100;
  122. }
  123. tm->tm_wday = data[4] & 0x07;
  124. }
  125. return rc;
  126. }
  127. /*!
  128. * \brief Set an PCF8563 hardware clock.
  129. *
  130. * New time will be taken over at the beginning of the next second.
  131. *
  132. * \param tm Points to a structure which contains the date and time
  133. * information.
  134. *
  135. * \return 0 on success or -1 in case of an error.
  136. */
  137. int PcfRtcSetClock(NUTRTC *rtc, const struct _tm *tm)
  138. {
  139. uint8_t data[8];
  140. memset(data, 0, sizeof(data));
  141. if (tm) {
  142. data[0] = 0x02;
  143. data[1] = BIN2BCD(tm->tm_sec);
  144. data[2] = BIN2BCD(tm->tm_min);
  145. data[3] = BIN2BCD(tm->tm_hour);
  146. data[4] = BIN2BCD(tm->tm_mday);
  147. data[5] = tm->tm_wday;
  148. data[6] = BIN2BCD(tm->tm_mon + 1);
  149. if (tm->tm_year > 99) {
  150. data[7] = BIN2BCD(tm->tm_year - 100);
  151. data[6] |= 0x80;
  152. }
  153. else {
  154. data[7] = BIN2BCD(tm->tm_year);
  155. }
  156. }
  157. return PcfRtcWrite(0, data, 8);
  158. }
  159. /*!
  160. * \brief Get alarm date and time of an PCF8563 hardware clock.
  161. *
  162. * Not implemented.
  163. *
  164. * \param idx Zero based index. Two alarms are supported.
  165. * \param tm Points to a structure that receives the date and time
  166. * information.
  167. * \param aflgs Points to an unsigned long that receives the enable flags.
  168. *
  169. * \return 0 on success or -1 in case of an error.
  170. *
  171. */
  172. int PcfRtcGetAlarm(NUTRTC *rtc, int idx, struct _tm *tm, int *aflgs)
  173. {
  174. return -1;
  175. }
  176. /*!
  177. * \brief Set alarm of an PCF8563 hardware clock.
  178. *
  179. * Not implemented.
  180. *
  181. * \param idx Zero based index. Two alarms are supported.
  182. * \param tm Points to a structure which contains the date and time
  183. * information. May be NULL to clear the alarm.
  184. * \param aflgs Each bit enables a specific comparision.
  185. * - Bit 0: Seconds
  186. * - Bit 1: Minutes
  187. * - Bit 2: Hours
  188. * - Bit 3: Day of month
  189. * - Bit 4: Month
  190. * - Bit 7: Day of week (Sunday is zero)
  191. *
  192. * \return 0 on success or -1 in case of an error.
  193. */
  194. int PcfRtcSetAlarm(NUTRTC *rtc, int idx, const struct _tm *tm, int aflgs)
  195. {
  196. return -1;
  197. }
  198. /*!
  199. * \brief Query RTC status flags.
  200. *
  201. * \param sflgs Points to an unsigned long that receives the status flags.
  202. * - Bit 0: Power fail.
  203. * - Bit 5: Alarm 0 occured (not implemented).
  204. * - Bit 6: Alarm 1 occured (not implemented).
  205. *
  206. * \return 0 on success or -1 in case of an error.
  207. */
  208. int PcfRtcGetStatus(NUTRTC *rtc, uint32_t *sflgs)
  209. {
  210. int rc;
  211. uint8_t data;
  212. if ((rc = PcfRtcReadRegs(0x02, &data, 1)) == 0) {
  213. if (data & 0x80) {
  214. rtc_status = RTC_STATUS_PF;
  215. }
  216. *sflgs = rtc_status;
  217. }
  218. return rc;
  219. }
  220. /*!
  221. * \brief Clear RTC status flags.
  222. *
  223. * \param sflgs Status flags to clear.
  224. *
  225. * \return Always 0.
  226. */
  227. int PcfRtcClearStatus(NUTRTC *rtc, uint32_t sflgs)
  228. {
  229. rtc_status &= ~sflgs;
  230. return 0;
  231. }
  232. /*!
  233. * \brief Initialize the interface to an Intersil PCF8563 hardware clock.
  234. *
  235. * \return 0 on success or -1 in case of an error.
  236. *
  237. */
  238. int PcfRtcInit(NUTRTC *rtc)
  239. {
  240. int rc;
  241. uint32_t tmp;
  242. if ((rc = TwInit(0)) == 0) {
  243. rc = PcfRtcGetStatus(rtc, &tmp);
  244. }
  245. return rc;
  246. }
  247. NUTRTC rtcPcf8563 = {
  248. /*.dcb = */ NULL, /*!< Driver control block */
  249. /*.rtc_init = */ PcfRtcInit, /*!< Hardware initializatiuon, rtc_init */
  250. /*.rtc_gettime = */ PcfRtcGetClock, /*!< Read date and time, rtc_gettime */
  251. /*.rtc_settime = */ PcfRtcSetClock, /*!< Set date and time, rtc_settime */
  252. /*.rtc_getalarm = */ PcfRtcGetAlarm, /*!< Read alarm date and time, rtc_getalarm */
  253. /*.rtc_setalarm = */ PcfRtcSetAlarm, /*!< Set alarm date and time, rtc_setalarm */
  254. /*.rtc_getstatus = */ PcfRtcGetStatus, /*!< Read status flags, rtc_getstatus */
  255. /*.rtc_clrstatus = */ PcfRtcClearStatus, /*!< Clear status flags, rtc_clrstatus */
  256. /*.alarm = */ NULL, /*!< Handle for alarm event queue, not supported right now */
  257. };