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