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