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