annotate main.c @ 16:8b1aeff120e9

add max/min sanity checks
author Matt Johnston <matt@ucc.asn.au>
date Thu, 13 Jun 2013 00:22:37 +0800
parents 915be6f0ff13
children 21717153e0f1
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
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
22 LOCKBITS = (LB_MODE_3 & BLB0_MODE_4 & BLB1_MODE_4);
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
23
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 // TICK should be 8 or less (8 untested). all timers need
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 // to be a multiple.
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
30 #define TICK 1
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
31 #define SLEEP_COMPARE (2000000/64) // == 31250 exactly
15
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
32 #define COUNTER_DIV (F_CPU / 2000000)
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
15
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
38 #define BAUD 38400
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 #define UBRR ((F_CPU)/8/(BAUD)-1)
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
4
fd28c7358ce8 more additions, set ports
Matt Johnston <matt@ucc.asn.au>
parents: 2
diff changeset
49 #define PORT_LED PORTD
fd28c7358ce8 more additions, set ports
Matt Johnston <matt@ucc.asn.au>
parents: 2
diff changeset
50 #define DDR_LED DDRD
fd28c7358ce8 more additions, set ports
Matt Johnston <matt@ucc.asn.au>
parents: 2
diff changeset
51 #define PIN_LED 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
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 uint8_t rem;
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
15
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
64 // OCR1A ticks COUNTER_DIV(=4) times a second, we divide it down.
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
65 static uint8_t counter_div = 0;
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
66
16
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
67 #define WATCHDOG_LONG_MIN (60L*40) // 40 mins
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
68 #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
69 #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
70
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
71 #define WATCHDOG_SHORT_MIN (60L*15) // 15 mins
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
72
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
73 #define NEWBOOT_DEFAULT (60*10) // 10 minutes
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
74 #define NEWBOOT_MIN (60*2) // 2 minutes
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
75 #define NEWBOOT_MAX (60*30) // 30 mins
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
76
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
77 // eeprom-settable parameters, default values defined here.
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
78 // 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
79 static uint32_t watchdog_long_limit = WATCHDOG_LONG_DEFAULT;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
80 static uint32_t watchdog_short_limit = 0;
16
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
81 static uint32_t newboot_limit = NEWBOOT_DEFAULT;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
83 // avr proves itself
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
84 static uint8_t avr_keys[NKEYS][KEYLEN] = {{0}};
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 // ---- Atomic guards required accessing these variables
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 // clock_epoch in seconds
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 static uint32_t clock_epoch;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
89 // watchdog counts up
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
90 static uint32_t watchdog_long_count;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
91 static uint32_t watchdog_short_count;
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
92 // newboot counts down
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
93 static uint32_t newboot_count;
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
94 // oneshot counts down
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
95 static uint32_t oneshot_count;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
96
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 // ---- End atomic guards required
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 // boolean flags
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
100 static uint8_t watchdog_long_hit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
101 static uint8_t watchdog_short_hit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
102 static uint8_t newboot_hit;
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
103 static uint8_t oneshot_hit;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104
15
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
105 // 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
106 // 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
107 // command
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
108 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
109
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 static uint8_t readpos;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
111 static char readbuf[50];
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 static uint8_t have_cmd;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 int uart_putchar(char c, FILE *stream);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 static void long_delay(int ms);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 static void blink();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 static uint16_t adc_vcc();
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
118 static void set_pi_boot_normal(uint8_t normal);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 _FDEV_SETUP_WRITE);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 // thanks to http://projectgus.com/2010/07/eeprom-access-with-arduino/
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 #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
125 #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
126 #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
127 #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
128
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
129 #define EXPECT_MAGIC 0xdf83
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 struct __attribute__ ((__packed__)) __eeprom_data {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
132 uint32_t watchdog_long_limit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
133 uint32_t watchdog_short_limit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
134 uint32_t newboot_limit;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
136 uint8_t avr_keys[NKEYS][KEYLEN];
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 uint16_t magic;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 };
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 // Very first setup
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 setup_chip()
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 cli();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 // stop watchdog timer (might have been used to cause a reset)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 wdt_reset();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 MCUSR &= ~_BV(WDRF);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 WDTCSR |= _BV(WDCE) | _BV(WDE);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 WDTCSR = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152
15
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
153 // 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
154 wdt_enable(WDTO_8S);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
155
15
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
156 // Set scaler to /1, -> clock to 8mhz
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 CLKPR = _BV(CLKPCE);
15
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
158 CLKPR = 0;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 // enable pullups
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
161 // XXX matt pihelp
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
162 //PORTB = 0xff; // XXX change when using SPI
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
163 //PORTD = 0xff;
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
164 //PORTC = 0xff;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 // 3.3v power for bluetooth and SD
15
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
167 //DDR_LED |= _BV(PIN_LED);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
169 #if 0
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 // set pullup
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 PORTD |= _BV(PD2);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 // INT0 setup
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 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
174 EIMSK = _BV(INT0);
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
175 #endif
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 // comparator disable
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 ACSR = _BV(ACD);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 // disable adc pin input buffers
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 DIDR0 = 0x3F; // acd0-adc5
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 DIDR1 = (1<<AIN1D)|(1<<AIN0D); // ain0/ain1
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 sei();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 get_epoch_ticks(struct epoch_ticks *t)
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 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 t->ticks = clock_epoch;
7
76f3ed943180 a few fixes
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
193 t->rem = TCNT1;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 setup_tick_counter()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
200 // set up counter1
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
201
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202 // set up counter2.
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203 // COM21 COM20 Set OC2 on Compare Match (p116)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 // WGM21 Clear counter on compare
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 //TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206 // toggle on match
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
207 TCCR1A = _BV(COM1A0);
8
03da5ff767e9 Some debug ifdefs
Matt Johnston <matt@ucc.asn.au>
parents: 7
diff changeset
208 #ifdef SIM_DEBUG
03da5ff767e9 Some debug ifdefs
Matt Johnston <matt@ucc.asn.au>
parents: 7
diff changeset
209 // systemclock/8
03da5ff767e9 Some debug ifdefs
Matt Johnston <matt@ucc.asn.au>
parents: 7
diff changeset
210 TCCR1B = _BV(CS11);
03da5ff767e9 Some debug ifdefs
Matt Johnston <matt@ucc.asn.au>
parents: 7
diff changeset
211 #else
7
76f3ed943180 a few fixes
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
212 // systemclock/64
76f3ed943180 a few fixes
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
213 TCCR1B = _BV(CS11) | _BV(CS10);
8
03da5ff767e9 Some debug ifdefs
Matt Johnston <matt@ucc.asn.au>
parents: 7
diff changeset
214 #endif
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
215 TCNT1 = 0;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
216 OCR1A = SLEEP_COMPARE;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217 // interrupt
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
218 TIMSK1 = _BV(OCIE1A);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 uart_on()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 // Power reduction register
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 PRR &= ~_BV(PRUSART0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227 // 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
228 // baud rate
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 UBRR0H = (unsigned char)(UBRR >> 8);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 UBRR0L = (unsigned char)UBRR;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 // set 2x clock, improves accuracy of UBRR
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232 UCSR0A |= _BV(U2X0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234 //8N1
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239 uart_off()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241 // Turn off interrupts and disable tx/rx
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242 UCSR0B = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 // Power reduction register
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245 PRR |= _BV(PRUSART0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248 int
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 uart_putchar(char c, FILE *stream)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 // XXX could perhaps sleep in the loop for power.
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252 if (c == '\n')
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254 loop_until_bit_is_set(UCSR0A, UDRE0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 UDR0 = '\r';
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257 loop_until_bit_is_set(UCSR0A, UDRE0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258 UDR0 = c;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
259 if (c == '\r')
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 loop_until_bit_is_set(UCSR0A, UDRE0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
262 UDR0 = '\n';
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264 return (unsigned char)c;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268 cmd_reset()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270 printf_P(PSTR("reset\n"));
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 _delay_ms(100);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272 cli(); // disable interrupts
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 wdt_enable(WDTO_15MS); // enable watchdog
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274 while(1); // wait for watchdog to reset processor
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
277 static void
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
278 cmd_newboot()
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
279 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
280 set_pi_boot_normal(1);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
281 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
282 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
283 newboot_count = newboot_limit;
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 printf_P(PSTR("newboot for %d secs"), newboot_limit);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
286 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
287
15
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
288 static void
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
289 cmd_oldboot()
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
290 {
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
291 set_pi_boot_normal(0);
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
292 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
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 newboot_count = 0;
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
295 }
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
296 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
297 }
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
298
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
299
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
300 static void
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
301 cmd_get_params()
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
302 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
303 uint32_t cur_watchdog_long, cur_watchdog_short, cur_newboot, cur_oneshot;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
304 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
305 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
306 cur_watchdog_long = watchdog_long_count;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
307 cur_watchdog_short = watchdog_short_count;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
308 cur_newboot = newboot_count;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
309 cur_oneshot = oneshot_count;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
310 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
311
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
312 printf_P(PSTR("limit (count) : watchdog_long %lu (%lu) watchdog_short %lu (%lu) newboot %lu (%lu) oneshot (%lu)\n"),
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
313 watchdog_long_limit,
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
314 cur_watchdog_long,
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
315 watchdog_short_limit,
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
316 cur_watchdog_short,
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
317 newboot_limit,
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
318 cur_newboot,
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
319 cur_oneshot);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
320 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
321
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
322 static void
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
323 cmd_set_params(const char *params)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
324 {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
325 uint32_t new_watchdog_long_limit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
326 uint32_t new_watchdog_short_limit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
327 uint32_t new_newboot_limit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
328
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
329 int ret = sscanf_P(params, PSTR("%lu %lu %lu"),
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
330 &new_watchdog_long_limit,
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
331 &new_watchdog_short_limit,
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
332 &new_newboot_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
333
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 if (ret != 3)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
336 {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
337 printf_P(PSTR("Bad values\n"));
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
338 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
339 else
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
340 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
341 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
342 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
343 eeprom_write(new_watchdog_long_limit, watchdog_long_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
344 eeprom_write(new_watchdog_short_limit, watchdog_short_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
345 eeprom_write(new_newboot_limit, newboot_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
346 uint16_t magic = EXPECT_MAGIC;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
347 eeprom_write(magic, magic);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
348 }
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
349 printf_P(PSTR("set_params for next boot\n"));
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
350 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
351 new_watchdog_long_limit,
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
352 new_watchdog_short_limit,
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
353 new_newboot_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
354
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
355 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
356 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
357
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
358 uint8_t from_hex(char c)
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 if (c >= '0' && c <= '9') {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
361 return c-'0';
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
362 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
363 if (c >= 'a' && c <= 'f') {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
364 return c-'a' + 0xa;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
365 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
366 if (c >= 'A' && c <= 'F') {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
367 return c-'A' + 0xa;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
368 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
369 return 0;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
370 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
371
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
372 static void
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
373 printhex_nibble(const unsigned char b, FILE *stream)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
374 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
375 unsigned char c = b & 0x0f;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
376 if ( c > 9 ) {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
377 c += 'A'-10;
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 else {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
380 c += '0';
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
381 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
382 fputc(c, stream);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
383 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
384
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
385 void
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
386 printhex_byte(const unsigned char b, FILE *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 printhex_nibble( b >> 4, stream);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
389 printhex_nibble( b, stream);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
390 }
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 void
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
393 printhex(uint8_t *id, uint8_t n, FILE *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 for (uint8_t i = 0; i < n; i++)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
396 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
397 if (i > 0)
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 fputc(' ', stream);
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) {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
411 printf_P(PSTR("Wrong length key\n"));
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
412 return -1;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
413 }
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 if (params[1] != ' ')
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
416 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
417 printf_P(PSTR("Missing space\n"));
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
418 return -1;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
419 }
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 *key_index = from_hex(params[0]);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
422 if (*key_index >= NKEYS)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
423 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
424 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
425 return -1;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
426 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
427
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
428 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
429 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
430 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
431 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
432 return 0;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
433 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
434
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
435 static void
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
436 cmd_set_avr_key(const char *params)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
437 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
438 uint8_t new_key[KEYLEN];
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
439 uint8_t key_index;
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
440 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
441 {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
442 return;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
443 }
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
444 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
445 #ifndef SIM_DEBUG
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
446 eeprom_write(avr_keys, avr_keys);
8
03da5ff767e9 Some debug ifdefs
Matt Johnston <matt@ucc.asn.au>
parents: 7
diff changeset
447 #endif
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
448 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
449
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
450 static void
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
451 cmd_hmac(const char *params)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
452 {
11
e83b35e864d7 hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents: 8
diff changeset
453 uint8_t indata[2+HMACLEN] = {'H', ':'};
7
76f3ed943180 a few fixes
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
454 uint8_t outdata[HMACLEN];
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
455 uint8_t key_index;
11
e83b35e864d7 hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents: 8
diff changeset
456 if (parse_key(params, &key_index, &indata[2], HMACLEN) != 0)
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
457 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
458 printf_P(PSTR("FAIL: Bad input\n"));
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
459 return;
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
460 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
461
8
03da5ff767e9 Some debug ifdefs
Matt Johnston <matt@ucc.asn.au>
parents: 7
diff changeset
462 #ifndef SIM_DEBUG
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
463 long_delay(200);
8
03da5ff767e9 Some debug ifdefs
Matt Johnston <matt@ucc.asn.au>
parents: 7
diff changeset
464 #endif
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
465
11
e83b35e864d7 hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents: 8
diff changeset
466 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
467 printf_P(PSTR("HMAC: "));
7
76f3ed943180 a few fixes
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
468 printhex(outdata, HMACLEN, stdout);
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
469 fputc('\n', stdout);
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
470 }
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
471
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
472 static void
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
473 cmd_decrypt(const char *params)
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
474 {
11
e83b35e864d7 hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents: 8
diff changeset
475 uint8_t indata[HMACLEN+AESLEN]; // XXX
e83b35e864d7 hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents: 8
diff changeset
476 // a temporary buffer
e83b35e864d7 hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents: 8
diff changeset
477 uint8_t output[HMACLEN] = {'D', ':'};
e83b35e864d7 hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents: 8
diff changeset
478 _Static_assert(AESLEN+2 <= sizeof(output), "sufficient output buffer");
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
479 uint8_t key_index;
11
e83b35e864d7 hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents: 8
diff changeset
480 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
481 {
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
482 printf_P(PSTR("FAIL: Bad input\n"));
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
483 return;
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
484 }
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
485
8
03da5ff767e9 Some debug ifdefs
Matt Johnston <matt@ucc.asn.au>
parents: 7
diff changeset
486 #ifndef SIM_DEBUG
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
487 long_delay(200);
8
03da5ff767e9 Some debug ifdefs
Matt Johnston <matt@ucc.asn.au>
parents: 7
diff changeset
488 #endif
7
76f3ed943180 a few fixes
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
489
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
490 // check the signature
11
e83b35e864d7 hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents: 8
diff changeset
491 memcpy(&output[2], &indata[HMACLEN], AESLEN);
12
aec45c673a60 Fix decryption
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
492 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
493
11
e83b35e864d7 hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents: 8
diff changeset
494 if (memcmp(output, indata, HMACLEN) != 0) {
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
495 printf_P(PSTR("FAIL: hmac mismatch\n"));
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
496 }
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 uint8_t tmpbuf[256];
e83b35e864d7 hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents: 8
diff changeset
499 aesInit(avr_keys[key_index], tmpbuf);
e83b35e864d7 hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents: 8
diff changeset
500 aesDecrypt(&indata[HMACLEN], NULL);
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
501
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
502 printf_P(PSTR("DECRYPTED: "));
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
503 printhex(output, AESLEN, stdout);
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
504 fputc('\n', stdout);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
505 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
506
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
507 static void
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
508 cmd_oneshot_reboot(const char *params)
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 uint32_t new_delay = strtoul(params, NULL, 10);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
511 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
512 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
513 oneshot_count = new_delay;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
514 }
15
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
515 printf_P(PSTR("oneshot new delay %lu\n"), new_delay);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
516 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
517
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
518 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
519 load_params()
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 uint16_t magic;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
522 eeprom_read(magic, magic);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
523 if (magic == EXPECT_MAGIC)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
524 {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
525 eeprom_read(watchdog_long_limit, watchdog_long_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
526 eeprom_read(watchdog_short_limit, watchdog_short_limit);
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
527 eeprom_read(newboot_limit, newboot_limit);
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
528 }
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
529
16
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
530 if (watchdog_long_limit < WATCHDOG_LONG_MIN
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
531 || watchdog_long_limit > WATCHDOG_LONG_MAX)
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
532 {
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
533 watchdog_long_limit = WATCHDOG_LONG_DEFAULT;
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
534 }
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
535
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
536 if (watchdog_short_limit != 0
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
537 && watchdog_short_limit < WATCHDOG_SHORT_MIN)
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
538 {
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
539 watchdog_short_limit = 0;
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
540 }
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
541
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
542 if (newboot_limit < NEWBOOT_MIN || newboot_limit > NEWBOOT_MAX)
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
543 {
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
544 newboot_limit = NEWBOOT_DEFAULT;
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
545 }
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
546
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
547 _Static_assert(NEWBOOT_MAX < WATCHDOG_LONG_MIN, "newboot max shorter than watchdog min");
8b1aeff120e9 add max/min sanity checks
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
548
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
549 eeprom_read(avr_keys, avr_keys);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
550 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
551
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
552 static void
15
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
553 cmd_alive()
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
554 {
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
555 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
556 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
557 {
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
558 watchdog_long_count = 0;
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
559 watchdog_short_count = 0;
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
560 }
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
561 long_reboot_mode = 0;
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
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
564 static void
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
565 cmd_vcc()
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
566 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
567 uint16_t vcc = adc_vcc();
12
aec45c673a60 Fix decryption
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
568 printf_P(PSTR("vcc: %u mV\n"), vcc);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
569 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
570
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
571 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
572 read_handler()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
573 {
15
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
574 // TODO: make this an array, print automatic help
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
575 if (strcmp_P(readbuf, PSTR("get_params")) == 0)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
576 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
577 cmd_get_params();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
578 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
579 else if (strncmp_P(readbuf, PSTR("set_params "), 11) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
580 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
581 cmd_set_params(&readbuf[11]);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
582 }
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
583 else if (strncmp_P(readbuf, PSTR("set_key "), 8) == 0)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
584 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
585 cmd_set_avr_key(&readbuf[8]);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
586 }
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
587 else if (strncmp_P(readbuf, PSTR("oneshot "), 8) == 0)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
588 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
589 cmd_oneshot_reboot(&readbuf[8]);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
590 }
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
591 else if (strncmp_P(readbuf, PSTR("hmac "), 5) == 0)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
592 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
593 cmd_hmac(&readbuf[5]);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
594 }
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
595 else if (strncmp_P(readbuf, PSTR("decrypt "), 8) == 0)
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
596 {
12
aec45c673a60 Fix decryption
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
597 cmd_decrypt(&readbuf[8]);
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
598 }
15
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
599 else if (strcmp_P(readbuf, PSTR("alive")) == 0)
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
600 {
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
601 cmd_alive();
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
602 }
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
603 else if (strcmp_P(readbuf, PSTR("vcc")) == 0)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
604 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
605 cmd_vcc();
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
606 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
607 else if (strcmp_P(readbuf, PSTR("reset")) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
608 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
609 cmd_reset();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
610 }
4
fd28c7358ce8 more additions, set ports
Matt Johnston <matt@ucc.asn.au>
parents: 2
diff changeset
611 else if (strcmp_P(readbuf, PSTR("newboot")) == 0)
fd28c7358ce8 more additions, set ports
Matt Johnston <matt@ucc.asn.au>
parents: 2
diff changeset
612 {
fd28c7358ce8 more additions, set ports
Matt Johnston <matt@ucc.asn.au>
parents: 2
diff changeset
613 cmd_newboot();
fd28c7358ce8 more additions, set ports
Matt Johnston <matt@ucc.asn.au>
parents: 2
diff changeset
614 }
15
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
615 else if (strcmp_P(readbuf, PSTR("oldboot")) == 0)
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
616 {
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
617 cmd_oldboot();
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
618 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
619 else
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
620 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
621 printf_P(PSTR("Bad command '%s'\n"), readbuf);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
622 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
623 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
624
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
625 ISR(INT0_vect)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
626 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
627 blink();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
628 _delay_ms(100);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
629 blink();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
630 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
631
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
632 ISR(USART_RX_vect)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
633 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
634 char c = UDR0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
635 #ifdef HAVE_UART_ECHO
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
636 uart_putchar(c, NULL);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
637 #endif
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
638 if (c == '\r' || c == '\n')
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
639 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
640 if (readpos > 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
641 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
642 readbuf[readpos] = '\0';
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
643 have_cmd = 1;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
644 readpos = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
645 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
646 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
647 else
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
648 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
649 readbuf[readpos] = c;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
650 readpos++;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
651 if (readpos >= sizeof(readbuf))
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
652 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
653 readpos = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
654 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
655 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
656 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
657
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
658 ISR(TIMER1_COMPA_vect)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
659 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
660 TCNT1 = 0;
15
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
661 counter_div++;
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
662 if (counter_div < COUNTER_DIV)
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
663 {
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
664 return;
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
665 }
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
666 counter_div = 0;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
667
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
668 clock_epoch += TICK;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
669
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
670 // watchdogs count up, continuous
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
671 if (watchdog_long_limit > 0) {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
672 watchdog_long_count += TICK;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
673 if (watchdog_long_count >= watchdog_long_limit)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
674 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
675 watchdog_long_count = 0;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
676 watchdog_long_hit = 1;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
677 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
678 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
679
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
680 if (watchdog_short_limit > 0) {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
681 watchdog_short_count += TICK;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
682 if (watchdog_short_count >= watchdog_short_limit)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
683 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
684 watchdog_short_count = 0;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
685 watchdog_short_hit = 1;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
686 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
687 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
688
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
689 // newboot counts down
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
690 if (newboot_count > 0)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
691 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
692 newboot_count-=TICK;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
693 if (newboot_count <= 0)
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
694 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
695 newboot_hit = 1;
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
696 newboot_count = 0;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
697 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
698 }
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
699
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
700 if (oneshot_count > 0)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
701 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
702 oneshot_count-=TICK;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
703 if (oneshot_count <= 0)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
704 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
705 oneshot_hit = 1;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
706 oneshot_count = 0;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
707 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
708 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
709 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
710
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
711 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
712 idle_sleep()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
713 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
714 set_sleep_mode(SLEEP_MODE_IDLE);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
715 sleep_mode();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
716 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
717
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
718 static uint16_t
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
719 adc_vcc()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
720 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
721 PRR &= ~_BV(PRADC);
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 // /16 prescaler
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
724 ADCSRA = _BV(ADEN) | _BV(ADPS2);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
725
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
726 // set to measure 1.1 reference
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
727 ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
728 // average a number of samples
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
729 uint16_t sum = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
730 uint8_t num = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
731 for (uint8_t n = 0; n < 20; n++)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
732 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
733 ADCSRA |= _BV(ADSC);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
734 loop_until_bit_is_clear(ADCSRA, ADSC);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
735
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
736 uint8_t low_11 = ADCL;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
737 uint8_t high_11 = ADCH;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
738 uint16_t val = low_11 + (high_11 << 8);
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 if (n >= 4)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
741 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
742 sum += val;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
743 num++;
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 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
746 ADCSRA = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
747 PRR |= _BV(PRADC);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
748
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
749 //float res_volts = 1.1 * 1024 * num / sum;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
750 //return 1000 * res_volts;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
751 return ((uint32_t)1100*1024*num) / sum;
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 static void
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
755 reboot_pi()
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
756 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
757 // pull it low for 30ms
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
758 PORT_PI_RESET &= ~_BV(PIN_PI_RESET);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
759 DDR_PI_RESET |= _BV(PIN_PI_RESET);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
760 _delay_ms(30);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
761 DDR_PI_RESET &= ~_BV(PIN_PI_RESET);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
762 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
763
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
764 static void
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
765 set_pi_boot_normal(uint8_t normal)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
766 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
767 PORT_PI_BOOT &= ~_BV(PIN_PI_BOOT);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
768 if (normal)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
769 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
770 // tristate
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
771 DDR_PI_BOOT &= ~_BV(PIN_PI_BOOT);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
772 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
773 else
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
774 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
775 // pull it low
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
776 DDR_PI_RESET |= _BV(PIN_PI_BOOT);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
777
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
778 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
779 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
780
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
781 static void
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
782 check_flags()
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
783 {
15
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
784 if (watchdog_long_hit)
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
785 {
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
786 // 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
787 if (long_reboot_mode)
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
788 {
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
789 cmd_newboot();
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
790 }
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
791 long_reboot_mode ^= 1;
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
792 }
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
793
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
794 if (watchdog_long_hit
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
795 || watchdog_short_hit
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
796 || oneshot_hit)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
797 {
12
aec45c673a60 Fix decryption
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
798 printf_P(PSTR("Rebooting! long %d, short %d, oneshot %d\n"),
aec45c673a60 Fix decryption
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
799 watchdog_long_hit, watchdog_short_hit, oneshot_hit);
aec45c673a60 Fix decryption
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
800 long_delay(300);
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
801 reboot_pi();
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
802 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
803
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
804 if (newboot_hit) {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
805 set_pi_boot_normal(0);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
806 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
807
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
808 watchdog_long_hit = 0;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
809 watchdog_short_hit = 0;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
810 newboot_hit = 0;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
811 oneshot_hit = 0;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
812 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
813
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
814 static void
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
815 do_comms()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
816 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
817 // avoid receiving rubbish, perhaps
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
818 uart_on();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
819
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
820 // write sd card here? same 3.3v regulator...
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
821
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
822 while (1)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
823 {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
824 wdt_reset();
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
825
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
826 check_flags();
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
827
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
828 if (have_cmd)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
829 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
830 have_cmd = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
831 read_handler();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
832 continue;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
833 }
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 // wait for commands from the master
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
836 idle_sleep();
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 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
839
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
840 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
841 blink()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
842 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
843 PORT_LED &= ~_BV(PIN_LED);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
844 _delay_ms(1);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
845 PORT_LED |= _BV(PIN_LED);
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 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
849 long_delay(int ms)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
850 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
851 int iter = ms / 100;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
852
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
853 for (int i = 0; i < iter; i++)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
854 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
855 _delay_ms(100);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
856 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
857 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
858
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
859 ISR(BADISR_vect)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
860 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
861 //uart_on();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
862 printf_P(PSTR("Bad interrupt\n"));
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
863 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
864
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
865 int main(void)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
866 {
15
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
867 _Static_assert(F_CPU % 2000000 == 0, "F_CPU is a multiple of 2mhz for counter division");
915be6f0ff13 fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents: 12
diff changeset
868
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
869 setup_chip();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
870 blink();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
871
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
872 stdout = &mystdout;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
873 uart_on();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
874
12
aec45c673a60 Fix decryption
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
875 printf_P(PSTR("Pi Watchdog\nMatt Johnston [email protected]"));
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
876
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
877 set_pi_boot_normal(0);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
878
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
879 load_params();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
880
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
881 setup_tick_counter();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
882
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
883 sei();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
884
8
03da5ff767e9 Some debug ifdefs
Matt Johnston <matt@ucc.asn.au>
parents: 7
diff changeset
885 #if 0
03da5ff767e9 Some debug ifdefs
Matt Johnston <matt@ucc.asn.au>
parents: 7
diff changeset
886 // encryption test
03da5ff767e9 Some debug ifdefs
Matt Johnston <matt@ucc.asn.au>
parents: 7
diff changeset
887 cmd_set_avr_key("1 6161626263636464656566666767686800000000");
03da5ff767e9 Some debug ifdefs
Matt Johnston <matt@ucc.asn.au>
parents: 7
diff changeset
888 cmd_set_avr_key("2 7979757569696f6f646465656666717164646969");
12
aec45c673a60 Fix decryption
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
889 //cmd_decrypt("1 ecd858ee07a8e16575723513d2d072a7565865e40ba302059bfc650d4491268448102119");
aec45c673a60 Fix decryption
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
890 cmd_decrypt("1 5a587b50fd48688bbda1b510cf9a3fab6fd4737b" "0ba302059bfc650d4491268448102119");
aec45c673a60 Fix decryption
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
891 cmd_hmac("2 7979757569696f6f646465656666717164646969");
8
03da5ff767e9 Some debug ifdefs
Matt Johnston <matt@ucc.asn.au>
parents: 7
diff changeset
892 #endif
03da5ff767e9 Some debug ifdefs
Matt Johnston <matt@ucc.asn.au>
parents: 7
diff changeset
893
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
894 // doesn't return
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
895 do_comms();
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
896
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
897 return 0; /* never reached */
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
898 }