13#ifndef ZEPHYR_INCLUDE_DRIVERS_RTC_H_
14#define ZEPHYR_INCLUDE_DRIVERS_RTC_H_
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)
103typedef int (*rtc_api_set_time)(
const struct device *dev,
const struct rtc_time *timeptr);
109typedef int (*rtc_api_get_time)(
const struct device *dev,
struct rtc_time *timeptr);
115typedef int (*rtc_api_alarm_get_supported_fields)(
const struct device *dev,
uint16_t id,
136typedef int (*rtc_api_alarm_is_pending)(
const struct device *dev,
uint16_t id);
142typedef int (*rtc_api_alarm_set_callback)(
const struct device *dev,
uint16_t id,
149typedef int (*rtc_api_update_set_callback)(
const struct device *dev,
156typedef int (*rtc_api_set_calibration)(
const struct device *dev,
int32_t calibration);
162typedef int (*rtc_api_get_calibration)(
const struct device *dev,
int32_t *calibration);
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;
177#if defined(CONFIG_RTC_UPDATE) || defined(__DOXYGEN__)
178 rtc_api_update_set_callback update_set_callback;
180#if defined(CONFIG_RTC_CALIBRATION) || defined(__DOXYGEN__)
181 rtc_api_set_calibration set_calibration;
182 rtc_api_get_calibration get_calibration;
200static inline int z_impl_rtc_set_time(
const struct device *dev,
const struct rtc_time *timeptr)
202 const struct rtc_driver_api *api = (
const struct rtc_driver_api *)dev->
api;
204 return api->set_time(dev, timeptr);
219static inline int z_impl_rtc_get_time(
const struct device *dev,
struct rtc_time *timeptr)
221 const struct rtc_driver_api *api = (
const struct rtc_driver_api *)dev->
api;
223 return api->get_time(dev, timeptr);
230#if defined(CONFIG_RTC_ALARM) || defined(__DOXYGEN__)
249static inline int z_impl_rtc_alarm_get_supported_fields(
const struct device *dev,
uint16_t id,
252 const struct rtc_driver_api *api = (
const struct rtc_driver_api *)dev->
api;
254 if (api->alarm_get_supported_fields == NULL) {
258 return api->alarm_get_supported_fields(dev,
id, mask);
290 const struct rtc_driver_api *api = (
const struct rtc_driver_api *)dev->
api;
292 if (api->alarm_set_time == NULL) {
296 return api->alarm_set_time(dev,
id, mask, timeptr);
320 const struct rtc_driver_api *api = (
const struct rtc_driver_api *)dev->
api;
322 if (api->alarm_get_time == NULL) {
326 return api->alarm_get_time(dev,
id, mask, timeptr);
346static inline int z_impl_rtc_alarm_is_pending(
const struct device *dev,
uint16_t id)
348 const struct rtc_driver_api *api = (
const struct rtc_driver_api *)dev->
api;
350 if (api->alarm_is_pending == NULL) {
354 return api->alarm_is_pending(dev,
id);
386static inline int z_impl_rtc_alarm_set_callback(
const struct device *dev,
uint16_t id,
389 const struct rtc_driver_api *api = (
const struct rtc_driver_api *)dev->
api;
391 if (api->alarm_set_callback == NULL) {
395 return api->alarm_set_callback(dev,
id, callback, user_data);
407#if defined(CONFIG_RTC_UPDATE) || defined(__DOXYGEN__)
431static inline int z_impl_rtc_update_set_callback(
const struct device *dev,
434 const struct rtc_driver_api *api = (
const struct rtc_driver_api *)dev->
api;
436 if (api->update_set_callback == NULL) {
440 return api->update_set_callback(dev, callback, user_data);
452#if defined(CONFIG_RTC_CALIBRATION) || defined(__DOXYGEN__)
474static inline int z_impl_rtc_set_calibration(
const struct device *dev,
int32_t calibration)
476 const struct rtc_driver_api *api = (
const struct rtc_driver_api *)dev->
api;
478 if (api->set_calibration == NULL) {
482 return api->set_calibration(dev, calibration);
497static inline int z_impl_rtc_get_calibration(
const struct device *dev,
int32_t *calibration)
499 const struct rtc_driver_api *api = (
const struct rtc_driver_api *)dev->
api;
501 if (api->get_calibration == NULL) {
505 return api->get_calibration(dev, calibration);
529 return (
struct tm *)timeptr;
541 __ASSERT_NO_MSG(frequency > 0);
543 return (
int32_t)((1000000000000000000LL / frequency) - 1000000000);
558#include <zephyr/syscalls/rtc.h>
void(* rtc_alarm_callback)(const struct device *dev, uint16_t id, void *user_data)
RTC alarm triggered callback.
Definition: rtc.h:91
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_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
__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:403
const void * api
Address of the API structure exposed by the device instance.
Definition: device.h:409
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