1 | #include <avr/io.h> |
---|
2 | #include <avr/interrupt.h> |
---|
3 | #include <avr/sleep.h> |
---|
4 | |
---|
5 | #include "events.h" |
---|
6 | |
---|
7 | //#include "sched.h" |
---|
8 | #include "uart.h" |
---|
9 | #include "base.h" |
---|
10 | |
---|
11 | static struct { |
---|
12 | struct uart_request req; |
---|
13 | const __flash char *str; |
---|
14 | } ctx; |
---|
15 | |
---|
16 | static void uart_puts(const __flash char *str) |
---|
17 | { |
---|
18 | ctx.str = str; |
---|
19 | |
---|
20 | char c = *(ctx.str++); |
---|
21 | |
---|
22 | ctx.req.c = c; |
---|
23 | uart_tx(&ctx.req); |
---|
24 | } |
---|
25 | |
---|
26 | int main(void) |
---|
27 | { |
---|
28 | led_init(); |
---|
29 | led_on(); |
---|
30 | |
---|
31 | uart_init(baud2ubrr(115200)); |
---|
32 | |
---|
33 | // uart_puts(F("Hello World!\n")); |
---|
34 | |
---|
35 | set_sleep_mode(SLEEP_MODE_IDLE); |
---|
36 | sleep_enable(); |
---|
37 | |
---|
38 | static void (* const __flash workers[EV_COUNT])(void) = { |
---|
39 | [ EV_UART_UDRE ] = uart_udre |
---|
40 | }; |
---|
41 | |
---|
42 | for (;;) { |
---|
43 | uint8_t events = EVENTS_REG; |
---|
44 | if (events == 0) { |
---|
45 | led_off(); |
---|
46 | sei(); |
---|
47 | sleep_cpu(); |
---|
48 | led_on(); |
---|
49 | cli(); |
---|
50 | continue; |
---|
51 | } |
---|
52 | EVENTS_REG = 0; |
---|
53 | sei(); |
---|
54 | uint8_t idx = 0; |
---|
55 | for (;;) { |
---|
56 | if (events & 1) { |
---|
57 | workers[idx](); |
---|
58 | } |
---|
59 | if ((events >>= 1) == 0) { |
---|
60 | break; |
---|
61 | } |
---|
62 | ++idx; |
---|
63 | }; |
---|
64 | cli(); |
---|
65 | } |
---|
66 | } |
---|