annotate main.c @ 5:87c8d0a11906

make it work
author Matt Johnston <matt@ucc.asn.au>
date Wed, 05 Jun 2013 21:29:02 +0800
parents fd28c7358ce8
children 76f3ed943180
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
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
32 #define NKEYS 10
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
33 #define HMACLEN 20
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
34 #define AESLEN 16
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
35 #define KEYLEN HMACLEN
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
36
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 #define BAUD 19200
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 #define UBRR ((F_CPU)/8/(BAUD)-1)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
40 #define PORT_PI_BOOT PORTD
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
41 #define DDR_PI_BOOT DDRD
4
fd28c7358ce8 more additions, set ports
Matt Johnston <matt@ucc.asn.au>
parents: 2
diff changeset
42 #define PIN_PI_BOOT PD5
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
43
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
44 #define PORT_PI_RESET PORTD
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
45 #define DDR_PI_RESET DDRD
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
46 #define PIN_PI_RESET PD6
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
47
4
fd28c7358ce8 more additions, set ports
Matt Johnston <matt@ucc.asn.au>
parents: 2
diff changeset
48 #define PORT_LED PORTD
fd28c7358ce8 more additions, set ports
Matt Johnston <matt@ucc.asn.au>
parents: 2
diff changeset
49 #define DDR_LED DDRD
fd28c7358ce8 more additions, set ports
Matt Johnston <matt@ucc.asn.au>
parents: 2
diff changeset
50 #define PIN_LED PD7
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 // #define HAVE_UART_ECHO
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 // stores a value of clock_epoch combined with the remainder of TCNT2,
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 // for 1/32 second accuracy
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 struct epoch_ticks
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 uint32_t ticks;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 // remainder
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 uint8_t rem;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 };
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
63 // eeprom-settable parameters, default values defined here.
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
64 // all timeouts should be a multiple of TICK
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
65 static uint32_t watchdog_long_limit = (60L*60*24);
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
66 static uint32_t watchdog_short_limit = 0;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
67 static uint32_t newboot_limit = 60*10;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
69 // avr proves itself
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
70 static uint8_t avr_keys[NKEYS][KEYLEN] = {{0}};
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 // ---- Atomic guards required accessing these variables
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 // clock_epoch in seconds
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 static uint32_t clock_epoch;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
75 // watchdog counts up
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
76 static uint32_t watchdog_long_count;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
77 static uint32_t watchdog_short_count;
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
78 // newboot counts down
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
79 static uint32_t newboot_count;
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
80 // oneshot counts down
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
81 static uint32_t oneshot_count;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
82
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 // ---- End atomic guards required
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 // boolean flags
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
86 static uint8_t watchdog_long_hit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
87 static uint8_t watchdog_short_hit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
88 static uint8_t newboot_hit;
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
89 static uint8_t oneshot_hit;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 static uint8_t readpos;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
92 static char readbuf[50];
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 static uint8_t have_cmd;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 int uart_putchar(char c, FILE *stream);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 static void long_delay(int ms);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 static void blink();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 static uint16_t adc_vcc();
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
99 static void set_pi_boot_normal(uint8_t normal);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 _FDEV_SETUP_WRITE);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 // thanks to http://projectgus.com/2010/07/eeprom-access-with-arduino/
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 #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
106 #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
107 #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
108 #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
109
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
110 #define EXPECT_MAGIC 0xdf83
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 struct __attribute__ ((__packed__)) __eeprom_data {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
113 uint32_t watchdog_long_limit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
114 uint32_t watchdog_short_limit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
115 uint32_t newboot_limit;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
117 uint8_t avr_keys[NKEYS][KEYLEN];
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 uint16_t magic;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 };
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 // Very first setup
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 setup_chip()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 cli();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 // stop watchdog timer (might have been used to cause a reset)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 wdt_reset();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 MCUSR &= ~_BV(WDRF);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 WDTCSR |= _BV(WDCE) | _BV(WDE);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 WDTCSR = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
134 // set to 8S, in case sha1 is slow etc.
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
135 wdt_enable(WDTO_8S);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
136
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 // Set clock to 2mhz
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 CLKPR = _BV(CLKPCE);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 CLKPR = _BV(CLKPS1);
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 // enable pullups
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
142 // XXX matt pihelp
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
143 //PORTB = 0xff; // XXX change when using SPI
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
144 //PORTD = 0xff;
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
145 //PORTC = 0xff;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 // 3.3v power for bluetooth and SD
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 DDR_LED |= _BV(PIN_LED);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
150 #if 0
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 // set pullup
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 PORTD |= _BV(PD2);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 // INT0 setup
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 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
155 EIMSK = _BV(INT0);
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
156 #endif
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 // comparator disable
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 ACSR = _BV(ACD);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 // disable adc pin input buffers
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 DIDR0 = 0x3F; // acd0-adc5
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 DIDR1 = (1<<AIN1D)|(1<<AIN0D); // ain0/ain1
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 sei();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 get_epoch_ticks(struct epoch_ticks *t)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 t->ticks = clock_epoch;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 t->rem = TCNT2;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 }
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
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 setup_tick_counter()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
181 // set up counter1
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
182
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 // set up counter2.
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 // COM21 COM20 Set OC2 on Compare Match (p116)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 // WGM21 Clear counter on compare
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 //TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 // toggle on match
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
188 TCCR1A = _BV(COM1A0);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
189 // systemclock/1024
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
190 TCCR1B = _BV(CS12) | _BV(CS10);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
191 TCNT1 = 0;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
192 OCR1A = SLEEP_COMPARE;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 // interrupt
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
194 TIMSK1 = _BV(OCIE1A);
0
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 uart_on()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200 // Power reduction register
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 PRR &= ~_BV(PRUSART0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203 // 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
204 // baud rate
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 UBRR0H = (unsigned char)(UBRR >> 8);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206 UBRR0L = (unsigned char)UBRR;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 // set 2x clock, improves accuracy of UBRR
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208 UCSR0A |= _BV(U2X0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210 //8N1
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215 uart_off()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217 // Turn off interrupts and disable tx/rx
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
218 UCSR0B = 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 // Power reduction register
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 PRR |= _BV(PRUSART0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 }
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 int
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 uart_putchar(char c, FILE *stream)
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 // XXX could perhaps sleep in the loop for power.
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228 if (c == '\n')
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 loop_until_bit_is_set(UCSR0A, UDRE0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 UDR0 = '\r';
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 loop_until_bit_is_set(UCSR0A, UDRE0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234 UDR0 = c;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235 if (c == '\r')
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 loop_until_bit_is_set(UCSR0A, UDRE0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238 UDR0 = '\n';
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240 return (unsigned char)c;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 cmd_reset()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246 printf_P(PSTR("reset\n"));
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 _delay_ms(100);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248 cli(); // disable interrupts
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 wdt_enable(WDTO_15MS); // enable watchdog
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 while(1); // wait for watchdog to reset processor
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
253 static void
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
254 cmd_newboot()
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
255 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
256 set_pi_boot_normal(1);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
257 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
258 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
259 newboot_count = newboot_limit;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
260 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
261 printf_P(PSTR("newboot for %d secs"), newboot_limit);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
262 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
263
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
264
0
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 static void
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
267 cmd_get_params()
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
269 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
270 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
271 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
272 cur_watchdog_long = watchdog_long_count;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
273 cur_watchdog_short = watchdog_short_count;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
274 cur_newboot = newboot_count;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
275 cur_oneshot = oneshot_count;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
276 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
277
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
278 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
279 watchdog_long_limit,
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
280 cur_watchdog_long,
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
281 watchdog_short_limit,
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
282 cur_watchdog_short,
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
283 newboot_limit,
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
284 cur_newboot,
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
285 cur_oneshot);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
286 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
287
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
288 static void
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
289 cmd_set_params(const char *params)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
290 {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
291 uint32_t new_watchdog_long_limit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
292 uint32_t new_watchdog_short_limit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
293 uint32_t new_newboot_limit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
294
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
295 int ret = sscanf_P(params, PSTR("%lu %lu %lu"),
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
296 &new_watchdog_long_limit,
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
297 &new_watchdog_short_limit,
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
298 &new_newboot_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
299
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
300
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
301 if (ret != 3)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
302 {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
303 printf_P(PSTR("Bad values\n"));
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
304 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
305 else
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
306 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
307 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
308 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
309 eeprom_write(new_watchdog_long_limit, watchdog_long_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
310 eeprom_write(new_watchdog_short_limit, watchdog_short_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
311 eeprom_write(new_newboot_limit, newboot_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
312 uint16_t magic = EXPECT_MAGIC;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
313 eeprom_write(magic, magic);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
314 }
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
315 printf_P(PSTR("set_params for next boot\n"));
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
316 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
317 new_watchdog_long_limit,
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
318 new_watchdog_short_limit,
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
319 new_newboot_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
320
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
321 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
322 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
323
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
324 uint8_t from_hex(char c)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
325 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
326 if (c >= '0' && c <= '9') {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
327 return c-'0';
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 if (c >= 'a' && c <= 'f') {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
330 return c-'a' + 0xa;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
331 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
332 if (c >= 'A' && c <= 'F') {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
333 return c-'A' + 0xa;
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 return 0;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
336 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
337
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
338 static void
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
339 printhex_nibble(const unsigned char b, FILE *stream)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
340 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
341 unsigned char c = b & 0x0f;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
342 if ( c > 9 ) {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
343 c += 'A'-10;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
344 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
345 else {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
346 c += '0';
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
347 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
348 fputc(c, stream);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
349 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
350
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
351 void
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
352 printhex_byte(const unsigned char b, FILE *stream)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
353 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
354 printhex_nibble( b >> 4, stream);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
355 printhex_nibble( b, stream);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
356 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
357
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
358 void
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
359 printhex(uint8_t *id, uint8_t n, FILE *stream)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
360 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
361 for (uint8_t i = 0; i < n; i++)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
362 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
363 if (i > 0)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
364 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
365 fputc(' ', stream);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
366 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
367 printhex_byte(id[i], stream);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
368 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
369 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
370
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
371 static int8_t
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
372 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
373 uint8_t bytes_len)
2
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 // "N HEXKEY"
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
376 if (strlen(params) != bytes_len*2 + 2) {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
377 printf_P(PSTR("Wrong length key\n"));
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
378 return -1;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
379 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
380
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
381 if (params[1] != ' ')
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
382 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
383 printf_P(PSTR("Missing space\n"));
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
384 return -1;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
385 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
386
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
387 *key_index = from_hex(params[0]);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
388 if (*key_index >= NKEYS)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
389 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
390 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
391 return -1;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
392 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
393
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
394 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
395 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
396 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
397 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
398 return 0;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
399 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
400
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
401 static void
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
402 cmd_set_avr_key(const char *params)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
403 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
404 uint8_t new_key[KEYLEN];
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
405 uint8_t key_index;
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
406 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
407 {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
408 return;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
409 }
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
410 memcpy(avr_keys[key_index], new_key, sizeof(new_key));
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
411 eeprom_write(avr_keys, avr_keys);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
412 }
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 static void
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
415 cmd_hmac(const char *params)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
416 {
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
417 uint8_t data[HMACLEN];
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
418 uint8_t key_index;
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
419 if (parse_key(params, &key_index, data, sizeof(data)) != 0)
2
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 printf_P(PSTR("FAIL: Bad input\n"));
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
422 return;
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
423 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
424
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
425 if (key_index % 2 == 0)
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
426 {
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
427 printf_P(PSTR("Only hmac with even keys\n"));
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
428 return;
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
429 }
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
430
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
431 long_delay(200);
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
432
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
433 hmac_sha1_ctx_t ctx;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
434 hmac_sha1_init(&ctx, avr_keys[key_index], KEYLEN);
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
435 hmac_sha1_lastBlock(&ctx, data, HMACLEN);
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
436 hmac_sha1_final(data, &ctx);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
437
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
438 printf_P(PSTR("HMAC: "));
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
439 printhex(data, HMACLEN, stdout);
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
440 fputc('\n', stdout);
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
441 }
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
442
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
443 static void
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
444 cmd_decrypt(const char *params)
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
445 {
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
446 uint8_t data[HMACLEN+AESLEN];
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
447 uint8_t output[HMACLEN];
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
448 uint8_t key_index;
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
449 if (parse_key(params, &key_index, data, sizeof(data)) != 0)
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
450 {
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
451 printf_P(PSTR("FAIL: Bad input\n"));
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
452 return;
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
453 }
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
454
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
455 if (key_index % 2)
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
456 {
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
457 printf_P(PSTR("Only decrypt with odd keys\n"));
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
458 return;
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
459 }
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
460
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
461 long_delay(200);
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
462
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
463 // check the signature
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
464 hmac_sha1_ctx_t ctx;
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
465 hmac_sha1_init(&ctx, avr_keys[key_index], KEYLEN);
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
466 hmac_sha1_lastBlock(&ctx, &data[HMACLEN], AESLEN);
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
467 hmac_sha1_final(output, &ctx);
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
468
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
469 if (memcmp(output, data, HMACLEN) != 0) {
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
470 printf_P(PSTR("FAIL: hmac mismatch\n"));
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
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
473 uint8_t expkey[AES_EXPKEY_SIZE];
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
474 ExpandKey(avr_keys[key_index], expkey);
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
475 Decrypt(&data[HMACLEN], expkey, output);
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
476
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
477 printf_P(PSTR("DECRYPTED: "));
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
478 printhex(output, AESLEN, stdout);
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
479 fputc('\n', stdout);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
480 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
481
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
482 static void
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
483 cmd_oneshot_reboot(const char *params)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
484 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
485 uint32_t new_delay = strtoul(params, NULL, 10);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
486 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
487 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
488 oneshot_count = new_delay;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
489 }
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
490 printf_P(PSTR("oneshot delay %lu\n"), new_delay);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
491 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
492
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
493 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
494 load_params()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
495 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
496 uint16_t magic;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
497 eeprom_read(magic, magic);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
498 if (magic == EXPECT_MAGIC)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
499 {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
500 eeprom_read(watchdog_long_limit, watchdog_long_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
501 eeprom_read(watchdog_short_limit, watchdog_short_limit);
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
502 eeprom_read(newboot_limit, newboot_limit);
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
503 }
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
504
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
505 eeprom_read(avr_keys, avr_keys);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
506 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
507
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
508 static void
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
509 cmd_vcc()
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
510 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
511 uint16_t vcc = adc_vcc();
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
512 printf_P("vcc: %u mV\n", vcc);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
513 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
514
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
515 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
516 read_handler()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
517 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
518 if (strcmp_P(readbuf, PSTR("get_params")) == 0)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
519 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
520 cmd_get_params();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
521 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
522 else if (strncmp_P(readbuf, PSTR("set_params "), 11) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
523 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
524 cmd_set_params(&readbuf[11]);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
525 }
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
526 else if (strncmp_P(readbuf, PSTR("set_key "), 8) == 0)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
527 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
528 cmd_set_avr_key(&readbuf[8]);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
529 }
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
530 else if (strncmp_P(readbuf, PSTR("oneshot "), 8) == 0)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
531 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
532 cmd_oneshot_reboot(&readbuf[8]);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
533 }
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
534 else if (strncmp_P(readbuf, PSTR("hmac "), 5) == 0)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
535 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
536 cmd_hmac(&readbuf[5]);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
537 }
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
538 else if (strncmp_P(readbuf, PSTR("decrypt "), 8) == 0)
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
539 {
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
540 cmd_hmac(&readbuf[8]);
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
541 }
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
542 else if (strcmp_P(readbuf, PSTR("vcc")) == 0)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
543 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
544 cmd_vcc();
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
545 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
546 else if (strcmp_P(readbuf, PSTR("reset")) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
547 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
548 cmd_reset();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
549 }
4
fd28c7358ce8 more additions, set ports
Matt Johnston <matt@ucc.asn.au>
parents: 2
diff changeset
550 else if (strcmp_P(readbuf, PSTR("newboot")) == 0)
fd28c7358ce8 more additions, set ports
Matt Johnston <matt@ucc.asn.au>
parents: 2
diff changeset
551 {
fd28c7358ce8 more additions, set ports
Matt Johnston <matt@ucc.asn.au>
parents: 2
diff changeset
552 cmd_newboot();
fd28c7358ce8 more additions, set ports
Matt Johnston <matt@ucc.asn.au>
parents: 2
diff changeset
553 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
554 else
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
555 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
556 printf_P(PSTR("Bad command '%s'\n"), readbuf);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
557 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
558 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
559
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
560 ISR(INT0_vect)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
561 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
562 blink();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
563 _delay_ms(100);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
564 blink();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
565 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
566
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
567 ISR(USART_RX_vect)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
568 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
569 char c = UDR0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
570 #ifdef HAVE_UART_ECHO
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
571 uart_putchar(c, NULL);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
572 #endif
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
573 if (c == '\r' || c == '\n')
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
574 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
575 if (readpos > 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 readbuf[readpos] = '\0';
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
578 have_cmd = 1;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
579 readpos = 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 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
582 else
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
583 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
584 readbuf[readpos] = c;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
585 readpos++;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
586 if (readpos >= sizeof(readbuf))
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
587 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
588 readpos = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
589 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
590 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
591 }
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 ISR(TIMER1_COMPA_vect)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
594 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
595 TCNT1 = 0;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
596
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
597 clock_epoch += TICK;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
598
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
599 // watchdogs count up, continuous
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
600 if (watchdog_long_limit > 0) {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
601 watchdog_long_count += TICK;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
602 if (watchdog_long_count >= watchdog_long_limit)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
603 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
604 watchdog_long_count = 0;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
605 watchdog_long_hit = 1;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
606 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
607 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
608
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
609 if (watchdog_short_limit > 0) {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
610 watchdog_short_count += TICK;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
611 if (watchdog_short_count >= watchdog_short_limit)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
612 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
613 watchdog_short_count = 0;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
614 watchdog_short_hit = 1;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
615 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
616 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
617
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
618 // newboot counts down
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
619 if (newboot_count > 0)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
620 {
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
621 newboot_count-=TICK;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
622 if (newboot_count <= 0)
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
623 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
624 newboot_hit = 1;
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
625 newboot_count = 0;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
626 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
627 }
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
628
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
629 if (oneshot_count > 0)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
630 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
631 oneshot_count-=TICK;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
632 if (oneshot_count <= 0)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
633 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
634 oneshot_hit = 1;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
635 oneshot_count = 0;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
636 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
637 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
638 }
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 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
641 idle_sleep()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
642 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
643 set_sleep_mode(SLEEP_MODE_IDLE);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
644 sleep_mode();
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 static uint16_t
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
648 adc_vcc()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
649 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
650 PRR &= ~_BV(PRADC);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
651
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
652 // /16 prescaler
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
653 ADCSRA = _BV(ADEN) | _BV(ADPS2);
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 // set to measure 1.1 reference
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
656 ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
657 // average a number of samples
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
658 uint16_t sum = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
659 uint8_t num = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
660 for (uint8_t n = 0; n < 20; n++)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
661 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
662 ADCSRA |= _BV(ADSC);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
663 loop_until_bit_is_clear(ADCSRA, ADSC);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
664
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
665 uint8_t low_11 = ADCL;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
666 uint8_t high_11 = ADCH;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
667 uint16_t val = low_11 + (high_11 << 8);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
668
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
669 if (n >= 4)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
670 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
671 sum += val;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
672 num++;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
673 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
674 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
675 ADCSRA = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
676 PRR |= _BV(PRADC);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
677
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
678 //float res_volts = 1.1 * 1024 * num / sum;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
679 //return 1000 * res_volts;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
680 return ((uint32_t)1100*1024*num) / sum;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
681 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
682
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
683 static void
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
684 reboot_pi()
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
685 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
686 // pull it low for 30ms
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
687 PORT_PI_RESET &= ~_BV(PIN_PI_RESET);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
688 DDR_PI_RESET |= _BV(PIN_PI_RESET);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
689 _delay_ms(30);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
690 DDR_PI_RESET &= ~_BV(PIN_PI_RESET);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
691 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
692
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
693 static void
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
694 set_pi_boot_normal(uint8_t normal)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
695 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
696 PORT_PI_BOOT &= ~_BV(PIN_PI_BOOT);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
697 if (normal)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
698 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
699 // tristate
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
700 DDR_PI_BOOT &= ~_BV(PIN_PI_BOOT);
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 else
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
703 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
704 // pull it low
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
705 DDR_PI_RESET |= _BV(PIN_PI_BOOT);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
706
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 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
709
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
710 static void
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
711 check_flags()
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
712 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
713 if (watchdog_long_hit
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
714 || watchdog_short_hit
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
715 || oneshot_hit)
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
716 {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
717 reboot_pi();
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
718 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
719
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
720 if (newboot_hit) {
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
721 set_pi_boot_normal(0);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
722 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
723
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
724 watchdog_long_hit = 0;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
725 watchdog_short_hit = 0;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
726 newboot_hit = 0;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
727 oneshot_hit = 0;
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
728 }
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
729
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
730 static void
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
731 do_comms()
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 // avoid receiving rubbish, perhaps
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
734 uart_on();
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 // write sd card here? same 3.3v regulator...
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
737
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
738 while (1)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
739 {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
740 wdt_reset();
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
741
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
742 check_flags();
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
743
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
744 if (have_cmd)
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 have_cmd = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
747 read_handler();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
748 continue;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
749 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
750
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
751 // wait for commands from the master
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
752 idle_sleep();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
753 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
754 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
755
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
756 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
757 blink()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
758 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
759 PORT_LED &= ~_BV(PIN_LED);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
760 _delay_ms(1);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
761 PORT_LED |= _BV(PIN_LED);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
762 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
763
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
764 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
765 long_delay(int ms)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
766 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
767 int iter = ms / 100;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
768
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
769 for (int i = 0; i < iter; i++)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
770 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
771 _delay_ms(100);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
772 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
773 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
774
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
775 ISR(BADISR_vect)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
776 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
777 //uart_on();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
778 printf_P(PSTR("Bad interrupt\n"));
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
779 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
780
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
781 int main(void)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
782 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
783 setup_chip();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
784 blink();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
785
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
786 stdout = &mystdout;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
787 uart_on();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
788
5
87c8d0a11906 make it work
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
789 printf(PSTR("Pi Watchdog\nMatt Johnston [email protected]"));
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
790
2
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
791 set_pi_boot_normal(0);
0a6cbbb8c2b7 mostly there
Matt Johnston <matt@ucc.asn.au>
parents: 1
diff changeset
792
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
793 load_params();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
794
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
795 setup_tick_counter();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
796
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
797 sei();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
798
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
799 // doesn't return
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
800 do_comms();
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
801
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
802 return 0; /* never reached */
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
803 }