hal_rtc.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /*
  14. Concepts and parts of this file have been contributed by Uladzimir Pylinsky
  15. aka barthess.
  16. */
  17. /**
  18. * @file hal_rtc.h
  19. * @brief RTC Driver macros and structures.
  20. *
  21. * @addtogroup RTC
  22. * @{
  23. */
  24. #ifndef HAL_RTC_H
  25. #define HAL_RTC_H
  26. #if (HAL_USE_RTC == TRUE) || defined(__DOXYGEN__)
  27. /*lint -save -e829 [21.10] The header is required.*/
  28. #include <time.h>
  29. /*lint -restore*/
  30. /*===========================================================================*/
  31. /* Driver constants. */
  32. /*===========================================================================*/
  33. /**
  34. * @brief Base year of the calendar.
  35. */
  36. #define RTC_BASE_YEAR 1980U
  37. /**
  38. * @name Date/Time bit masks for FAT format
  39. * @{
  40. */
  41. #define RTC_FAT_TIME_SECONDS_MASK 0x0000001FU
  42. #define RTC_FAT_TIME_MINUTES_MASK 0x000007E0U
  43. #define RTC_FAT_TIME_HOURS_MASK 0x0000F800U
  44. #define RTC_FAT_DATE_DAYS_MASK 0x001F0000U
  45. #define RTC_FAT_DATE_MONTHS_MASK 0x01E00000U
  46. #define RTC_FAT_DATE_YEARS_MASK 0xFE000000U
  47. /** @} */
  48. /**
  49. * @name Day of week encoding
  50. * @{
  51. */
  52. #define RTC_DAY_CATURDAY 0U
  53. #define RTC_DAY_MONDAY 1U
  54. #define RTC_DAY_TUESDAY 2U
  55. #define RTC_DAY_WEDNESDAY 3U
  56. #define RTC_DAY_THURSDAY 4U
  57. #define RTC_DAY_FRIDAY 5U
  58. #define RTC_DAY_SATURDAY 6U
  59. #define RTC_DAY_SUNDAY 7U
  60. /** @} */
  61. /*===========================================================================*/
  62. /* Driver pre-compile time settings. */
  63. /*===========================================================================*/
  64. /*===========================================================================*/
  65. /* Derived constants and error checks. */
  66. /*===========================================================================*/
  67. /*===========================================================================*/
  68. /* Driver data structures and types. */
  69. /*===========================================================================*/
  70. /**
  71. * @brief Type of a structure representing an RTC driver.
  72. */
  73. typedef struct RTCDriver RTCDriver;
  74. /**
  75. * @brief Type of an RTC alarm number.
  76. */
  77. typedef unsigned int rtcalarm_t;
  78. /**
  79. * @brief Type of a structure representing an RTC date/time stamp.
  80. */
  81. typedef struct {
  82. /*lint -save -e46 [6.1] In this case uint32_t is fine.*/
  83. uint32_t year: 8; /**< @brief Years since 1980. */
  84. uint32_t month: 4; /**< @brief Months 1..12. */
  85. uint32_t dstflag: 1; /**< @brief DST correction flag. */
  86. uint32_t dayofweek: 3; /**< @brief Day of week 1..7. */
  87. uint32_t day: 5; /**< @brief Day of the month 1..31. */
  88. uint32_t millisecond: 27; /**< @brief Milliseconds since midnight.*/
  89. /*lint -restore*/
  90. } RTCDateTime;
  91. /**
  92. * @brief BasePersistentStorage specific methods.
  93. */
  94. #define _rtc_driver_methods \
  95. _base_pers_storage_methods
  96. #include "hal_rtc_lld.h"
  97. /* Some more checks, must happen after inclusion of the LLD header, this is
  98. why are placed here.*/
  99. #if !defined(RTC_SUPPORTS_CALLBACKS)
  100. #error "RTC LLD does not define the required RTC_SUPPORTS_CALLBACKS macro"
  101. #endif
  102. #if !defined(RTC_ALARMS)
  103. #error "RTC LLD does not define the required RTC_ALARMS macro"
  104. #endif
  105. #if !defined(RTC_HAS_STORAGE)
  106. #error "RTC LLD does not define the required RTC_HAS_STORAGE macro"
  107. #endif
  108. #if (RTC_HAS_STORAGE == TRUE) || defined(__DOXYGEN__)
  109. /**
  110. * @extends FileStream
  111. *
  112. * @brief @p RTCDriver virtual methods table.
  113. */
  114. struct RTCDriverVMT {
  115. _rtc_driver_methods
  116. };
  117. #endif
  118. /**
  119. * @brief Structure representing an RTC driver.
  120. */
  121. struct RTCDriver {
  122. #if (RTC_HAS_STORAGE == TRUE) || defined(__DOXYGEN__)
  123. /**
  124. * @brief Virtual Methods Table.
  125. */
  126. const struct RTCDriverVMT *vmt;
  127. #endif
  128. #if defined(RTC_DRIVER_EXT_FIELDS)
  129. RTC_DRIVER_EXT_FIELDS
  130. #endif
  131. /* End of the mandatory fields.*/
  132. rtc_lld_driver_fields;
  133. };
  134. /*===========================================================================*/
  135. /* Driver macros. */
  136. /*===========================================================================*/
  137. /*===========================================================================*/
  138. /* External declarations. */
  139. /*===========================================================================*/
  140. #if !defined(__DOXYGEN__)
  141. extern RTCDriver RTCD1;
  142. #if RTC_HAS_STORAGE == TRUE
  143. extern struct RTCDriverVMT _rtc_lld_vmt;
  144. #endif
  145. #endif
  146. #ifdef __cplusplus
  147. extern "C" {
  148. #endif
  149. void rtcInit(void);
  150. void rtcObjectInit(RTCDriver *rtcp);
  151. void rtcSetTime(RTCDriver *rtcp, const RTCDateTime *timespec);
  152. void rtcGetTime(RTCDriver *rtcp, RTCDateTime *timespec);
  153. #if RTC_ALARMS > 0
  154. void rtcSetAlarm(RTCDriver *rtcp,
  155. rtcalarm_t alarm,
  156. const RTCAlarm *alarmspec);
  157. void rtcGetAlarm(RTCDriver *rtcp, rtcalarm_t alarm, RTCAlarm *alarmspec);
  158. #endif
  159. #if RTC_SUPPORTS_CALLBACKS == TRUE
  160. void rtcSetCallback(RTCDriver *rtcp, rtccb_t callback);
  161. #endif
  162. void rtcConvertDateTimeToStructTm(const RTCDateTime *timespec,
  163. struct tm *timp,
  164. uint32_t *tv_msec);
  165. void rtcConvertStructTmToDateTime(const struct tm *timp,
  166. uint32_t tv_msec,
  167. RTCDateTime *timespec);
  168. uint32_t rtcConvertDateTimeToFAT(const RTCDateTime *timespec);
  169. #ifdef __cplusplus
  170. }
  171. #endif
  172. #endif /* HAL_USE_RTC == TRUE */
  173. #endif /* HAL_RTC_H */
  174. /** @} */