Zephyr API Documentation 4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
rtc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Trackunit Corporation
3 * Copyright (c) 2023 Bjarki Arge Andreasen
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
13#ifndef ZEPHYR_INCLUDE_DRIVERS_RTC_H_
14#define ZEPHYR_INCLUDE_DRIVERS_RTC_H_
15
25#include <zephyr/kernel.h>
26#include <zephyr/device.h>
27#include <errno.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
39#define RTC_ALARM_TIME_MASK_SECOND BIT(0)
40#define RTC_ALARM_TIME_MASK_MINUTE BIT(1)
41#define RTC_ALARM_TIME_MASK_HOUR BIT(2)
42#define RTC_ALARM_TIME_MASK_MONTHDAY BIT(3)
43#define RTC_ALARM_TIME_MASK_MONTH BIT(4)
44#define RTC_ALARM_TIME_MASK_YEAR BIT(5)
45#define RTC_ALARM_TIME_MASK_WEEKDAY BIT(6)
46#define RTC_ALARM_TIME_MASK_YEARDAY BIT(7)
47#define RTC_ALARM_TIME_MASK_NSEC BIT(8)
61struct rtc_time {
62 int tm_sec;
63 int tm_min;
64 int tm_hour;
65 int tm_mday;
66 int tm_mon;
67 int tm_year;
68 int tm_wday;
69 int tm_yday;
71 int tm_nsec;
72};
73
81typedef void (*rtc_update_callback)(const struct device *dev, void *user_data);
82
91typedef void (*rtc_alarm_callback)(const struct device *dev, uint16_t id, void *user_data);
92
103typedef int (*rtc_api_set_time)(const struct device *dev, const struct rtc_time *timeptr);
104
109typedef int (*rtc_api_get_time)(const struct device *dev, struct rtc_time *timeptr);
110
115typedef int (*rtc_api_alarm_get_supported_fields)(const struct device *dev, uint16_t id,
116 uint16_t *mask);
117
122typedef int (*rtc_api_alarm_set_time)(const struct device *dev, uint16_t id, uint16_t mask,
123 const struct rtc_time *timeptr);
124
129typedef int (*rtc_api_alarm_get_time)(const struct device *dev, uint16_t id, uint16_t *mask,
130 struct rtc_time *timeptr);
131
136typedef int (*rtc_api_alarm_is_pending)(const struct device *dev, uint16_t id);
137
142typedef int (*rtc_api_alarm_set_callback)(const struct device *dev, uint16_t id,
143 rtc_alarm_callback callback, void *user_data);
144
149typedef int (*rtc_api_update_set_callback)(const struct device *dev,
150 rtc_update_callback callback, void *user_data);
151
156typedef int (*rtc_api_set_calibration)(const struct device *dev, int32_t calibration);
157
162typedef int (*rtc_api_get_calibration)(const struct device *dev, int32_t *calibration);
163
167__subsystem struct rtc_driver_api {
168 rtc_api_set_time set_time;
169 rtc_api_get_time get_time;
170#if defined(CONFIG_RTC_ALARM) || defined(__DOXYGEN__)
171 rtc_api_alarm_get_supported_fields alarm_get_supported_fields;
172 rtc_api_alarm_set_time alarm_set_time;
173 rtc_api_alarm_get_time alarm_get_time;
174 rtc_api_alarm_is_pending alarm_is_pending;
175 rtc_api_alarm_set_callback alarm_set_callback;
176#endif /* CONFIG_RTC_ALARM */
177#if defined(CONFIG_RTC_UPDATE) || defined(__DOXYGEN__)
178 rtc_api_update_set_callback update_set_callback;
179#endif /* CONFIG_RTC_UPDATE */
180#if defined(CONFIG_RTC_CALIBRATION) || defined(__DOXYGEN__)
181 rtc_api_set_calibration set_calibration;
182 rtc_api_get_calibration get_calibration;
183#endif /* CONFIG_RTC_CALIBRATION */
184};
185
198__syscall int rtc_set_time(const struct device *dev, const struct rtc_time *timeptr);
199
200static inline int z_impl_rtc_set_time(const struct device *dev, const struct rtc_time *timeptr)
201{
202 return DEVICE_API_GET(rtc, dev)->set_time(dev, timeptr);
203}
204
215__syscall int rtc_get_time(const struct device *dev, struct rtc_time *timeptr);
216
217static inline int z_impl_rtc_get_time(const struct device *dev, struct rtc_time *timeptr)
218{
219 return DEVICE_API_GET(rtc, dev)->get_time(dev, timeptr);
220}
221
226#if defined(CONFIG_RTC_ALARM) || defined(__DOXYGEN__)
227
242__syscall int rtc_alarm_get_supported_fields(const struct device *dev, uint16_t id,
243 uint16_t *mask);
244
245static inline int z_impl_rtc_alarm_get_supported_fields(const struct device *dev, uint16_t id,
246 uint16_t *mask)
247{
248 if (DEVICE_API_GET(rtc, dev)->alarm_get_supported_fields == NULL) {
249 return -ENOSYS;
250 }
251
252 return DEVICE_API_GET(rtc, dev)->alarm_get_supported_fields(dev, id, mask);
253}
254
278__syscall int rtc_alarm_set_time(const struct device *dev, uint16_t id, uint16_t mask,
279 const struct rtc_time *timeptr);
280
281static inline int z_impl_rtc_alarm_set_time(const struct device *dev, uint16_t id, uint16_t mask,
282 const struct rtc_time *timeptr)
283{
284 if (DEVICE_API_GET(rtc, dev)->alarm_set_time == NULL) {
285 return -ENOSYS;
286 }
287
288 return DEVICE_API_GET(rtc, dev)->alarm_set_time(dev, id, mask, timeptr);
289}
290
306__syscall int rtc_alarm_get_time(const struct device *dev, uint16_t id, uint16_t *mask,
307 struct rtc_time *timeptr);
308
309static inline int z_impl_rtc_alarm_get_time(const struct device *dev, uint16_t id, uint16_t *mask,
310 struct rtc_time *timeptr)
311{
312 if (DEVICE_API_GET(rtc, dev)->alarm_get_time == NULL) {
313 return -ENOSYS;
314 }
315
316 return DEVICE_API_GET(rtc, dev)->alarm_get_time(dev, id, mask, timeptr);
317}
318
334__syscall int rtc_alarm_is_pending(const struct device *dev, uint16_t id);
335
336static inline int z_impl_rtc_alarm_is_pending(const struct device *dev, uint16_t id)
337{
338 if (DEVICE_API_GET(rtc, dev)->alarm_is_pending == NULL) {
339 return -ENOSYS;
340 }
341
342 return DEVICE_API_GET(rtc, dev)->alarm_is_pending(dev, id);
343}
344
371__syscall int rtc_alarm_set_callback(const struct device *dev, uint16_t id,
372 rtc_alarm_callback callback, void *user_data);
373
374static inline int z_impl_rtc_alarm_set_callback(const struct device *dev, uint16_t id,
375 rtc_alarm_callback callback, void *user_data)
376{
377 if (DEVICE_API_GET(rtc, dev)->alarm_set_callback == NULL) {
378 return -ENOSYS;
379 }
380
381 return DEVICE_API_GET(rtc, dev)->alarm_set_callback(dev, id, callback, user_data);
382}
383
384#endif /* CONFIG_RTC_ALARM */
393#if defined(CONFIG_RTC_UPDATE) || defined(__DOXYGEN__)
394
414__syscall int rtc_update_set_callback(const struct device *dev, rtc_update_callback callback,
415 void *user_data);
416
417static inline int z_impl_rtc_update_set_callback(const struct device *dev,
418 rtc_update_callback callback, void *user_data)
419{
420 if (DEVICE_API_GET(rtc, dev)->update_set_callback == NULL) {
421 return -ENOSYS;
422 }
423
424 return DEVICE_API_GET(rtc, dev)->update_set_callback(dev, callback, user_data);
425}
426
427#endif /* CONFIG_RTC_UPDATE */
436#if defined(CONFIG_RTC_CALIBRATION) || defined(__DOXYGEN__)
437
456__syscall int rtc_set_calibration(const struct device *dev, int32_t calibration);
457
458static inline int z_impl_rtc_set_calibration(const struct device *dev, int32_t calibration)
459{
460 if (DEVICE_API_GET(rtc, dev)->set_calibration == NULL) {
461 return -ENOSYS;
462 }
463
464 return DEVICE_API_GET(rtc, dev)->set_calibration(dev, calibration);
465}
466
477__syscall int rtc_get_calibration(const struct device *dev, int32_t *calibration);
478
479static inline int z_impl_rtc_get_calibration(const struct device *dev, int32_t *calibration)
480{
481 if (DEVICE_API_GET(rtc, dev)->get_calibration == NULL) {
482 return -ENOSYS;
483 }
484
485 return DEVICE_API_GET(rtc, dev)->get_calibration(dev, calibration);
486}
487
488#endif /* CONFIG_RTC_CALIBRATION */
501struct tm;
502
507static inline struct tm *rtc_time_to_tm(struct rtc_time *timeptr)
508{
509 return (struct tm *)timeptr;
510}
511
520{
521 __ASSERT_NO_MSG(frequency > 0);
522
523 return (int32_t)((1000000000000000000LL / frequency) - 1000000000);
524}
525
534#ifdef __cplusplus
535}
536#endif
537
538#include <zephyr/syscalls/rtc.h>
539
540#endif /* ZEPHYR_INCLUDE_DRIVERS_RTC_H_ */
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1221
System error numbers.
int rtc_set_calibration(const struct device *dev, int32_t calibration)
API for setting RTC calibration.
int rtc_alarm_set_time(const struct device *dev, uint16_t id, uint16_t mask, const struct rtc_time *timeptr)
API for setting RTC alarm time.
int rtc_get_time(const struct device *dev, struct rtc_time *timeptr)
API for getting RTC time.
int rtc_set_time(const struct device *dev, const struct rtc_time *timeptr)
API for setting RTC time.
int rtc_alarm_is_pending(const struct device *dev, uint16_t id)
API for testing if RTC alarm is pending.
int rtc_alarm_get_supported_fields(const struct device *dev, uint16_t id, uint16_t *mask)
API for getting the supported fields of the RTC alarm time.
int rtc_alarm_set_callback(const struct device *dev, uint16_t id, rtc_alarm_callback callback, void *user_data)
API for setting alarm callback.
void(* rtc_alarm_callback)(const struct device *dev, uint16_t id, void *user_data)
RTC alarm triggered callback.
Definition rtc.h:91
void(* rtc_update_callback)(const struct device *dev, void *user_data)
RTC update event callback.
Definition rtc.h:81
int rtc_get_calibration(const struct device *dev, int32_t *calibration)
API for getting RTC calibration.
static struct tm * rtc_time_to_tm(struct rtc_time *timeptr)
Convenience function for safely casting a rtc_time pointer to a tm pointer.
Definition rtc.h:507
int rtc_alarm_get_time(const struct device *dev, uint16_t id, uint16_t *mask, struct rtc_time *timeptr)
API for getting RTC alarm time.
static int32_t rtc_calibration_from_frequency(uint32_t frequency)
Determine required calibration to 1 Hertz from frequency.
Definition rtc.h:519
int rtc_update_set_callback(const struct device *dev, rtc_update_callback callback, void *user_data)
API for setting update callback.
#define ENOSYS
Function not implemented.
Definition errno.h:82
Public kernel APIs.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Runtime device structure (in ROM) per driver instance.
Definition device.h:411
Structure for storing date and time values with sub-second precision.
Definition rtc.h:61
int tm_yday
Day of the year [0, 365] (Unknown = -1)
Definition rtc.h:69
int tm_nsec
Nanoseconds [0, 999999999] (Unknown = 0)
Definition rtc.h:71
int tm_min
Minutes [0, 59].
Definition rtc.h:63
int tm_mon
Month [0, 11].
Definition rtc.h:66
int tm_isdst
Daylight saving time flag [-1] (Unknown = -1)
Definition rtc.h:70
int tm_wday
Day of the week [0, 6] (Sunday = 0) (Unknown = -1)
Definition rtc.h:68
int tm_year
Year - 1900.
Definition rtc.h:67
int tm_sec
Seconds [0, 59].
Definition rtc.h:62
int tm_hour
Hours [0, 23].
Definition rtc.h:64
int tm_mday
Day of the month [1, 31].
Definition rtc.h:65
Definition time.h:24