Zephyr Project API 4.2.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
14#ifndef ZEPHYR_INCLUDE_DRIVERS_RTC_H_
15#define ZEPHYR_INCLUDE_DRIVERS_RTC_H_
16
26#include <zephyr/kernel.h>
27#include <zephyr/device.h>
28#include <errno.h>
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
40#define RTC_ALARM_TIME_MASK_SECOND BIT(0)
41#define RTC_ALARM_TIME_MASK_MINUTE BIT(1)
42#define RTC_ALARM_TIME_MASK_HOUR BIT(2)
43#define RTC_ALARM_TIME_MASK_MONTHDAY BIT(3)
44#define RTC_ALARM_TIME_MASK_MONTH BIT(4)
45#define RTC_ALARM_TIME_MASK_YEAR BIT(5)
46#define RTC_ALARM_TIME_MASK_WEEKDAY BIT(6)
47#define RTC_ALARM_TIME_MASK_YEARDAY BIT(7)
48#define RTC_ALARM_TIME_MASK_NSEC BIT(8)
62struct rtc_time {
63 int tm_sec;
64 int tm_min;
65 int tm_hour;
66 int tm_mday;
67 int tm_mon;
68 int tm_year;
69 int tm_wday;
70 int tm_yday;
72 int tm_nsec;
73};
74
82typedef void (*rtc_update_callback)(const struct device *dev, void *user_data);
83
92typedef void (*rtc_alarm_callback)(const struct device *dev, uint16_t id, void *user_data);
93
104typedef int (*rtc_api_set_time)(const struct device *dev, const struct rtc_time *timeptr);
105
110typedef int (*rtc_api_get_time)(const struct device *dev, struct rtc_time *timeptr);
111
116typedef int (*rtc_api_alarm_get_supported_fields)(const struct device *dev, uint16_t id,
117 uint16_t *mask);
118
123typedef int (*rtc_api_alarm_set_time)(const struct device *dev, uint16_t id, uint16_t mask,
124 const struct rtc_time *timeptr);
125
130typedef int (*rtc_api_alarm_get_time)(const struct device *dev, uint16_t id, uint16_t *mask,
131 struct rtc_time *timeptr);
132
137typedef int (*rtc_api_alarm_is_pending)(const struct device *dev, uint16_t id);
138
143typedef int (*rtc_api_alarm_set_callback)(const struct device *dev, uint16_t id,
144 rtc_alarm_callback callback, void *user_data);
145
150typedef int (*rtc_api_update_set_callback)(const struct device *dev,
151 rtc_update_callback callback, void *user_data);
152
157typedef int (*rtc_api_set_calibration)(const struct device *dev, int32_t calibration);
158
163typedef int (*rtc_api_get_calibration)(const struct device *dev, int32_t *calibration);
164
168__subsystem struct rtc_driver_api {
169 rtc_api_set_time set_time;
170 rtc_api_get_time get_time;
171#if defined(CONFIG_RTC_ALARM) || defined(__DOXYGEN__)
172 rtc_api_alarm_get_supported_fields alarm_get_supported_fields;
173 rtc_api_alarm_set_time alarm_set_time;
174 rtc_api_alarm_get_time alarm_get_time;
175 rtc_api_alarm_is_pending alarm_is_pending;
176 rtc_api_alarm_set_callback alarm_set_callback;
177#endif /* CONFIG_RTC_ALARM */
178#if defined(CONFIG_RTC_UPDATE) || defined(__DOXYGEN__)
179 rtc_api_update_set_callback update_set_callback;
180#endif /* CONFIG_RTC_UPDATE */
181#if defined(CONFIG_RTC_CALIBRATION) || defined(__DOXYGEN__)
182 rtc_api_set_calibration set_calibration;
183 rtc_api_get_calibration get_calibration;
184#endif /* CONFIG_RTC_CALIBRATION */
185};
186
199__syscall int rtc_set_time(const struct device *dev, const struct rtc_time *timeptr);
200
201static inline int z_impl_rtc_set_time(const struct device *dev, const struct rtc_time *timeptr)
202{
203 return DEVICE_API_GET(rtc, dev)->set_time(dev, timeptr);
204}
205
216__syscall int rtc_get_time(const struct device *dev, struct rtc_time *timeptr);
217
218static inline int z_impl_rtc_get_time(const struct device *dev, struct rtc_time *timeptr)
219{
220 return DEVICE_API_GET(rtc, dev)->get_time(dev, timeptr);
221}
222
227#if defined(CONFIG_RTC_ALARM) || defined(__DOXYGEN__)
228
243__syscall int rtc_alarm_get_supported_fields(const struct device *dev, uint16_t id,
244 uint16_t *mask);
245
246static inline int z_impl_rtc_alarm_get_supported_fields(const struct device *dev, uint16_t id,
247 uint16_t *mask)
248{
249 if (DEVICE_API_GET(rtc, dev)->alarm_get_supported_fields == NULL) {
250 return -ENOSYS;
251 }
252
253 return DEVICE_API_GET(rtc, dev)->alarm_get_supported_fields(dev, id, mask);
254}
255
279__syscall int rtc_alarm_set_time(const struct device *dev, uint16_t id, uint16_t mask,
280 const struct rtc_time *timeptr);
281
282static inline int z_impl_rtc_alarm_set_time(const struct device *dev, uint16_t id, uint16_t mask,
283 const struct rtc_time *timeptr)
284{
285 if (DEVICE_API_GET(rtc, dev)->alarm_set_time == NULL) {
286 return -ENOSYS;
287 }
288
289 return DEVICE_API_GET(rtc, dev)->alarm_set_time(dev, id, mask, timeptr);
290}
291
307__syscall int rtc_alarm_get_time(const struct device *dev, uint16_t id, uint16_t *mask,
308 struct rtc_time *timeptr);
309
310static inline int z_impl_rtc_alarm_get_time(const struct device *dev, uint16_t id, uint16_t *mask,
311 struct rtc_time *timeptr)
312{
313 if (DEVICE_API_GET(rtc, dev)->alarm_get_time == NULL) {
314 return -ENOSYS;
315 }
316
317 return DEVICE_API_GET(rtc, dev)->alarm_get_time(dev, id, mask, timeptr);
318}
319
335__syscall int rtc_alarm_is_pending(const struct device *dev, uint16_t id);
336
337static inline int z_impl_rtc_alarm_is_pending(const struct device *dev, uint16_t id)
338{
339 if (DEVICE_API_GET(rtc, dev)->alarm_is_pending == NULL) {
340 return -ENOSYS;
341 }
342
343 return DEVICE_API_GET(rtc, dev)->alarm_is_pending(dev, id);
344}
345
372static inline int rtc_alarm_set_callback(const struct device *dev, uint16_t id,
373 rtc_alarm_callback callback, void *user_data)
374{
375 if (DEVICE_API_GET(rtc, dev)->alarm_set_callback == NULL) {
376 return -ENOSYS;
377 }
378
379 return DEVICE_API_GET(rtc, dev)->alarm_set_callback(dev, id, callback, user_data);
380}
381
382#endif /* CONFIG_RTC_ALARM */
391#if defined(CONFIG_RTC_UPDATE) || defined(__DOXYGEN__)
392
412static inline int rtc_update_set_callback(const struct device *dev,
413 rtc_update_callback callback, void *user_data)
414{
415 if (DEVICE_API_GET(rtc, dev)->update_set_callback == NULL) {
416 return -ENOSYS;
417 }
418
419 return DEVICE_API_GET(rtc, dev)->update_set_callback(dev, callback, user_data);
420}
421
422#endif /* CONFIG_RTC_UPDATE */
431#if defined(CONFIG_RTC_CALIBRATION) || defined(__DOXYGEN__)
432
451__syscall int rtc_set_calibration(const struct device *dev, int32_t calibration);
452
453static inline int z_impl_rtc_set_calibration(const struct device *dev, int32_t calibration)
454{
455 if (DEVICE_API_GET(rtc, dev)->set_calibration == NULL) {
456 return -ENOSYS;
457 }
458
459 return DEVICE_API_GET(rtc, dev)->set_calibration(dev, calibration);
460}
461
472__syscall int rtc_get_calibration(const struct device *dev, int32_t *calibration);
473
474static inline int z_impl_rtc_get_calibration(const struct device *dev, int32_t *calibration)
475{
476 if (DEVICE_API_GET(rtc, dev)->get_calibration == NULL) {
477 return -ENOSYS;
478 }
479
480 return DEVICE_API_GET(rtc, dev)->get_calibration(dev, calibration);
481}
482
483#endif /* CONFIG_RTC_CALIBRATION */
496struct tm;
497
502static inline struct tm *rtc_time_to_tm(struct rtc_time *timeptr)
503{
504 return (struct tm *)timeptr;
505}
506
515{
516 __ASSERT_NO_MSG(frequency > 0);
517
518 return (int32_t)((1000000000000000000LL / frequency) - 1000000000);
519}
520
529#ifdef __cplusplus
530}
531#endif
532
533#include <zephyr/syscalls/rtc.h>
534
535#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:1350
System error numbers.
void(* rtc_alarm_callback)(const struct device *dev, uint16_t id, void *user_data)
RTC alarm triggered callback.
Definition rtc.h:92
static int rtc_update_set_callback(const struct device *dev, rtc_update_callback callback, void *user_data)
API for setting update callback.
Definition rtc.h:412
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.
static int rtc_alarm_set_callback(const struct device *dev, uint16_t id, rtc_alarm_callback callback, void *user_data)
API for setting alarm callback.
Definition rtc.h:372
void(* rtc_update_callback)(const struct device *dev, void *user_data)
RTC update event callback.
Definition rtc.h:82
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:502
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:514
#define ENOSYS
Function not implemented.
Definition errno.h:82
#define NULL
Definition iar_missing_defs.h:20
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:510
Structure for storing date and time values with sub-second precision.
Definition rtc.h:62
int tm_yday
Day of the year [0, 365] (Unknown = -1)
Definition rtc.h:70
int tm_nsec
Nanoseconds [0, 999999999] (Unknown = 0)
Definition rtc.h:72
int tm_min
Minutes [0, 59].
Definition rtc.h:64
int tm_mon
Month [0, 11].
Definition rtc.h:67
int tm_isdst
Daylight saving time flag [-1] (Unknown = -1)
Definition rtc.h:71
int tm_wday
Day of the week [0, 6] (Sunday = 0) (Unknown = -1)
Definition rtc.h:69
int tm_year
Year - 1900.
Definition rtc.h:68
int tm_sec
Seconds [0, 59].
Definition rtc.h:63
int tm_hour
Hours [0, 23].
Definition rtc.h:65
int tm_mday
Day of the month [1, 31].
Definition rtc.h:66
Definition time.h:24