Mercurial > pihelp
annotate main.c @ 49:12236bceb6b0 default tip
snapshot export of the schematic
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 25 Jun 2014 21:29:52 +0800 |
parents | 747695bd4e0d |
children |
rev | line source |
---|---|
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 | |
45 | 16 #include "buildid.h" |
17 | |
2 | 18 #include "hmac-sha1.h" |
5 | 19 #include "aes.h" |
2 | 20 |
43 | 21 #include "fat.h" |
22 #include "fat_config.h" | |
23 #include "partition.h" | |
24 #include "sd_raw.h" | |
25 #include "sd_raw_config.h" | |
26 | |
1 | 27 //#include "simple_ds18b20.h" |
28 //#include "onewire.h" | |
0 | 29 |
20 | 30 // not set via bootloader |
31 //LOCKBITS = (LB_MODE_3 & BLB0_MODE_4 & BLB1_MODE_4); | |
5 | 32 |
0 | 33 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) |
34 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) | |
35 | |
36 // TICK should be 8 or less (8 untested). all timers need | |
37 // to be a multiple. | |
38 | |
1 | 39 #define TICK 1 |
47 | 40 #define SLEEP_COMPARE (F_CPU/1024) // == 10800 for 11059200hz |
5 | 41 #define NKEYS 10 |
42 #define HMACLEN 20 | |
43 #define AESLEN 16 | |
44 #define KEYLEN HMACLEN | |
45 | 45 // 64 bits is enough for a realtime challenge |
46 #define CHALLEN 8 | |
47 | 47 #define HMAC_PRINT_LEN 3 |
1 | 48 |
27 | 49 #define BAUD 115200 |
20 | 50 #define UBRR ((F_CPU)/(16*(BAUD))-1) |
0 | 51 |
2 | 52 #define PORT_PI_BOOT PORTD |
53 #define DDR_PI_BOOT DDRD | |
4 | 54 #define PIN_PI_BOOT PD5 |
2 | 55 |
56 #define PORT_PI_RESET PORTD | |
57 #define DDR_PI_RESET DDRD | |
58 #define PIN_PI_RESET PD6 | |
59 | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
60 #define PORT_PI_WARNING PORTD |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
61 #define DDR_PI_WARNING DDRD |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
62 #define PIN_PI_WARNING PD7 |
0 | 63 |
64 // #define HAVE_UART_ECHO | |
65 | |
7 | 66 // stores a value of clock_epoch combined with the remainder of TCNT1, |
0 | 67 // for 1/32 second accuracy |
68 struct epoch_ticks | |
69 { | |
70 uint32_t ticks; | |
71 // remainder | |
20 | 72 uint16_t rem; |
0 | 73 }; |
74 | |
16 | 75 #define WATCHDOG_LONG_MIN (60L*40) // 40 mins |
76 #define WATCHDOG_LONG_MAX (60L*60*72) // 72 hours | |
77 #define WATCHDOG_LONG_DEFAULT (60L*60*6) // 6 hours | |
78 | |
79 #define WATCHDOG_SHORT_MIN (60L*15) // 15 mins | |
80 | |
81 #define NEWBOOT_DEFAULT (60*10) // 10 minutes | |
82 #define NEWBOOT_MIN (60*2) // 2 minutes | |
83 #define NEWBOOT_MAX (60*30) // 30 mins | |
84 | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
85 #define WARNING_TIME 10 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
86 |
1 | 87 // eeprom-settable parameters, default values defined here. |
88 // all timeouts should be a multiple of TICK | |
16 | 89 static uint32_t watchdog_long_limit = WATCHDOG_LONG_DEFAULT; |
1 | 90 static uint32_t watchdog_short_limit = 0; |
16 | 91 static uint32_t newboot_limit = NEWBOOT_DEFAULT; |
0 | 92 |
47 | 93 // key index 0 is special, used internally for bootid |
2 | 94 static uint8_t avr_keys[NKEYS][KEYLEN] = {{0}}; |
0 | 95 |
96 // ---- Atomic guards required accessing these variables | |
97 // clock_epoch in seconds | |
98 static uint32_t clock_epoch; | |
1 | 99 // watchdog counts up |
100 static uint32_t watchdog_long_count; | |
101 static uint32_t watchdog_short_count; | |
2 | 102 // newboot counts down |
1 | 103 static uint32_t newboot_count; |
2 | 104 // oneshot counts down |
105 static uint32_t oneshot_count; | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
106 // countdown after the warning. |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
107 static uint8_t reboot_count; |
27 | 108 // set by adc completion interrupt |
109 static uint8_t adc_done; | |
2 | 110 |
0 | 111 // ---- End atomic guards required |
112 | |
113 // boolean flags | |
1 | 114 static uint8_t watchdog_long_hit; |
115 static uint8_t watchdog_short_hit; | |
116 static uint8_t newboot_hit; | |
2 | 117 static uint8_t oneshot_hit; |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
118 static uint8_t reboot_hit; |
0 | 119 |
22 | 120 // informational for status messages |
121 static uint8_t boot_normal_status; | |
122 | |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
123 // flips between 0 and 1 each watchdog_long_hit, so eventually a |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
124 // working firmware should boot. set back to 0 for each 'alive' |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
125 // command |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
126 static uint8_t long_reboot_mode = 0; |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
127 |
45 | 128 static uint8_t boot_id[HMACLEN]; |
129 | |
0 | 130 static uint8_t readpos; |
20 | 131 static char readbuf[150]; |
0 | 132 static uint8_t have_cmd; |
133 | |
134 int uart_putchar(char c, FILE *stream); | |
22 | 135 static void long_delay(uint16_t ms); |
0 | 136 static void blink(); |
137 static uint16_t adc_vcc(); | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
138 static uint16_t adc_5v(uint16_t vcc); |
20 | 139 static uint16_t adc_temp(); |
2 | 140 static void set_pi_boot_normal(uint8_t normal); |
45 | 141 static void adc_random(uint8_t admux, |
142 uint8_t *out, uint16_t num, uint32_t *tries); | |
47 | 143 void printhex(uint8_t *id, uint8_t n, FILE *stream); |
144 static void cmd_rebootpi(); | |
17 | 145 |
47 | 146 uint8_t uart_hmac_print; |
147 uint32_t uart_hmac_counter; | |
148 hmac_sha1_ctx_t uart_hmac_ctx; | |
149 uint8_t uart_hmac_buf[SHA1_BLOCK_BITS/8]; | |
150 uint8_t uart_hmac_index; | |
0 | 151 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, |
152 _FDEV_SETUP_WRITE); | |
47 | 153 static FILE _rawstdout = FDEV_SETUP_STREAM(uart_putchar, NULL, |
154 _FDEV_SETUP_WRITE); | |
155 static FILE *raw_stdout = &_rawstdout; | |
0 | 156 |
157 // thanks to http://projectgus.com/2010/07/eeprom-access-with-arduino/ | |
158 #define eeprom_read_to(dst_p, eeprom_field, dst_size) eeprom_read_block((dst_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (dst_size)) | |
159 #define eeprom_read(dst, eeprom_field) eeprom_read_to((&dst), eeprom_field, sizeof(dst)) | |
160 #define eeprom_write_from(src_p, eeprom_field, src_size) eeprom_write_block((src_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (src_size)) | |
161 #define eeprom_write(src, eeprom_field) { eeprom_write_from(&src, eeprom_field, sizeof(src)); } | |
162 | |
1 | 163 #define EXPECT_MAGIC 0xdf83 |
0 | 164 |
165 struct __attribute__ ((__packed__)) __eeprom_data { | |
1 | 166 uint32_t watchdog_long_limit; |
167 uint32_t watchdog_short_limit; | |
168 uint32_t newboot_limit; | |
0 | 169 |
2 | 170 uint8_t avr_keys[NKEYS][KEYLEN]; |
0 | 171 |
172 uint16_t magic; | |
173 }; | |
174 | |
175 // Very first setup | |
176 static void | |
177 setup_chip() | |
178 { | |
179 cli(); | |
180 | |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
181 // set to 8 seconds, in case sha1 is slow etc. |
1 | 182 wdt_enable(WDTO_8S); |
183 | |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
184 // Set scaler to /1, -> clock to 8mhz |
0 | 185 CLKPR = _BV(CLKPCE); |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
186 CLKPR = 0; |
0 | 187 |
188 // enable pullups | |
1 | 189 // XXX matt pihelp |
5 | 190 //PORTB = 0xff; // XXX change when using SPI |
191 //PORTD = 0xff; | |
192 //PORTC = 0xff; | |
0 | 193 |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
194 DDR_PI_WARNING |= _BV(PIN_PI_WARNING); |
0 | 195 |
41 | 196 DDR_PI_RESET &= ~_BV(PIN_PI_RESET); |
197 | |
5 | 198 #if 0 |
0 | 199 // set pullup |
200 PORTD |= _BV(PD2); | |
201 // INT0 setup | |
202 EICRA = (1<<ISC01); // falling edge - data sheet says it won't work? | |
203 EIMSK = _BV(INT0); | |
5 | 204 #endif |
0 | 205 |
206 // comparator disable | |
207 ACSR = _BV(ACD); | |
208 | |
209 // disable adc pin input buffers | |
210 DIDR0 = 0x3F; // acd0-adc5 | |
211 DIDR1 = (1<<AIN1D)|(1<<AIN0D); // ain0/ain1 | |
212 | |
213 sei(); | |
214 } | |
215 | |
216 static void | |
217 get_epoch_ticks(struct epoch_ticks *t) | |
218 { | |
219 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
220 { | |
221 t->ticks = clock_epoch; | |
7 | 222 t->rem = TCNT1; |
0 | 223 } |
224 } | |
225 | |
226 static void | |
227 setup_tick_counter() | |
228 { | |
2 | 229 // set up counter1 |
230 | |
0 | 231 // set up counter2. |
232 // COM21 COM20 Set OC2 on Compare Match (p116) | |
233 // WGM21 Clear counter on compare | |
234 //TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21); | |
235 // toggle on match | |
2 | 236 TCCR1A = _BV(COM1A0); |
8 | 237 #ifdef SIM_DEBUG |
238 // systemclock/8 | |
239 TCCR1B = _BV(CS11); | |
240 #else | |
47 | 241 // systemclock/1024 |
242 TCCR1B = _BV(CS12) | _BV(CS10); | |
8 | 243 #endif |
2 | 244 TCNT1 = 0; |
245 OCR1A = SLEEP_COMPARE; | |
0 | 246 // interrupt |
2 | 247 TIMSK1 = _BV(OCIE1A); |
0 | 248 } |
249 | |
250 static void | |
251 uart_on() | |
252 { | |
253 // Power reduction register | |
254 PRR &= ~_BV(PRUSART0); | |
255 | |
256 // All of this needs to be done each time after turning off the PRR | |
257 // baud rate | |
258 UBRR0H = (unsigned char)(UBRR >> 8); | |
259 UBRR0L = (unsigned char)UBRR; | |
260 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); | |
261 //8N1 | |
262 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); | |
263 } | |
264 | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
265 #ifdef SIM_DEBUG |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
266 static char sim_out[140]; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
267 static uint8_t sim_idx = 0; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
268 static uint8_t last_sim_idx = 0; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
269 #endif |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
270 |
47 | 271 static void |
272 reset_uart_hmac(void) | |
273 { | |
274 uart_hmac_counter++; | |
275 uint8_t key[KEYLEN + sizeof(boot_id) + sizeof(uart_hmac_counter)]; | |
276 uint8_t *k = key; | |
277 memcpy(k, avr_keys[0], KEYLEN); | |
278 k += KEYLEN; | |
279 memcpy(k, boot_id, sizeof(boot_id)); | |
280 k += sizeof(boot_id); | |
281 memcpy(k, &uart_hmac_counter, sizeof(uart_hmac_counter)); | |
282 | |
283 hmac_sha1_init(&uart_hmac_ctx, key, 8*sizeof(key)); | |
284 uart_hmac_index = 0; | |
285 } | |
286 | |
287 static void | |
288 uart_hmac(char c) | |
289 { | |
290 if (c == '\r' || c == '\n') | |
291 { | |
292 hmac_sha1_lastBlock(&uart_hmac_ctx, uart_hmac_buf, 8*uart_hmac_index); | |
293 uint8_t hmac_out[HMACLEN]; | |
294 hmac_sha1_final(hmac_out, &uart_hmac_ctx); | |
295 | |
296 if (uart_hmac_print) | |
297 { | |
298 fputc(' ', raw_stdout); | |
299 printhex((uint8_t*)&uart_hmac_counter, sizeof(uart_hmac_counter), raw_stdout); | |
300 fputc(' ', raw_stdout); | |
301 printhex(hmac_out, HMAC_PRINT_LEN, raw_stdout); | |
302 } | |
303 | |
304 reset_uart_hmac(); | |
305 | |
306 return; | |
307 } | |
308 | |
309 if (uart_hmac_index == sizeof(uart_hmac_buf)) | |
310 { | |
311 hmac_sha1_nextBlock(&uart_hmac_ctx, uart_hmac_buf); | |
312 uart_hmac_index = 0; | |
313 } | |
314 | |
315 uart_hmac_buf[uart_hmac_index] = c; | |
316 uart_hmac_index++; | |
317 } | |
318 | |
319 static void | |
320 cmd_printhmac(const char *arg) | |
321 { | |
322 uart_hmac_print = (strtoul(arg, NULL, 10) != 0); | |
323 if (uart_hmac_print) | |
324 { | |
325 printf_P(PSTR("hmac printing on\n")); | |
326 } | |
327 else | |
328 { | |
329 printf_P(PSTR("hmac printing off\n")); | |
330 } | |
331 } | |
332 | |
0 | 333 int |
334 uart_putchar(char c, FILE *stream) | |
335 { | |
47 | 336 if (stream != raw_stdout) |
337 { | |
338 uart_hmac(c); | |
339 } | |
0 | 340 // XXX could perhaps sleep in the loop for power. |
341 if (c == '\n') | |
342 { | |
343 loop_until_bit_is_set(UCSR0A, UDRE0); | |
344 UDR0 = '\r'; | |
345 } | |
346 loop_until_bit_is_set(UCSR0A, UDRE0); | |
347 UDR0 = c; | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
348 #ifdef SIM_DEBUG |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
349 sim_out[sim_idx] = c; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
350 sim_idx++; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
351 sim_idx %= sizeof(sim_out); |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
352 #endif |
0 | 353 if (c == '\r') |
354 { | |
355 loop_until_bit_is_set(UCSR0A, UDRE0); | |
356 UDR0 = '\n'; | |
357 } | |
358 return (unsigned char)c; | |
359 } | |
360 | |
45 | 361 #if 0 |
362 | |
43 | 363 uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry) |
364 { | |
365 while(fat_read_dir(dd, dir_entry)) | |
366 { | |
367 if(strcmp(dir_entry->long_name, name) == 0) | |
368 { | |
369 fat_reset_dir(dd); | |
370 return 1; | |
371 } | |
372 } | |
373 | |
374 return 0; | |
375 } | |
376 | |
377 struct fat_file_struct* | |
378 open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name) | |
379 { | |
380 struct fat_dir_entry_struct file_entry; | |
381 if(!find_file_in_dir(fs, dd, name, &file_entry)) | |
382 return 0; | |
383 | |
384 return fat_open_file(fs, &file_entry); | |
385 } | |
386 | |
387 static uint32_t sd_serial = 0; | |
388 static char conf_start[30]; | |
389 | |
390 static void | |
391 hmac_file(const char* fn) | |
392 { | |
393 uint8_t res; | |
394 | |
395 struct sd_raw_info disk_info; | |
396 sd_raw_get_info(&disk_info); | |
397 sd_serial = disk_info.serial; | |
398 printf_P(PSTR("serial %lx\n"), sd_serial); | |
399 | |
400 struct partition_struct* partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, sd_raw_write_interval, 1); | |
401 | |
402 if (!partition) | |
403 { | |
404 sprintf(conf_start, "part"); | |
405 return; | |
406 } | |
407 | |
408 struct fat_fs_struct* fs = fat_open(partition); | |
409 if (!fs) | |
410 { | |
411 sprintf(conf_start, "bad fs"); | |
412 return; | |
413 } | |
414 struct fat_dir_entry_struct directory; | |
415 res = fat_get_dir_entry_of_path(fs, "/", &directory); | |
416 if (!res) | |
417 { | |
418 sprintf(conf_start, "bad direc"); | |
419 return; | |
420 } | |
421 | |
422 struct fat_dir_struct* dd = fat_open_dir(fs, &directory); | |
423 if (!dd) | |
424 { | |
425 sprintf(conf_start, "bad dd"); | |
426 return; | |
427 } | |
428 struct fat_file_struct* fd = open_file_in_dir(fs, dd, fn); | |
429 if (!fd) | |
430 { | |
431 sprintf(conf_start, "bad fd"); | |
432 return; | |
433 } | |
434 | |
435 fat_read_file(fd, (uint8_t*)conf_start, sizeof(conf_start)-1); | |
436 conf_start[sizeof(conf_start)-1] = '\0'; | |
437 | |
438 fat_close_file(fd); | |
439 fd = NULL; | |
440 fat_close_dir(dd); | |
441 dd = NULL; | |
442 fat_close(fs); | |
443 fs = NULL; | |
444 partition_close(partition); | |
445 partition = NULL; | |
446 | |
447 #if 0 | |
448 char c = 0; | |
449 char buf[512]; | |
450 for (int i = 0; i < 10; i++) | |
451 { | |
452 fat_read_file(fd, buf, sizeof(buf)); | |
453 c ^= buf[0]; | |
454 } | |
455 printf("total %d\n", c); | |
456 #endif | |
457 } | |
458 | |
459 | |
460 static void | |
461 cmd_testsd(const char *param) | |
462 { | |
45 | 463 boot_id_set = 0; |
43 | 464 PORT_PI_RESET &= ~_BV(PIN_PI_RESET); |
465 DDR_PI_RESET |= _BV(PIN_PI_RESET); | |
466 long_delay(200); | |
467 | |
468 printf_P(PSTR("about to raw init\n")); | |
469 | |
470 sd_raw_init(); | |
471 printf_P(PSTR("done raw init\n")); | |
472 hmac_file(param); | |
473 printf_P(PSTR("conf_start '%s'\n"), conf_start); | |
474 sd_raw_deinit(); | |
475 | |
476 long_delay(200); | |
477 | |
478 DDR_PI_RESET &= ~_BV(PIN_PI_RESET); | |
479 } | |
45 | 480 #endif |
43 | 481 |
20 | 482 static void cmd_reset() __attribute__ ((noreturn)); |
0 | 483 static void |
20 | 484 cmd_reset() |
0 | 485 { |
486 printf_P(PSTR("reset\n")); | |
45 | 487 long_delay(100); |
0 | 488 cli(); // disable interrupts |
489 wdt_enable(WDTO_15MS); // enable watchdog | |
490 while(1); // wait for watchdog to reset processor | |
491 } | |
492 | |
2 | 493 static void |
494 cmd_newboot() | |
495 { | |
496 set_pi_boot_normal(1); | |
497 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
498 { | |
499 newboot_count = newboot_limit; | |
500 } | |
20 | 501 printf_P(PSTR("newboot for %d secs\n"), newboot_limit); |
2 | 502 } |
503 | |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
504 static void |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
505 cmd_oldboot() |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
506 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
507 set_pi_boot_normal(0); |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
508 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
509 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
510 newboot_count = 0; |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
511 } |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
512 printf_P(PSTR("back to old boot\n")); |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
513 } |
1 | 514 |
0 | 515 |
516 static void | |
20 | 517 cmd_status() |
0 | 518 { |
2 | 519 uint32_t cur_watchdog_long, cur_watchdog_short, cur_newboot, cur_oneshot; |
20 | 520 struct epoch_ticks t; |
521 | |
522 get_epoch_ticks(&t); | |
523 | |
1 | 524 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
525 { | |
2 | 526 cur_watchdog_long = watchdog_long_count; |
527 cur_watchdog_short = watchdog_short_count; | |
528 cur_newboot = newboot_count; | |
529 cur_oneshot = oneshot_count; | |
1 | 530 } |
531 | |
20 | 532 printf_P(PSTR("limit (current)\n" |
533 "watchdog_long %lu (%lu) watchdog newboot mode %d\n" | |
534 "watchdog_short %lu (%lu)\n" | |
535 "newboot %lu (%lu)\n" | |
536 "oneshot (%lu)\n" | |
537 "uptime %lu rem %u\n" | |
22 | 538 "boot normal %hhu\n" |
20 | 539 ), |
540 watchdog_long_limit, cur_watchdog_long, long_reboot_mode, | |
541 watchdog_short_limit, cur_watchdog_short, | |
542 newboot_limit, cur_newboot, | |
543 cur_oneshot, | |
22 | 544 t.ticks, t.rem, |
45 | 545 boot_normal_status); |
0 | 546 } |
547 | |
548 static void | |
1 | 549 cmd_set_params(const char *params) |
0 | 550 { |
1 | 551 uint32_t new_watchdog_long_limit; |
552 uint32_t new_watchdog_short_limit; | |
553 uint32_t new_newboot_limit; | |
554 | |
555 int ret = sscanf_P(params, PSTR("%lu %lu %lu"), | |
556 &new_watchdog_long_limit, | |
557 &new_watchdog_short_limit, | |
558 &new_newboot_limit); | |
559 | |
560 if (ret != 3) | |
0 | 561 { |
1 | 562 printf_P(PSTR("Bad values\n")); |
563 } | |
564 else | |
565 { | |
566 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
567 { | |
568 eeprom_write(new_watchdog_long_limit, watchdog_long_limit); | |
569 eeprom_write(new_watchdog_short_limit, watchdog_short_limit); | |
570 eeprom_write(new_newboot_limit, newboot_limit); | |
571 uint16_t magic = EXPECT_MAGIC; | |
572 eeprom_write(magic, magic); | |
0 | 573 } |
1 | 574 printf_P(PSTR("set_params for next boot\n")); |
575 printf_P(PSTR("watchdog_long %lu watchdog_short %lu newboot %lu\n"), | |
576 new_watchdog_long_limit, | |
577 new_watchdog_short_limit, | |
578 new_newboot_limit); | |
579 } | |
580 } | |
0 | 581 |
35 | 582 // returns 1 if they are equal, timing independent |
583 static uint8_t | |
584 safe_mem_eq(const uint8_t *a, const uint8_t *b, int len) | |
585 { | |
586 uint8_t result = 0; | |
587 for (int i = 0; i < len; i++) | |
588 { | |
589 result |= a[i] ^ b[i]; | |
590 } | |
591 return result == 0; | |
592 } | |
593 | |
594 // returns 1 if they are equal | |
595 static uint8_t | |
596 safe_str_eq(const char *a, const char *b) | |
597 { | |
598 int la = strlen(a); | |
599 if (la != strlen(b)) | |
600 { | |
601 return 0; | |
602 } | |
603 return safe_mem_eq((const uint8_t*)a, (const uint8_t*)b, la); | |
604 } | |
605 | |
1 | 606 uint8_t from_hex(char c) |
607 { | |
608 if (c >= '0' && c <= '9') { | |
609 return c-'0'; | |
610 } | |
611 if (c >= 'a' && c <= 'f') { | |
612 return c-'a' + 0xa; | |
613 } | |
614 if (c >= 'A' && c <= 'F') { | |
615 return c-'A' + 0xa; | |
616 } | |
617 return 0; | |
618 } | |
619 | |
2 | 620 static void |
621 printhex_nibble(const unsigned char b, FILE *stream) | |
622 { | |
623 unsigned char c = b & 0x0f; | |
624 if ( c > 9 ) { | |
625 c += 'A'-10; | |
626 } | |
627 else { | |
628 c += '0'; | |
629 } | |
630 fputc(c, stream); | |
631 } | |
632 | |
633 void | |
634 printhex_byte(const unsigned char b, FILE *stream) | |
635 { | |
636 printhex_nibble( b >> 4, stream); | |
637 printhex_nibble( b, stream); | |
638 } | |
639 | |
640 void | |
641 printhex(uint8_t *id, uint8_t n, FILE *stream) | |
642 { | |
643 for (uint8_t i = 0; i < n; i++) | |
644 { | |
645 printhex_byte(id[i], stream); | |
646 } | |
647 } | |
648 | |
649 static int8_t | |
5 | 650 parse_key(const char *params, uint8_t *key_index, uint8_t *bytes, |
651 uint8_t bytes_len) | |
2 | 652 { |
653 // "N HEXKEY" | |
5 | 654 if (strlen(params) != bytes_len*2 + 2) { |
20 | 655 printf_P(PSTR("Wrong length key. wanted %d, got %d, '%s'\n"), |
656 bytes_len*2+2, strlen(params), params); | |
2 | 657 return -1; |
658 } | |
659 | |
660 if (params[1] != ' ') | |
661 { | |
662 printf_P(PSTR("Missing space\n")); | |
663 return -1; | |
664 } | |
665 | |
666 *key_index = from_hex(params[0]); | |
667 if (*key_index >= NKEYS) | |
668 { | |
669 printf_P(PSTR("Bad key index %d, max %d\n"), *key_index, NKEYS); | |
670 return -1; | |
671 } | |
672 | |
5 | 673 for (int i = 0, p = 0; i < bytes_len; i++, p += 2) |
2 | 674 { |
675 bytes[i] = (from_hex(params[p+2]) << 4) | from_hex(params[p+3]); | |
676 } | |
677 return 0; | |
678 } | |
679 | |
1 | 680 static void |
681 cmd_set_avr_key(const char *params) | |
682 { | |
2 | 683 uint8_t new_key[KEYLEN]; |
684 uint8_t key_index; | |
5 | 685 if (parse_key(params, &key_index, new_key, sizeof(new_key)) != 0) |
2 | 686 { |
1 | 687 return; |
0 | 688 } |
2 | 689 memcpy(avr_keys[key_index], new_key, sizeof(new_key)); |
8 | 690 #ifndef SIM_DEBUG |
2 | 691 eeprom_write(avr_keys, avr_keys); |
8 | 692 #endif |
20 | 693 printf_P(PSTR("Set key %d: "), key_index); |
694 printhex(new_key, sizeof(new_key), stdout); | |
695 putchar('\n'); | |
2 | 696 } |
697 | |
698 static void | |
699 cmd_hmac(const char *params) | |
700 { | |
42
082c8294c86b
get rid of hmac message padding and aes hmac
Matt Johnston <matt@ucc.asn.au>
parents:
41
diff
changeset
|
701 uint8_t indata[HMACLEN]; |
7 | 702 uint8_t outdata[HMACLEN]; |
2 | 703 uint8_t key_index; |
42
082c8294c86b
get rid of hmac message padding and aes hmac
Matt Johnston <matt@ucc.asn.au>
parents:
41
diff
changeset
|
704 if (parse_key(params, &key_index, indata, HMACLEN) != 0) |
2 | 705 { |
706 printf_P(PSTR("FAIL: Bad input\n")); | |
5 | 707 return; |
2 | 708 } |
0 | 709 |
47 | 710 if (key_index == 0) |
711 { | |
712 printf_P(PSTR("Can't use key index 0\n")); | |
713 return; | |
714 } | |
715 | |
8 | 716 #ifndef SIM_DEBUG |
5 | 717 long_delay(200); |
8 | 718 #endif |
5 | 719 |
11
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
720 hmac_sha1(outdata, avr_keys[key_index], KEYLEN*8, indata, sizeof(indata)*8); |
2 | 721 printf_P(PSTR("HMAC: ")); |
7 | 722 printhex(outdata, HMACLEN, stdout); |
20 | 723 putchar('\n'); |
5 | 724 } |
725 | |
726 static void | |
727 cmd_decrypt(const char *params) | |
728 { | |
42
082c8294c86b
get rid of hmac message padding and aes hmac
Matt Johnston <matt@ucc.asn.au>
parents:
41
diff
changeset
|
729 uint8_t indata[AESLEN]; |
082c8294c86b
get rid of hmac message padding and aes hmac
Matt Johnston <matt@ucc.asn.au>
parents:
41
diff
changeset
|
730 uint8_t output[AESLEN]; |
5 | 731 uint8_t key_index; |
11
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
732 if (parse_key(params, &key_index, indata, sizeof(indata)) != 0) |
5 | 733 { |
734 printf_P(PSTR("FAIL: Bad input\n")); | |
735 return; | |
736 } | |
737 | |
47 | 738 if (key_index == 0) |
739 { | |
740 printf_P(PSTR("Can't use key index 0\n")); | |
741 return; | |
742 } | |
743 | |
8 | 744 #ifndef SIM_DEBUG |
5 | 745 long_delay(200); |
8 | 746 #endif |
7 | 747 |
11
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
748 uint8_t tmpbuf[256]; |
27 | 749 aesInit(avr_keys[key_index], tmpbuf); |
42
082c8294c86b
get rid of hmac message padding and aes hmac
Matt Johnston <matt@ucc.asn.au>
parents:
41
diff
changeset
|
750 aesDecrypt(indata, NULL); |
5 | 751 |
752 printf_P(PSTR("DECRYPTED: ")); | |
753 printhex(output, AESLEN, stdout); | |
20 | 754 putchar('\n'); |
2 | 755 } |
756 | |
757 static void | |
758 cmd_oneshot_reboot(const char *params) | |
759 { | |
760 uint32_t new_delay = strtoul(params, NULL, 10); | |
761 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
1 | 762 { |
2 | 763 oneshot_count = new_delay; |
1 | 764 } |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
765 printf_P(PSTR("oneshot new delay %lu\n"), new_delay); |
0 | 766 } |
767 | |
768 static void | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
769 clamp_params() |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
770 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
771 if (watchdog_long_limit < WATCHDOG_LONG_MIN |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
772 || watchdog_long_limit > WATCHDOG_LONG_MAX) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
773 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
774 watchdog_long_limit = WATCHDOG_LONG_DEFAULT; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
775 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
776 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
777 if (watchdog_short_limit != 0 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
778 && watchdog_short_limit < WATCHDOG_SHORT_MIN) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
779 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
780 watchdog_short_limit = 0; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
781 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
782 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
783 if (newboot_limit < NEWBOOT_MIN || newboot_limit > NEWBOOT_MAX) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
784 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
785 newboot_limit = NEWBOOT_DEFAULT; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
786 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
787 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
788 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
789 static void |
0 | 790 load_params() |
791 { | |
792 uint16_t magic; | |
793 eeprom_read(magic, magic); | |
794 if (magic == EXPECT_MAGIC) | |
795 { | |
1 | 796 eeprom_read(watchdog_long_limit, watchdog_long_limit); |
797 eeprom_read(watchdog_short_limit, watchdog_short_limit); | |
2 | 798 eeprom_read(newboot_limit, newboot_limit); |
1 | 799 } |
2 | 800 |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
801 clamp_params(); |
16 | 802 |
2 | 803 eeprom_read(avr_keys, avr_keys); |
0 | 804 } |
805 | |
2 | 806 static void |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
807 cmd_alive() |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
808 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
809 printf_P(PSTR("Ah, good.\n")); |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
810 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
811 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
812 watchdog_long_count = 0; |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
813 watchdog_short_count = 0; |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
814 } |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
815 long_reboot_mode = 0; |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
816 } |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
817 |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
818 static void |
35 | 819 cmd_poke() |
820 { | |
821 printf_P(PSTR("Ah, good.\n")); | |
822 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
823 { | |
824 watchdog_short_count = 0; | |
825 } | |
826 } | |
827 | |
828 static void | |
2 | 829 cmd_vcc() |
0 | 830 { |
2 | 831 uint16_t vcc = adc_vcc(); |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
832 uint16_t v5 = adc_5v(vcc); |
21 | 833 uint16_t temp = adc_temp(); |
834 // roughly? | |
835 uint16_t temp_deg = temp - 290; | |
836 printf_P(PSTR("vcc: %u mV\n" | |
837 "5v: %u mV\n" | |
26 | 838 "temp: %u mV (%dºC)\n" |
839 ), | |
27 | 840 vcc, v5, temp, temp_deg); |
0 | 841 } |
842 | |
45 | 843 static void |
844 get_random(uint8_t *out) | |
845 { | |
846 uint32_t tries; | |
847 uint8_t rnd[20]; | |
848 adc_random(0x44, rnd, sizeof(rnd), &tries); | |
849 hmac_sha1(out, buildid, sizeof(buildid)*8, rnd, sizeof(rnd)*8); | |
850 } | |
851 | |
852 static void | |
47 | 853 cmd_newbootid() |
854 { | |
855 get_random(boot_id); | |
856 reset_uart_hmac(); | |
857 printf_P(PSTR("bootid cleared\n")); | |
858 } | |
859 | |
860 static void | |
45 | 861 cmd_bootid(const char *arg) |
862 { | |
863 uint8_t hmac[HMACLEN]; | |
864 uint8_t input[CHALLEN+sizeof(boot_id)]; | |
46 | 865 |
45 | 866 if (strlen(arg) != CHALLEN*2) |
867 { | |
868 printf_P(PSTR("Bad challenge\n")); | |
869 } | |
870 for (int i = 0, p = 0; i < CHALLEN; i++, p += 2) | |
871 { | |
872 input[i] = (from_hex(arg[p]) << 4) | from_hex(arg[p+1]); | |
873 } | |
874 memcpy(&input[CHALLEN], boot_id, sizeof(boot_id)); | |
875 | |
876 hmac_sha1(hmac, avr_keys[0], KEYLEN*8, input, sizeof(input)*8); | |
877 printf_P(PSTR("bootid: ")); | |
878 printhex(boot_id, sizeof(boot_id), stdout); | |
879 putchar(' '); | |
880 printhex(hmac, sizeof(hmac), stdout); | |
881 putchar('\n'); | |
882 } | |
20 | 883 |
27 | 884 void(*bootloader)() __attribute__ ((noreturn)) = (void*)0x7800; |
885 | |
20 | 886 static void |
887 cmd_prog(const char* arg) | |
888 { | |
46 | 889 uint8_t pw_hmac[HMACLEN]; |
890 uint8_t good_hmac[HMACLEN]; | |
891 | |
892 const static char prog_hmac[HMACLEN] PROGMEM = { | |
893 0x73, 0x4d, 0xa6, 0x3f, 0x3b, 0x7e, 0x4d, 0xa4, 0x65, 0xae, 0xea, 0xf9, 0x19, 0xbc, 0x4f, 0x45, 0xa7, 0x8d, 0x5a, 0xce, | |
894 }; | |
895 | |
896 memcpy_P(good_hmac, prog_hmac, HMACLEN); | |
897 hmac_sha1(pw_hmac, arg, strlen(arg)*8, "pihelp", strlen("pihelp")*8); | |
898 if (!safe_mem_eq(pw_hmac, good_hmac, HMACLEN)) | |
20 | 899 { |
900 printf_P(PSTR("Bad prog password\n")); | |
901 return; | |
902 } | |
903 | |
36 | 904 printf_P(PSTR("Programming...\n")); |
905 long_delay(100); | |
906 | |
20 | 907 // disable wdt |
908 wdt_disable(); | |
41 | 909 MCUSR = 0; |
20 | 910 |
911 // disable interrupts | |
912 TIMSK0 = 0; | |
913 TIMSK1 = 0; | |
914 TIMSK2 = 0; | |
915 EIMSK = 0; | |
916 PCMSK0 = 0; | |
917 PCMSK1 = 0; | |
918 PCMSK2 = 0; | |
919 ACSR &= ~_BV(ACIE); | |
920 ADCSRA &= ~_BV(ADIE); | |
921 UCSR0B &= ~_BV(RXCIE0); | |
922 UCSR0B &= _BV(TXCIE0); | |
923 // doesn't do TWI, other uart, probably others | |
924 | |
925 _delay_ms(20); | |
926 | |
927 bootloader(); | |
928 } | |
27 | 929 |
930 | |
931 static void | |
932 adc_sleep() | |
933 { | |
934 set_sleep_mode(SLEEP_MODE_IDLE); | |
935 sleep_mode(); | |
936 } | |
937 | |
938 #define BITSET(v, n) (((v) >> (n)) & 1) | |
939 | |
940 static inline uint8_t | |
941 popcnt(uint8_t v) | |
942 { | |
943 return BITSET(v, 0) | |
944 + BITSET(v, 1) | |
945 + BITSET(v, 2) | |
946 + BITSET(v, 3) | |
947 + BITSET(v, 4) | |
948 + BITSET(v, 5) | |
949 + BITSET(v, 6) | |
950 + BITSET(v, 7); | |
951 } | |
952 | |
953 static uint8_t | |
954 adc_bit() | |
955 { | |
956 ADCSRA |= _BV(ADSC); | |
957 loop_until_bit_is_clear(ADCSRA, ADSC); | |
958 uint8_t low = ADCL; | |
959 uint8_t high = ADCH; | |
35 | 960 uint8_t ret = (popcnt(low)&1) ^ (popcnt(high)&1); |
961 return ret; | |
27 | 962 } |
963 | |
964 static void | |
965 adc_random(uint8_t admux, | |
966 uint8_t *out, uint16_t num, uint32_t *tries) | |
967 { | |
968 PRR &= ~_BV(PRADC); | |
969 // /16 prescaler for 691mhz, no interrupt | |
970 ADCSRA = _BV(ADEN) | _BV(ADPS2); | |
971 | |
35 | 972 ADMUX = admux; |
973 | |
27 | 974 *tries = 0; |
975 for (int i = 0; i < num; i++) | |
976 { | |
35 | 977 uint8_t ret = 0; |
978 uint8_t count = 0; | |
979 | |
27 | 980 while (count <= 7) |
981 { | |
982 (*tries)++; | |
983 | |
984 // Von Neumann extractor | |
985 uint8_t one = adc_bit(); | |
986 uint8_t two = adc_bit(); | |
987 if (one == two) | |
988 { | |
989 continue; | |
990 } | |
991 ret |= one << count; | |
992 count++; | |
993 } | |
994 out[i] = ret; | |
995 } | |
996 ADCSRA = 0; | |
997 PRR |= _BV(PRADC); | |
998 } | |
999 | |
1000 ISR(ADC_vect) | |
1001 { | |
1002 adc_done = 1; | |
1003 } | |
1004 | |
1005 static void | |
1006 adc_generic(uint8_t admux, uint8_t *ret_num, uint16_t *ret_sum) | |
1007 { | |
1008 PRR &= ~_BV(PRADC); | |
1009 | |
31 | 1010 // /128 prescaler (86kHz), interrupt |
1011 ADCSRA = _BV(ADEN) | |
1012 | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0) | |
1013 | _BV(ADIE); | |
27 | 1014 |
1015 // set to measure 1.1 reference | |
1016 ADMUX = admux; | |
1017 | |
1018 // delay after setting reference etc, allow settling | |
1019 long_delay(300); | |
1020 // average a number of samples | |
1021 uint16_t sum = 0; | |
1022 uint8_t num = 0; | |
1023 for (uint8_t n = 0; n < 20; n++) | |
1024 { | |
1025 while (1) | |
1026 { | |
1027 adc_done = 0; | |
1028 ADCSRA |= _BV(ADSC); | |
1029 adc_sleep(); | |
1030 | |
1031 uint8_t done; | |
1032 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
1033 { | |
1034 done = adc_done; | |
1035 } | |
1036 if (done) | |
1037 { | |
1038 break; | |
1039 } | |
1040 } | |
1041 | |
1042 uint8_t low_11 = ADCL; | |
1043 uint8_t high_11 = ADCH; | |
1044 uint16_t val = low_11 + (high_11 << 8); | |
1045 | |
1046 if (n >= 4) | |
1047 { | |
1048 sum += val; | |
1049 num++; | |
1050 } | |
1051 } | |
1052 ADCSRA = 0; | |
1053 PRR |= _BV(PRADC); | |
1054 | |
1055 *ret_num = num; | |
1056 *ret_sum = sum; | |
1057 } | |
1058 | |
1059 static uint16_t | |
1060 adc_vcc() | |
1061 { | |
1062 const uint8_t mux = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); | |
1063 uint16_t sum; | |
1064 uint8_t num; | |
1065 | |
1066 adc_generic(mux, &num, &sum); | |
1067 | |
1068 //float res_volts = 1.1 * 1024 * num / sum; | |
1069 //return 1000 * res_volts; | |
1070 return ((uint32_t)1100*1024*num) / sum; | |
1071 } | |
1072 | |
1073 #define SCALER_5V 2 | |
1074 | |
1075 static uint16_t | |
1076 adc_5v(uint16_t vcc) | |
1077 { | |
1078 // set to measure ADC4 against AVCC | |
1079 const uint8_t mux = _BV(REFS0) | _BV(MUX2); | |
1080 uint16_t sum; | |
1081 uint8_t num; | |
1082 | |
1083 adc_generic(mux, &num, &sum); | |
1084 | |
1085 return ((uint32_t)vcc*sum*SCALER_5V/(num*1024)); | |
1086 } | |
1087 | |
1088 static uint16_t | |
1089 adc_temp() | |
1090 { | |
1091 // set to measure temperature against 1.1v reference. | |
1092 const uint8_t mux = _BV(REFS0) | _BV(REFS1) | _BV(MUX3); | |
1093 uint16_t sum; | |
1094 uint8_t num; | |
1095 | |
1096 adc_generic(mux, &num, &sum); | |
1097 | |
1098 // return the voltage | |
1099 | |
1100 return ((uint32_t)1100*sum) / (num*1024); | |
1101 } | |
1102 | |
1103 static void | |
1104 cmd_random(const char* params) | |
1105 { | |
1106 uint8_t admux; | |
1107 uint16_t num; | |
1108 uint8_t buf[100]; | |
1109 | |
1110 int ret = sscanf_P(params, PSTR("%hhu %u"), | |
1111 &admux, &num); | |
1112 if (!ret) | |
1113 { | |
1114 printf_P(PSTR("Bad arguments\n")); | |
1115 return; | |
1116 } | |
1117 uint32_t tries = 0; | |
1118 printf_P(PSTR("output: ")); | |
1119 for (int i = 0; i < num; i+= sizeof(buf)) | |
1120 { | |
1121 uint32_t t; | |
1122 uint16_t nr = MIN(num-i, sizeof(buf)); | |
1123 adc_random(admux, buf, nr, &t); | |
1124 printhex(buf, nr, stdout); | |
1125 tries += t; | |
1126 } | |
1127 putchar('\n'); | |
1128 printf_P(PSTR("%ld tries\n"), tries); | |
1129 } | |
1130 | |
1131 | |
20 | 1132 |
0 | 1133 static void |
1134 read_handler() | |
1135 { | |
17 | 1136 #define LOCAL_PSTR(x) const static char x ## _str[] PROGMEM = #x; |
1137 #define LOCAL_HELP(x, d) const static char x ## _help[] PROGMEM = d; | |
1138 | |
1139 LOCAL_PSTR(set_params); | |
1140 LOCAL_PSTR(set_key); | |
1141 LOCAL_PSTR(oneshot); | |
1142 LOCAL_PSTR(hmac); | |
1143 LOCAL_PSTR(decrypt); | |
1144 LOCAL_PSTR(alive); | |
35 | 1145 LOCAL_PSTR(poke); |
17 | 1146 LOCAL_PSTR(vcc); |
1147 LOCAL_PSTR(reset); | |
1148 LOCAL_PSTR(newboot); | |
1149 LOCAL_PSTR(oldboot); | |
20 | 1150 LOCAL_PSTR(status); |
27 | 1151 LOCAL_PSTR(random); |
1152 LOCAL_PSTR(prog); | |
45 | 1153 LOCAL_PSTR(bootid); |
47 | 1154 LOCAL_PSTR(newbootid); |
1155 LOCAL_PSTR(rebootpi); | |
1156 LOCAL_PSTR(printhmac); | |
17 | 1157 LOCAL_HELP(set_params, "<long_limit> <short_limit> <newboot_limit>"); |
1158 LOCAL_HELP(set_key, "20_byte_hex>"); | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1159 LOCAL_HELP(oneshot, "<timeout>"); |
27 | 1160 LOCAL_HELP(prog, "<password>"); |
1161 LOCAL_HELP(random, "<admux> <nbytes>"); | |
17 | 1162 LOCAL_HELP(hmac, "<key_index> <20_byte_hex_data>"); |
1163 LOCAL_HELP(decrypt, "<key_index> <20_byte_hmac|16_byte_aes_block>"); | |
45 | 1164 LOCAL_HELP(bootid, "<8_byte_challenge>") |
47 | 1165 LOCAL_HELP(printhmac, "<0|1>") |
17 | 1166 |
1167 static const struct handler { | |
1168 PGM_P name; | |
1169 void(*cmd)(const char *param); | |
1170 // existence of arg_help indicates if the cmd takes a parameter. | |
1171 PGM_P arg_help; | |
20 | 1172 } handlers[] PROGMEM = |
17 | 1173 { |
20 | 1174 {alive_str, cmd_alive, NULL}, |
35 | 1175 {poke_str, cmd_poke, NULL}, |
20 | 1176 {newboot_str, cmd_newboot, NULL}, |
1177 {oldboot_str, cmd_oldboot, NULL}, | |
1178 {oneshot_str, cmd_oneshot_reboot, oneshot_help}, | |
1179 {status_str, cmd_status, NULL}, | |
1180 {hmac_str, cmd_hmac, hmac_help}, | |
1181 {decrypt_str, cmd_decrypt, decrypt_help}, | |
17 | 1182 {set_params_str, cmd_set_params, set_params_help}, |
1183 {set_key_str, cmd_set_avr_key, set_key_help}, | |
27 | 1184 {random_str, cmd_random, random_help}, |
17 | 1185 {vcc_str, cmd_vcc, NULL}, |
1186 {reset_str, cmd_reset, NULL}, | |
47 | 1187 {rebootpi_str, cmd_rebootpi, NULL}, |
27 | 1188 {prog_str, cmd_prog, prog_help}, |
45 | 1189 {bootid_str, cmd_bootid, bootid_help}, |
47 | 1190 {newbootid_str, cmd_newbootid, NULL}, |
1191 {printhmac_str, cmd_printhmac, printhmac_help}, | |
17 | 1192 }; |
1193 | |
1194 if (readbuf[0] == '\0') | |
1195 { | |
1196 return; | |
1197 } | |
1198 | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1199 if (strcmp_P(readbuf, PSTR("help")) == 0) |
17 | 1200 { |
1201 printf_P(PSTR("Commands:---\n")); | |
1202 for (int i = 0; i < sizeof(handlers) / sizeof(handlers[0]); i++) | |
1203 { | |
1204 struct handler h; | |
1205 memcpy_P(&h, &handlers[i], sizeof(h)); | |
1206 printf_P(h.name); | |
1207 if (h.arg_help) | |
1208 { | |
1209 putchar(' '); | |
1210 printf_P(h.arg_help); | |
1211 } | |
1212 putchar('\n'); | |
1213 }; | |
1214 printf_P(PSTR("---\n")); | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1215 return; |
17 | 1216 } |
1217 | |
1218 for (int i = 0; i < sizeof(handlers) / sizeof(handlers[0]); i++) | |
1219 { | |
1220 struct handler h; | |
1221 memcpy_P(&h, &handlers[i], sizeof(h)); | |
1222 | |
1223 const int h_len = strlen_P(h.name); | |
1224 if (strncmp_P(readbuf, h.name, h_len) == 0) | |
1225 { | |
1226 if (h.arg_help) | |
1227 { | |
1228 if (readbuf[h_len] == ' ') | |
1229 { | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1230 h.cmd(&readbuf[h_len+1]); |
17 | 1231 return; |
1232 } | |
1233 } | |
1234 else | |
1235 { | |
1236 if (readbuf[h_len] == '\0') | |
1237 { | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1238 void(*void_cmd)() = h.cmd; |
17 | 1239 void_cmd(); |
1240 return; | |
1241 } | |
1242 } | |
1243 } | |
1244 } | |
1245 | |
1246 printf_P(PSTR("Bad command '%s'\n"), readbuf); | |
0 | 1247 } |
1248 | |
1249 ISR(INT0_vect) | |
1250 { | |
1251 blink(); | |
45 | 1252 long_delay(100); |
0 | 1253 blink(); |
1254 } | |
1255 | |
1256 ISR(USART_RX_vect) | |
1257 { | |
1258 char c = UDR0; | |
1259 #ifdef HAVE_UART_ECHO | |
1260 uart_putchar(c, NULL); | |
1261 #endif | |
1262 if (c == '\r' || c == '\n') | |
1263 { | |
1264 if (readpos > 0) | |
1265 { | |
1266 readbuf[readpos] = '\0'; | |
1267 have_cmd = 1; | |
1268 readpos = 0; | |
1269 } | |
1270 } | |
1271 else | |
1272 { | |
1273 readbuf[readpos] = c; | |
1274 readpos++; | |
1275 if (readpos >= sizeof(readbuf)) | |
1276 { | |
1277 readpos = 0; | |
1278 } | |
1279 } | |
1280 } | |
1281 | |
2 | 1282 ISR(TIMER1_COMPA_vect) |
0 | 1283 { |
2 | 1284 TCNT1 = 0; |
0 | 1285 |
1286 clock_epoch += TICK; | |
1287 | |
1 | 1288 // watchdogs count up, continuous |
1289 if (watchdog_long_limit > 0) { | |
2 | 1290 watchdog_long_count += TICK; |
1 | 1291 if (watchdog_long_count >= watchdog_long_limit) |
1292 { | |
1293 watchdog_long_count = 0; | |
1294 watchdog_long_hit = 1; | |
1295 } | |
0 | 1296 } |
1297 | |
1 | 1298 if (watchdog_short_limit > 0) { |
2 | 1299 watchdog_short_count += TICK; |
1 | 1300 if (watchdog_short_count >= watchdog_short_limit) |
1301 { | |
1302 watchdog_short_count = 0; | |
1303 watchdog_short_hit = 1; | |
1304 } | |
0 | 1305 } |
1306 | |
2 | 1307 // newboot counts down |
1 | 1308 if (newboot_count > 0) |
0 | 1309 { |
2 | 1310 newboot_count-=TICK; |
1311 if (newboot_count <= 0) | |
1 | 1312 { |
1313 newboot_hit = 1; | |
2 | 1314 newboot_count = 0; |
1 | 1315 } |
0 | 1316 } |
1 | 1317 |
2 | 1318 if (oneshot_count > 0) |
1319 { | |
1320 oneshot_count-=TICK; | |
1321 if (oneshot_count <= 0) | |
1322 { | |
1323 oneshot_hit = 1; | |
1324 oneshot_count = 0; | |
1325 } | |
1326 } | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1327 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1328 if (reboot_count > 0) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1329 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1330 reboot_count -= TICK; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1331 if (reboot_count <= 0) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1332 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1333 reboot_hit = 1; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1334 reboot_count = 0; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1335 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1336 } |
0 | 1337 } |
1338 | |
1339 static void | |
1340 idle_sleep() | |
1341 { | |
1342 set_sleep_mode(SLEEP_MODE_IDLE); | |
1343 sleep_mode(); | |
1344 } | |
1345 | |
21 | 1346 static void |
2 | 1347 reboot_pi() |
1348 { | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1349 printf_P(PSTR("Real reboot now\n")); |
47 | 1350 cmd_newbootid(); |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1351 // pull it low for 200ms |
2 | 1352 PORT_PI_RESET &= ~_BV(PIN_PI_RESET); |
1353 DDR_PI_RESET |= _BV(PIN_PI_RESET); | |
45 | 1354 long_delay(200); |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1355 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1356 PORT_PI_WARNING &= ~_BV(PIN_PI_WARNING); |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1357 DDR_PI_RESET &= ~_BV(PIN_PI_RESET); |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1358 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1359 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1360 static void |
47 | 1361 cmd_rebootpi() |
1362 { | |
1363 printf_P(PSTR("Rebooting.\n")); | |
1364 long_delay(1000); | |
1365 reboot_pi(); | |
1366 } | |
1367 | |
1368 | |
1369 static void | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1370 wait_reboot_pi() |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1371 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1372 PORT_PI_WARNING |= _BV(PIN_PI_WARNING); |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1373 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1374 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1375 reboot_count = WARNING_TIME; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1376 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1377 printf_P(PSTR("Rebooting in %hhu seconds\n"), reboot_count); |
2 | 1378 } |
1379 | |
1380 static void | |
1381 set_pi_boot_normal(uint8_t normal) | |
1382 { | |
22 | 1383 boot_normal_status = normal; |
2 | 1384 PORT_PI_BOOT &= ~_BV(PIN_PI_BOOT); |
1385 if (normal) | |
1386 { | |
1387 // tristate | |
1388 DDR_PI_BOOT &= ~_BV(PIN_PI_BOOT); | |
1389 } | |
1390 else | |
1391 { | |
1392 // pull it low | |
1393 DDR_PI_RESET |= _BV(PIN_PI_BOOT); | |
1394 } | |
1395 } | |
1396 | |
1397 static void | |
1398 check_flags() | |
1399 { | |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1400 if (watchdog_long_hit) |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1401 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1402 // alternate between booting normal and emergency |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1403 if (long_reboot_mode) |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1404 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1405 cmd_newboot(); |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1406 } |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1407 long_reboot_mode ^= 1; |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1408 } |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1409 |
2 | 1410 if (watchdog_long_hit |
1411 || watchdog_short_hit | |
1412 || oneshot_hit) | |
1413 { | |
12 | 1414 printf_P(PSTR("Rebooting! long %d, short %d, oneshot %d\n"), |
1415 watchdog_long_hit, watchdog_short_hit, oneshot_hit); | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1416 wait_reboot_pi(); |
2 | 1417 } |
1418 | |
1419 if (newboot_hit) { | |
1420 set_pi_boot_normal(0); | |
1421 } | |
1422 | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1423 if (reboot_hit) { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1424 reboot_pi(); |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1425 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1426 |
2 | 1427 watchdog_long_hit = 0; |
1428 watchdog_short_hit = 0; | |
1429 newboot_hit = 0; | |
1430 oneshot_hit = 0; | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1431 reboot_hit = 0; |
2 | 1432 } |
1433 | |
1434 static void | |
0 | 1435 do_comms() |
1436 { | |
1437 // avoid receiving rubbish, perhaps | |
1438 uart_on(); | |
1439 | |
1440 // write sd card here? same 3.3v regulator... | |
1441 | |
1 | 1442 while (1) |
0 | 1443 { |
1 | 1444 wdt_reset(); |
2 | 1445 |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1446 #ifdef SIM_DEBUG |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1447 if (sim_idx != last_sim_idx) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1448 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1449 last_sim_idx = sim_idx; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1450 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1451 #endif |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1452 |
2 | 1453 check_flags(); |
1454 | |
0 | 1455 if (have_cmd) |
1456 { | |
1457 have_cmd = 0; | |
1458 read_handler(); | |
1459 continue; | |
1460 } | |
1461 | |
1462 // wait for commands from the master | |
1463 idle_sleep(); | |
1464 } | |
1465 } | |
1466 | |
1467 static void | |
1468 blink() | |
1469 { | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1470 #if 0 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1471 PORT_ &= ~_BV(PIN_LED); |
0 | 1472 _delay_ms(1); |
1473 PORT_LED |= _BV(PIN_LED); | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1474 #endif |
0 | 1475 } |
1476 | |
1477 static void | |
22 | 1478 long_delay(uint16_t ms) |
0 | 1479 { |
22 | 1480 uint16_t iter = ms / 10; |
0 | 1481 |
22 | 1482 for (uint16_t i = 0; i < iter; i++) |
0 | 1483 { |
22 | 1484 _delay_ms(10); |
0 | 1485 } |
1486 } | |
1487 | |
1488 ISR(BADISR_vect) | |
1489 { | |
1490 //uart_on(); | |
1491 printf_P(PSTR("Bad interrupt\n")); | |
1492 } | |
1493 | |
20 | 1494 // disable watchdog on boot |
1495 void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3"))); | |
1496 void wdt_init(void) | |
1497 { | |
1498 MCUSR = 0; | |
1499 wdt_disable(); | |
1500 } | |
1501 | |
0 | 1502 int main(void) |
1503 { | |
47 | 1504 _Static_assert(F_CPU % 1024 == 0, "clock prescaler remainder 0"); |
17 | 1505 _Static_assert(NEWBOOT_MAX < WATCHDOG_LONG_MIN, "newboot max shorter than watchdog min"); |
20 | 1506 _Static_assert((F_CPU)%(16*(BAUD)) == 0, "baud rate good multiple"); |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1507 |
0 | 1508 setup_chip(); |
1509 blink(); | |
1510 | |
47 | 1511 stdout = raw_stdout; |
0 | 1512 uart_on(); |
1513 | |
20 | 1514 long_delay(500); |
1515 printf_P(PSTR("Pi Watchdog\nMatt Johnston [email protected]\n")); | |
0 | 1516 |
2 | 1517 set_pi_boot_normal(0); |
1518 | |
0 | 1519 load_params(); |
1520 | |
47 | 1521 cmd_newbootid(); |
1522 reset_uart_hmac(); | |
1523 stdout = &mystdout; | |
1524 | |
0 | 1525 setup_tick_counter(); |
1526 | |
1527 sei(); | |
1528 | |
8 | 1529 #if 0 |
1530 // encryption test | |
1531 cmd_set_avr_key("1 6161626263636464656566666767686800000000"); | |
1532 cmd_set_avr_key("2 7979757569696f6f646465656666717164646969"); | |
12 | 1533 //cmd_decrypt("1 ecd858ee07a8e16575723513d2d072a7565865e40ba302059bfc650d4491268448102119"); |
1534 cmd_decrypt("1 5a587b50fd48688bbda1b510cf9a3fab6fd4737b" "0ba302059bfc650d4491268448102119"); | |
1535 cmd_hmac("2 7979757569696f6f646465656666717164646969"); | |
8 | 1536 #endif |
1537 | |
1 | 1538 // doesn't return |
1539 do_comms(); | |
0 | 1540 |
1541 return 0; /* never reached */ | |
1542 } |