HPM SDK
HPMicro Software Development Kit
hpm_rtc_drv.h
Go to the documentation of this file.
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 
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 
28 typedef struct {
29  uint16_t index;
30  uint16_t type;
31  time_t period;
33 
37 #define RTC_ALARM_TYPE_ONE_SHOT (0U)
38 #define RTC_ALARM_TYPE_PERIODIC (1U)
39 #define RTC_ALARM_TYPE_ABSOLUTE_TIME_ONE_SHOT (2U)
44 #define ALARM_PERIOD_ONE_SEC (1UL)
45 #define ALARM_PERIOD_ONE_MIN (60UL)
46 #define ALARM_PERIOD_ONE_HOUR (3600U)
47 #define ALARM_PERIOD_ONE_DAY (ALARM_PERIOD_ONE_HOUR * 24UL)
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
59 hpm_stat_t rtc_config_time(RTC_Type *base, time_t time);
60 
68 
74 time_t rtc_get_time(RTC_Type *base);
75 
76 
83 struct timeval rtc_get_timeval(RTC_Type *base);
84 
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 
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 
128 static inline void rtc_clear_alarm_flags(RTC_Type *base, uint32_t masks)
129 {
130  base->ALARM_FLAG = masks;
131 }
132 
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 
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 
168 #endif /* HPM_RTC_DRV_H */
#define RTC_ALARM_EN_ENABLE1_MASK
Definition: hpm_rtc_regs.h:142
#define RTC_ALARM_EN_ENABLE0_MASK
Definition: hpm_rtc_regs.h:154
#define RTC_ALARM_FLAG_ALARM0_MASK
Definition: hpm_rtc_regs.h:129
#define RTC_ALARM_FLAG_ALARM1_MASK
Definition: hpm_rtc_regs.h:119
uint32_t hpm_stat_t
Definition: hpm_common.h:126
#define IS_HPM_BITMASK_SET(val, mask)
Definition: hpm_common.h:61
static void rtc_enable_alarm_interrupt(RTC_Type *base, uint32_t index, bool enable)
Enable RTC alarm interrupt.
Definition: hpm_rtc_drv.h:93
hpm_stat_t rtc_config_alarm(RTC_Type *base, rtc_alarm_config_t *config)
Configure RTC Alarm.
Definition: hpm_rtc_drv.c:39
static uint32_t rtc_get_alarm_flags(RTC_Type *base)
Get the RTC alarm flags.
Definition: hpm_rtc_drv.h:154
hpm_stat_t rtc_config_time(RTC_Type *base, time_t time)
Configure the RTC time.
Definition: hpm_rtc_drv.c:12
static bool rtc_is_alarm_flag_asserted(RTC_Type *base, uint32_t index)
Check whether RTC alarm flag is set or not.
Definition: hpm_rtc_drv.h:139
static void rtc_clear_alarm_flags(RTC_Type *base, uint32_t masks)
Clear RTC alarm flags based on flag masks.
Definition: hpm_rtc_drv.h:128
struct timeval rtc_get_timeval(RTC_Type *base)
Get accurate time return by RTC module.
Definition: hpm_rtc_drv.c:24
static void rtc_clear_alarm_flag(RTC_Type *base, uint32_t index)
Clear RTC alarm flag based on alarm index.
Definition: hpm_rtc_drv.h:113
time_t rtc_get_time(RTC_Type *base)
Get the time returned by RTC module.
Definition: hpm_rtc_drv.c:18
Definition: hpm_rtc_regs.h:12
__RW uint32_t ALARM_FLAG
Definition: hpm_rtc_regs.h:21
__RW uint32_t ALARM_EN
Definition: hpm_rtc_regs.h:22
RTC alarm configuration.
Definition: hpm_rtc_drv.h:28
uint16_t type
Definition: hpm_rtc_drv.h:30
uint16_t index
Definition: hpm_rtc_drv.h:29
time_t period
Definition: hpm_rtc_drv.h:31