-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.c
309 lines (257 loc) · 5.55 KB
/
clock.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <stdbool.h>
extern void assert(bool);
#include "conf.h"
#include "rtc.h"
#include "rcc.h"
#include "pwr.h"
#include "exti.h"
#include "clock.h"
#include "uart.h"
void (*alarm_callb)() = NULL;
#if 1
void i_rtc_alra()
{
#ifdef CONF_L4
exti->pr1.pif18 = 1; /* writing 1 clears bit */
#else
exti->pr.pif17 = 1; /* writing 1 clears bit */
#endif
rtc->isr.alraf = 0;
if (alarm_callb == NULL)
return;
alarm_callb();
}
void clock_alarm(struct rtc_alrmar alrm, struct rtc_alrmassr ss, void (*cb)())
{
rtc->wpr.key = 0xca; /* unlock write protection */
rtc->wpr.key = 0x53;
rtc->cr.alrae = 0;
while (!rtc->isr.alrawf);
ss.maskss = 0xf;
rtc->alrmar = alrm;
rtc->alrmassr = ss;
rtc->cr.osel = RTC_OSEL_ALARM_A;
rtc->cr.alraie = 1;
rtc->cr.alrae = 1;
rtc->wpr.key = 0x11; /* relock write protection */
alarm_callb = cb;
}
void clock_alarm_stop(void (*cb)())
{
assert(alarm_callb != NULL);
assert(alarm_callb == cb);
alarm_callb = NULL;
rtc->wpr.key = 0xca; /* unlock write protection */
rtc->wpr.key = 0x53;
rtc->cr.osel = RTC_OSEL_DISABLED;
rtc->cr.alrae = 0;
rtc->cr.alraie = 0;
rtc->wpr.key = 0x11; /* relock write protection */
}
#endif
void clock_set(struct rtc_dr date, struct rtc_tr time)
{
rtc->wpr.key = 0xca; /* unlock write protection */
rtc->wpr.key = 0x53;
rtc->isr.init = 1;
while (!rtc->isr.initf); /* wait for init mode */
rtc->tr = time;
rtc->dr = date;
rtc->isr.init = 0;
rtc->wpr.key = 0x11; /* relock write protection */
while (!rtc->isr.rsf);
}
void clock_init_wuk()
{
rtc->wpr.key = 0xca; /* unlock write protection */
rtc->wpr.key = 0x53;
#ifdef CONF_L0
rtc->cr.wute = 0;
while (!rtc->isr.wutwf);
rtc->cr.wucksel = 0;
rtc->wutr.wut = 2048;
rtc->isr.wutf = 0;
exti->rtsr.rt20 = 1;
exti->imr.im20 = 1;
//exti->pr.pif20 = 1;
//exti->emr.em20 = 1;
//exti->rtsr.rt20 = 1;
nvic_iser[0] |= 1 << 2;
rtc->cr.wutie = 1;
rtc->cr.wute = 1;
#endif
rtc->wpr.key = 0xff;
}
void clock_init_lse()
{
#if defined(CONF_F0) || defined(CONF_L4)
rcc->bdcr.rtcsel = RCC_RTCSEL_LSE;
rcc->bdcr.lseon = 1;
rcc->bdcr.lsebyp = 0;
while (!rcc->bdcr.lserdy); /* wait for lse to get ready */
#endif
#if defined(CONF_L0)
rcc->csr.rtcsel = RCC_RTCSEL_LSE;
rcc->csr.lseon = 1;
rcc->csr.lsebyp = 0;
while (!rcc->csr.lserdy); /* wait for lse to get ready */
#endif
}
void setup_clock(void)
{
#ifdef CONF_L4
rcc->apb1enr1.pwren = 1;
while (!rcc->apb1enr1.pwren);
pwr->cr1.dbp = 1;
#elif defined(CONF_L0) || defined(CONF_F0)
rcc->apb1enr.pwren = 1;
while (!rcc->apb1enr.pwren);
pwr->cr.dbp = 1;
#endif
#ifdef LSI
_Static_assert(1==2, "Not supported?");
rcc->bdcr.rtcsel = RCC_RTCSEL_LSI;
rcc->csr.lsion = 1;
while (!rcc->csr.lsirdy); /* wait for lsi to get ready */
#else
clock_init_lse();
#endif
clock_init_wuk();
#ifndef CONF_L0
rcc->bdcr.rtcen = 1;
#else
rcc->csr.rtcen = 1;
#endif
/*
* Enable RTC interrupts
* On F0, L0:
* exti 17: alr
* exti 20: wut
*
* On L4:
* exti 18: alr
* exti 20: wut
*/
#ifdef CONF_L4
exti->imr1.im20 = exti->imr1.im18 = 1;
exti->rtsr1.rt20 = exti->rtsr1.rt18 = 1;
exti->pr1.pif20 = exti->pr1.pif18 = 1;
nvic_iser[0] |= 1 << 3; // wut
nvic_iser[1] |= 1 << 9; // alr
#else
exti->imr.im20 = exti->imr.im17 = 1;
exti->rtsr.rt20 = exti->rtsr.rt17 = 1;
exti->pr.pif20 = exti->pr.pif17 = 1;
nvic_iser[0] |= 1 << 2; // glob
#endif
if (rtc->isr.inits) /* already setup */
return;
/* ... RTC hasn't been run before */
rtc->wpr.key = 0xca; /* unlock write protection */
rtc->wpr.key = 0x53;
rtc->isr.init = 1;
while (!rtc->isr.initf); /* wait for init mode */
#ifdef LSI
/* prediv lsi (~40kHz) to gen 1Hz */
rtc->prer.prediv_s = 320 - 1;
rtc->prer.prediv_a = 125 - 1;
#else
/* prediv lse (32kHz) to gen 1Hz */
rtc->prer.prediv_a = 128 - 1;
rtc->prer.prediv_s = 256 - 1;
#endif
/* load initial date/time */
struct rtc_tr a = {
.st = 3, .su = 1,
.mnt = 3, .mnu = 7,
.ht = 0, .hu = 2,
.pm = RTC_PM_24H,
};
rtc->tr = a;
struct rtc_dr b = {
.dt = 3, .du = 1,
.mt = 0, .mu = 5,
.wdu = 7,
.yt = 1, .yu = 5,
};
rtc->dr = b;
rtc->cr.fmt = RTC_FMT_24H;
rtc->isr.init = 0;
while (!rtc->isr.rsf);
rtc->wpr.key = 0xff; /* random key write enables protection again */
}
void clock_get(struct rtc_dr *date, struct rtc_tr *time,
struct rtc_ssr *subsec)
{
assert(rtc->isr.rsf);
if (time != NULL)
*time = rtc->tr;
if (date != NULL)
*date = rtc->dr;
if (subsec != NULL)
*subsec = rtc->ssr;
}
unsigned clock_seconds = 0;
void clock_cmd(char *cmd, int len)
{
assert(*cmd == 'c');
if (len < 2) {
uart_puts("Say cg(et) or cs(et) instead.\r\n");
return;
}
if (cmd[1] == 'g') {
struct rtc_tr time;
clock_get(NULL, &time, NULL);
const char out[] = {
'0' + time.ht, '0' + time.hu,
':',
'0' + time.mnt, '0' + time.mnu,
':',
'0' + time.st, '0' + time.su,
'\r', '\n'
};
uart_puts(out);
} else if (cmd[1] == 's') {
uart_puts("Functionality coming reel suun.\r\n");
} else if (cmd[1] == 'n') {
uart_puts("Seconds since startup: ");
uart_puts_unsigned(clock_seconds);
uart_puts("\r\n");
}
}
__attribute__ ((weak)) void dyn_1hz() {}
__attribute__ ((weak)) void rpm_collect_1hz() {}
__attribute__ ((weak)) void i_rtc_alrb() {}
void i_rtc_wut()
{
rtc->isr.wutf = 0;
#ifdef CONF_L4
exti->pr1.pif20 = 1;
#else
exti->pr.pif20 = 1;
#endif
clock_seconds++;
rpm_collect_1hz();
dyn_1hz();
}
#ifdef CONF_L4
void i_rtc_alr()
{
if (rtc->isr.alraf)
i_rtc_alra();
if (rtc->isr.alrbf)
i_rtc_alrb();
}
#else
void i_rtc_global()
{
if (rtc->isr.wutf)
i_rtc_wut();
if (rtc->isr.alraf)
i_rtc_alra();
#ifdef CONF_L0
if (rtc->isr.alrbf)
i_rtc_alrb();
#endif
}
#endif