rtc.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/rtc.c
  35. * \brief RTC for Philips PCF8563 clock chip.
  36. *
  37. * \verbatim
  38. *
  39. * $Log$
  40. * Revision 1.3 2008/08/11 06:59:42 haraldkipp
  41. * BSD types replaced by stdint types (feature request #1282721).
  42. *
  43. * Revision 1.2 2006/10/08 16:48:09 haraldkipp
  44. * Documentation fixed
  45. *
  46. * Revision 1.1 2006/10/05 17:18:49 haraldkipp
  47. * Hardware independant RTC layer added.
  48. *
  49. *
  50. * \endverbatim
  51. */
  52. #include <dev/rtc.h>
  53. static NUTRTC *reg_rtc;
  54. /*!
  55. * \brief Register a specified RTC.
  56. *
  57. * This will also initialize any hardware, if required.
  58. *
  59. * \param rtc Pointer to the NUTRTC structure.
  60. *
  61. * \return 0 on success or -1 in case of an error.
  62. *
  63. */
  64. int NutRegisterRtc(NUTRTC * rtc)
  65. {
  66. reg_rtc = rtc;
  67. if (rtc && reg_rtc->rtc_init) {
  68. if ((*reg_rtc->rtc_init) (reg_rtc)) {
  69. reg_rtc = NULL;
  70. return -1;
  71. }
  72. }
  73. return 0;
  74. }
  75. /*!
  76. * \brief Get date and time from the registered RTC.
  77. *
  78. * Portable applications should use standard C functions.
  79. *
  80. * \param tm Points to a structure that receives the date and time
  81. * information.
  82. *
  83. * \return 0 on success or -1 in case of an error.
  84. */
  85. int NutRtcGetTime(struct _tm *tm)
  86. {
  87. if (reg_rtc && reg_rtc->rtc_gettime && tm) {
  88. return (*reg_rtc->rtc_gettime) (reg_rtc, tm);
  89. }
  90. return -1;
  91. }
  92. /*!
  93. * \brief Set date and time of the registered RTC.
  94. *
  95. * Portable applications should stime().
  96. *
  97. * \param tm Points to a structure which contains the date and time
  98. * information.
  99. *
  100. * \return 0 on success or -1 in case of an error.
  101. */
  102. int NutRtcSetTime(const struct _tm *tm)
  103. {
  104. if (reg_rtc && reg_rtc->rtc_settime && tm) {
  105. return (*reg_rtc->rtc_settime) (reg_rtc, tm);
  106. }
  107. return -1;
  108. }
  109. /*!
  110. * \brief Get alarm date and time from the registered RTC.
  111. *
  112. * \param idx Zero based index. Two alarms are supported.
  113. * \param tm Points to a structure that receives the date and time
  114. * information.
  115. * \param aflags Points to an unsigned long that receives the enable flags.
  116. *
  117. * \return 0 on success or -1 in case of an error.
  118. *
  119. */
  120. int NutRtcGetAlarm(int idx, struct _tm *tm, int *aflags)
  121. {
  122. if (reg_rtc && reg_rtc->rtc_getalarm) {
  123. return (*reg_rtc->rtc_getalarm) (reg_rtc, idx, tm, aflags);
  124. }
  125. return -1;
  126. }
  127. /*!
  128. * \brief Set alarm date and time of the registered RTC.
  129. *
  130. * \param idx Zero based index. Two alarms are supported.
  131. * \param tm Points to a structure which contains the date and time
  132. * information. May be NULL to clear the alarm.
  133. * \param aflags Each bit enables a specific comparision.
  134. * - Bit 0: Seconds
  135. * - Bit 1: Minutes
  136. * - Bit 2: Hours
  137. * - Bit 3: Day of month
  138. * - Bit 4: Month
  139. * - Bit 7: Day of week (Sunday is zero)
  140. *
  141. * \return 0 on success or -1 in case of an error.
  142. */
  143. int NutRtcSetAlarm(int idx, const struct _tm *tm, int aflags)
  144. {
  145. if (reg_rtc && reg_rtc->rtc_setalarm) {
  146. return (*reg_rtc->rtc_setalarm) (reg_rtc, idx, tm, aflags);
  147. }
  148. return -1;
  149. }
  150. /*!
  151. * \brief Query status flags from the registered RTC.
  152. *
  153. * \param sflags Points to an unsigned long that receives the status flags.
  154. * - Bit 0: Power fail.
  155. * - Bit 5: Alarm 0 occured.
  156. * - Bit 6: Alarm 1 occured.
  157. *
  158. * \return 0 on success or -1 in case of an error.
  159. */
  160. int NutRtcGetStatus(uint32_t * sflags)
  161. {
  162. if (reg_rtc && reg_rtc->rtc_getstatus) {
  163. return (*reg_rtc->rtc_getstatus) (reg_rtc, sflags);
  164. }
  165. return -1;
  166. }
  167. /*!
  168. * \brief Clear status flags of the registered RTC.
  169. *
  170. * \param sflags Status flags to clear.
  171. *
  172. * \return Always 0.
  173. */
  174. int NutRtcClearStatus(uint32_t sflags)
  175. {
  176. if (reg_rtc && reg_rtc->rtc_clrstatus) {
  177. return (*reg_rtc->rtc_clrstatus) (reg_rtc, sflags);
  178. }
  179. return -1;
  180. }