-
Notifications
You must be signed in to change notification settings - Fork 6
/
rtc_software.c
114 lines (103 loc) · 2.26 KB
/
rtc_software.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <FreeRTOS.h>
#include <stdint.h>
#include <rtc.h>
#include <rtc_software.h>
static bool rtc_software_timerCallback(struct timer *timer, void *data) {
struct rtc_software *rtc = data;
rtc->sec++;
return false;
}
RTC_INIT(software, index) {
int32_t ret;
struct rtc_software *rtc = RTC_GET_DEV(index);
if (rtc == NULL) {
goto rtc_software_init_error0;
}
ret = rtc_genericInit((struct rtc *) rtc);
if (ret < 0) {
goto rtc_software_init_error0;
}
if (ret == RTC_ALREDY_INITED) {
goto rtc_software_init_exit;
}
rtc_software_init_exit:
return (struct rtc *) rtc;
rtc_software_init_error0:
return NULL;
}
RTC_DEINIT(software, r) {
return 0;
}
int32_t rtc_software_connect(struct rtc *r, struct timer *timer) {
int32_t ret;
struct rtc_software *rtc = (struct rtc_software *) r;
rtc->timer = timer;
rtc->sec = 0;
ret = timer_setOverflowCallback(rtc->timer, rtc_software_timerCallback, rtc);
if (ret < 0) {
goto rtc_software_connect_error0;
}
/* Start Timer */
ret = timer_periodic(rtc->timer, 1000000UL);
if (ret < 0) {
goto rtc_software_connect_error1;
}
return 0;
rtc_software_connect_error1:
timer_setOverflowCallback(rtc->timer, NULL, NULL);
rtc_software_connect_error0:
return -1;
}
RTC_ADJUST(software, r, offset, waitime) {
/* TODO Support */
return -1;
}
RTC_GET_TIME(software, r, time, waitime) {
int32_t ret;
rtc_lock(r, waitime, -1);
ret = rtc_getTimeISR(r, time);
if (ret < 0) {
rtc_unlock(r, -1);
return -1;
}
rtc_unlock(r, -1);
return 0;
}
RTC_SET_TIME(software, r, time, waitime) {
int ret;
rtc_lock(r, waitime, -1);
ret = rtc_setTimeISR(r, time);
if (ret < 0) {
rtc_unlock(r, -1);
return -1;
}
rtc_unlock(r, -1);
return 0;
}
RTC_ADJUST_ISR(software, r, offset) {
return -1;
}
RTC_GET_TIME_ISR(software, r, time) {
uint64_t ret;
struct rtc_software *rtc = (struct rtc_software *) r;
time->tv_sec = rtc->sec;
ret = timer_getTime(rtc->timer);
time->tv_nsec = ret * 1000;
return 0;
}
RTC_SET_TIME_ISR(software, r, time) {
int32_t ret;
struct rtc_software *rtc = (struct rtc_software *) r;
ret = timer_stop(rtc->timer);
if (ret < 0) {
return -1;
}
rtc->sec = time->tv_sec;
/* TODO: Set nsec not supported */
ret = timer_periodic(rtc->timer, 1000000UL);
if (ret < 0) {
return -1;
}
return 0;
}
RTC_OPS(software);