Mercurial > pihelp
annotate main.c @ 22:a55d7c2440fd colopi1
basically done
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 16 Jun 2013 22:05:55 +0800 |
parents | 83a0663c082f |
children | 292cd17de2a4 |
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 | |
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 |
20 | 22 // not set via bootloader |
23 //LOCKBITS = (LB_MODE_3 & BLB0_MODE_4 & BLB1_MODE_4); | |
5 | 24 |
0 | 25 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) |
26 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) | |
27 | |
28 // TICK should be 8 or less (8 untested). all timers need | |
29 // to be a multiple. | |
30 | |
1 | 31 #define TICK 1 |
17 | 32 #define SLEEP_COMPARE (F_CPU/256) // == 19200 for 4915200mhz |
5 | 33 #define NKEYS 10 |
34 #define HMACLEN 20 | |
35 #define AESLEN 16 | |
36 #define KEYLEN HMACLEN | |
1 | 37 |
20 | 38 #define BAUD 38400 |
39 #define UBRR ((F_CPU)/(16*(BAUD))-1) | |
0 | 40 |
2 | 41 #define PORT_PI_BOOT PORTD |
42 #define DDR_PI_BOOT DDRD | |
4 | 43 #define PIN_PI_BOOT PD5 |
2 | 44 |
45 #define PORT_PI_RESET PORTD | |
46 #define DDR_PI_RESET DDRD | |
47 #define PIN_PI_RESET PD6 | |
48 | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
49 #define PORT_PI_WARNING PORTD |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
50 #define DDR_PI_WARNING DDRD |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
51 #define PIN_PI_WARNING PD7 |
0 | 52 |
53 // #define HAVE_UART_ECHO | |
54 | |
7 | 55 // stores a value of clock_epoch combined with the remainder of TCNT1, |
0 | 56 // for 1/32 second accuracy |
57 struct epoch_ticks | |
58 { | |
59 uint32_t ticks; | |
60 // remainder | |
20 | 61 uint16_t rem; |
0 | 62 }; |
63 | |
16 | 64 #define WATCHDOG_LONG_MIN (60L*40) // 40 mins |
65 #define WATCHDOG_LONG_MAX (60L*60*72) // 72 hours | |
66 #define WATCHDOG_LONG_DEFAULT (60L*60*6) // 6 hours | |
67 | |
68 #define WATCHDOG_SHORT_MIN (60L*15) // 15 mins | |
69 | |
70 #define NEWBOOT_DEFAULT (60*10) // 10 minutes | |
71 #define NEWBOOT_MIN (60*2) // 2 minutes | |
72 #define NEWBOOT_MAX (60*30) // 30 mins | |
73 | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
74 #define WARNING_TIME 10 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
75 |
1 | 76 // eeprom-settable parameters, default values defined here. |
77 // all timeouts should be a multiple of TICK | |
16 | 78 static uint32_t watchdog_long_limit = WATCHDOG_LONG_DEFAULT; |
1 | 79 static uint32_t watchdog_short_limit = 0; |
16 | 80 static uint32_t newboot_limit = NEWBOOT_DEFAULT; |
0 | 81 |
1 | 82 // avr proves itself |
2 | 83 static uint8_t avr_keys[NKEYS][KEYLEN] = {{0}}; |
0 | 84 |
85 // ---- Atomic guards required accessing these variables | |
86 // clock_epoch in seconds | |
87 static uint32_t clock_epoch; | |
1 | 88 // watchdog counts up |
89 static uint32_t watchdog_long_count; | |
90 static uint32_t watchdog_short_count; | |
2 | 91 // newboot counts down |
1 | 92 static uint32_t newboot_count; |
2 | 93 // oneshot counts down |
94 static uint32_t oneshot_count; | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
95 // countdown after the warning. |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
96 static uint8_t reboot_count; |
2 | 97 |
0 | 98 // ---- End atomic guards required |
99 | |
100 // boolean flags | |
1 | 101 static uint8_t watchdog_long_hit; |
102 static uint8_t watchdog_short_hit; | |
103 static uint8_t newboot_hit; | |
2 | 104 static uint8_t oneshot_hit; |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
105 static uint8_t reboot_hit; |
0 | 106 |
22 | 107 // informational for status messages |
108 static uint8_t boot_normal_status; | |
109 | |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
110 // 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
|
111 // 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
|
112 // command |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
113 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
|
114 |
0 | 115 static uint8_t readpos; |
20 | 116 static char readbuf[150]; |
0 | 117 static uint8_t have_cmd; |
118 | |
119 int uart_putchar(char c, FILE *stream); | |
22 | 120 static void long_delay(uint16_t ms); |
0 | 121 static void blink(); |
122 static uint16_t adc_vcc(); | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
123 static uint16_t adc_5v(uint16_t vcc); |
20 | 124 static uint16_t adc_temp(); |
2 | 125 static void set_pi_boot_normal(uint8_t normal); |
0 | 126 |
17 | 127 |
0 | 128 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, |
129 _FDEV_SETUP_WRITE); | |
130 | |
131 // thanks to http://projectgus.com/2010/07/eeprom-access-with-arduino/ | |
132 #define eeprom_read_to(dst_p, eeprom_field, dst_size) eeprom_read_block((dst_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (dst_size)) | |
133 #define eeprom_read(dst, eeprom_field) eeprom_read_to((&dst), eeprom_field, sizeof(dst)) | |
134 #define eeprom_write_from(src_p, eeprom_field, src_size) eeprom_write_block((src_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (src_size)) | |
135 #define eeprom_write(src, eeprom_field) { eeprom_write_from(&src, eeprom_field, sizeof(src)); } | |
136 | |
1 | 137 #define EXPECT_MAGIC 0xdf83 |
0 | 138 |
139 struct __attribute__ ((__packed__)) __eeprom_data { | |
1 | 140 uint32_t watchdog_long_limit; |
141 uint32_t watchdog_short_limit; | |
142 uint32_t newboot_limit; | |
0 | 143 |
2 | 144 uint8_t avr_keys[NKEYS][KEYLEN]; |
0 | 145 |
146 uint16_t magic; | |
147 }; | |
148 | |
149 // Very first setup | |
150 static void | |
151 setup_chip() | |
152 { | |
153 cli(); | |
154 | |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
155 // set to 8 seconds, in case sha1 is slow etc. |
1 | 156 wdt_enable(WDTO_8S); |
157 | |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
158 // Set scaler to /1, -> clock to 8mhz |
0 | 159 CLKPR = _BV(CLKPCE); |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
160 CLKPR = 0; |
0 | 161 |
162 // enable pullups | |
1 | 163 // XXX matt pihelp |
5 | 164 //PORTB = 0xff; // XXX change when using SPI |
165 //PORTD = 0xff; | |
166 //PORTC = 0xff; | |
0 | 167 |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
168 DDR_PI_WARNING |= _BV(PIN_PI_WARNING); |
0 | 169 |
5 | 170 #if 0 |
0 | 171 // set pullup |
172 PORTD |= _BV(PD2); | |
173 // INT0 setup | |
174 EICRA = (1<<ISC01); // falling edge - data sheet says it won't work? | |
175 EIMSK = _BV(INT0); | |
5 | 176 #endif |
0 | 177 |
178 // comparator disable | |
179 ACSR = _BV(ACD); | |
180 | |
181 // disable adc pin input buffers | |
182 DIDR0 = 0x3F; // acd0-adc5 | |
183 DIDR1 = (1<<AIN1D)|(1<<AIN0D); // ain0/ain1 | |
184 | |
185 sei(); | |
186 } | |
187 | |
188 static void | |
189 get_epoch_ticks(struct epoch_ticks *t) | |
190 { | |
191 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
192 { | |
193 t->ticks = clock_epoch; | |
7 | 194 t->rem = TCNT1; |
0 | 195 } |
196 } | |
197 | |
198 static void | |
199 setup_tick_counter() | |
200 { | |
2 | 201 // set up counter1 |
202 | |
0 | 203 // set up counter2. |
204 // COM21 COM20 Set OC2 on Compare Match (p116) | |
205 // WGM21 Clear counter on compare | |
206 //TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21); | |
207 // toggle on match | |
2 | 208 TCCR1A = _BV(COM1A0); |
8 | 209 #ifdef SIM_DEBUG |
210 // systemclock/8 | |
211 TCCR1B = _BV(CS11); | |
212 #else | |
20 | 213 // systemclock/256 |
214 TCCR1B = _BV(CS12); | |
8 | 215 #endif |
2 | 216 TCNT1 = 0; |
217 OCR1A = SLEEP_COMPARE; | |
0 | 218 // interrupt |
2 | 219 TIMSK1 = _BV(OCIE1A); |
0 | 220 } |
221 | |
222 static void | |
223 uart_on() | |
224 { | |
225 // Power reduction register | |
226 PRR &= ~_BV(PRUSART0); | |
227 | |
228 // All of this needs to be done each time after turning off the PRR | |
229 // baud rate | |
230 UBRR0H = (unsigned char)(UBRR >> 8); | |
231 UBRR0L = (unsigned char)UBRR; | |
232 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); | |
233 //8N1 | |
234 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); | |
235 } | |
236 | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
237 #ifdef SIM_DEBUG |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
238 static char sim_out[140]; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
239 static uint8_t sim_idx = 0; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
240 static uint8_t last_sim_idx = 0; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
241 #endif |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
242 |
0 | 243 int |
244 uart_putchar(char c, FILE *stream) | |
245 { | |
246 // XXX could perhaps sleep in the loop for power. | |
247 if (c == '\n') | |
248 { | |
249 loop_until_bit_is_set(UCSR0A, UDRE0); | |
250 UDR0 = '\r'; | |
251 } | |
252 loop_until_bit_is_set(UCSR0A, UDRE0); | |
253 UDR0 = c; | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
254 #ifdef SIM_DEBUG |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
255 sim_out[sim_idx] = c; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
256 sim_idx++; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
257 sim_idx %= sizeof(sim_out); |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
258 #endif |
0 | 259 if (c == '\r') |
260 { | |
261 loop_until_bit_is_set(UCSR0A, UDRE0); | |
262 UDR0 = '\n'; | |
263 } | |
264 return (unsigned char)c; | |
265 } | |
266 | |
20 | 267 static void cmd_reset() __attribute__ ((noreturn)); |
0 | 268 static void |
20 | 269 cmd_reset() |
0 | 270 { |
271 printf_P(PSTR("reset\n")); | |
272 _delay_ms(100); | |
273 cli(); // disable interrupts | |
274 wdt_enable(WDTO_15MS); // enable watchdog | |
275 while(1); // wait for watchdog to reset processor | |
276 } | |
277 | |
2 | 278 static void |
279 cmd_newboot() | |
280 { | |
281 set_pi_boot_normal(1); | |
282 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
283 { | |
284 newboot_count = newboot_limit; | |
285 } | |
20 | 286 printf_P(PSTR("newboot for %d secs\n"), newboot_limit); |
2 | 287 } |
288 | |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
289 static void |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
290 cmd_oldboot() |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
291 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
292 set_pi_boot_normal(0); |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
293 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
294 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
295 newboot_count = 0; |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
296 } |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
297 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
|
298 } |
1 | 299 |
0 | 300 |
301 static void | |
20 | 302 cmd_status() |
0 | 303 { |
2 | 304 uint32_t cur_watchdog_long, cur_watchdog_short, cur_newboot, cur_oneshot; |
20 | 305 struct epoch_ticks t; |
306 | |
307 get_epoch_ticks(&t); | |
308 | |
1 | 309 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
310 { | |
2 | 311 cur_watchdog_long = watchdog_long_count; |
312 cur_watchdog_short = watchdog_short_count; | |
313 cur_newboot = newboot_count; | |
314 cur_oneshot = oneshot_count; | |
1 | 315 } |
316 | |
20 | 317 printf_P(PSTR("limit (current)\n" |
318 "watchdog_long %lu (%lu) watchdog newboot mode %d\n" | |
319 "watchdog_short %lu (%lu)\n" | |
320 "newboot %lu (%lu)\n" | |
321 "oneshot (%lu)\n" | |
322 "uptime %lu rem %u\n" | |
22 | 323 "boot normal %hhu\n" |
20 | 324 ), |
325 watchdog_long_limit, cur_watchdog_long, long_reboot_mode, | |
326 watchdog_short_limit, cur_watchdog_short, | |
327 newboot_limit, cur_newboot, | |
328 cur_oneshot, | |
22 | 329 t.ticks, t.rem, |
330 boot_normal_status); | |
0 | 331 } |
332 | |
333 static void | |
1 | 334 cmd_set_params(const char *params) |
0 | 335 { |
1 | 336 uint32_t new_watchdog_long_limit; |
337 uint32_t new_watchdog_short_limit; | |
338 uint32_t new_newboot_limit; | |
339 | |
340 int ret = sscanf_P(params, PSTR("%lu %lu %lu"), | |
341 &new_watchdog_long_limit, | |
342 &new_watchdog_short_limit, | |
343 &new_newboot_limit); | |
344 | |
345 if (ret != 3) | |
0 | 346 { |
1 | 347 printf_P(PSTR("Bad values\n")); |
348 } | |
349 else | |
350 { | |
351 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
352 { | |
353 eeprom_write(new_watchdog_long_limit, watchdog_long_limit); | |
354 eeprom_write(new_watchdog_short_limit, watchdog_short_limit); | |
355 eeprom_write(new_newboot_limit, newboot_limit); | |
356 uint16_t magic = EXPECT_MAGIC; | |
357 eeprom_write(magic, magic); | |
0 | 358 } |
1 | 359 printf_P(PSTR("set_params for next boot\n")); |
360 printf_P(PSTR("watchdog_long %lu watchdog_short %lu newboot %lu\n"), | |
361 new_watchdog_long_limit, | |
362 new_watchdog_short_limit, | |
363 new_newboot_limit); | |
364 } | |
365 } | |
0 | 366 |
1 | 367 uint8_t from_hex(char c) |
368 { | |
369 if (c >= '0' && c <= '9') { | |
370 return c-'0'; | |
371 } | |
372 if (c >= 'a' && c <= 'f') { | |
373 return c-'a' + 0xa; | |
374 } | |
375 if (c >= 'A' && c <= 'F') { | |
376 return c-'A' + 0xa; | |
377 } | |
378 return 0; | |
379 } | |
380 | |
2 | 381 static void |
382 printhex_nibble(const unsigned char b, FILE *stream) | |
383 { | |
384 unsigned char c = b & 0x0f; | |
385 if ( c > 9 ) { | |
386 c += 'A'-10; | |
387 } | |
388 else { | |
389 c += '0'; | |
390 } | |
391 fputc(c, stream); | |
392 } | |
393 | |
394 void | |
395 printhex_byte(const unsigned char b, FILE *stream) | |
396 { | |
397 printhex_nibble( b >> 4, stream); | |
398 printhex_nibble( b, stream); | |
399 } | |
400 | |
401 void | |
402 printhex(uint8_t *id, uint8_t n, FILE *stream) | |
403 { | |
404 for (uint8_t i = 0; i < n; i++) | |
405 { | |
406 printhex_byte(id[i], stream); | |
407 } | |
408 } | |
409 | |
410 static int8_t | |
5 | 411 parse_key(const char *params, uint8_t *key_index, uint8_t *bytes, |
412 uint8_t bytes_len) | |
2 | 413 { |
414 // "N HEXKEY" | |
5 | 415 if (strlen(params) != bytes_len*2 + 2) { |
20 | 416 printf_P(PSTR("Wrong length key. wanted %d, got %d, '%s'\n"), |
417 bytes_len*2+2, strlen(params), params); | |
2 | 418 return -1; |
419 } | |
420 | |
421 if (params[1] != ' ') | |
422 { | |
423 printf_P(PSTR("Missing space\n")); | |
424 return -1; | |
425 } | |
426 | |
427 *key_index = from_hex(params[0]); | |
428 if (*key_index >= NKEYS) | |
429 { | |
430 printf_P(PSTR("Bad key index %d, max %d\n"), *key_index, NKEYS); | |
431 return -1; | |
432 } | |
433 | |
5 | 434 for (int i = 0, p = 0; i < bytes_len; i++, p += 2) |
2 | 435 { |
436 bytes[i] = (from_hex(params[p+2]) << 4) | from_hex(params[p+3]); | |
437 } | |
438 return 0; | |
439 } | |
440 | |
1 | 441 static void |
442 cmd_set_avr_key(const char *params) | |
443 { | |
2 | 444 uint8_t new_key[KEYLEN]; |
445 uint8_t key_index; | |
5 | 446 if (parse_key(params, &key_index, new_key, sizeof(new_key)) != 0) |
2 | 447 { |
1 | 448 return; |
0 | 449 } |
2 | 450 memcpy(avr_keys[key_index], new_key, sizeof(new_key)); |
8 | 451 #ifndef SIM_DEBUG |
2 | 452 eeprom_write(avr_keys, avr_keys); |
8 | 453 #endif |
20 | 454 printf_P(PSTR("Set key %d: "), key_index); |
455 printhex(new_key, sizeof(new_key), stdout); | |
456 putchar('\n'); | |
2 | 457 } |
458 | |
459 static void | |
460 cmd_hmac(const char *params) | |
461 { | |
11
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
462 uint8_t indata[2+HMACLEN] = {'H', ':'}; |
7 | 463 uint8_t outdata[HMACLEN]; |
2 | 464 uint8_t key_index; |
11
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
465 if (parse_key(params, &key_index, &indata[2], HMACLEN) != 0) |
2 | 466 { |
467 printf_P(PSTR("FAIL: Bad input\n")); | |
5 | 468 return; |
2 | 469 } |
0 | 470 |
8 | 471 #ifndef SIM_DEBUG |
5 | 472 long_delay(200); |
8 | 473 #endif |
5 | 474 |
11
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
475 hmac_sha1(outdata, avr_keys[key_index], KEYLEN*8, indata, sizeof(indata)*8); |
2 | 476 printf_P(PSTR("HMAC: ")); |
7 | 477 printhex(outdata, HMACLEN, stdout); |
20 | 478 putchar('\n'); |
5 | 479 } |
480 | |
481 static void | |
482 cmd_decrypt(const char *params) | |
483 { | |
11
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
484 uint8_t indata[HMACLEN+AESLEN]; // XXX |
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
485 // a temporary buffer |
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
486 uint8_t output[HMACLEN] = {'D', ':'}; |
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
487 _Static_assert(AESLEN+2 <= sizeof(output), "sufficient output buffer"); |
5 | 488 uint8_t key_index; |
11
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
489 if (parse_key(params, &key_index, indata, sizeof(indata)) != 0) |
5 | 490 { |
491 printf_P(PSTR("FAIL: Bad input\n")); | |
492 return; | |
493 } | |
494 | |
8 | 495 #ifndef SIM_DEBUG |
5 | 496 long_delay(200); |
8 | 497 #endif |
7 | 498 |
5 | 499 // check the signature |
11
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
500 memcpy(&output[2], &indata[HMACLEN], AESLEN); |
12 | 501 hmac_sha1(output, avr_keys[key_index], KEYLEN*8, output, (2+AESLEN)*8); |
5 | 502 |
11
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
503 if (memcmp(output, indata, HMACLEN) != 0) { |
5 | 504 printf_P(PSTR("FAIL: hmac mismatch\n")); |
505 } | |
506 | |
11
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
507 uint8_t tmpbuf[256]; |
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
508 aesInit(avr_keys[key_index], tmpbuf); |
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
509 aesDecrypt(&indata[HMACLEN], NULL); |
5 | 510 |
511 printf_P(PSTR("DECRYPTED: ")); | |
512 printhex(output, AESLEN, stdout); | |
20 | 513 putchar('\n'); |
2 | 514 } |
515 | |
516 static void | |
517 cmd_oneshot_reboot(const char *params) | |
518 { | |
519 uint32_t new_delay = strtoul(params, NULL, 10); | |
520 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
1 | 521 { |
2 | 522 oneshot_count = new_delay; |
1 | 523 } |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
524 printf_P(PSTR("oneshot new delay %lu\n"), new_delay); |
0 | 525 } |
526 | |
527 static void | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
528 clamp_params() |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
529 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
530 if (watchdog_long_limit < WATCHDOG_LONG_MIN |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
531 || watchdog_long_limit > WATCHDOG_LONG_MAX) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
532 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
533 watchdog_long_limit = WATCHDOG_LONG_DEFAULT; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
534 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
535 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
536 if (watchdog_short_limit != 0 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
537 && watchdog_short_limit < WATCHDOG_SHORT_MIN) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
538 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
539 watchdog_short_limit = 0; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
540 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
541 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
542 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
|
543 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
544 newboot_limit = NEWBOOT_DEFAULT; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
545 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
546 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
547 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
548 static void |
0 | 549 load_params() |
550 { | |
551 uint16_t magic; | |
552 eeprom_read(magic, magic); | |
553 if (magic == EXPECT_MAGIC) | |
554 { | |
1 | 555 eeprom_read(watchdog_long_limit, watchdog_long_limit); |
556 eeprom_read(watchdog_short_limit, watchdog_short_limit); | |
2 | 557 eeprom_read(newboot_limit, newboot_limit); |
1 | 558 } |
2 | 559 |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
560 clamp_params(); |
16 | 561 |
2 | 562 eeprom_read(avr_keys, avr_keys); |
0 | 563 } |
564 | |
2 | 565 static void |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
566 cmd_alive() |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
567 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
568 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
|
569 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
570 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
571 watchdog_long_count = 0; |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
572 watchdog_short_count = 0; |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
573 } |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
574 long_reboot_mode = 0; |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
575 } |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
576 |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
577 static void |
2 | 578 cmd_vcc() |
0 | 579 { |
2 | 580 uint16_t vcc = adc_vcc(); |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
581 uint16_t v5 = adc_5v(vcc); |
21 | 582 uint16_t temp = adc_temp(); |
583 // roughly? | |
584 uint16_t temp_deg = temp - 290; | |
585 printf_P(PSTR("vcc: %u mV\n" | |
586 "5v: %u mV\n" | |
587 "temp: %u mV (%dºC)\n"), | |
588 vcc, v5, temp, temp_deg); | |
0 | 589 } |
590 | |
20 | 591 void(*bootloader)() __attribute__ ((noreturn)) = (void*)0x7e00; |
592 | |
593 #if 0 | |
594 #ifndef PROG_PASSWORD | |
595 #define PROG_PASSWORD "Y2vvjxO5" | |
596 #endif | |
597 | |
598 static void | |
599 cmd_prog(const char* arg) | |
600 { | |
601 if (strcmp(arg, PROG_PASSWORD) != 0) | |
602 { | |
603 printf_P(PSTR("Bad prog password\n")); | |
604 return; | |
605 } | |
606 | |
607 // disable wdt | |
608 wdt_disable(); | |
609 | |
610 // disable interrupts | |
611 TIMSK0 = 0; | |
612 TIMSK1 = 0; | |
613 TIMSK2 = 0; | |
614 EIMSK = 0; | |
615 PCMSK0 = 0; | |
616 PCMSK1 = 0; | |
617 PCMSK2 = 0; | |
618 ACSR &= ~_BV(ACIE); | |
619 ADCSRA &= ~_BV(ADIE); | |
620 UCSR0B &= ~_BV(RXCIE0); | |
621 UCSR0B &= _BV(TXCIE0); | |
622 // doesn't do TWI, other uart, probably others | |
623 | |
624 _delay_ms(20); | |
625 | |
626 bootloader(); | |
627 } | |
628 #endif | |
629 | |
0 | 630 static void |
631 read_handler() | |
632 { | |
17 | 633 #define LOCAL_PSTR(x) const static char x ## _str[] PROGMEM = #x; |
634 #define LOCAL_HELP(x, d) const static char x ## _help[] PROGMEM = d; | |
635 | |
636 LOCAL_PSTR(set_params); | |
637 LOCAL_PSTR(set_key); | |
638 LOCAL_PSTR(oneshot); | |
639 LOCAL_PSTR(hmac); | |
640 LOCAL_PSTR(decrypt); | |
641 LOCAL_PSTR(alive); | |
642 LOCAL_PSTR(vcc); | |
643 LOCAL_PSTR(reset); | |
644 LOCAL_PSTR(newboot); | |
645 LOCAL_PSTR(oldboot); | |
20 | 646 LOCAL_PSTR(status); |
17 | 647 LOCAL_HELP(set_params, "<long_limit> <short_limit> <newboot_limit>"); |
648 LOCAL_HELP(set_key, "20_byte_hex>"); | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
649 LOCAL_HELP(oneshot, "<timeout>"); |
17 | 650 LOCAL_HELP(hmac, "<key_index> <20_byte_hex_data>"); |
651 LOCAL_HELP(decrypt, "<key_index> <20_byte_hmac|16_byte_aes_block>"); | |
652 | |
653 static const struct handler { | |
654 PGM_P name; | |
655 void(*cmd)(const char *param); | |
656 // existence of arg_help indicates if the cmd takes a parameter. | |
657 PGM_P arg_help; | |
20 | 658 } handlers[] PROGMEM = |
17 | 659 { |
20 | 660 {alive_str, cmd_alive, NULL}, |
661 {newboot_str, cmd_newboot, NULL}, | |
662 {oldboot_str, cmd_oldboot, NULL}, | |
663 {oneshot_str, cmd_oneshot_reboot, oneshot_help}, | |
664 {status_str, cmd_status, NULL}, | |
665 {hmac_str, cmd_hmac, hmac_help}, | |
666 {decrypt_str, cmd_decrypt, decrypt_help}, | |
17 | 667 {set_params_str, cmd_set_params, set_params_help}, |
668 {set_key_str, cmd_set_avr_key, set_key_help}, | |
669 {vcc_str, cmd_vcc, NULL}, | |
670 {reset_str, cmd_reset, NULL}, | |
671 }; | |
672 | |
673 if (readbuf[0] == '\0') | |
674 { | |
675 return; | |
676 } | |
677 | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
678 if (strcmp_P(readbuf, PSTR("help")) == 0) |
17 | 679 { |
680 printf_P(PSTR("Commands:---\n")); | |
681 for (int i = 0; i < sizeof(handlers) / sizeof(handlers[0]); i++) | |
682 { | |
683 struct handler h; | |
684 memcpy_P(&h, &handlers[i], sizeof(h)); | |
685 printf_P(h.name); | |
686 if (h.arg_help) | |
687 { | |
688 putchar(' '); | |
689 printf_P(h.arg_help); | |
690 } | |
691 putchar('\n'); | |
692 }; | |
693 printf_P(PSTR("---\n")); | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
694 return; |
17 | 695 } |
696 | |
697 for (int i = 0; i < sizeof(handlers) / sizeof(handlers[0]); i++) | |
698 { | |
699 struct handler h; | |
700 memcpy_P(&h, &handlers[i], sizeof(h)); | |
701 | |
702 const int h_len = strlen_P(h.name); | |
703 if (strncmp_P(readbuf, h.name, h_len) == 0) | |
704 { | |
705 if (h.arg_help) | |
706 { | |
707 if (readbuf[h_len] == ' ') | |
708 { | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
709 h.cmd(&readbuf[h_len+1]); |
17 | 710 return; |
711 } | |
712 } | |
713 else | |
714 { | |
715 if (readbuf[h_len] == '\0') | |
716 { | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
717 void(*void_cmd)() = h.cmd; |
17 | 718 void_cmd(); |
719 return; | |
720 } | |
721 } | |
722 } | |
723 } | |
724 | |
725 printf_P(PSTR("Bad command '%s'\n"), readbuf); | |
0 | 726 } |
727 | |
728 ISR(INT0_vect) | |
729 { | |
730 blink(); | |
731 _delay_ms(100); | |
732 blink(); | |
733 } | |
734 | |
735 ISR(USART_RX_vect) | |
736 { | |
737 char c = UDR0; | |
738 #ifdef HAVE_UART_ECHO | |
739 uart_putchar(c, NULL); | |
740 #endif | |
741 if (c == '\r' || c == '\n') | |
742 { | |
743 if (readpos > 0) | |
744 { | |
745 readbuf[readpos] = '\0'; | |
746 have_cmd = 1; | |
747 readpos = 0; | |
748 } | |
749 } | |
750 else | |
751 { | |
752 readbuf[readpos] = c; | |
753 readpos++; | |
754 if (readpos >= sizeof(readbuf)) | |
755 { | |
756 readpos = 0; | |
757 } | |
758 } | |
759 } | |
760 | |
2 | 761 ISR(TIMER1_COMPA_vect) |
0 | 762 { |
2 | 763 TCNT1 = 0; |
0 | 764 |
765 clock_epoch += TICK; | |
766 | |
1 | 767 // watchdogs count up, continuous |
768 if (watchdog_long_limit > 0) { | |
2 | 769 watchdog_long_count += TICK; |
1 | 770 if (watchdog_long_count >= watchdog_long_limit) |
771 { | |
772 watchdog_long_count = 0; | |
773 watchdog_long_hit = 1; | |
774 } | |
0 | 775 } |
776 | |
1 | 777 if (watchdog_short_limit > 0) { |
2 | 778 watchdog_short_count += TICK; |
1 | 779 if (watchdog_short_count >= watchdog_short_limit) |
780 { | |
781 watchdog_short_count = 0; | |
782 watchdog_short_hit = 1; | |
783 } | |
0 | 784 } |
785 | |
2 | 786 // newboot counts down |
1 | 787 if (newboot_count > 0) |
0 | 788 { |
2 | 789 newboot_count-=TICK; |
790 if (newboot_count <= 0) | |
1 | 791 { |
792 newboot_hit = 1; | |
2 | 793 newboot_count = 0; |
1 | 794 } |
0 | 795 } |
1 | 796 |
2 | 797 if (oneshot_count > 0) |
798 { | |
799 oneshot_count-=TICK; | |
800 if (oneshot_count <= 0) | |
801 { | |
802 oneshot_hit = 1; | |
803 oneshot_count = 0; | |
804 } | |
805 } | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
806 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
807 if (reboot_count > 0) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
808 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
809 reboot_count -= TICK; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
810 if (reboot_count <= 0) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
811 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
812 reboot_hit = 1; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
813 reboot_count = 0; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
814 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
815 } |
0 | 816 } |
817 | |
818 static void | |
819 idle_sleep() | |
820 { | |
821 set_sleep_mode(SLEEP_MODE_IDLE); | |
822 sleep_mode(); | |
823 } | |
824 | |
21 | 825 static void |
826 adc_generic(uint8_t admux, uint8_t *ret_num, uint16_t *ret_sum) | |
0 | 827 { |
828 PRR &= ~_BV(PRADC); | |
829 | |
830 // /16 prescaler | |
831 ADCSRA = _BV(ADEN) | _BV(ADPS2); | |
832 | |
833 // set to measure 1.1 reference | |
21 | 834 ADMUX = admux; |
22 | 835 |
836 // delay after setting reference etc, allow settling | |
837 long_delay(300); | |
0 | 838 // average a number of samples |
839 uint16_t sum = 0; | |
840 uint8_t num = 0; | |
841 for (uint8_t n = 0; n < 20; n++) | |
842 { | |
843 ADCSRA |= _BV(ADSC); | |
844 loop_until_bit_is_clear(ADCSRA, ADSC); | |
845 | |
846 uint8_t low_11 = ADCL; | |
847 uint8_t high_11 = ADCH; | |
848 uint16_t val = low_11 + (high_11 << 8); | |
849 | |
850 if (n >= 4) | |
851 { | |
852 sum += val; | |
853 num++; | |
854 } | |
855 } | |
856 ADCSRA = 0; | |
857 PRR |= _BV(PRADC); | |
858 | |
21 | 859 *ret_num = num; |
860 *ret_sum = sum; | |
861 } | |
862 | |
863 static uint16_t | |
864 adc_vcc() | |
865 { | |
866 const uint8_t mux = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); | |
867 uint16_t sum; | |
868 uint8_t num; | |
869 | |
870 adc_generic(mux, &num, &sum); | |
871 | |
0 | 872 //float res_volts = 1.1 * 1024 * num / sum; |
873 //return 1000 * res_volts; | |
874 return ((uint32_t)1100*1024*num) / sum; | |
875 } | |
876 | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
877 #define SCALER_5V 2 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
878 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
879 static uint16_t |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
880 adc_5v(uint16_t vcc) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
881 { |
21 | 882 // set to measure ADC4 against AVCC |
883 const uint8_t mux = _BV(REFS0) | _BV(MUX2); | |
884 uint16_t sum; | |
885 uint8_t num; | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
886 |
21 | 887 adc_generic(mux, &num, &sum); |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
888 |
21 | 889 return ((uint32_t)vcc*sum*SCALER_5V/(num*1024)); |
890 } | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
891 |
21 | 892 static uint16_t |
893 adc_temp() | |
894 { | |
895 // set to measure temperature against 1.1v reference. | |
22 | 896 const uint8_t mux = _BV(REFS0) | _BV(REFS1) | _BV(MUX3); |
21 | 897 uint16_t sum; |
898 uint8_t num; | |
899 | |
900 adc_generic(mux, &num, &sum); | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
901 |
21 | 902 // return the voltage |
22 | 903 |
904 return ((uint32_t)1100*sum) / (num*1024); | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
905 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
906 |
0 | 907 static void |
2 | 908 reboot_pi() |
909 { | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
910 printf_P(PSTR("Real reboot now\n")); |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
911 // pull it low for 200ms |
2 | 912 PORT_PI_RESET &= ~_BV(PIN_PI_RESET); |
913 DDR_PI_RESET |= _BV(PIN_PI_RESET); | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
914 _delay_ms(200); |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
915 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
916 PORT_PI_WARNING &= ~_BV(PIN_PI_WARNING); |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
917 DDR_PI_RESET &= ~_BV(PIN_PI_RESET); |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
918 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
919 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
920 static void |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
921 wait_reboot_pi() |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
922 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
923 PORT_PI_WARNING |= _BV(PIN_PI_WARNING); |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
924 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
925 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
926 reboot_count = WARNING_TIME; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
927 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
928 printf_P(PSTR("Rebooting in %hhu seconds\n"), reboot_count); |
2 | 929 } |
930 | |
931 static void | |
932 set_pi_boot_normal(uint8_t normal) | |
933 { | |
22 | 934 boot_normal_status = normal; |
2 | 935 PORT_PI_BOOT &= ~_BV(PIN_PI_BOOT); |
936 if (normal) | |
937 { | |
938 // tristate | |
939 DDR_PI_BOOT &= ~_BV(PIN_PI_BOOT); | |
940 } | |
941 else | |
942 { | |
943 // pull it low | |
944 DDR_PI_RESET |= _BV(PIN_PI_BOOT); | |
945 | |
946 } | |
947 } | |
948 | |
949 static void | |
950 check_flags() | |
951 { | |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
952 if (watchdog_long_hit) |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
953 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
954 // 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
|
955 if (long_reboot_mode) |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
956 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
957 cmd_newboot(); |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
958 } |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
959 long_reboot_mode ^= 1; |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
960 } |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
961 |
2 | 962 if (watchdog_long_hit |
963 || watchdog_short_hit | |
964 || oneshot_hit) | |
965 { | |
12 | 966 printf_P(PSTR("Rebooting! long %d, short %d, oneshot %d\n"), |
967 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
|
968 wait_reboot_pi(); |
2 | 969 } |
970 | |
971 if (newboot_hit) { | |
972 set_pi_boot_normal(0); | |
973 } | |
974 | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
975 if (reboot_hit) { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
976 reboot_pi(); |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
977 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
978 |
2 | 979 watchdog_long_hit = 0; |
980 watchdog_short_hit = 0; | |
981 newboot_hit = 0; | |
982 oneshot_hit = 0; | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
983 reboot_hit = 0; |
2 | 984 } |
985 | |
986 static void | |
0 | 987 do_comms() |
988 { | |
989 // avoid receiving rubbish, perhaps | |
990 uart_on(); | |
991 | |
992 // write sd card here? same 3.3v regulator... | |
993 | |
1 | 994 while (1) |
0 | 995 { |
1 | 996 wdt_reset(); |
2 | 997 |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
998 #ifdef SIM_DEBUG |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
999 if (sim_idx != last_sim_idx) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1000 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1001 last_sim_idx = sim_idx; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1002 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1003 #endif |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1004 |
2 | 1005 check_flags(); |
1006 | |
0 | 1007 if (have_cmd) |
1008 { | |
1009 have_cmd = 0; | |
1010 read_handler(); | |
1011 continue; | |
1012 } | |
1013 | |
1014 // wait for commands from the master | |
1015 idle_sleep(); | |
1016 } | |
1017 } | |
1018 | |
1019 static void | |
1020 blink() | |
1021 { | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1022 #if 0 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1023 PORT_ &= ~_BV(PIN_LED); |
0 | 1024 _delay_ms(1); |
1025 PORT_LED |= _BV(PIN_LED); | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1026 #endif |
0 | 1027 } |
1028 | |
1029 static void | |
22 | 1030 long_delay(uint16_t ms) |
0 | 1031 { |
22 | 1032 uint16_t iter = ms / 10; |
0 | 1033 |
22 | 1034 for (uint16_t i = 0; i < iter; i++) |
0 | 1035 { |
22 | 1036 _delay_ms(10); |
0 | 1037 } |
1038 } | |
1039 | |
1040 ISR(BADISR_vect) | |
1041 { | |
1042 //uart_on(); | |
1043 printf_P(PSTR("Bad interrupt\n")); | |
1044 } | |
1045 | |
20 | 1046 // disable watchdog on boot |
1047 void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3"))); | |
1048 void wdt_init(void) | |
1049 { | |
1050 MCUSR = 0; | |
1051 wdt_disable(); | |
1052 } | |
1053 | |
0 | 1054 int main(void) |
1055 { | |
17 | 1056 _Static_assert(F_CPU % 256 == 0, "clock prescaler remainder 0"); |
1057 _Static_assert(NEWBOOT_MAX < WATCHDOG_LONG_MIN, "newboot max shorter than watchdog min"); | |
20 | 1058 _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
|
1059 |
0 | 1060 setup_chip(); |
1061 blink(); | |
1062 | |
1063 stdout = &mystdout; | |
1064 uart_on(); | |
1065 | |
20 | 1066 long_delay(500); |
1067 printf_P(PSTR("Pi Watchdog\nMatt Johnston [email protected]\n")); | |
0 | 1068 |
2 | 1069 set_pi_boot_normal(0); |
1070 | |
0 | 1071 load_params(); |
1072 | |
1073 setup_tick_counter(); | |
1074 | |
1075 sei(); | |
1076 | |
8 | 1077 #if 0 |
1078 // encryption test | |
1079 cmd_set_avr_key("1 6161626263636464656566666767686800000000"); | |
1080 cmd_set_avr_key("2 7979757569696f6f646465656666717164646969"); | |
12 | 1081 //cmd_decrypt("1 ecd858ee07a8e16575723513d2d072a7565865e40ba302059bfc650d4491268448102119"); |
1082 cmd_decrypt("1 5a587b50fd48688bbda1b510cf9a3fab6fd4737b" "0ba302059bfc650d4491268448102119"); | |
1083 cmd_hmac("2 7979757569696f6f646465656666717164646969"); | |
8 | 1084 #endif |
1085 | |
1 | 1086 // doesn't return |
1087 do_comms(); | |
0 | 1088 |
1089 return 0; /* never reached */ | |
1090 } |