Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Intel Corporation. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef _RTC_H_ |
| 8 | #define _RTC_H_ |
Kumar Gala | 7890816 | 2017-04-19 10:32:08 -0500 | [diff] [blame] | 9 | #include <zephyr/types.h> |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 10 | #include <device.h> |
| 11 | #include <misc/util.h> |
| 12 | |
Peter Mitsis | a0e4568 | 2016-01-22 12:38:49 -0500 | [diff] [blame] | 13 | #ifdef __cplusplus |
| 14 | extern "C" { |
| 15 | #endif |
| 16 | |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 17 | enum clk_rtc_div { |
Vinicius Costa Gomes | 45dcfe1 | 2015-12-18 18:47:17 -0200 | [diff] [blame] | 18 | RTC_CLK_DIV_1, |
| 19 | RTC_CLK_DIV_2, |
| 20 | RTC_CLK_DIV_4, |
| 21 | RTC_CLK_DIV_8, |
| 22 | RTC_CLK_DIV_16, |
| 23 | RTC_CLK_DIV_32, |
| 24 | RTC_CLK_DIV_64, |
| 25 | RTC_CLK_DIV_128, |
| 26 | RTC_CLK_DIV_256, |
| 27 | RTC_CLK_DIV_512, |
| 28 | RTC_CLK_DIV_1024, |
| 29 | RTC_CLK_DIV_2048, |
| 30 | RTC_CLK_DIV_4096, |
| 31 | RTC_CLK_DIV_8192, |
| 32 | RTC_CLK_DIV_16384, |
| 33 | RTC_CLK_DIV_32768 |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 34 | }; |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 35 | |
Vinicius Costa Gomes | 45dcfe1 | 2015-12-18 18:47:17 -0200 | [diff] [blame] | 36 | #define RTC_DIVIDER RTC_CLK_DIV_1 |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 37 | |
| 38 | /** Number of RTC ticks in a second */ |
Vinicius Costa Gomes | 45dcfe1 | 2015-12-18 18:47:17 -0200 | [diff] [blame] | 39 | #define RTC_ALARM_SECOND (32768 / (1UL << RTC_DIVIDER)) |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 40 | |
| 41 | /** Number of RTC ticks in a minute */ |
| 42 | #define RTC_ALARM_MINUTE (RTC_ALARM_SECOND * 60) |
| 43 | |
| 44 | /** Number of RTC ticks in an hour */ |
| 45 | #define RTC_ALARM_HOUR (RTC_ALARM_MINUTE * 60) |
| 46 | |
| 47 | /** Number of RTC ticks in a day */ |
| 48 | #define RTC_ALARM_DAY (RTC_ALARM_HOUR * 24) |
| 49 | |
| 50 | |
| 51 | |
| 52 | struct rtc_config { |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 53 | u32_t init_val; |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 54 | /*!< enable/disable alarm */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 55 | u8_t alarm_enable; |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 56 | /*!< initial configuration value for the 32bit RTC alarm value */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 57 | u32_t alarm_val; |
Tomasz Bursztyka | 466cb00 | 2015-12-02 19:06:24 +0100 | [diff] [blame] | 58 | /*!< Pointer to function to call when alarm value |
| 59 | * matches current RTC value */ |
| 60 | void (*cb_fn)(struct device *dev); |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 61 | }; |
| 62 | |
Tomasz Bursztyka | 466cb00 | 2015-12-02 19:06:24 +0100 | [diff] [blame] | 63 | typedef void (*rtc_api_enable)(struct device *dev); |
| 64 | typedef void (*rtc_api_disable)(struct device *dev); |
| 65 | typedef int (*rtc_api_set_config)(struct device *dev, |
| 66 | struct rtc_config *config); |
| 67 | typedef int (*rtc_api_set_alarm)(struct device *dev, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 68 | const u32_t alarm_val); |
| 69 | typedef u32_t (*rtc_api_read)(struct device *dev); |
| 70 | typedef u32_t (*rtc_api_get_pending_int)(struct device *dev); |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 71 | |
| 72 | struct rtc_driver_api { |
| 73 | rtc_api_enable enable; |
| 74 | rtc_api_disable disable; |
| 75 | rtc_api_read read; |
| 76 | rtc_api_set_config set_config; |
| 77 | rtc_api_set_alarm set_alarm; |
Julien Delayen | ee04a3f | 2016-11-03 13:45:31 +0000 | [diff] [blame] | 78 | rtc_api_get_pending_int get_pending_int; |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 79 | }; |
| 80 | |
Andrew Boie | 9ca42fb | 2017-10-25 13:26:14 -0700 | [diff] [blame] | 81 | __syscall u32_t rtc_read(struct device *dev); |
| 82 | |
| 83 | static inline u32_t _impl_rtc_read(struct device *dev) |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 84 | { |
Marcus Shawcroft | 39d34d7 | 2016-10-22 10:01:38 +0100 | [diff] [blame] | 85 | const struct rtc_driver_api *api = dev->driver_api; |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 86 | |
Tomasz Bursztyka | 466cb00 | 2015-12-02 19:06:24 +0100 | [diff] [blame] | 87 | return api->read(dev); |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Andrew Boie | 9ca42fb | 2017-10-25 13:26:14 -0700 | [diff] [blame] | 90 | __syscall void rtc_enable(struct device *dev); |
| 91 | |
| 92 | static inline void _impl_rtc_enable(struct device *dev) |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 93 | { |
Marcus Shawcroft | 39d34d7 | 2016-10-22 10:01:38 +0100 | [diff] [blame] | 94 | const struct rtc_driver_api *api = dev->driver_api; |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 95 | |
Tomasz Bursztyka | 466cb00 | 2015-12-02 19:06:24 +0100 | [diff] [blame] | 96 | api->enable(dev); |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 97 | } |
| 98 | |
Andrew Boie | 9ca42fb | 2017-10-25 13:26:14 -0700 | [diff] [blame] | 99 | __syscall void rtc_disable(struct device *dev); |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 100 | |
Andrew Boie | 9ca42fb | 2017-10-25 13:26:14 -0700 | [diff] [blame] | 101 | static inline void _impl_rtc_disable(struct device *dev) |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 102 | { |
Marcus Shawcroft | 39d34d7 | 2016-10-22 10:01:38 +0100 | [diff] [blame] | 103 | const struct rtc_driver_api *api = dev->driver_api; |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 104 | |
Tomasz Bursztyka | 466cb00 | 2015-12-02 19:06:24 +0100 | [diff] [blame] | 105 | api->disable(dev); |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Tomasz Bursztyka | 466cb00 | 2015-12-02 19:06:24 +0100 | [diff] [blame] | 108 | static inline int rtc_set_config(struct device *dev, |
| 109 | struct rtc_config *cfg) |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 110 | { |
Marcus Shawcroft | 39d34d7 | 2016-10-22 10:01:38 +0100 | [diff] [blame] | 111 | const struct rtc_driver_api *api = dev->driver_api; |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 112 | |
Tomasz Bursztyka | 466cb00 | 2015-12-02 19:06:24 +0100 | [diff] [blame] | 113 | return api->set_config(dev, cfg); |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 114 | } |
| 115 | |
Andrew Boie | 9ca42fb | 2017-10-25 13:26:14 -0700 | [diff] [blame] | 116 | __syscall int rtc_set_alarm(struct device *dev, const u32_t alarm_val); |
| 117 | |
| 118 | static inline int _impl_rtc_set_alarm(struct device *dev, |
| 119 | const u32_t alarm_val) |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 120 | { |
Marcus Shawcroft | 39d34d7 | 2016-10-22 10:01:38 +0100 | [diff] [blame] | 121 | const struct rtc_driver_api *api = dev->driver_api; |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 122 | |
Tomasz Bursztyka | 466cb00 | 2015-12-02 19:06:24 +0100 | [diff] [blame] | 123 | return api->set_alarm(dev, alarm_val); |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Julien Delayen | ee04a3f | 2016-11-03 13:45:31 +0000 | [diff] [blame] | 126 | /** |
| 127 | * @brief Function to get pending interrupts |
| 128 | * |
| 129 | * The purpose of this function is to return the interrupt |
| 130 | * status register for the device. |
| 131 | * This is especially useful when waking up from |
| 132 | * low power states to check the wake up source. |
| 133 | * |
| 134 | * @param dev Pointer to the device structure for the driver instance. |
| 135 | * |
| 136 | * @retval 1 if the rtc interrupt is pending. |
| 137 | * @retval 0 if no rtc interrupt is pending. |
| 138 | */ |
Andrew Boie | 9ca42fb | 2017-10-25 13:26:14 -0700 | [diff] [blame] | 139 | __syscall int rtc_get_pending_int(struct device *dev); |
| 140 | |
| 141 | static inline int _impl_rtc_get_pending_int(struct device *dev) |
Julien Delayen | ee04a3f | 2016-11-03 13:45:31 +0000 | [diff] [blame] | 142 | { |
| 143 | struct rtc_driver_api *api; |
| 144 | |
| 145 | api = (struct rtc_driver_api *)dev->driver_api; |
| 146 | return api->get_pending_int(dev); |
| 147 | } |
| 148 | |
Peter Mitsis | a0e4568 | 2016-01-22 12:38:49 -0500 | [diff] [blame] | 149 | #ifdef __cplusplus |
| 150 | } |
| 151 | #endif |
| 152 | |
Andrew Boie | 9ca42fb | 2017-10-25 13:26:14 -0700 | [diff] [blame] | 153 | #include <syscalls/rtc.h> |
| 154 | |
Anas Nashif | 9d6deb4 | 2015-11-21 20:58:15 -0500 | [diff] [blame] | 155 | #endif |