1 /*
2 * Copyright (c) 2021-2024 HPMicro
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7 #ifndef HPM_RTC_DRV_H
8 #define HPM_RTC_DRV_H
9
10 /**
11 * @brief RTC driver APIs
12 * @defgroup rtc_interface RTC driver APIs
13 * @ingroup io_interfaces
14 * @{
15 *
16 */
17
18 #include "hpm_common.h"
19 #include "hpm_rtc_regs.h"
20 #ifndef __ICCRISCV__
21 #include <sys/time.h>
22 #endif
23 #include <time.h>
24
25 /**
26 * @brief RTC alarm configuration
27 */
28 typedef struct {
29 uint16_t index; /**< RTC alarm index */
30 uint16_t type; /**< Alarm type */
31 time_t period; /**< Alarm period */
32 } rtc_alarm_config_t;
33
34 /**
35 * @brief RTC Alarm type
36 */
37 #define RTC_ALARM_TYPE_ONE_SHOT (0U) /**< The RTC alarm will be triggered only once */
38 #define RTC_ALARM_TYPE_PERIODIC (1U) /**< The RTC alarm will be triggered periodically */
39 #define RTC_ALARM_TYPE_ABSOLUTE_TIME_ONE_SHOT (2U) /**< The RTC alarm will be triggered via the absolute time provided via period */
40
41 /**
42 * @brief Typical RTC alarm period definitions
43 */
44 #define ALARM_PERIOD_ONE_SEC (1UL) /**< Alarm period: 1 second */
45 #define ALARM_PERIOD_ONE_MIN (60UL) /**< Alarm period: 1 minute */
46 #define ALARM_PERIOD_ONE_HOUR (3600U) /**< Alarm period: 1 hour */
47 #define ALARM_PERIOD_ONE_DAY (ALARM_PERIOD_ONE_HOUR * 24UL) /**< Alarm period: 1 day */
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 /**
54 * @brief Configure the RTC time
55 * @param [in] base RTC base address
56 * @param [in] time seconds since 1970.1.1, 0:0:0
57 * @retval API execution status status_success or status_invalid_argument
58 */
59 hpm_stat_t rtc_config_time(RTC_Type *base, time_t time);
60
61 /**
62 * @brief Configure RTC Alarm
63 * @param [in] base RTC base address
64 * @param [in] config RTC alarm configuration pointer
65 * @retval API execution status status_success or status_invalid_argument;
66 */
67 hpm_stat_t rtc_config_alarm(RTC_Type *base, rtc_alarm_config_t *config);
68
69 /**
70 * @brief Get the time returned by RTC module
71 * @param [in] base RTC base address
72 * @retval RTC time
73 */
74 time_t rtc_get_time(RTC_Type *base);
75
76
77 /**
78 * @brief Get accurate time return by RTC module
79 * @param [in] base RTC base address
80 *
81 * @return accurate time(including second and subsecond)
82 */
83 struct timeval rtc_get_timeval(RTC_Type *base);
84
85 /**
86 * @brief Enable RTC alarm interrupt
87 * @param [in] base RTC base address
88 * @param [in] index RTC alarm index, valid value is 0 or 1
89 * @param [in] enable RTC alarm enable flag
90 * @arg true Enable specified RTC alarm
91 * @arg false Disable specified RTC alarm
92 */
rtc_enable_alarm_interrupt(RTC_Type * base,uint32_t index,bool enable)93 static inline void rtc_enable_alarm_interrupt(RTC_Type *base, uint32_t index, bool enable)
94 {
95 if (index > 1) {
96 return;
97 }
98
99 uint32_t mask = (index == 0U) ? RTC_ALARM_EN_ENABLE0_MASK : RTC_ALARM_EN_ENABLE1_MASK;
100
101 if (enable) {
102 base->ALARM_EN |= mask;
103 } else {
104 base->ALARM_EN &= ~mask;
105 }
106 }
107
108 /**
109 * @brief Clear RTC alarm flag based on alarm index
110 * @param [in] base RTC base address
111 * @param [in] index RTC alarm index, valid value is 0 or 1
112 */
rtc_clear_alarm_flag(RTC_Type * base,uint32_t index)113 static inline void rtc_clear_alarm_flag(RTC_Type *base, uint32_t index)
114 {
115 if (index > 1) {
116 return;
117 }
118 uint32_t mask = (index == 0U) ? RTC_ALARM_FLAG_ALARM0_MASK : RTC_ALARM_FLAG_ALARM1_MASK;
119
120 base->ALARM_FLAG = mask;
121 }
122
123 /**
124 * @brief Clear RTC alarm flags based on flag masks
125 * @param [in] base RTC base address
126 * @param [in] masks RTC alarm masks
127 */
rtc_clear_alarm_flags(RTC_Type * base,uint32_t masks)128 static inline void rtc_clear_alarm_flags(RTC_Type *base, uint32_t masks)
129 {
130 base->ALARM_FLAG = masks;
131 }
132
133 /**
134 * @brief Check whether RTC alarm flag is set or not
135 * @param [in] base RTC base address
136 * @param [in] index RTC alarm index, valid value is 0 or 1
137 * @retval RTC alarm flag. Valid value is true or false
138 */
rtc_is_alarm_flag_asserted(RTC_Type * base,uint32_t index)139 static inline bool rtc_is_alarm_flag_asserted(RTC_Type *base, uint32_t index)
140 {
141 if (index > 1) {
142 return false;
143 }
144 uint32_t mask = (index == 0U) ? RTC_ALARM_FLAG_ALARM0_MASK : RTC_ALARM_FLAG_ALARM1_MASK;
145
146 return IS_HPM_BITMASK_SET(base->ALARM_FLAG, mask);
147 }
148
149 /**
150 * @brief Get the RTC alarm flags
151 * @param [in] base RTC base address
152 * @return RTC alarm flags
153 */
rtc_get_alarm_flags(RTC_Type * base)154 static inline uint32_t rtc_get_alarm_flags(RTC_Type *base)
155 {
156 return base->ALARM_FLAG;
157 }
158
159 #ifdef __cplusplus
160 }
161 #endif
162
163 /**
164 * @}
165 *
166 */
167
168 #endif /* HPM_RTC_DRV_H */
169