HPM SDK
HPMicro Software Development Kit
hpm_rtc_drv.h
Go to the documentation of this file.
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 
18 #include "hpm_common.h"
19 #include "hpm_rtc_regs.h"
20 #include <time.h>
21 
25 typedef struct {
26  uint16_t index;
27  uint16_t type;
28  time_t period;
30 
34 #define RTC_ALARM_TYPE_ONE_SHOT (0U)
35 #define RTC_ALARM_TYPE_PERIODIC (1U)
36 #define RTC_ALARM_TYPE_ABSOLUTE_TIME_ONE_SHOT (2U)
41 #define ALARM_PERIOD_ONE_SEC (1UL)
42 #define ALARM_PERIOD_ONE_MIN (60UL)
43 #define ALARM_PERIOD_ONE_HOUR (3600U)
44 #define ALARM_PERIOD_ONE_DAY (ALARM_PERIOD_ONE_HOUR * 24UL)
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
56 hpm_stat_t rtc_config_time(RTC_Type *base, time_t time);
57 
65 
71 time_t rtc_get_time(RTC_Type *base);
72 
73 
80 struct timeval rtc_get_timeval(RTC_Type *base);
81 
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 
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 
125 static inline void rtc_clear_alarm_flags(RTC_Type *base, uint32_t masks)
126 {
127  base->ALARM_FLAG = masks;
128 }
129 
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 
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 
165 #endif /* HPM_RTC_DRV_H */
uint32_t hpm_stat_t
Definition: hpm_common.h:119
#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:90
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:151
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:136
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:125
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:110
time_t rtc_get_time(RTC_Type *base)
Get the time returned by RTC module.
Definition: hpm_rtc_drv.c:18
#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
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:25
uint16_t type
Definition: hpm_rtc_drv.h:27
uint16_t index
Definition: hpm_rtc_drv.h:26
time_t period
Definition: hpm_rtc_drv.h:28