annotate main.c @ 19:5f9a40d6991b

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