ChibiOS/HAL  6.1.0
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 a structure representing an RTC date/time stamp.
92  */
93 typedef struct {
94  /*lint -save -e46 [6.1] In this case uint32_t is fine.*/
95  uint32_t year: 8; /**< @brief Years since 1980. */
96  uint32_t month: 4; /**< @brief Months 1..12. */
97  uint32_t dstflag: 1; /**< @brief DST correction flag. */
98  uint32_t dayofweek: 3; /**< @brief Day of week 1..7. */
99  uint32_t day: 5; /**< @brief Day of the month 1..31. */
100  uint32_t millisecond: 27; /**< @brief Milliseconds since midnight.*/
101  /*lint -restore*/
102 } RTCDateTime;
103 
104 #include "hal_rtc_lld.h"
105 
106 /*===========================================================================*/
107 /* Driver macros. */
108 /*===========================================================================*/
109 
110 /*===========================================================================*/
111 /* External declarations. */
112 /*===========================================================================*/
113 
114 #ifdef __cplusplus
115 extern "C" {
116 #endif
117  void rtcInit(void);
118  void rtcObjectInit(RTCDriver *rtcp);
119  void rtcSetTime(RTCDriver *rtcp, const RTCDateTime *timespec);
120  void rtcGetTime(RTCDriver *rtcp, RTCDateTime *timespec);
121 #if RTC_ALARMS > 0
122  void rtcSetAlarm(RTCDriver *rtcp,
123  rtcalarm_t alarm,
124  const RTCAlarm *alarmspec);
125  void rtcGetAlarm(RTCDriver *rtcp, rtcalarm_t alarm, RTCAlarm *alarmspec);
126 #endif
127 #if RTC_SUPPORTS_CALLBACKS == TRUE
128  void rtcSetCallback(RTCDriver *rtcp, rtccb_t callback);
129 #endif
130  void rtcConvertDateTimeToStructTm(const RTCDateTime *timespec,
131  struct tm *timp,
132  uint32_t *tv_msec);
133  void rtcConvertStructTmToDateTime(const struct tm *timp,
134  uint32_t tv_msec,
135  RTCDateTime *timespec);
136  uint32_t rtcConvertDateTimeToFAT(const RTCDateTime *timespec);
137 #ifdef __cplusplus
138 }
139 #endif
140 
141 #endif /* HAL_USE_RTC == TRUE */
142 #endif /* HAL_RTC_H */
143 
144 /** @} */
PLATFORM RTC subsystem low level driver header.
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:111
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
uint32_t rtcalarm_t
Type of an RTC alarm number.
Definition: hal_rtc_lld.h:92
Type of a structure representing an RTC date/time stamp.
Definition: hal_rtc.h:93
Structure representing an RTC driver.
Definition: hal_rtc_lld.h:130
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:105
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