Zephyr API Documentation 4.0.0-rc3
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 const struct rtc_driver_api *api = (const struct rtc_driver_api *)dev->api;
203
204 return api->set_time(dev, timeptr);
205}
206
217__syscall int rtc_get_time(const struct device *dev, struct rtc_time *timeptr);
218
219static inline int z_impl_rtc_get_time(const struct device *dev, struct rtc_time *timeptr)
220{
221 const struct rtc_driver_api *api = (const struct rtc_driver_api *)dev->api;
222
223 return api->get_time(dev, timeptr);
224}
225
230#if defined(CONFIG_RTC_ALARM) || defined(__DOXYGEN__)
231
246__syscall int rtc_alarm_get_supported_fields(const struct device *dev, uint16_t id,
247 uint16_t *mask);
248
249static inline int z_impl_rtc_alarm_get_supported_fields(const struct device *dev, uint16_t id,
250 uint16_t *mask)
251{
252 const struct rtc_driver_api *api = (const struct rtc_driver_api *)dev->api;
253
254 if (api->alarm_get_supported_fields == NULL) {
255 return -ENOSYS;
256 }
257
258 return api->alarm_get_supported_fields(dev, id, mask);
259}
260
284__syscall int rtc_alarm_set_time(const struct device *dev, uint16_t id, uint16_t mask,
285 const struct rtc_time *timeptr);
286
287static inline int z_impl_rtc_alarm_set_time(const struct device *dev, uint16_t id, uint16_t mask,
288 const struct rtc_time *timeptr)
289{
290 const struct rtc_driver_api *api = (const struct rtc_driver_api *)dev->api;
291
292 if (api->alarm_set_time == NULL) {
293 return -ENOSYS;
294 }
295
296 return api->alarm_set_time(dev, id, mask, timeptr);
297}
298
314__syscall int rtc_alarm_get_time(const struct device *dev, uint16_t id, uint16_t *mask,
315 struct rtc_time *timeptr);
316
317static inline int z_impl_rtc_alarm_get_time(const struct device *dev, uint16_t id, uint16_t *mask,
318 struct rtc_time *timeptr)
319{
320 const struct rtc_driver_api *api = (const struct rtc_driver_api *)dev->api;
321
322 if (api->alarm_get_time == NULL) {
323 return -ENOSYS;
324 }
325
326 return api->alarm_get_time(dev, id, mask, timeptr);
327}
328
344__syscall int rtc_alarm_is_pending(const struct device *dev, uint16_t id);
345
346static inline int z_impl_rtc_alarm_is_pending(const struct device *dev, uint16_t id)
347{
348 const struct rtc_driver_api *api = (const struct rtc_driver_api *)dev->api;
349
350 if (api->alarm_is_pending == NULL) {
351 return -ENOSYS;
352 }
353
354 return api->alarm_is_pending(dev, id);
355}
356
383__syscall int rtc_alarm_set_callback(const struct device *dev, uint16_t id,
384 rtc_alarm_callback callback, void *user_data);
385
386static inline int z_impl_rtc_alarm_set_callback(const struct device *dev, uint16_t id,
387 rtc_alarm_callback callback, void *user_data)
388{
389 const struct rtc_driver_api *api = (const struct rtc_driver_api *)dev->api;
390
391 if (api->alarm_set_callback == NULL) {
392 return -ENOSYS;
393 }
394
395 return api->alarm_set_callback(dev, id, callback, user_data);
396}
397
398#endif /* CONFIG_RTC_ALARM */
407#if defined(CONFIG_RTC_UPDATE) || defined(__DOXYGEN__)
408
428__syscall int rtc_update_set_callback(const struct device *dev, rtc_update_callback callback,
429 void *user_data);
430
431static inline int z_impl_rtc_update_set_callback(const struct device *dev,
432 rtc_update_callback callback, void *user_data)
433{
434 const struct rtc_driver_api *api = (const struct rtc_driver_api *)dev->api;
435
436 if (api->update_set_callback == NULL) {
437 return -ENOSYS;
438 }
439
440 return api->update_set_callback(dev, callback, user_data);
441}
442
443#endif /* CONFIG_RTC_UPDATE */
452#if defined(CONFIG_RTC_CALIBRATION) || defined(__DOXYGEN__)
453
472__syscall int rtc_set_calibration(const struct device *dev, int32_t calibration);
473
474static inline int z_impl_rtc_set_calibration(const struct device *dev, int32_t calibration)
475{
476 const struct rtc_driver_api *api = (const struct rtc_driver_api *)dev->api;
477
478 if (api->set_calibration == NULL) {
479 return -ENOSYS;
480 }
481
482 return api->set_calibration(dev, calibration);
483}
484
495__syscall int rtc_get_calibration(const struct device *dev, int32_t *calibration);
496
497static inline int z_impl_rtc_get_calibration(const struct device *dev, int32_t *calibration)
498{
499 const struct rtc_driver_api *api = (const struct rtc_driver_api *)dev->api;
500
501 if (api->get_calibration == NULL) {
502 return -ENOSYS;
503 }
504
505 return api->get_calibration(dev, calibration);
506}
507
508#endif /* CONFIG_RTC_CALIBRATION */
521struct tm;
522
527static inline struct tm *rtc_time_to_tm(struct rtc_time *timeptr)
528{
529 return (struct tm *)timeptr;
530}
531
540{
541 __ASSERT_NO_MSG(frequency > 0);
542
543 return (int32_t)((1000000000000000000LL / frequency) - 1000000000);
544}
545
554#ifdef __cplusplus
555}
556#endif
557
558#include <zephyr/syscalls/rtc.h>
559
560#endif /* ZEPHYR_INCLUDE_DRIVERS_RTC_H_ */
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:527
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:539
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:412
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:418
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