SemiDrive SSDK Appication Program Interface PTG3.0
sdrv_rtc.h
Go to the documentation of this file.
1
8#ifndef _SDRV_RTC_H_
9#define _SDRV_RTC_H_
10
11#include <stdint.h>
12#include <types.h>
13#include <sdrv_common.h>
14#include <reg.h>
15#include <regs_base.h>
16
17#define SDRV_RTC_REG_PARITY_ERR_ENABLE() \
18 sdrv_rtc_reg_parity_err_int_enable(APB_RTC1_BASE); \
19 sdrv_rtc_reg_parity_err_int_enable(APB_RTC2_BASE);
20
21#define SDRV_RTC_REG_PARITY_ERR_DISABLE() \
22 sdrv_rtc_reg_parity_err_int_disable(APB_RTC1_BASE); \
23 sdrv_rtc_reg_parity_err_int_disable(APB_RTC2_BASE);
24
25typedef int (*rtc_cb_t)(uint32_t irq, void *arg);
26
32struct rtc_time {
33 uint32_t tm_subsec;/* subseconds, unit millisecond, 0~999 */
34 uint32_t tm_sec; /* seconds, 0~61 */
35 uint32_t tm_min; /* minutes, 0~59 */
36 uint32_t tm_hour; /* hour, 0~23 */
37 uint32_t tm_mday; /* day, 1~31 */
38 uint32_t tm_mon; /* month, 0~11 */
39 uint32_t tm_year; /* years since 1900 */
40 uint32_t tm_wday; /* day since Sunday, 0~6 */
41 uint32_t tm_yday; /* days since Jan. 1, 0~365 */
42 uint32_t tm_isdst; /* daylight saving time flag */
43};
44
46 SDRV_RTC_VIO_NONE = 0, /* rtc no violation */
47 SDRV_RTC_VIO_OVERFLOW = 1, /* rtc violation disable */
48 SDRV_RTC_VIO_DISABLE = 2, /* rtc violation overflow */
49 SDRV_RTC_VIO_ALL = 3, /* rtc violation overflow & disable */
51
55typedef struct sdrv_rtc {
56 paddr_t base;
57 uint32_t wakeup_intr;
58 uint32_t periodic_intr;
59 uint32_t violation_intr;
63
64struct rtc_wkalrm {
65 unsigned char enable;
66 unsigned char pending;
67 struct rtc_time tm;
68};
69
70/* Define RTC WakeUp Enable Type */
72 SDRV_WKUP_ENABLE = 0, /* Only enable wakeup */
73 SDRV_WKUP_ENABLE_IRQ = 1, /* Enable wakeup and IRQ */
74 SDRV_WKUP_ENABLE_REQ = 2, /* Enable wakeup and pmu requst */
75 SDRV_WKUP_ENABLE_ALL = 3, /* Enable wakeup, IRQ and pmu requst */
77
79 SDRV_RTC_GP0, /* RTC general purpose register 0 */
80 SDRV_RTC_GP1, /* RTC general purpose register 1 */
81 SDRV_RTC_GP2, /* RTC general purpose register 2 */
82 SDRV_RTC_GP3, /* RTC general purpose register 3 */
84
91};
92
98static inline void sdrv_rtc_reg_parity_err_int_enable(uint32_t base)
99{
100 RMWREG32(base + 0x68U, 0, 1, 1);
101}
102
108static inline void sdrv_rtc_reg_parity_err_int_disable(uint32_t base)
109{
110 RMWREG32(base + 0x68U, 0, 1, 0);
111}
112
119static inline bool is_leap_year(uint32_t year)
120{
121 return (!(year % 4) && (year % 100)) || !(year % 400);
122}
123
132uint64_t rtc_to_epoch(struct rtc_time *tm);
133
143status_t epoch_to_rtc(struct rtc_time *tm, uint64_t epoch_sec);
144
153uint64_t rtc_to_epoch_ms(struct rtc_time *tm);
154
164status_t epoch_ms_to_rtc(struct rtc_time *tm, uint64_t epoch_millisec);
165
174
184 sdrv_rtc_wakeup_enable_type_e type, bool enable);
185
193
201
209
219
229 sdrv_rtc_violation_e vio, bool en);
230
241
252
263
274
283
290status_t sdrv_rtc_enable_it(uint32_t source, void *arg, rtc_cb_t cb);
291
298
309
319
329#endif /*_SDRV_RTC_H_*/
SemiDrive driver common header file.
@ SDRV_STATUS_GROUP_RTC
Definition: sdrv_common.h:53
int32_t status_t
Type used for all status and error return values.
Definition: sdrv_common.h:82
#define SDRV_ERROR_STATUS(group, code)
Construct a status code value from a group and code number. All the error statuses are negetive numbe...
Definition: sdrv_common.h:17
sdrv_general_purpose
Definition: sdrv_rtc.h:78
@ SDRV_RTC_GP0
Definition: sdrv_rtc.h:79
@ SDRV_RTC_GP3
Definition: sdrv_rtc.h:82
@ SDRV_RTC_GP2
Definition: sdrv_rtc.h:81
@ SDRV_RTC_GP1
Definition: sdrv_rtc.h:80
status_t sdrv_rtc_write_general_purpose_reg(sdrv_rtc_t *dev, sdrv_general_purpose_e gp_num, uint32_t value)
Write rtc general purpose register.
sdrv_rtc_violation_enum
Definition: sdrv_rtc.h:45
@ SDRV_RTC_VIO_NONE
Definition: sdrv_rtc.h:46
@ SDRV_RTC_VIO_ALL
Definition: sdrv_rtc.h:49
@ SDRV_RTC_VIO_OVERFLOW
Definition: sdrv_rtc.h:47
@ SDRV_RTC_VIO_DISABLE
Definition: sdrv_rtc.h:48
uint32_t sdrv_rtc_read_general_purpose_reg(sdrv_rtc_t *dev, sdrv_general_purpose_e gp_num)
Read rtc general purpose register.
status_t epoch_ms_to_rtc(struct rtc_time *tm, uint64_t epoch_millisec)
Simple algorithm to convert epoch milliseconds to RTC time.
enum sdrv_rtc_violation_enum sdrv_rtc_violation_e
uint64_t rtc_to_epoch_ms(struct rtc_time *tm)
Simple algorithm to convert RTC time to Epoch milliseconds.
static void sdrv_rtc_reg_parity_err_int_enable(uint32_t base)
Enable RTC Register parity error interrupt status.
Definition: sdrv_rtc.h:98
status_t sdrv_rtc_wakeup_enable(sdrv_rtc_t *dev, sdrv_rtc_wakeup_enable_type_e type, bool enable)
sdrv enable rtc wake up.
status_t sdrv_rtc_enable_it(uint32_t source, void *arg, rtc_cb_t cb)
Enable rtc interrupt.
sdrv_rtc_error
RTC status error code.
Definition: sdrv_rtc.h:88
@ SDRV_RTC_STATUS_INVALID_RTC_TIME
Definition: sdrv_rtc.h:89
@ SDRV_RTC_STATUS_LOCK
Definition: sdrv_rtc.h:90
struct sdrv_rtc sdrv_rtc_t
RTC device structure.
uint64_t rtc_to_epoch(struct rtc_time *tm)
Simple algorithm to convert RTC time to Epoch seconds.
status_t sdrv_rtc_enable(sdrv_rtc_t *dev, bool enable)
sdrv rtc enable.
status_t epoch_to_rtc(struct rtc_time *tm, uint64_t epoch_sec)
Simple algorithm to convert epoch seconds to RTC time.
int(* rtc_cb_t)(uint32_t irq, void *arg)
Definition: sdrv_rtc.h:25
status_t sdrv_rtc_lock(sdrv_rtc_t *dev)
rtc lock.
status_t sdrv_rtc_clear_violation_status(sdrv_rtc_t *dev, sdrv_rtc_violation_e vio)
sdrv rtc clear violation status.
static bool is_leap_year(uint32_t year)
check is leap yaer
Definition: sdrv_rtc.h:119
status_t sdrv_rtc_get_time(sdrv_rtc_t *dev, struct rtc_time *tm)
rtc get time
status_t sdrv_rtc_clear_wakeup_status(sdrv_rtc_t *dev)
sdrv clear rtc wake up status.
status_t sdrv_rtc_enable_alarm(sdrv_rtc_t *dev, bool enable)
Enable rtc alarm.
status_t sdrv_rtc_disable_it(uint32_t source)
disable rtc interrput.
status_t sdrv_rtc_set_alarm(sdrv_rtc_t *dev, struct rtc_wkalrm *alrm)
rtc set alarm
bool sdrv_rtc_get_wakeup_status(sdrv_rtc_t *dev)
sdrv get rtc wake up status.
status_t sdrv_rtc_get_alarm(sdrv_rtc_t *dev, struct rtc_wkalrm *alrm)
rtc get alarm
sdrv_rtc_wakeup_enable_type
Definition: sdrv_rtc.h:71
@ SDRV_WKUP_ENABLE_ALL
Definition: sdrv_rtc.h:75
@ SDRV_WKUP_ENABLE_REQ
Definition: sdrv_rtc.h:74
@ SDRV_WKUP_ENABLE_IRQ
Definition: sdrv_rtc.h:73
@ SDRV_WKUP_ENABLE
Definition: sdrv_rtc.h:72
enum sdrv_rtc_wakeup_enable_type sdrv_rtc_wakeup_enable_type_e
static void sdrv_rtc_reg_parity_err_int_disable(uint32_t base)
Disable RTC Register parity error interrupt status.
Definition: sdrv_rtc.h:108
status_t sdrv_rtc_set_time(sdrv_rtc_t *dev, struct rtc_time *tm)
rtc set time
status_t sdrv_rtc_violation_intr_enable(sdrv_rtc_t *dev, sdrv_rtc_violation_e vio, bool en)
sdrv rtc violation enable.
enum sdrv_general_purpose sdrv_general_purpose_e
sdrv_rtc_violation_e sdrv_rtc_get_violation_status(sdrv_rtc_t *dev)
sdrv get rtc violation status.
RTC time structure.
Definition: sdrv_rtc.h:32
uint32_t tm_mon
Definition: sdrv_rtc.h:38
uint32_t tm_min
Definition: sdrv_rtc.h:35
uint32_t tm_year
Definition: sdrv_rtc.h:39
uint32_t tm_subsec
Definition: sdrv_rtc.h:33
uint32_t tm_wday
Definition: sdrv_rtc.h:40
uint32_t tm_mday
Definition: sdrv_rtc.h:37
uint32_t tm_yday
Definition: sdrv_rtc.h:41
uint32_t tm_isdst
Definition: sdrv_rtc.h:42
uint32_t tm_hour
Definition: sdrv_rtc.h:36
uint32_t tm_sec
Definition: sdrv_rtc.h:34
Definition: sdrv_rtc.h:64
struct rtc_time tm
Definition: sdrv_rtc.h:67
unsigned char enable
Definition: sdrv_rtc.h:65
unsigned char pending
Definition: sdrv_rtc.h:66
RTC device structure.
Definition: sdrv_rtc.h:55
rtc_cb_t cb
Definition: sdrv_rtc.h:60
uint32_t wakeup_intr
Definition: sdrv_rtc.h:57
uint32_t violation_intr
Definition: sdrv_rtc.h:59
uint32_t periodic_intr
Definition: sdrv_rtc.h:58
paddr_t base
Definition: sdrv_rtc.h:56
sdrv_rtc_violation_e vio_mask
Definition: sdrv_rtc.h:61