0
|
1 #include <stdio.h> |
|
2 #include <string.h> |
|
3 #include <stddef.h> |
|
4 #include <stdbool.h> |
|
5 #include <stdlib.h> |
|
6 #include <avr/io.h> |
|
7 #include <avr/interrupt.h> |
|
8 #include <avr/sleep.h> |
|
9 #include <util/delay.h> |
|
10 #include <avr/pgmspace.h> |
|
11 #include <avr/eeprom.h> |
|
12 #include <avr/wdt.h> |
|
13 #include <util/atomic.h> |
|
14 #include <util/crc16.h> |
|
15 |
2
|
16 #include "hmac-sha1.h" |
|
17 |
1
|
18 //#include "simple_ds18b20.h" |
|
19 //#include "onewire.h" |
0
|
20 |
|
21 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) |
|
22 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) |
|
23 |
|
24 // TICK should be 8 or less (8 untested). all timers need |
|
25 // to be a multiple. |
|
26 |
1
|
27 #define TICK 1 |
2
|
28 #define SLEEP_COMPARE (2000000/64) // == 31250 exactly |
|
29 #define NKEYS 6 |
1
|
30 #define KEYLEN 20 |
|
31 |
0
|
32 #define BAUD 19200 |
|
33 #define UBRR ((F_CPU)/8/(BAUD)-1) |
|
34 |
2
|
35 // XXX |
|
36 #define PORT_PI_BOOT PORTD |
|
37 #define DDR_PI_BOOT DDRD |
|
38 #define PIN_PI_BOOT PD7 |
|
39 |
|
40 // XXX |
|
41 #define PORT_PI_RESET PORTD |
|
42 #define DDR_PI_RESET DDRD |
|
43 #define PIN_PI_RESET PD6 |
|
44 |
|
45 |
0
|
46 #define PORT_LED PORTC |
|
47 #define DDR_LED DDRC |
|
48 #define PIN_LED PC4 |
|
49 |
|
50 // #define HAVE_UART_ECHO |
|
51 |
|
52 // stores a value of clock_epoch combined with the remainder of TCNT2, |
|
53 // for 1/32 second accuracy |
|
54 struct epoch_ticks |
|
55 { |
|
56 uint32_t ticks; |
|
57 // remainder |
|
58 uint8_t rem; |
|
59 }; |
|
60 |
1
|
61 // eeprom-settable parameters, default values defined here. |
|
62 // all timeouts should be a multiple of TICK |
2
|
63 static uint32_t watchdog_long_limit = (60L*60*24); |
1
|
64 static uint32_t watchdog_short_limit = 0; |
|
65 static uint32_t newboot_limit = 60*10; |
0
|
66 |
1
|
67 // avr proves itself |
2
|
68 static uint8_t avr_keys[NKEYS][KEYLEN] = {{0}}; |
0
|
69 |
|
70 // ---- Atomic guards required accessing these variables |
|
71 // clock_epoch in seconds |
|
72 static uint32_t clock_epoch; |
1
|
73 // watchdog counts up |
|
74 static uint32_t watchdog_long_count; |
|
75 static uint32_t watchdog_short_count; |
2
|
76 // newboot counts down |
1
|
77 static uint32_t newboot_count; |
2
|
78 // oneshot counts down |
|
79 static uint32_t oneshot_count; |
|
80 |
0
|
81 // ---- End atomic guards required |
|
82 |
|
83 // boolean flags |
1
|
84 static uint8_t watchdog_long_hit; |
|
85 static uint8_t watchdog_short_hit; |
|
86 static uint8_t newboot_hit; |
2
|
87 static uint8_t oneshot_hit; |
0
|
88 |
|
89 static uint8_t readpos; |
1
|
90 static char readbuf[50]; |
0
|
91 static uint8_t have_cmd; |
|
92 |
|
93 int uart_putchar(char c, FILE *stream); |
|
94 static void long_delay(int ms); |
|
95 static void blink(); |
|
96 static uint16_t adc_vcc(); |
2
|
97 static void set_pi_boot_normal(uint8_t normal); |
0
|
98 |
|
99 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, |
|
100 _FDEV_SETUP_WRITE); |
|
101 |
|
102 // thanks to http://projectgus.com/2010/07/eeprom-access-with-arduino/ |
|
103 #define eeprom_read_to(dst_p, eeprom_field, dst_size) eeprom_read_block((dst_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (dst_size)) |
|
104 #define eeprom_read(dst, eeprom_field) eeprom_read_to((&dst), eeprom_field, sizeof(dst)) |
|
105 #define eeprom_write_from(src_p, eeprom_field, src_size) eeprom_write_block((src_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (src_size)) |
|
106 #define eeprom_write(src, eeprom_field) { eeprom_write_from(&src, eeprom_field, sizeof(src)); } |
|
107 |
1
|
108 #define EXPECT_MAGIC 0xdf83 |
0
|
109 |
|
110 struct __attribute__ ((__packed__)) __eeprom_data { |
1
|
111 uint32_t watchdog_long_limit; |
|
112 uint32_t watchdog_short_limit; |
|
113 uint32_t newboot_limit; |
0
|
114 |
2
|
115 uint8_t avr_keys[NKEYS][KEYLEN]; |
0
|
116 |
|
117 uint16_t magic; |
|
118 }; |
|
119 |
|
120 // Very first setup |
|
121 static void |
|
122 setup_chip() |
|
123 { |
|
124 cli(); |
|
125 |
|
126 // stop watchdog timer (might have been used to cause a reset) |
|
127 wdt_reset(); |
|
128 MCUSR &= ~_BV(WDRF); |
|
129 WDTCSR |= _BV(WDCE) | _BV(WDE); |
|
130 WDTCSR = 0; |
|
131 |
1
|
132 // set to 8S, in case sha1 is slow etc. |
|
133 wdt_enable(WDTO_8S); |
|
134 |
0
|
135 // Set clock to 2mhz |
|
136 CLKPR = _BV(CLKPCE); |
|
137 CLKPR = _BV(CLKPS1); |
|
138 |
|
139 // enable pullups |
1
|
140 // XXX matt pihelp |
0
|
141 PORTB = 0xff; // XXX change when using SPI |
|
142 PORTD = 0xff; |
|
143 PORTC = 0xff; |
|
144 |
|
145 // 3.3v power for bluetooth and SD |
|
146 DDR_LED |= _BV(PIN_LED); |
|
147 |
|
148 // set pullup |
|
149 PORTD |= _BV(PD2); |
|
150 // INT0 setup |
|
151 EICRA = (1<<ISC01); // falling edge - data sheet says it won't work? |
|
152 EIMSK = _BV(INT0); |
|
153 |
|
154 // comparator disable |
|
155 ACSR = _BV(ACD); |
|
156 |
|
157 // disable adc pin input buffers |
|
158 DIDR0 = 0x3F; // acd0-adc5 |
|
159 DIDR1 = (1<<AIN1D)|(1<<AIN0D); // ain0/ain1 |
|
160 |
|
161 sei(); |
|
162 } |
|
163 |
|
164 static void |
|
165 get_epoch_ticks(struct epoch_ticks *t) |
|
166 { |
|
167 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
|
168 { |
|
169 t->ticks = clock_epoch; |
|
170 t->rem = TCNT2; |
|
171 } |
|
172 } |
|
173 |
|
174 static void |
|
175 setup_tick_counter() |
|
176 { |
2
|
177 // set up counter1 |
|
178 |
0
|
179 // set up counter2. |
|
180 // COM21 COM20 Set OC2 on Compare Match (p116) |
|
181 // WGM21 Clear counter on compare |
|
182 //TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21); |
|
183 // toggle on match |
2
|
184 TCCR1A = _BV(COM1A0); |
|
185 // systemclock/1024 |
|
186 TCCR1B = _BV(CS12) | _BV(CS10); |
|
187 TCNT1 = 0; |
|
188 OCR1A = SLEEP_COMPARE; |
0
|
189 // interrupt |
2
|
190 TIMSK1 = _BV(OCIE1A); |
0
|
191 } |
|
192 |
|
193 static void |
|
194 uart_on() |
|
195 { |
|
196 // Power reduction register |
|
197 PRR &= ~_BV(PRUSART0); |
|
198 |
|
199 // All of this needs to be done each time after turning off the PRR |
|
200 // baud rate |
|
201 UBRR0H = (unsigned char)(UBRR >> 8); |
|
202 UBRR0L = (unsigned char)UBRR; |
|
203 // set 2x clock, improves accuracy of UBRR |
|
204 UCSR0A |= _BV(U2X0); |
|
205 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); |
|
206 //8N1 |
|
207 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); |
|
208 } |
|
209 |
|
210 static void |
|
211 uart_off() |
|
212 { |
|
213 // Turn off interrupts and disable tx/rx |
|
214 UCSR0B = 0; |
|
215 |
|
216 // Power reduction register |
|
217 PRR |= _BV(PRUSART0); |
|
218 } |
|
219 |
|
220 int |
|
221 uart_putchar(char c, FILE *stream) |
|
222 { |
|
223 // XXX could perhaps sleep in the loop for power. |
|
224 if (c == '\n') |
|
225 { |
|
226 loop_until_bit_is_set(UCSR0A, UDRE0); |
|
227 UDR0 = '\r'; |
|
228 } |
|
229 loop_until_bit_is_set(UCSR0A, UDRE0); |
|
230 UDR0 = c; |
|
231 if (c == '\r') |
|
232 { |
|
233 loop_until_bit_is_set(UCSR0A, UDRE0); |
|
234 UDR0 = '\n'; |
|
235 } |
|
236 return (unsigned char)c; |
|
237 } |
|
238 |
|
239 static void |
|
240 cmd_reset() |
|
241 { |
|
242 printf_P(PSTR("reset\n")); |
|
243 _delay_ms(100); |
|
244 cli(); // disable interrupts |
|
245 wdt_enable(WDTO_15MS); // enable watchdog |
|
246 while(1); // wait for watchdog to reset processor |
|
247 } |
|
248 |
2
|
249 static void |
|
250 cmd_newboot() |
|
251 { |
|
252 set_pi_boot_normal(1); |
|
253 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
|
254 { |
|
255 newboot_count = newboot_limit; |
|
256 } |
|
257 printf_P(PSTR("newboot for %d secs"), newboot_limit); |
|
258 } |
|
259 |
1
|
260 |
0
|
261 |
|
262 static void |
1
|
263 cmd_get_params() |
0
|
264 { |
2
|
265 uint32_t cur_watchdog_long, cur_watchdog_short, cur_newboot, cur_oneshot; |
1
|
266 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
|
267 { |
2
|
268 cur_watchdog_long = watchdog_long_count; |
|
269 cur_watchdog_short = watchdog_short_count; |
|
270 cur_newboot = newboot_count; |
|
271 cur_oneshot = oneshot_count; |
1
|
272 } |
|
273 |
2
|
274 printf_P(PSTR("limit (count) : watchdog_long %lu (%lu) watchdog_short %lu (%lu) newboot %lu (%lu) oneshot (%lu)\n"), |
1
|
275 watchdog_long_limit, |
2
|
276 cur_watchdog_long, |
1
|
277 watchdog_short_limit, |
2
|
278 cur_watchdog_short, |
1
|
279 newboot_limit, |
2
|
280 cur_newboot, |
|
281 cur_oneshot); |
0
|
282 } |
|
283 |
|
284 static void |
1
|
285 cmd_set_params(const char *params) |
0
|
286 { |
1
|
287 uint32_t new_watchdog_long_limit; |
|
288 uint32_t new_watchdog_short_limit; |
|
289 uint32_t new_newboot_limit; |
|
290 |
|
291 int ret = sscanf_P(params, PSTR("%lu %lu %lu"), |
|
292 &new_watchdog_long_limit, |
|
293 &new_watchdog_short_limit, |
|
294 &new_newboot_limit); |
|
295 |
|
296 |
|
297 if (ret != 3) |
0
|
298 { |
1
|
299 printf_P(PSTR("Bad values\n")); |
|
300 } |
|
301 else |
|
302 { |
|
303 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
|
304 { |
|
305 eeprom_write(new_watchdog_long_limit, watchdog_long_limit); |
|
306 eeprom_write(new_watchdog_short_limit, watchdog_short_limit); |
|
307 eeprom_write(new_newboot_limit, newboot_limit); |
|
308 uint16_t magic = EXPECT_MAGIC; |
|
309 eeprom_write(magic, magic); |
0
|
310 } |
1
|
311 printf_P(PSTR("set_params for next boot\n")); |
|
312 printf_P(PSTR("watchdog_long %lu watchdog_short %lu newboot %lu\n"), |
|
313 new_watchdog_long_limit, |
|
314 new_watchdog_short_limit, |
|
315 new_newboot_limit); |
|
316 |
|
317 } |
|
318 } |
0
|
319 |
1
|
320 uint8_t from_hex(char c) |
|
321 { |
|
322 if (c >= '0' && c <= '9') { |
|
323 return c-'0'; |
|
324 } |
|
325 if (c >= 'a' && c <= 'f') { |
|
326 return c-'a' + 0xa; |
|
327 } |
|
328 if (c >= 'A' && c <= 'F') { |
|
329 return c-'A' + 0xa; |
|
330 } |
|
331 return 0; |
|
332 } |
|
333 |
2
|
334 static void |
|
335 printhex_nibble(const unsigned char b, FILE *stream) |
|
336 { |
|
337 unsigned char c = b & 0x0f; |
|
338 if ( c > 9 ) { |
|
339 c += 'A'-10; |
|
340 } |
|
341 else { |
|
342 c += '0'; |
|
343 } |
|
344 fputc(c, stream); |
|
345 } |
|
346 |
|
347 void |
|
348 printhex_byte(const unsigned char b, FILE *stream) |
|
349 { |
|
350 printhex_nibble( b >> 4, stream); |
|
351 printhex_nibble( b, stream); |
|
352 } |
|
353 |
|
354 void |
|
355 printhex(uint8_t *id, uint8_t n, FILE *stream) |
|
356 { |
|
357 for (uint8_t i = 0; i < n; i++) |
|
358 { |
|
359 if (i > 0) |
|
360 { |
|
361 fputc(' ', stream); |
|
362 } |
|
363 printhex_byte(id[i], stream); |
|
364 } |
|
365 } |
|
366 |
|
367 static int8_t |
|
368 parse_key(const char *params, uint8_t *key_index, uint8_t bytes[KEYLEN]) |
|
369 { |
|
370 // "N HEXKEY" |
|
371 if (strlen(params) != KEYLEN*2+2) { |
|
372 printf_P(PSTR("Wrong length key\n")); |
|
373 return -1; |
|
374 } |
|
375 |
|
376 if (params[1] != ' ') |
|
377 { |
|
378 printf_P(PSTR("Missing space\n")); |
|
379 return -1; |
|
380 } |
|
381 |
|
382 *key_index = from_hex(params[0]); |
|
383 if (*key_index >= NKEYS) |
|
384 { |
|
385 printf_P(PSTR("Bad key index %d, max %d\n"), *key_index, NKEYS); |
|
386 return -1; |
|
387 } |
|
388 |
|
389 for (int i = 0, p = 0; i < KEYLEN; i++, p += 2) |
|
390 { |
|
391 bytes[i] = (from_hex(params[p+2]) << 4) | from_hex(params[p+3]); |
|
392 } |
|
393 return 0; |
|
394 } |
|
395 |
1
|
396 static void |
|
397 cmd_set_avr_key(const char *params) |
|
398 { |
2
|
399 uint8_t new_key[KEYLEN]; |
|
400 uint8_t key_index; |
|
401 if (parse_key(params, &key_index, new_key) != 0) |
|
402 { |
1
|
403 return; |
0
|
404 } |
2
|
405 memcpy(avr_keys[key_index], new_key, sizeof(new_key)); |
|
406 eeprom_write(avr_keys, avr_keys); |
|
407 } |
|
408 |
|
409 static void |
|
410 cmd_hmac(const char *params) |
|
411 { |
|
412 uint8_t data[KEYLEN]; |
|
413 uint8_t key_index; |
|
414 if (parse_key(params, &key_index, data) != 0) |
|
415 { |
|
416 printf_P(PSTR("FAIL: Bad input\n")); |
|
417 } |
0
|
418 |
2
|
419 hmac_sha1_ctx_t ctx; |
|
420 hmac_sha1_init(&ctx, avr_keys[key_index], KEYLEN); |
|
421 hmac_sha1_lastBlock(&ctx, data, KEYLEN); |
|
422 hmac_sha1_final(data, &ctx); |
|
423 |
|
424 printf_P(PSTR("HMAC: ")); |
|
425 printhex(data, KEYLEN, stdout); |
|
426 fputc('\n', stdout); |
|
427 } |
|
428 |
|
429 static void |
|
430 cmd_oneshot_reboot(const char *params) |
|
431 { |
|
432 uint32_t new_delay = strtoul(params, NULL, 10); |
|
433 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
1
|
434 { |
2
|
435 oneshot_count = new_delay; |
1
|
436 } |
2
|
437 printf_P(PSTR("oneshot delay %lu\n"), new_delay); |
0
|
438 } |
|
439 |
|
440 static void |
|
441 load_params() |
|
442 { |
|
443 uint16_t magic; |
|
444 eeprom_read(magic, magic); |
|
445 if (magic == EXPECT_MAGIC) |
|
446 { |
1
|
447 eeprom_read(watchdog_long_limit, watchdog_long_limit); |
|
448 eeprom_read(watchdog_short_limit, watchdog_short_limit); |
2
|
449 eeprom_read(newboot_limit, newboot_limit); |
1
|
450 } |
2
|
451 |
|
452 eeprom_read(avr_keys, avr_keys); |
0
|
453 } |
|
454 |
2
|
455 static void |
|
456 cmd_vcc() |
0
|
457 { |
2
|
458 uint16_t vcc = adc_vcc(); |
|
459 printf_P("vcc: %u mV\n", vcc); |
0
|
460 } |
|
461 |
|
462 static void |
|
463 read_handler() |
|
464 { |
2
|
465 if (strcmp_P(readbuf, PSTR("get_params")) == 0) |
0
|
466 { |
|
467 cmd_get_params(); |
|
468 } |
|
469 else if (strncmp_P(readbuf, PSTR("set_params "), 11) == 0) |
|
470 { |
|
471 cmd_set_params(&readbuf[11]); |
|
472 } |
2
|
473 else if (strncmp_P(readbuf, PSTR("set_key "), 8) == 0) |
0
|
474 { |
2
|
475 cmd_set_avr_key(&readbuf[8]); |
0
|
476 } |
2
|
477 else if (strncmp_P(readbuf, PSTR("oneshot "), 8) == 0) |
0
|
478 { |
2
|
479 cmd_oneshot_reboot(&readbuf[8]); |
0
|
480 } |
2
|
481 else if (strncmp_P(readbuf, PSTR("hmac "), 5) == 0) |
0
|
482 { |
2
|
483 cmd_hmac(&readbuf[5]); |
0
|
484 } |
2
|
485 else if (strcmp_P(readbuf, PSTR("vcc")) == 0) |
0
|
486 { |
2
|
487 cmd_vcc(); |
0
|
488 } |
|
489 else if (strcmp_P(readbuf, PSTR("reset")) == 0) |
|
490 { |
|
491 cmd_reset(); |
|
492 } |
|
493 else |
|
494 { |
|
495 printf_P(PSTR("Bad command '%s'\n"), readbuf); |
|
496 } |
|
497 } |
|
498 |
|
499 ISR(INT0_vect) |
|
500 { |
|
501 blink(); |
|
502 _delay_ms(100); |
|
503 blink(); |
|
504 } |
|
505 |
|
506 |
|
507 ISR(USART_RX_vect) |
|
508 { |
|
509 char c = UDR0; |
|
510 #ifdef HAVE_UART_ECHO |
|
511 uart_putchar(c, NULL); |
|
512 #endif |
|
513 if (c == '\r' || c == '\n') |
|
514 { |
|
515 if (readpos > 0) |
|
516 { |
|
517 readbuf[readpos] = '\0'; |
|
518 have_cmd = 1; |
|
519 readpos = 0; |
|
520 } |
|
521 } |
|
522 else |
|
523 { |
|
524 readbuf[readpos] = c; |
|
525 readpos++; |
|
526 if (readpos >= sizeof(readbuf)) |
|
527 { |
|
528 readpos = 0; |
|
529 } |
|
530 } |
|
531 } |
|
532 |
2
|
533 ISR(TIMER1_COMPA_vect) |
0
|
534 { |
2
|
535 TCNT1 = 0; |
0
|
536 |
|
537 clock_epoch += TICK; |
|
538 |
1
|
539 // watchdogs count up, continuous |
|
540 if (watchdog_long_limit > 0) { |
2
|
541 watchdog_long_count += TICK; |
1
|
542 if (watchdog_long_count >= watchdog_long_limit) |
|
543 { |
|
544 watchdog_long_count = 0; |
|
545 watchdog_long_hit = 1; |
|
546 } |
0
|
547 } |
|
548 |
1
|
549 if (watchdog_short_limit > 0) { |
2
|
550 watchdog_short_count += TICK; |
1
|
551 if (watchdog_short_count >= watchdog_short_limit) |
|
552 { |
|
553 watchdog_short_count = 0; |
|
554 watchdog_short_hit = 1; |
|
555 } |
0
|
556 } |
|
557 |
2
|
558 // newboot counts down |
1
|
559 if (newboot_count > 0) |
0
|
560 { |
2
|
561 newboot_count-=TICK; |
|
562 if (newboot_count <= 0) |
1
|
563 { |
|
564 newboot_hit = 1; |
2
|
565 newboot_count = 0; |
1
|
566 } |
0
|
567 } |
1
|
568 |
2
|
569 if (oneshot_count > 0) |
|
570 { |
|
571 oneshot_count-=TICK; |
|
572 if (oneshot_count <= 0) |
|
573 { |
|
574 oneshot_hit = 1; |
|
575 oneshot_count = 0; |
|
576 } |
|
577 } |
0
|
578 |
|
579 |
|
580 } |
|
581 |
|
582 static void |
|
583 idle_sleep() |
|
584 { |
|
585 set_sleep_mode(SLEEP_MODE_IDLE); |
|
586 sleep_mode(); |
|
587 } |
|
588 |
|
589 static uint16_t |
|
590 adc_vcc() |
|
591 { |
|
592 PRR &= ~_BV(PRADC); |
|
593 |
|
594 // /16 prescaler |
|
595 ADCSRA = _BV(ADEN) | _BV(ADPS2); |
|
596 |
|
597 // set to measure 1.1 reference |
|
598 ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); |
|
599 // average a number of samples |
|
600 uint16_t sum = 0; |
|
601 uint8_t num = 0; |
|
602 for (uint8_t n = 0; n < 20; n++) |
|
603 { |
|
604 ADCSRA |= _BV(ADSC); |
|
605 loop_until_bit_is_clear(ADCSRA, ADSC); |
|
606 |
|
607 uint8_t low_11 = ADCL; |
|
608 uint8_t high_11 = ADCH; |
|
609 uint16_t val = low_11 + (high_11 << 8); |
|
610 |
|
611 if (n >= 4) |
|
612 { |
|
613 sum += val; |
|
614 num++; |
|
615 } |
|
616 } |
|
617 ADCSRA = 0; |
|
618 PRR |= _BV(PRADC); |
|
619 |
|
620 //float res_volts = 1.1 * 1024 * num / sum; |
|
621 //return 1000 * res_volts; |
|
622 return ((uint32_t)1100*1024*num) / sum; |
|
623 } |
|
624 |
|
625 static void |
2
|
626 reboot_pi() |
|
627 { |
|
628 // pull it low for 30ms |
|
629 PORT_PI_RESET &= ~_BV(PIN_PI_RESET); |
|
630 DDR_PI_RESET |= _BV(PIN_PI_RESET); |
|
631 _delay_ms(30); |
|
632 DDR_PI_RESET &= ~_BV(PIN_PI_RESET); |
|
633 } |
|
634 |
|
635 static void |
|
636 set_pi_boot_normal(uint8_t normal) |
|
637 { |
|
638 PORT_PI_BOOT &= ~_BV(PIN_PI_BOOT); |
|
639 if (normal) |
|
640 { |
|
641 // tristate |
|
642 DDR_PI_BOOT &= ~_BV(PIN_PI_BOOT); |
|
643 } |
|
644 else |
|
645 { |
|
646 // pull it low |
|
647 DDR_PI_RESET |= _BV(PIN_PI_BOOT); |
|
648 |
|
649 } |
|
650 } |
|
651 |
|
652 static void |
|
653 check_flags() |
|
654 { |
|
655 if (watchdog_long_hit |
|
656 || watchdog_short_hit |
|
657 || oneshot_hit) |
|
658 { |
|
659 reboot_pi(); |
|
660 } |
|
661 |
|
662 if (newboot_hit) { |
|
663 set_pi_boot_normal(0); |
|
664 } |
|
665 |
|
666 watchdog_long_hit = 0; |
|
667 watchdog_short_hit = 0; |
|
668 newboot_hit = 0; |
|
669 oneshot_hit = 0; |
|
670 } |
|
671 |
|
672 static void |
0
|
673 do_comms() |
|
674 { |
|
675 // avoid receiving rubbish, perhaps |
|
676 uart_on(); |
|
677 |
|
678 // write sd card here? same 3.3v regulator... |
|
679 |
1
|
680 while (1) |
0
|
681 { |
1
|
682 wdt_reset(); |
2
|
683 |
|
684 check_flags(); |
|
685 |
0
|
686 if (have_cmd) |
|
687 { |
|
688 have_cmd = 0; |
|
689 read_handler(); |
|
690 continue; |
|
691 } |
|
692 |
|
693 // wait for commands from the master |
|
694 idle_sleep(); |
|
695 } |
|
696 } |
|
697 |
|
698 static void |
|
699 blink() |
|
700 { |
|
701 PORT_LED &= ~_BV(PIN_LED); |
|
702 _delay_ms(1); |
|
703 PORT_LED |= _BV(PIN_LED); |
|
704 } |
|
705 |
|
706 static void |
|
707 long_delay(int ms) |
|
708 { |
|
709 int iter = ms / 100; |
|
710 |
|
711 for (int i = 0; i < iter; i++) |
|
712 { |
|
713 _delay_ms(100); |
|
714 } |
|
715 } |
|
716 |
|
717 ISR(BADISR_vect) |
|
718 { |
|
719 //uart_on(); |
|
720 printf_P(PSTR("Bad interrupt\n")); |
|
721 } |
|
722 |
|
723 int main(void) |
|
724 { |
|
725 setup_chip(); |
|
726 blink(); |
|
727 |
|
728 stdout = &mystdout; |
|
729 uart_on(); |
|
730 |
|
731 printf(PSTR("Started.\n")); |
|
732 |
2
|
733 set_pi_boot_normal(0); |
|
734 |
0
|
735 load_params(); |
|
736 |
|
737 setup_tick_counter(); |
|
738 |
|
739 sei(); |
|
740 |
1
|
741 // doesn't return |
|
742 do_comms(); |
0
|
743 |
|
744 return 0; /* never reached */ |
|
745 } |