rtc.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #ifndef _RTC_H_
  2. #define _RTC_H_
  3. /* ========================================================================
  4. * [PROJECT] SIR
  5. * [MODULE] Real Time Clock
  6. * [TITLE] High- and low level Routines for INtersil X1205 RTC chip
  7. * [FILE] rtc.c
  8. * [VSN] 1.0
  9. * [CREATED] 13042007
  10. * [LASTCHNGD] 131042007
  11. * [COPYRIGHT] Copyright (C) STREAMIT BV 2010
  12. * [PURPOSE] contains all interface- and low-level routines to
  13. * read/write date/time/status strings from the X1205
  14. * ======================================================================== */
  15. #include <time.h> // for tm-struct
  16. //
  17. /*! \name Non-Volatile Alarm Registers */
  18. /*@{*/
  19. #define X12RTC_SCA0 0x00 /*!< \brief Alarm 0 second. */
  20. #define X12RTC_SCA1 0x08 /*!< \brief Alarm 1 second. */
  21. #define X12RTC_SCA_ESC 0x80 /*!< \brief Second alarm enabled. */
  22. #define X12RTC_MNA0 0x01 /*!< \brief Alarm 0 minute. */
  23. #define X12RTC_MNA1 0x09 /*!< \brief Alarm 1 minute. */
  24. #define X12RTC_MNA_EMN 0x80 /*!< \brief Minute alarm enabled. */
  25. #define X12RTC_HRA0 0x02 /*!< \brief Alarm 0 hour. */
  26. #define X12RTC_HRA1 0x0A /*!< \brief Alarm 1 hour. */
  27. #define X12RTC_HRA_EHR 0x80 /*!< \brief Hour alarm enabled. */
  28. #define X12RTC_DTA0 0x03 /*!< \brief Alarm 0 day of month. */
  29. #define X12RTC_DTA1 0x0B /*!< \brief Alarm 1 day of month. */
  30. #define X12RTC_DTA_EDT 0x80 /*!< \brief Day of month alarm enabled. */
  31. #define X12RTC_MOA0 0x04 /*!< \brief Alarm 0 month. */
  32. #define X12RTC_MOA1 0x0C /*!< \brief Alarm 1 month. */
  33. #define X12RTC_MOA_EMO 0x80 /*!< \brief Month alarm enabled. */
  34. #define X12RTC_YRA0 0x05 /*!< \brief Currently unused alarm 0 register. */
  35. #define X12RTC_YRA1 0x0D /*!< \brief Currently unused alarm 1 register. */
  36. #define X12RTC_DWA0 0x06 /*!< \brief Alarm 0 weekday. */
  37. #define X12RTC_DWA1 0x0E /*!< \brief Alarm 1 weekday. */
  38. #define X12RTC_DWA_EDW 0x80 /*!< \brief Weekday alarm enabled. */
  39. #define X12RTC_Y2K0 0x07 /*!< \brief Alarm 0 . */
  40. #define X12RTC_Y2K1 0x0F /*!< \brief Alarm 1 . */
  41. /*@}*/
  42. /*! \name Non-Volatile Control Registers */
  43. /*@{*/
  44. #define X12RTC_BL 0x10 /*!< \brief Block protection and watchdog register. */
  45. #define X12RTC_BL_WD 0x14 /*!< \brief Watchdog configuration. */
  46. #define X12RTC_BL_WD_1750 0x00 /*!< \brief Timeout after 1.75 seconds. */
  47. #define X12RTC_BL_WD_750 0x04 /*!< \brief Timeout after 750 milliseconds. */
  48. #define X12RTC_BL_WD_250 0x10 /*!< \brief Timeout after 250 milliseconds. */
  49. #define X12RTC_BL_WD_OFF 0x14 /*!< \brief Disabled. */
  50. #define X12RTC_BL_BP 0xE0 /*!< \brief Block protection. */
  51. #define X12RTC_BL_BP_NONE 0x00 /*!< \brief No protection. */
  52. #define X12RTC_BL_BP_UQUAD 0x20 /*!< \brief Upper quarter protected. */
  53. #define X12RTC_BL_BP_UHALF 0x40 /*!< \brief Upper half protected. */
  54. #define X12RTC_BL_BP_FULL 0x60 /*!< \brief Full array protected. */
  55. #define X12RTC_BL_BP_FIRST1 0x80 /*!< \brief First page protected. */
  56. #define X12RTC_BL_BP_FIRST2 0xA0 /*!< \brief First 2 pages protected. */
  57. #define X12RTC_BL_BP_FIRST3 0xC0 /*!< \brief First 4 pages protected. */
  58. #define X12RTC_BL_BP_FIRST8 0xE0 /*!< \brief First 8 pages protected. */
  59. #define X12RTC_INT 0x11 /*!< \brief Interrupt control and freq. output register. */
  60. #define X12RTC_INT_FO 0x14 /*!< \brief Programmable frequency output bits. */
  61. #define X12RTC_INT_FO_IRQ 0x00 /*!< \brief Alarm interrupt. */
  62. #define X12RTC_INT_FO_32KHZ 0x04 /*!< \brief 32.768kHz. */
  63. #define X12RTC_INT_FO_100HZ 0x10 /*!< \brief 100Hz. */
  64. #define X12RTC_INT_FO_1HZ 0x14 /*!< \brief 1Hz. */
  65. #define X12RTC_INT_AL0E 0x20 /*!< \brief Alarm 0 interrupt enable. */
  66. #define X12RTC_INT_AL1E 0x40 /*!< \brief Alarm 1 interrupt enable. */
  67. #define X12RTC_INT_IM 0x80 /*!< \brief Repetitive alarm. */
  68. #define X12RTC_ATR 0x12 /*!< \brief Analog trimming register. */
  69. #define X12RTC_DTR 0x13 /*!< \brief Digital trimming register. */
  70. #define X12RTC_DTR_NONE 0x00 /*!< \brief 0 PPM. */
  71. #define X12RTC_DTR_PLUS10 0x02 /*!< \brief +10 PPM. */
  72. #define X12RTC_DTR_PLUS20 0x01 /*!< \brief +20 PPM. */
  73. #define X12RTC_DTR_PLUS30 0x03 /*!< \brief +30 PPM. */
  74. #define X12RTC_DTR_MINUS10 0x06 /*!< \brief -10 PPM. */
  75. #define X12RTC_DTR_MINUS20 0x05 /*!< \brief -20 PPM. */
  76. #define X12RTC_DTR_MINUS30 0x07 /*!< \brief -30 PPM. */
  77. /*@}*/
  78. /*! \name Volatile Date and Time Registers */
  79. /*@{*/
  80. #define X12RTC_SC 0x30 /*!< Seconds register, 0 - 59. */
  81. #define X12RTC_MN 0x31 /*!< Minutes register, 0 - 59. */
  82. #define X12RTC_HR 0x32 /*!< Hours register, 0 - 23. */
  83. #define X12RTC_HR_MIL 0x80 /*!< Use 24h format. */
  84. #define X12RTC_DT 0x33 /*!< Day register, 1 - 31. */
  85. #define X12RTC_MO 0x34 /*!< Month register, 1 - 12. */
  86. #define X12RTC_YR 0x35 /*!< Year register, 0 - 99. */
  87. #define X12RTC_DW 0x36 /*!< Day of the weeks register, 0 - 6. */
  88. #define X128xRTC_SSEC 0x37 /*!< X1286 1/100 second register, 0 - 99 (read only). */
  89. #define X122xRTC_Y2K 0x37 /*!< X1226 epoch register, 19 or 20. */
  90. /*@}*/
  91. /*! \name Volatile Status Register */
  92. /*@{*/
  93. #define X12RTC_SR 0x3F /*!< Status register. */
  94. #define X12RTC_SR_RTCF 0x01 /*!< Power failure. */
  95. #define X12RTC_SR_WEL 0x02 /*!< Memory write enable. */
  96. #define X12RTC_SR_RWEL 0x04 /*!< Register write enable. */
  97. #define X12RTC_SR_AL0 0x20 /*!< Alarm 0 indicator. */
  98. #define X12RTC_SR_AL1 0x40 /*!< Alarm 1 indicator. */
  99. #define X12RTC_SR_BAT 0x80 /*!< Operating from battery. */
  100. /*@}*/
  101. #define RTC_STATUS_PF 0x00000001
  102. #define RTC_STATUS_AL0 0x00000020
  103. #define RTC_STATUS_AL1 0x00000040
  104. #define RTC_ALARM_SECOND 0x00000001
  105. #define RTC_ALARM_MINUTE 0x00000002
  106. #define RTC_ALARM_HOUR 0x00000004
  107. #define RTC_ALARM_MDAY 0x00000008
  108. #define RTC_ALARM_MONTH 0x00000010
  109. #define RTC_ALARM_WDAY 0x00000080
  110. /*!
  111. * \brief Convert binary coded decimal to binary value.
  112. */
  113. #define BCD2BIN(x) ((((u_char)(x)) >> 4) * 10 + ((x) & 0x0F))
  114. /*!
  115. * \brief Convert binary to binary coded decimal value.
  116. */
  117. #define BIN2BCD(x) (((((u_char)(x)) / 10) << 4) + (x) % 10)
  118. /* Prototypes */
  119. extern int X12Init(void);
  120. extern int X12RtcGetClock(tm *tm);
  121. extern int X12RtcSetClock(CONST tm *tm);
  122. extern int X12RtcGetAlarm(int idx, tm *tm, int *aflgs);
  123. extern int X12RtcSetAlarm(int idx, CONST tm *tm, int aflgs);
  124. extern int X12RtcGetStatus(u_long *sflgs);
  125. extern int X12RtcClearStatus(u_long sflgs);
  126. extern int X12RtcReadRegs(u_char addr, u_char *buff, size_t len);
  127. extern int X12RtcWrite(int nv, CONST u_char *buff, size_t len);
  128. extern int X12EepromRead(u_int addr, void *buff, size_t len);
  129. extern int X12EepromWrite(u_int addr, CONST void *buff, size_t len);
  130. extern tm GetRTCTime(void);
  131. /* End of prototypes */
  132. #endif