-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys.c
469 lines (398 loc) · 12.1 KB
/
sys.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include "sys.h"
#include "led.h"
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <avr/sleep.h>
#include "signal.h"
static void sys_initIO();
static void sys_init();
#if CONFIG_SLEEP >= DEF_SLEEP_STANDBY
#if CONFIG_STANDBY_SLOWCLOCK
static inline void sys_slowClock();
static inline void sys_fastClock();
#endif
static inline void time_resetStabdbyTimer();
static inline void time_enableStabdbyTimer();
static inline void time_disableStabdbyTimer();
#endif
Signal_Publisher(sys_init_io);
Signal_Publisher(sys_init_early);
Signal_Publisher(sys_init);
Signal_Publisher(sys_start);
Signal_Publisher(sys_finit);
Signal_Publisher(sys_abort);
Signal_Publisher(sys_loop);
Signal_Publisher(sys_standby_enter);
Signal_Publisher(sys_standby_exit);
static volatile Sys_AbortCode sys_fault;
volatile Sys_Signal sys_signalValue;
ISR(BADISR_vect) {
sys_abort(SysAbortBadIRQ);
}
#define _CONFIG_ABORT_COUNT ((CONFIG_ABORT_FLAGS) & DEF_ABORT_FLAGS_COUNT_bm)
#define _CONFIG_ABORT_RESTART (((CONFIG_ABORT_FLAGS) & DEF_ABORT_FLAGS_RESTART) == DEF_ABORT_FLAGS_RESTART)
#define _CONFIG_ABORT_BREAKPOINT (((CONFIG_ABORT_FLAGS) & DEF_ABORT_FLAGS_BREAKPOINT) == DEF_ABORT_FLAGS_BREAKPOINT)
#ifndef CONFIG_ABORT_COLOR1
#define CONFIG_ABORT_COLOR1 DEF_ABORT_COLOR_RED
#endif
#ifndef CONFIG_ABORT_COLOR2
#define CONFIG_ABORT_COLOR2 DEF_ABORT_COLOR_BLACK
#endif
#if CONFIG_ABORT_COLOR1 == DEF_ABORT_COLOR_BLACK
#define _CONFIG_ABORT_COLOR1 led_color_black
#elif CONFIG_ABORT_COLOR1 == DEF_ABORT_COLOR_RED
#define _CONFIG_ABORT_COLOR1 led_color_red
#elif CONFIG_ABORT_COLOR1 == DEF_ABORT_COLOR_GREEN
#define _CONFIG_ABORT_COLOR1 led_color_green
#elif CONFIG_ABORT_COLOR1 == DEF_ABORT_COLOR_BLUE
#define _CONFIG_ABORT_COLOR1 led_color_blue
#elif CONFIG_ABORT_COLOR1 == DEF_ABORT_COLOR_WHITE
#define _CONFIG_ABORT_COLOR1 led_color_white
#else
#error "Invalid CONFIG_ABORT_COLOR1 selected."
#endif
#if CONFIG_ABORT_COLOR2 == DEF_ABORT_COLOR_BLACK
#define _CONFIG_ABORT_COLOR2 led_color_black
#elif CONFIG_ABORT_COLOR2 == DEF_ABORT_COLOR_RED
#define _CONFIG_ABORT_COLOR2 led_color_red
#elif CONFIG_ABORT_COLOR2 == DEF_ABORT_COLOR_GREEN
#define _CONFIG_ABORT_COLOR2 led_color_green
#elif CONFIG_ABORT_COLOR2 == DEF_ABORT_COLOR_BLUE
#define _CONFIG_ABORT_COLOR2 led_color_blue
#elif CONFIG_ABORT_COLOR2 == DEF_ABORT_COLOR_WHITE
#define _CONFIG_ABORT_COLOR2 led_color_white
#else
#error "Invalid CONFIG_ABORT_COLOR1 selected."
#endif
void sys_abort(Sys_AbortCode code) {
DISABLE_INTERRUPTS();
sys_fault = code;
#if _CONFIG_ABORT_BREAKPOINT
DEBUG_BREAKPOINT();
#endif
wdt_reset();
wdt_disable();
signal_dispatch(sys_finit);
signal_dispatch(sys_abort);
#if _CONFIG_ABORT_COUNT > 0 && CONFIG_LED_ENABLE
for (uint8_t count = 0; count < _CONFIG_ABORT_COUNT; count ++) {
led_setAll(&_CONFIG_ABORT_COLOR1);
led_update();
time_sleep(CONFIG_ABORT_TIMER_HI);
led_setAll(&_CONFIG_ABORT_COLOR2);
led_update();
time_sleep(CONFIG_ABORT_TIMER_LO);
}
#endif
#if _CONFIG_ABORT_RESTART
sys_restart();
#else
for (;;) {}
#endif
}
void sys_restart() {
for (;;) {
ccp_write_io((void*)&(RSTCTRL.SWRR),RSTCTRL_SWRE_bm);
}
}
int main () {
uint8_t resetCause = RSTCTRL.RSTFR;
if (resetCause & (RSTCTRL_BORF_bm|RSTCTRL_WDRF_bm|RSTCTRL_SWRF_bm)) {
DEBUG_BREAKPOINT();
}
RSTCTRL.RSTFR = 0xFF;
signal_dispatch(sys_init_io);
signal_dispatch(sys_init_early);
signal_dispatch(sys_init);
//wdt_reset();
sys_signalValue |= Sys_SignalWakeLock | Sys_SignalWorkerPending; // always do at least one loop at startup
ENABLE_INTERRUPTS();
signal_dispatch(sys_start);
while(1) {
wdt_reset();
#if CONFIG_SLEEP
DISABLE_INTERRUPTS();
Sys_Signal sigs;
sigs = sys_signalValue;
sys_signalValue = 0;
#if CONFIG_SLEEP >= DEF_SLEEP_STANDBY
if (sigs == Sys_SignalEnterStandby) {
signal_dispatch(sys_standby_enter);
time_disableStabdbyTimer();
#if CONFIG_SLEEP == DEF_SLEEP_STANDBY
set_sleep_mode(SLPCTRL_SMODE_STDBY_gc);
#elif CONFIG_SLEEP == DEF_SLEEP_POWERDOWN
set_sleep_mode(SLPCTRL_SMODE_PDOWN_gc);
#endif
#if CONFIG_STANDBY_SLOWCLOCK
sys_slowClock();
#endif
ENABLE_INTERRUPTS();
#if CONFIG_SLEEP != DEF_SLEEP_PRETEND // Enter standby unless we're faking it...
sleep_mode();
#endif
DISABLE_INTERRUPTS();
#if CONFIG_STANDBY_SLOWCLOCK
sys_fastClock();
#endif
time_enableStabdbyTimer();
signal_dispatch(sys_standby_exit);
ENABLE_INTERRUPTS();
continue;
}
#endif
if (!sigs) {
set_sleep_mode(SLPCTRL_SMODE_IDLE_gc);
ENABLE_INTERRUPTS();
sleep_mode();
continue;
}
ENABLE_INTERRUPTS();
#else
sys_signalValue = 0;
#endif
#if CONFIG_SLEEP >= DEF_SLEEP_STANDBY
if (sigs & Sys_SignalWakeLock) {
time_resetStabdbyTimer();
}
#endif
#if !CONFIG_SLEEP
// This is needed until I fix the test application mode.
signal_dispatch(sys_loop);
#else
if (sigs & Sys_SignalWorkerPending) {
signal_dispatch(sys_loop);
}
#endif
}
signal_dispatch(sys_finit);
}
SysInitIO_Subscribe(sys_initIO, Signal_Highest);
SysInitEarly_Subscribe(sys_init, Signal_Highest);
static void sys_initIO() {
#if CONFIG_HAS_PORT_A
for (uint8_t i = 0; i < 8; i++) {
*((uint8_t *)&PORTA + 0x10 + i) |= 1 << PORT_PULLUPEN_bp;
}
#endif
#if CONFIG_HAS_PORT_B
for (uint8_t i = 0; i < 8; i++) {
*((uint8_t *)&PORTB + 0x10 + i) |= 1 << PORT_PULLUPEN_bp;
}
#endif
#if CONFIG_HAS_PORT_C
for (uint8_t i = 0; i < 8; i++) {
*((uint8_t *)&PORTC + 0x10 + i) |= 1 << PORT_PULLUPEN_bp;
}
#endif
#if CONFIG_HAS_PORT_A
PORTA.DIR = CONFIG_PORT_A_DIR;
PORTA.OUT = CONFIG_PORT_A_OUT;
#if defined(CONFIG_PORT_A_PIN0)
PORTA.PIN0CTRL = CONFIG_PORT_A_PIN0;
#endif
#if defined(CONFIG_PORT_A_PIN1)
PORTA.PIN1CTRL = CONFIG_PORT_A_PIN1;
#endif
#if defined(CONFIG_PORT_A_PIN2)
PORTA.PIN2CTRL = CONFIG_PORT_A_PIN2;
#endif
#if defined(CONFIG_PORT_A_PIN3)
PORTA.PIN3CTRL = CONFIG_PORT_A_PIN3;
#endif
#if defined(CONFIG_PORT_A_PIN4)
PORTA.PIN4CTRL = CONFIG_PORT_A_PIN4;
#endif
#if defined(CONFIG_PORT_A_PIN5)
PORTA.PIN5CTRL = CONFIG_PORT_A_PIN5;
#endif
#if defined(CONFIG_PORT_A_PIN6)
PORTA.PIN6CTRL = CONFIG_PORT_A_PIN6;
#endif
#if defined(CONFIG_PORT_A_PIN7)
PORTA.PIN7CTRL = CONFIG_PORT_A_PIN7;
#endif
#endif
#if CONFIG_HAS_PORT_B
PORTB.DIR = CONFIG_PORT_B_DIR;
PORTB.OUT = CONFIG_PORT_B_OUT;
#if defined(CONFIG_PORT_B_PIN0)
PORTB.PIN0CTRL = CONFIG_PORT_A_PIN0;
#endif
#if defined(CONFIG_PORT_B_PIN1)
PORTB.PIN1CTRL = CONFIG_PORT_B_PIN1;
#endif
#if defined(CONFIG_PORT_B_PIN2)
PORTB.PIN2CTRL = CONFIG_PORT_B_PIN2;
#endif
#if defined(CONFIG_PORT_B_PIN3)
PORTB.PIN3CTRL = CONFIG_PORT_B_PIN3;
#endif
#if defined(CONFIG_PORT_B_PIN4)
PORTB.PIN4CTRL = CONFIG_PORT_B_PIN4;
#endif
#if defined(CONFIG_PORT_B_PIN5)
PORTB.PIN5CTRL = CONFIG_PORT_B_PIN5;
#endif
#if defined(CONFIG_PORT_B_PIN6)
PORTB.PIN6CTRL = CONFIG_PORT_B_PIN6;
#endif
#if defined(CONFIG_PORT_B_PIN7)
PORTB.PIN7CTRL = CONFIG_PORT_B_PIN7;
#endif
#endif
#if CONFIG_HAS_PORT_C
PORTC.DIR = CONFIG_PORT_C_DIR;
PORTC.OUT = CONFIG_PORT_C_OUT;
#if defined(CONFIG_PORT_C_PIN0)
PORTC.PIN0CTRL = CONFIG_PORT_C_PIN0;
#endif
#if defined(CONFIG_PORT_C_PIN1)
PORTC.PIN1CTRL = CONFIG_PORT_C_PIN1;
#endif
#if defined(CONFIG_PORT_C_PIN2)
PORTC.PIN2CTRL = CONFIG_PORT_C_PIN2;
#endif
#if defined(CONFIG_PORT_C_PIN3)
PORTC.PIN3CTRL = CONFIG_PORT_C_PIN3;
#endif
#if defined(CONFIG_PORT_C_PIN4)
PORTC.PIN4CTRL = CONFIG_PORT_C_PIN4;
#endif
#if defined(CONFIG_PORT_C_PIN5)
PORTC.PIN5CTRL = CONFIG_PORT_C_PIN5;
#endif
#if defined(CONFIG_PORT_C_PIN6)
PORTC.PIN6CTRL = CONFIG_PORT_C_PIN6;
#endif
#if defined(CONFIG_PORT_C_PIN7)
PORTC.PIN7CTRL = CONFIG_PORT_C_PIN7;
#endif
#endif
/* PORTMUX Initialization */
#if defined(CONFIG_PINMUX_A)
PORTMUX.CTRLA = CONFIG_PINMUX_A;
#endif
#if defined(CONFIG_PINMUX_B)
PORTMUX.CTRLB = CONFIG_PINMUX_B;
#endif
#if defined(CONFIG_PINMUX_C)
PORTMUX.CTRLC = CONFIG_PINMUX_C;
#endif
#if defined(CONFIG_PINMUX_D)
PORTMUX.CTRLD = CONFIG_PINMUX_D;
#endif
}
static void sys_init() {
// *************************************************************************
// ******** Watchdog Timer *************************************************
//wdt_enable(WDTO_2S);
// *************************************************************************
// ******** Brown Out Detector *********************************************
#if 0
ccp_write_io((void*)&(BOD.CTRLA),0x00);
//VLMCFG BELOW; VLMIE disabled;
BOD.INTCTRL = 0x00;
//VLMLVL 5ABOVE;
BOD.VLMCTRLA = 0x00;
#endif
// *************************************************************************
// ******** Sleep Controller ***********************************************
ccp_write_io((void*)&(SLPCTRL.CTRLA),0x00);
// *************************************************************************
// ******** Clock Init *****************************************************
//RUNSTDBY disabled;
ccp_write_io((void*)&(CLKCTRL.OSC32KCTRLA),0x00);
//RUNSTDBY disabled;
ccp_write_io((void*)&(CLKCTRL.OSC20MCTRLA),0x00);
//PDIV 2X; PEN disabled;
ccp_write_io((void*)&(CLKCTRL.MCLKCTRLB),0x00);
//CLKOUT disabled; CLKSEL OSC20M;
ccp_write_io((void*)&(CLKCTRL.MCLKCTRLA),0x00);
//LOCKEN disabled;
ccp_write_io((void*)&(CLKCTRL.MCLKLOCK),0x00);
// *************************************************************************
// ******** Interrupt Init *************************************************
//IVSEL disabled; CVT disabled; LVL0RR disabled;
ccp_write_io((void*)&(CPUINT.CTRLA),0x00);
#if CONFIG_HW_IRQ_PRIORITY
//LVL0PRI 0;
CPUINT.LVL0PRI = 0x00;
//LVL1VEC 23;
CPUINT.LVL1VEC = CONFIG_HW_IRQ_PRIORITY;
#endif
// *************************************************************************
// ******** Time Init ******************************************************
while (RTC.STATUS > 0) {}
#if CONFIG_SLEEP >= DEF_SLEEP_STANDBY
time_enableStabdbyTimer();
#else
RTC.CMP = 0x01;
RTC.INTCTRL = 0x00;
#endif
//Count
RTC.CNT = 0x00;
//Period
RTC.PER = 0xFFFF;
//Clock selection
RTC.CLKSEL = 0x00;
//RUNSTDBY disabled; PRESCALER DIV16; RTCEN enabled;
RTC.CTRLA = RTC_PRESCALER_DIV16_gc | RTC_RTCEN_bm;//0x21;/**/
// Wait for all register to be synchronized
while (RTC.PITSTATUS > 0) {}
//PI disabled;
RTC.PITINTCTRL = 0;
}
time16_t time_sleep(timer_interval_t interval) {
uint16_t start, now, delta;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
start = RTC.CNT;
}
for (;;) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
now = RTC.CNT;
}
delta = now-start;
if (delta>interval) {
return delta;
}
}
}
time16_t time_get16() {
uint16_t time;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
time = RTC.CNT;
}
return time;
}
#if CONFIG_STANDBY_SLOWCLOCK && CONFIG_SLEEP >= DEF_SLEEP_STANDBY
static inline void sys_slowClock() {
// Reduce CLK to 2MHz
ccp_write_io((void*)&(CLKCTRL.MCLKCTRLB),CLKCTRL_PDIV_24X_gc | CLKCTRL_PEN_bm);
}
static inline void sys_fastClock() {
ccp_write_io((void*)&(CLKCTRL.MCLKCTRLB),0x00);
}
#endif
#if CONFIG_SLEEP >= DEF_SLEEP_STANDBY
ISR(RTC_CNT_vect) {
RTC.INTFLAGS = RTC_CMP_bm;
sys_signalIRQ(Sys_SignalEnterStandby);
}
static inline void time_resetStabdbyTimer() {
// if it's busy, don't wait
if (RTC.STATUS & RTC_CMPBUSY_bm) {
return;
}
RTC.CMP = RTC.CNT + CONFIG_SLEEP_TIMEOUT;
}
static inline void time_enableStabdbyTimer() {
while (RTC.STATUS & RTC_CMPBUSY_bm) {}
RTC.CMP = RTC.CNT + CONFIG_SLEEP_TIMEOUT;
RTC.INTFLAGS = RTC_CMP_bm;
RTC.INTCTRL = RTC_CMP_bm;
}
static inline void time_disableStabdbyTimer() {
RTC.INTCTRL = 0x00;
}
#endif