annotate main.c @ 21:83a0663c082f

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