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