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