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 |
7
|
54 // stores a value of clock_epoch combined with the remainder of TCNT1, |
0
|
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; |
7
|
174 t->rem = TCNT1; |
0
|
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); |
8
|
189 #ifdef SIM_DEBUG |
|
190 // systemclock/8 |
|
191 TCCR1B = _BV(CS11); |
|
192 #else |
7
|
193 // systemclock/64 |
|
194 TCCR1B = _BV(CS11) | _BV(CS10); |
8
|
195 #endif |
2
|
196 TCNT1 = 0; |
|
197 OCR1A = SLEEP_COMPARE; |
0
|
198 // interrupt |
2
|
199 TIMSK1 = _BV(OCIE1A); |
0
|
200 } |
|
201 |
|
202 static void |
|
203 uart_on() |
|
204 { |
|
205 // Power reduction register |
|
206 PRR &= ~_BV(PRUSART0); |
|
207 |
|
208 // All of this needs to be done each time after turning off the PRR |
|
209 // baud rate |
|
210 UBRR0H = (unsigned char)(UBRR >> 8); |
|
211 UBRR0L = (unsigned char)UBRR; |
|
212 // set 2x clock, improves accuracy of UBRR |
|
213 UCSR0A |= _BV(U2X0); |
|
214 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); |
|
215 //8N1 |
|
216 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); |
|
217 } |
|
218 |
|
219 static void |
|
220 uart_off() |
|
221 { |
|
222 // Turn off interrupts and disable tx/rx |
|
223 UCSR0B = 0; |
|
224 |
|
225 // Power reduction register |
|
226 PRR |= _BV(PRUSART0); |
|
227 } |
|
228 |
|
229 int |
|
230 uart_putchar(char c, FILE *stream) |
|
231 { |
|
232 // XXX could perhaps sleep in the loop for power. |
|
233 if (c == '\n') |
|
234 { |
|
235 loop_until_bit_is_set(UCSR0A, UDRE0); |
|
236 UDR0 = '\r'; |
|
237 } |
|
238 loop_until_bit_is_set(UCSR0A, UDRE0); |
|
239 UDR0 = c; |
|
240 if (c == '\r') |
|
241 { |
|
242 loop_until_bit_is_set(UCSR0A, UDRE0); |
|
243 UDR0 = '\n'; |
|
244 } |
|
245 return (unsigned char)c; |
|
246 } |
|
247 |
|
248 static void |
|
249 cmd_reset() |
|
250 { |
|
251 printf_P(PSTR("reset\n")); |
|
252 _delay_ms(100); |
|
253 cli(); // disable interrupts |
|
254 wdt_enable(WDTO_15MS); // enable watchdog |
|
255 while(1); // wait for watchdog to reset processor |
|
256 } |
|
257 |
2
|
258 static void |
|
259 cmd_newboot() |
|
260 { |
|
261 set_pi_boot_normal(1); |
|
262 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
|
263 { |
|
264 newboot_count = newboot_limit; |
|
265 } |
|
266 printf_P(PSTR("newboot for %d secs"), newboot_limit); |
|
267 } |
|
268 |
1
|
269 |
0
|
270 |
|
271 static void |
1
|
272 cmd_get_params() |
0
|
273 { |
2
|
274 uint32_t cur_watchdog_long, cur_watchdog_short, cur_newboot, cur_oneshot; |
1
|
275 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
|
276 { |
2
|
277 cur_watchdog_long = watchdog_long_count; |
|
278 cur_watchdog_short = watchdog_short_count; |
|
279 cur_newboot = newboot_count; |
|
280 cur_oneshot = oneshot_count; |
1
|
281 } |
|
282 |
2
|
283 printf_P(PSTR("limit (count) : watchdog_long %lu (%lu) watchdog_short %lu (%lu) newboot %lu (%lu) oneshot (%lu)\n"), |
1
|
284 watchdog_long_limit, |
2
|
285 cur_watchdog_long, |
1
|
286 watchdog_short_limit, |
2
|
287 cur_watchdog_short, |
1
|
288 newboot_limit, |
2
|
289 cur_newboot, |
|
290 cur_oneshot); |
0
|
291 } |
|
292 |
|
293 static void |
1
|
294 cmd_set_params(const char *params) |
0
|
295 { |
1
|
296 uint32_t new_watchdog_long_limit; |
|
297 uint32_t new_watchdog_short_limit; |
|
298 uint32_t new_newboot_limit; |
|
299 |
|
300 int ret = sscanf_P(params, PSTR("%lu %lu %lu"), |
|
301 &new_watchdog_long_limit, |
|
302 &new_watchdog_short_limit, |
|
303 &new_newboot_limit); |
|
304 |
|
305 |
|
306 if (ret != 3) |
0
|
307 { |
1
|
308 printf_P(PSTR("Bad values\n")); |
|
309 } |
|
310 else |
|
311 { |
|
312 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
|
313 { |
|
314 eeprom_write(new_watchdog_long_limit, watchdog_long_limit); |
|
315 eeprom_write(new_watchdog_short_limit, watchdog_short_limit); |
|
316 eeprom_write(new_newboot_limit, newboot_limit); |
|
317 uint16_t magic = EXPECT_MAGIC; |
|
318 eeprom_write(magic, magic); |
0
|
319 } |
1
|
320 printf_P(PSTR("set_params for next boot\n")); |
|
321 printf_P(PSTR("watchdog_long %lu watchdog_short %lu newboot %lu\n"), |
|
322 new_watchdog_long_limit, |
|
323 new_watchdog_short_limit, |
|
324 new_newboot_limit); |
|
325 |
|
326 } |
|
327 } |
0
|
328 |
1
|
329 uint8_t from_hex(char c) |
|
330 { |
|
331 if (c >= '0' && c <= '9') { |
|
332 return c-'0'; |
|
333 } |
|
334 if (c >= 'a' && c <= 'f') { |
|
335 return c-'a' + 0xa; |
|
336 } |
|
337 if (c >= 'A' && c <= 'F') { |
|
338 return c-'A' + 0xa; |
|
339 } |
|
340 return 0; |
|
341 } |
|
342 |
2
|
343 static void |
|
344 printhex_nibble(const unsigned char b, FILE *stream) |
|
345 { |
|
346 unsigned char c = b & 0x0f; |
|
347 if ( c > 9 ) { |
|
348 c += 'A'-10; |
|
349 } |
|
350 else { |
|
351 c += '0'; |
|
352 } |
|
353 fputc(c, stream); |
|
354 } |
|
355 |
|
356 void |
|
357 printhex_byte(const unsigned char b, FILE *stream) |
|
358 { |
|
359 printhex_nibble( b >> 4, stream); |
|
360 printhex_nibble( b, stream); |
|
361 } |
|
362 |
|
363 void |
|
364 printhex(uint8_t *id, uint8_t n, FILE *stream) |
|
365 { |
|
366 for (uint8_t i = 0; i < n; i++) |
|
367 { |
|
368 if (i > 0) |
|
369 { |
|
370 fputc(' ', stream); |
|
371 } |
|
372 printhex_byte(id[i], stream); |
|
373 } |
|
374 } |
|
375 |
|
376 static int8_t |
5
|
377 parse_key(const char *params, uint8_t *key_index, uint8_t *bytes, |
|
378 uint8_t bytes_len) |
2
|
379 { |
|
380 // "N HEXKEY" |
5
|
381 if (strlen(params) != bytes_len*2 + 2) { |
2
|
382 printf_P(PSTR("Wrong length key\n")); |
|
383 return -1; |
|
384 } |
|
385 |
|
386 if (params[1] != ' ') |
|
387 { |
|
388 printf_P(PSTR("Missing space\n")); |
|
389 return -1; |
|
390 } |
|
391 |
|
392 *key_index = from_hex(params[0]); |
|
393 if (*key_index >= NKEYS) |
|
394 { |
|
395 printf_P(PSTR("Bad key index %d, max %d\n"), *key_index, NKEYS); |
|
396 return -1; |
|
397 } |
|
398 |
5
|
399 for (int i = 0, p = 0; i < bytes_len; i++, p += 2) |
2
|
400 { |
|
401 bytes[i] = (from_hex(params[p+2]) << 4) | from_hex(params[p+3]); |
|
402 } |
|
403 return 0; |
|
404 } |
|
405 |
1
|
406 static void |
|
407 cmd_set_avr_key(const char *params) |
|
408 { |
2
|
409 uint8_t new_key[KEYLEN]; |
|
410 uint8_t key_index; |
5
|
411 if (parse_key(params, &key_index, new_key, sizeof(new_key)) != 0) |
2
|
412 { |
1
|
413 return; |
0
|
414 } |
2
|
415 memcpy(avr_keys[key_index], new_key, sizeof(new_key)); |
8
|
416 #ifndef SIM_DEBUG |
2
|
417 eeprom_write(avr_keys, avr_keys); |
8
|
418 #endif |
2
|
419 } |
|
420 |
|
421 static void |
|
422 cmd_hmac(const char *params) |
|
423 { |
7
|
424 uint8_t indata[HMACLEN]; |
|
425 uint8_t outdata[HMACLEN]; |
2
|
426 uint8_t key_index; |
7
|
427 if (parse_key(params, &key_index, indata, sizeof(indata)) != 0) |
2
|
428 { |
|
429 printf_P(PSTR("FAIL: Bad input\n")); |
5
|
430 return; |
2
|
431 } |
0
|
432 |
7
|
433 if (key_index % 2 != 0) |
5
|
434 { |
|
435 printf_P(PSTR("Only hmac with even keys\n")); |
|
436 return; |
|
437 } |
|
438 |
8
|
439 #ifndef SIM_DEBUG |
5
|
440 long_delay(200); |
8
|
441 #endif |
5
|
442 |
7
|
443 hmac_sha1(outdata, avr_keys[key_index], KEYLEN*8, indata, HMACLEN*8); |
2
|
444 |
|
445 printf_P(PSTR("HMAC: ")); |
7
|
446 printhex(outdata, HMACLEN, stdout); |
5
|
447 fputc('\n', stdout); |
|
448 } |
|
449 |
|
450 static void |
|
451 cmd_decrypt(const char *params) |
|
452 { |
|
453 uint8_t data[HMACLEN+AESLEN]; |
|
454 uint8_t output[HMACLEN]; |
|
455 uint8_t key_index; |
|
456 if (parse_key(params, &key_index, data, sizeof(data)) != 0) |
|
457 { |
|
458 printf_P(PSTR("FAIL: Bad input\n")); |
|
459 return; |
|
460 } |
|
461 |
7
|
462 if (key_index % 2 == 0) |
5
|
463 { |
|
464 printf_P(PSTR("Only decrypt with odd keys\n")); |
|
465 return; |
|
466 } |
|
467 |
8
|
468 #ifndef SIM_DEBUG |
5
|
469 long_delay(200); |
8
|
470 #endif |
7
|
471 |
5
|
472 // check the signature |
8
|
473 hmac_sha1(output, avr_keys[key_index+1], KEYLEN*8, &data[HMACLEN], AESLEN*8); |
5
|
474 |
|
475 if (memcmp(output, data, HMACLEN) != 0) { |
|
476 printf_P(PSTR("FAIL: hmac mismatch\n")); |
|
477 } |
|
478 |
|
479 uint8_t expkey[AES_EXPKEY_SIZE]; |
|
480 ExpandKey(avr_keys[key_index], expkey); |
|
481 Decrypt(&data[HMACLEN], expkey, output); |
|
482 |
|
483 printf_P(PSTR("DECRYPTED: ")); |
|
484 printhex(output, AESLEN, stdout); |
2
|
485 fputc('\n', stdout); |
|
486 } |
|
487 |
|
488 static void |
|
489 cmd_oneshot_reboot(const char *params) |
|
490 { |
|
491 uint32_t new_delay = strtoul(params, NULL, 10); |
|
492 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
1
|
493 { |
2
|
494 oneshot_count = new_delay; |
1
|
495 } |
2
|
496 printf_P(PSTR("oneshot delay %lu\n"), new_delay); |
0
|
497 } |
|
498 |
|
499 static void |
|
500 load_params() |
|
501 { |
|
502 uint16_t magic; |
|
503 eeprom_read(magic, magic); |
|
504 if (magic == EXPECT_MAGIC) |
|
505 { |
1
|
506 eeprom_read(watchdog_long_limit, watchdog_long_limit); |
|
507 eeprom_read(watchdog_short_limit, watchdog_short_limit); |
2
|
508 eeprom_read(newboot_limit, newboot_limit); |
1
|
509 } |
2
|
510 |
|
511 eeprom_read(avr_keys, avr_keys); |
0
|
512 } |
|
513 |
2
|
514 static void |
|
515 cmd_vcc() |
0
|
516 { |
2
|
517 uint16_t vcc = adc_vcc(); |
|
518 printf_P("vcc: %u mV\n", vcc); |
0
|
519 } |
|
520 |
|
521 static void |
|
522 read_handler() |
|
523 { |
2
|
524 if (strcmp_P(readbuf, PSTR("get_params")) == 0) |
0
|
525 { |
|
526 cmd_get_params(); |
|
527 } |
|
528 else if (strncmp_P(readbuf, PSTR("set_params "), 11) == 0) |
|
529 { |
|
530 cmd_set_params(&readbuf[11]); |
|
531 } |
2
|
532 else if (strncmp_P(readbuf, PSTR("set_key "), 8) == 0) |
0
|
533 { |
2
|
534 cmd_set_avr_key(&readbuf[8]); |
0
|
535 } |
2
|
536 else if (strncmp_P(readbuf, PSTR("oneshot "), 8) == 0) |
0
|
537 { |
2
|
538 cmd_oneshot_reboot(&readbuf[8]); |
0
|
539 } |
2
|
540 else if (strncmp_P(readbuf, PSTR("hmac "), 5) == 0) |
0
|
541 { |
2
|
542 cmd_hmac(&readbuf[5]); |
0
|
543 } |
5
|
544 else if (strncmp_P(readbuf, PSTR("decrypt "), 8) == 0) |
|
545 { |
|
546 cmd_hmac(&readbuf[8]); |
|
547 } |
2
|
548 else if (strcmp_P(readbuf, PSTR("vcc")) == 0) |
0
|
549 { |
2
|
550 cmd_vcc(); |
0
|
551 } |
|
552 else if (strcmp_P(readbuf, PSTR("reset")) == 0) |
|
553 { |
|
554 cmd_reset(); |
|
555 } |
4
|
556 else if (strcmp_P(readbuf, PSTR("newboot")) == 0) |
|
557 { |
|
558 cmd_newboot(); |
|
559 } |
0
|
560 else |
|
561 { |
|
562 printf_P(PSTR("Bad command '%s'\n"), readbuf); |
|
563 } |
|
564 } |
|
565 |
|
566 ISR(INT0_vect) |
|
567 { |
|
568 blink(); |
|
569 _delay_ms(100); |
|
570 blink(); |
|
571 } |
|
572 |
|
573 ISR(USART_RX_vect) |
|
574 { |
|
575 char c = UDR0; |
|
576 #ifdef HAVE_UART_ECHO |
|
577 uart_putchar(c, NULL); |
|
578 #endif |
|
579 if (c == '\r' || c == '\n') |
|
580 { |
|
581 if (readpos > 0) |
|
582 { |
|
583 readbuf[readpos] = '\0'; |
|
584 have_cmd = 1; |
|
585 readpos = 0; |
|
586 } |
|
587 } |
|
588 else |
|
589 { |
|
590 readbuf[readpos] = c; |
|
591 readpos++; |
|
592 if (readpos >= sizeof(readbuf)) |
|
593 { |
|
594 readpos = 0; |
|
595 } |
|
596 } |
|
597 } |
|
598 |
2
|
599 ISR(TIMER1_COMPA_vect) |
0
|
600 { |
2
|
601 TCNT1 = 0; |
0
|
602 |
|
603 clock_epoch += TICK; |
|
604 |
1
|
605 // watchdogs count up, continuous |
|
606 if (watchdog_long_limit > 0) { |
2
|
607 watchdog_long_count += TICK; |
1
|
608 if (watchdog_long_count >= watchdog_long_limit) |
|
609 { |
|
610 watchdog_long_count = 0; |
|
611 watchdog_long_hit = 1; |
|
612 } |
0
|
613 } |
|
614 |
1
|
615 if (watchdog_short_limit > 0) { |
2
|
616 watchdog_short_count += TICK; |
1
|
617 if (watchdog_short_count >= watchdog_short_limit) |
|
618 { |
|
619 watchdog_short_count = 0; |
|
620 watchdog_short_hit = 1; |
|
621 } |
0
|
622 } |
|
623 |
2
|
624 // newboot counts down |
1
|
625 if (newboot_count > 0) |
0
|
626 { |
2
|
627 newboot_count-=TICK; |
|
628 if (newboot_count <= 0) |
1
|
629 { |
|
630 newboot_hit = 1; |
2
|
631 newboot_count = 0; |
1
|
632 } |
0
|
633 } |
1
|
634 |
2
|
635 if (oneshot_count > 0) |
|
636 { |
|
637 oneshot_count-=TICK; |
|
638 if (oneshot_count <= 0) |
|
639 { |
|
640 oneshot_hit = 1; |
|
641 oneshot_count = 0; |
|
642 } |
|
643 } |
0
|
644 } |
|
645 |
|
646 static void |
|
647 idle_sleep() |
|
648 { |
|
649 set_sleep_mode(SLEEP_MODE_IDLE); |
|
650 sleep_mode(); |
|
651 } |
|
652 |
|
653 static uint16_t |
|
654 adc_vcc() |
|
655 { |
|
656 PRR &= ~_BV(PRADC); |
|
657 |
|
658 // /16 prescaler |
|
659 ADCSRA = _BV(ADEN) | _BV(ADPS2); |
|
660 |
|
661 // set to measure 1.1 reference |
|
662 ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); |
|
663 // average a number of samples |
|
664 uint16_t sum = 0; |
|
665 uint8_t num = 0; |
|
666 for (uint8_t n = 0; n < 20; n++) |
|
667 { |
|
668 ADCSRA |= _BV(ADSC); |
|
669 loop_until_bit_is_clear(ADCSRA, ADSC); |
|
670 |
|
671 uint8_t low_11 = ADCL; |
|
672 uint8_t high_11 = ADCH; |
|
673 uint16_t val = low_11 + (high_11 << 8); |
|
674 |
|
675 if (n >= 4) |
|
676 { |
|
677 sum += val; |
|
678 num++; |
|
679 } |
|
680 } |
|
681 ADCSRA = 0; |
|
682 PRR |= _BV(PRADC); |
|
683 |
|
684 //float res_volts = 1.1 * 1024 * num / sum; |
|
685 //return 1000 * res_volts; |
|
686 return ((uint32_t)1100*1024*num) / sum; |
|
687 } |
|
688 |
|
689 static void |
2
|
690 reboot_pi() |
|
691 { |
|
692 // pull it low for 30ms |
|
693 PORT_PI_RESET &= ~_BV(PIN_PI_RESET); |
|
694 DDR_PI_RESET |= _BV(PIN_PI_RESET); |
|
695 _delay_ms(30); |
|
696 DDR_PI_RESET &= ~_BV(PIN_PI_RESET); |
|
697 } |
|
698 |
|
699 static void |
|
700 set_pi_boot_normal(uint8_t normal) |
|
701 { |
|
702 PORT_PI_BOOT &= ~_BV(PIN_PI_BOOT); |
|
703 if (normal) |
|
704 { |
|
705 // tristate |
|
706 DDR_PI_BOOT &= ~_BV(PIN_PI_BOOT); |
|
707 } |
|
708 else |
|
709 { |
|
710 // pull it low |
|
711 DDR_PI_RESET |= _BV(PIN_PI_BOOT); |
|
712 |
|
713 } |
|
714 } |
|
715 |
|
716 static void |
|
717 check_flags() |
|
718 { |
|
719 if (watchdog_long_hit |
|
720 || watchdog_short_hit |
|
721 || oneshot_hit) |
|
722 { |
|
723 reboot_pi(); |
|
724 } |
|
725 |
|
726 if (newboot_hit) { |
|
727 set_pi_boot_normal(0); |
|
728 } |
|
729 |
|
730 watchdog_long_hit = 0; |
|
731 watchdog_short_hit = 0; |
|
732 newboot_hit = 0; |
|
733 oneshot_hit = 0; |
|
734 } |
|
735 |
|
736 static void |
0
|
737 do_comms() |
|
738 { |
|
739 // avoid receiving rubbish, perhaps |
|
740 uart_on(); |
|
741 |
|
742 // write sd card here? same 3.3v regulator... |
|
743 |
1
|
744 while (1) |
0
|
745 { |
1
|
746 wdt_reset(); |
2
|
747 |
|
748 check_flags(); |
|
749 |
0
|
750 if (have_cmd) |
|
751 { |
|
752 have_cmd = 0; |
|
753 read_handler(); |
|
754 continue; |
|
755 } |
|
756 |
|
757 // wait for commands from the master |
|
758 idle_sleep(); |
|
759 } |
|
760 } |
|
761 |
|
762 static void |
|
763 blink() |
|
764 { |
|
765 PORT_LED &= ~_BV(PIN_LED); |
|
766 _delay_ms(1); |
|
767 PORT_LED |= _BV(PIN_LED); |
|
768 } |
|
769 |
|
770 static void |
|
771 long_delay(int ms) |
|
772 { |
|
773 int iter = ms / 100; |
|
774 |
|
775 for (int i = 0; i < iter; i++) |
|
776 { |
|
777 _delay_ms(100); |
|
778 } |
|
779 } |
|
780 |
|
781 ISR(BADISR_vect) |
|
782 { |
|
783 //uart_on(); |
|
784 printf_P(PSTR("Bad interrupt\n")); |
|
785 } |
|
786 |
|
787 int main(void) |
|
788 { |
|
789 setup_chip(); |
|
790 blink(); |
|
791 |
|
792 stdout = &mystdout; |
|
793 uart_on(); |
|
794 |
5
|
795 printf(PSTR("Pi Watchdog\nMatt Johnston [email protected]")); |
0
|
796 |
2
|
797 set_pi_boot_normal(0); |
|
798 |
0
|
799 load_params(); |
|
800 |
|
801 setup_tick_counter(); |
|
802 |
|
803 sei(); |
|
804 |
8
|
805 #if 0 |
|
806 // encryption test |
|
807 cmd_set_avr_key("1 6161626263636464656566666767686800000000"); |
|
808 cmd_set_avr_key("2 7979757569696f6f646465656666717164646969"); |
|
809 cmd_decrypt("1 ecd858ee07a8e16575723513d2d072a7565865e40ba302059bfc650d4491268448102119"); |
|
810 #endif |
|
811 |
1
|
812 // doesn't return |
|
813 do_comms(); |
0
|
814 |
|
815 return 0; /* never reached */ |
|
816 } |