annotate main.c @ 38:9e888708f33d

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