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