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