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 |
4
|
38 #define PIN_PI_BOOT PD5 |
2
|
39 |
|
40 // XXX |
|
41 #define PORT_PI_RESET PORTD |
|
42 #define DDR_PI_RESET DDRD |
|
43 #define PIN_PI_RESET PD6 |
|
44 |
|
45 |
4
|
46 #define PORT_LED PORTD |
|
47 #define DDR_LED DDRD |
|
48 #define PIN_LED PD7 |
0
|
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 } |
4
|
493 else if (strcmp_P(readbuf, PSTR("newboot")) == 0) |
|
494 { |
|
495 cmd_newboot(); |
|
496 } |
0
|
497 else |
|
498 { |
|
499 printf_P(PSTR("Bad command '%s'\n"), readbuf); |
|
500 } |
|
501 } |
|
502 |
|
503 ISR(INT0_vect) |
|
504 { |
|
505 blink(); |
|
506 _delay_ms(100); |
|
507 blink(); |
|
508 } |
|
509 |
|
510 |
|
511 ISR(USART_RX_vect) |
|
512 { |
|
513 char c = UDR0; |
|
514 #ifdef HAVE_UART_ECHO |
|
515 uart_putchar(c, NULL); |
|
516 #endif |
|
517 if (c == '\r' || c == '\n') |
|
518 { |
|
519 if (readpos > 0) |
|
520 { |
|
521 readbuf[readpos] = '\0'; |
|
522 have_cmd = 1; |
|
523 readpos = 0; |
|
524 } |
|
525 } |
|
526 else |
|
527 { |
|
528 readbuf[readpos] = c; |
|
529 readpos++; |
|
530 if (readpos >= sizeof(readbuf)) |
|
531 { |
|
532 readpos = 0; |
|
533 } |
|
534 } |
|
535 } |
|
536 |
2
|
537 ISR(TIMER1_COMPA_vect) |
0
|
538 { |
2
|
539 TCNT1 = 0; |
0
|
540 |
|
541 clock_epoch += TICK; |
|
542 |
1
|
543 // watchdogs count up, continuous |
|
544 if (watchdog_long_limit > 0) { |
2
|
545 watchdog_long_count += TICK; |
1
|
546 if (watchdog_long_count >= watchdog_long_limit) |
|
547 { |
|
548 watchdog_long_count = 0; |
|
549 watchdog_long_hit = 1; |
|
550 } |
0
|
551 } |
|
552 |
1
|
553 if (watchdog_short_limit > 0) { |
2
|
554 watchdog_short_count += TICK; |
1
|
555 if (watchdog_short_count >= watchdog_short_limit) |
|
556 { |
|
557 watchdog_short_count = 0; |
|
558 watchdog_short_hit = 1; |
|
559 } |
0
|
560 } |
|
561 |
2
|
562 // newboot counts down |
1
|
563 if (newboot_count > 0) |
0
|
564 { |
2
|
565 newboot_count-=TICK; |
|
566 if (newboot_count <= 0) |
1
|
567 { |
|
568 newboot_hit = 1; |
2
|
569 newboot_count = 0; |
1
|
570 } |
0
|
571 } |
1
|
572 |
2
|
573 if (oneshot_count > 0) |
|
574 { |
|
575 oneshot_count-=TICK; |
|
576 if (oneshot_count <= 0) |
|
577 { |
|
578 oneshot_hit = 1; |
|
579 oneshot_count = 0; |
|
580 } |
|
581 } |
0
|
582 } |
|
583 |
|
584 static void |
|
585 idle_sleep() |
|
586 { |
|
587 set_sleep_mode(SLEEP_MODE_IDLE); |
|
588 sleep_mode(); |
|
589 } |
|
590 |
|
591 static uint16_t |
|
592 adc_vcc() |
|
593 { |
|
594 PRR &= ~_BV(PRADC); |
|
595 |
|
596 // /16 prescaler |
|
597 ADCSRA = _BV(ADEN) | _BV(ADPS2); |
|
598 |
|
599 // set to measure 1.1 reference |
|
600 ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); |
|
601 // average a number of samples |
|
602 uint16_t sum = 0; |
|
603 uint8_t num = 0; |
|
604 for (uint8_t n = 0; n < 20; n++) |
|
605 { |
|
606 ADCSRA |= _BV(ADSC); |
|
607 loop_until_bit_is_clear(ADCSRA, ADSC); |
|
608 |
|
609 uint8_t low_11 = ADCL; |
|
610 uint8_t high_11 = ADCH; |
|
611 uint16_t val = low_11 + (high_11 << 8); |
|
612 |
|
613 if (n >= 4) |
|
614 { |
|
615 sum += val; |
|
616 num++; |
|
617 } |
|
618 } |
|
619 ADCSRA = 0; |
|
620 PRR |= _BV(PRADC); |
|
621 |
|
622 //float res_volts = 1.1 * 1024 * num / sum; |
|
623 //return 1000 * res_volts; |
|
624 return ((uint32_t)1100*1024*num) / sum; |
|
625 } |
|
626 |
|
627 static void |
2
|
628 reboot_pi() |
|
629 { |
|
630 // pull it low for 30ms |
|
631 PORT_PI_RESET &= ~_BV(PIN_PI_RESET); |
|
632 DDR_PI_RESET |= _BV(PIN_PI_RESET); |
|
633 _delay_ms(30); |
|
634 DDR_PI_RESET &= ~_BV(PIN_PI_RESET); |
|
635 } |
|
636 |
|
637 static void |
|
638 set_pi_boot_normal(uint8_t normal) |
|
639 { |
|
640 PORT_PI_BOOT &= ~_BV(PIN_PI_BOOT); |
|
641 if (normal) |
|
642 { |
|
643 // tristate |
|
644 DDR_PI_BOOT &= ~_BV(PIN_PI_BOOT); |
|
645 } |
|
646 else |
|
647 { |
|
648 // pull it low |
|
649 DDR_PI_RESET |= _BV(PIN_PI_BOOT); |
|
650 |
|
651 } |
|
652 } |
|
653 |
|
654 static void |
|
655 check_flags() |
|
656 { |
|
657 if (watchdog_long_hit |
|
658 || watchdog_short_hit |
|
659 || oneshot_hit) |
|
660 { |
|
661 reboot_pi(); |
|
662 } |
|
663 |
|
664 if (newboot_hit) { |
|
665 set_pi_boot_normal(0); |
|
666 } |
|
667 |
|
668 watchdog_long_hit = 0; |
|
669 watchdog_short_hit = 0; |
|
670 newboot_hit = 0; |
|
671 oneshot_hit = 0; |
|
672 } |
|
673 |
|
674 static void |
0
|
675 do_comms() |
|
676 { |
|
677 // avoid receiving rubbish, perhaps |
|
678 uart_on(); |
|
679 |
|
680 // write sd card here? same 3.3v regulator... |
|
681 |
1
|
682 while (1) |
0
|
683 { |
1
|
684 wdt_reset(); |
2
|
685 |
|
686 check_flags(); |
|
687 |
0
|
688 if (have_cmd) |
|
689 { |
|
690 have_cmd = 0; |
|
691 read_handler(); |
|
692 continue; |
|
693 } |
|
694 |
|
695 // wait for commands from the master |
|
696 idle_sleep(); |
|
697 } |
|
698 } |
|
699 |
|
700 static void |
|
701 blink() |
|
702 { |
|
703 PORT_LED &= ~_BV(PIN_LED); |
|
704 _delay_ms(1); |
|
705 PORT_LED |= _BV(PIN_LED); |
|
706 } |
|
707 |
|
708 static void |
|
709 long_delay(int ms) |
|
710 { |
|
711 int iter = ms / 100; |
|
712 |
|
713 for (int i = 0; i < iter; i++) |
|
714 { |
|
715 _delay_ms(100); |
|
716 } |
|
717 } |
|
718 |
|
719 ISR(BADISR_vect) |
|
720 { |
|
721 //uart_on(); |
|
722 printf_P(PSTR("Bad interrupt\n")); |
|
723 } |
|
724 |
|
725 int main(void) |
|
726 { |
|
727 setup_chip(); |
|
728 blink(); |
|
729 |
|
730 stdout = &mystdout; |
|
731 uart_on(); |
|
732 |
|
733 printf(PSTR("Started.\n")); |
|
734 |
2
|
735 set_pi_boot_normal(0); |
|
736 |
0
|
737 load_params(); |
|
738 |
|
739 setup_tick_counter(); |
|
740 |
|
741 sei(); |
|
742 |
1
|
743 // doesn't return |
|
744 do_comms(); |
0
|
745 |
|
746 return 0; /* never reached */ |
|
747 } |