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