]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1_FreedomStudio/freedom-metal/metal/rtc.h
Update RISCC-V-RV32-SiFive_HiFive1_FreedomStudio project to latest tools and metal...
[freertos] / FreeRTOS / Demo / RISC-V_RV32_SiFive_HiFive1_FreedomStudio / freedom-metal / metal / rtc.h
1 /* Copyright 2019 SiFive, Inc. */
2 /* SPDX-License-Identifier: Apache-2.0 */
3
4 #ifndef METAL__RTC_H
5 #define METAL__RTC_H
6
7 #include <stdint.h>
8
9 /*!
10  * @file rtc.h
11  * @brief API for Real-Time Clocks
12  */
13
14 struct metal_rtc;
15
16 /*!
17  * @brief List of RTC run behaviors
18  */
19 enum metal_rtc_run_option {
20     METAL_RTC_STOP = 0,
21     METAL_RTC_RUN,
22 };
23
24 struct metal_rtc_vtable {
25     uint64_t (*get_rate)(const struct metal_rtc *const rtc);
26     uint64_t (*set_rate)(const struct metal_rtc *const rtc, const uint64_t rate);
27     uint64_t (*get_compare)(const struct metal_rtc *const rtc);
28     uint64_t (*set_compare)(const struct metal_rtc *const rtc, const uint64_t compare);
29     uint64_t (*get_count)(const struct metal_rtc *const rtc);
30     uint64_t (*set_count)(const struct metal_rtc *const rtc, const uint64_t count);
31     int (*run)(const struct metal_rtc *const rtc, const enum metal_rtc_run_option option);
32     struct metal_interrupt *(*get_interrupt)(const struct metal_rtc *const rtc);
33     int (*get_interrupt_id)(const struct metal_rtc *const rtc);
34 };
35
36 /*!
37  * @brief Handle for a Real-Time Clock
38  */
39 struct metal_rtc {
40     const struct metal_rtc_vtable *vtable;
41 };
42
43 /*!
44  * @brief Get the rate of the RTC
45  * @return The rate in Hz
46  */
47 inline uint64_t metal_rtc_get_rate(const struct metal_rtc *const rtc) {
48     return rtc->vtable->get_rate(rtc);
49 }
50
51 /*!
52  * @brief Set (if possible) the rate of the RTC
53  * @return The new rate of the RTC (not guaranteed to be the same as requested)
54  */
55 inline uint64_t metal_rtc_set_rate(const struct metal_rtc *const rtc, const uint64_t rate) {
56     return rtc->vtable->set_rate(rtc, rate);
57 }
58
59 /*!
60  * @brief Get the compare value of the RTC
61  * @return The compare value
62  */
63 inline uint64_t metal_rtc_get_compare(const struct metal_rtc *const rtc) {
64     return rtc->vtable->get_compare(rtc);
65 }
66
67 /*!
68  * @brief Set the compare value of the RTC
69  * @return The set compare value (not guaranteed to be exactly the requested value)
70  *
71  * The RTC device might impose limits on the maximum compare value or the granularity
72  * of the compare value.
73  */
74 inline uint64_t metal_rtc_set_compare(const struct metal_rtc *const rtc, const uint64_t compare) {
75     return rtc->vtable->set_compare(rtc, compare);
76 }
77
78 /*!
79  * @brief Get the current count of the RTC
80  * @return The count
81  */
82 inline uint64_t metal_rtc_get_count(const struct metal_rtc *const rtc) {
83     return rtc->vtable->get_count(rtc);
84 }
85
86 /*!
87  * @brief Set the current count of the RTC
88  * @return The set value of the count (not guaranteed to be exactly the requested value)
89  *
90  * The RTC device might impose limits on the maximum value of the count
91  */
92 inline uint64_t metal_rtc_set_count(const struct metal_rtc *const rtc, const uint64_t count) {
93     return rtc->vtable->set_count(rtc, count);
94 }
95
96 /*!
97  * @brief Start or stop the RTC
98  * @return 0 if the RTC was successfully started/stopped
99  */
100 inline int metal_rtc_run(const struct metal_rtc *const rtc, const enum metal_rtc_run_option option) {
101     return rtc->vtable->run(rtc, option);
102 }
103
104 /*!
105  * @brief Get the interrupt handle for the RTC compare
106  * @return The interrupt handle
107  */
108 inline struct metal_interrupt *metal_rtc_get_interrupt(const struct metal_rtc *const rtc) {
109     return rtc->vtable->get_interrupt(rtc);
110 }
111
112 /*!
113  * @brief Get the interrupt ID for the RTC compare
114  * @return The interrupt ID
115  */
116 inline int metal_rtc_get_interrupt_id(const struct metal_rtc *const rtc) {
117     return rtc->vtable->get_interrupt_id(rtc);
118 }
119
120 /*!
121  * @brief Get the handle for an RTC by index
122  * @return The RTC handle, or NULL if none is available at that index
123  */
124 struct metal_rtc *metal_rtc_get_device(int index);
125
126 #endif
127