annotate main.c @ 1:e23c1b6f6080

few more changes
author Matt Johnston <matt@ucc.asn.au>
date Mon, 03 Jun 2013 19:17:36 +0800
parents 8705acff2494
children 0a6cbbb8c2b7
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
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
16 //#include "simple_ds18b20.h"
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
17 //#include "onewire.h"
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 // TICK should be 8 or less (8 untested). all timers need
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 // to be a multiple.
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
25 #define TICK 1
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 // we have 1024 prescaler, 32768 crystal.
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 #define SLEEP_COMPARE (32*TICK-1)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
29 #define KEYLEN 20
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
30
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 #define VALUE_NOSENSOR 0x07D0 // 125 degrees
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 #define VALUE_BROKEN 0x07D1 // 125.0625
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 #define OVERSHOOT_MAX_DIV 1800.0 // 30 mins
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 #define WORT_INVALID_TIME 900 // 15 mins
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 // fridge min/max are only used if the wort sensor is invalid
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 #define FRIDGE_AIR_MIN_RANGE 40 // 4º
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 #define FRIDGE_AIR_MAX_RANGE 40 // 4º
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 #define BAUD 19200
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 #define UBRR ((F_CPU)/8/(BAUD)-1)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 #define PORT_LED PORTC
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 #define DDR_LED DDRC
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 #define PIN_LED PC4
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 #define PORT_SHDN PORTD
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 #define DDR_SHDN DDRD
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 #define PIN_SHDN PD7
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 #define PORT_FRIDGE PORTD
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 #define DDR_FRIDGE DDRD
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 #define PIN_FRIDGE PD6
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 // total amount of 16bit values available for measurements.
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 // adjust emperically, be sure to allow enough stack space too
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 #define TOTAL_MEASUREMENTS 800
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 // each sensor slot uses 8 bytes
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 #define MAX_SENSORS 6
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 // fixed at 8, have a shorter name
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 #define ID_LEN OW_ROMCODE_SIZE
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 // #define HAVE_UART_ECHO
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 // stores a value of clock_epoch combined with the remainder of TCNT2,
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 // for 1/32 second accuracy
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 struct epoch_ticks
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 uint32_t ticks;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 // remainder
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 uint8_t rem;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 };
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
76 // eeprom-settable parameters, default values defined here.
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
77 // all timeouts should be a multiple of TICK
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
78 static uint32_t watchdog_long_limit = 60*60*24;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
79 static uint32_t watchdog_short_limit = 0;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
80 static uint32_t newboot_limit = 60*10;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
82 // avr proves itself
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
83 static uint8_t avr_keys[NKEYS][KEYLEN] = {0};
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
84
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 // ---- Atomic guards required accessing these variables
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 // clock_epoch in seconds
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 static uint32_t clock_epoch;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
89 // watchdog counts up
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
90 static uint32_t watchdog_long_count;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
91 static uint32_t watchdog_short_count;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
92 // newboot counts down - it's a one-shot
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
93 static uint32_t newboot_count;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 // ---- End atomic guards required
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 // boolean flags
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
97 static uint8_t watchdog_long_hit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
98 static uint8_t watchdog_short_hit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
99 static uint8_t newboot_hit;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 static uint8_t readpos;
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
102 static char readbuf[50];
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 static uint8_t have_cmd;
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 int uart_putchar(char c, FILE *stream);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 static void long_delay(int ms);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 static void blink();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 static uint16_t adc_vcc();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 _FDEV_SETUP_WRITE);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 // thanks to http://projectgus.com/2010/07/eeprom-access-with-arduino/
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 #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
115 #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
116 #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
117 #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
118
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
119 #define EXPECT_MAGIC 0xdf83
0
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 struct __attribute__ ((__packed__)) __eeprom_data {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
122 uint32_t watchdog_long_limit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
123 uint32_t watchdog_short_limit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
124 uint32_t newboot_limit;
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
126 uint8_t avr_key[NKEYS][KEYLEN];
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 uint16_t magic;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 };
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 static void deep_sleep();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 // Very first setup
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 setup_chip()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 cli();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 // stop watchdog timer (might have been used to cause a reset)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 wdt_reset();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 MCUSR &= ~_BV(WDRF);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 WDTCSR |= _BV(WDCE) | _BV(WDE);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 WDTCSR = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
145 // set to 8S, in case sha1 is slow etc.
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
146 wdt_enable(WDTO_8S);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
147
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 // Set clock to 2mhz
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 CLKPR = _BV(CLKPCE);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 // divide by 4
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 CLKPR = _BV(CLKPS1);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 // enable pullups
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
154 // XXX matt pihelp
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155 PORTB = 0xff; // XXX change when using SPI
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 PORTD = 0xff;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 PORTC = 0xff;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 // 3.3v power for bluetooth and SD
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 DDR_LED |= _BV(PIN_LED);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 // set pullup
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 PORTD |= _BV(PD2);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 // INT0 setup
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 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
166 EIMSK = _BV(INT0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 // comparator disable
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 ACSR = _BV(ACD);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 // disable adc pin input buffers
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 DIDR0 = 0x3F; // acd0-adc5
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 DIDR1 = (1<<AIN1D)|(1<<AIN0D); // ain0/ain1
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 sei();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 get_epoch_ticks(struct epoch_ticks *t)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 t->ticks = clock_epoch;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 t->rem = TCNT2;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 setup_tick_counter()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 // set up counter2.
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 // COM21 COM20 Set OC2 on Compare Match (p116)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 // WGM21 Clear counter on compare
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194 //TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195 // toggle on match
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196 TCCR2A = _BV(COM2A0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 // CS22 CS21 CS20 clk/1024
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199 // set async mode
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200 ASSR |= _BV(AS2);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 TCNT2 = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202 OCR2A = SLEEP_COMPARE;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203 // interrupt
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 TIMSK2 = _BV(OCIE2A);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208 uart_on()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210 // Power reduction register
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 PRR &= ~_BV(PRUSART0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 // 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
214 // baud rate
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215 UBRR0H = (unsigned char)(UBRR >> 8);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 UBRR0L = (unsigned char)UBRR;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217 // set 2x clock, improves accuracy of UBRR
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
218 UCSR0A |= _BV(U2X0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220 //8N1
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 uart_enabled = 1;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226 uart_off()
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 // Turn off interrupts and disable tx/rx
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 UCSR0B = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 uart_enabled = 0;
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
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236 int
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 uart_putchar(char c, FILE *stream)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239 if (!uart_enabled)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241 return EOF;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243 // XXX could perhaps sleep in the loop for power.
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 if (c == '\n')
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246 loop_until_bit_is_set(UCSR0A, UDRE0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 UDR0 = '\r';
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 loop_until_bit_is_set(UCSR0A, UDRE0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 UDR0 = c;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 if (stream == crc_stdout)
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 crc_out = _crc_ccitt_update(crc_out, c);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 if (c == '\r')
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257 loop_until_bit_is_set(UCSR0A, UDRE0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258 UDR0 = '\n';
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
259 if (stream == crc_stdout)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
260 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261 crc_out = _crc_ccitt_update(crc_out, '\n');
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
262 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264 return (unsigned char)c;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268 cmd_reset()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270 printf_P(PSTR("reset\n"));
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 _delay_ms(100);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272 cli(); // disable interrupts
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 wdt_enable(WDTO_15MS); // enable watchdog
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274 while(1); // wait for watchdog to reset processor
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
277
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 static void
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
280 cmd_get_params()
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
282 uint32_t cur_watchdog_long, cur_watchdog_short, cur_newboot;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
283 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
284 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
285 cur_watchdog_long = watchdot_long_count;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
286 cur_watchdog_short = watchdot_short_count;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
287 cur_newboot = newboot_limit_count;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
288 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
289
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
290 printf_P(PSTR("limit (count) : watchdog_long %lu (%lu) watchdog_short %lu (%lu) newboot %lu (%lu)\n"),
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
291 watchdog_long_limit,
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
292 watchdog_long_count,
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
293 watchdog_short_limit,
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
294 watchdog_short_count,
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
295 newboot_limit,
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
296 newboot_count);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
297 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
298
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
299 static void
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
300 cmd_set_params(const char *params)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
301 {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
302 uint32_t new_watchdog_long_limit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
303 uint32_t new_watchdog_short_limit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
304 uint32_t new_newboot_limit;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
305
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
306 int ret = sscanf_P(params, PSTR("%lu %lu %lu"),
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
307 &new_watchdog_long_limit,
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
308 &new_watchdog_short_limit,
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
309 &new_newboot_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
310
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
311
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
312 if (ret != 3)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
313 {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
314 printf_P(PSTR("Bad values\n"));
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
315 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
316 else
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
317 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
318 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
319 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
320 eeprom_write(new_watchdog_long_limit, watchdog_long_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
321 eeprom_write(new_watchdog_short_limit, watchdog_short_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
322 eeprom_write(new_newboot_limit, newboot_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
323 uint16_t magic = EXPECT_MAGIC;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
324 eeprom_write(magic, magic);
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
325 }
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
326 printf_P(PSTR("set_params for next boot\n"));
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
327 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
328 new_watchdog_long_limit,
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
329 new_watchdog_short_limit,
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
330 new_newboot_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
331
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
332 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
333 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
334
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
335 uint8_t from_hex(char c)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
336 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
337 if (c >= '0' && c <= '9') {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
338 return c-'0';
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
339 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
340 if (c >= 'a' && c <= 'f') {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
341 return c-'a' + 0xa;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
342 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
343 if (c >= 'A' && c <= 'F') {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
344 return c-'A' + 0xa;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
345 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
346 return 0;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
347 }
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
348
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
349 static void
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
350 cmd_set_avr_key(const char *params)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
351 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
352 // "N HEXKEY"
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
353 if (strlen(params)) != KEYLEN*2+2) {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
354 printf_P(PSTR("Wrong length key\n"));
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
355 return;
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
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
358 uint8_t new_key[KEYLEN];
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
359 for (int i = 0, p = 0; i < KEYLEN; i++, p += 2)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
360 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
361 new_key[i] = (fromhex(params[p]) << 4) | fromhex(params[p+1]);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
362 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
363 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
364
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
365 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
366 load_params()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
367 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
368 uint16_t magic;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
369 eeprom_read(magic, magic);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
370 if (magic == EXPECT_MAGIC)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
371 {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
372 eeprom_read(watchdog_long_limit, watchdog_long_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
373 eeprom_read(watchdog_short_limit, watchdog_short_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
374 eeprom_read(netboot_limit);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
375 eeprom_read(avr_key);
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
376 }
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 // returns true if eeprom was written
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
380 static bool
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
381 set_initial_eeprom()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
382 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
383 uint16_t magic;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
384 eeprom_read(magic, magic);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
385 if (magic == EXPECT_MAGIC)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
386 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
387 return false;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
388 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
389
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
390 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
391 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
392 eeprom_write(measure_wake, measure_wake);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
393 eeprom_write(comms_wake, comms_wake);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
394 eeprom_write(wake_secs, wake_secs);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
395 eeprom_write(fridge_setpoint, fridge_setpoint);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
396 eeprom_write(fridge_difference, fridge_difference);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
397 eeprom_write(fridge_delay, fridge_delay);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
398 eeprom_write(overshoot_delay, overshoot_delay);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
399 eeprom_write(overshoot_factor, overshoot_factor);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
400 magic = EXPECT_MAGIC;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
401 eeprom_write(magic, magic);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
402 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
403
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
404 return true;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
405 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
406
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
407 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
408 read_handler()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
409 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
410 if (strcmp_P(readbuf, PSTR("fetch")) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
411 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
412 cmd_fetch();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
413 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
414 else if (strcmp_P(readbuf, PSTR("clear")) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
415 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
416 cmd_clear();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
417 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
418 else if (strcmp_P(readbuf, PSTR("btoff")) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
419 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
420 cmd_btoff();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
421 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
422 else if (strcmp_P(readbuf, PSTR("measure")) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
423 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
424 cmd_measure();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
425 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
426 else if (strcmp_P(readbuf, PSTR("sensors")) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
427 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
428 cmd_sensors();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
429 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
430 else if (strcmp_P(readbuf, PSTR("get_params")) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
431 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
432 cmd_get_params();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
433 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
434 else if (strncmp_P(readbuf, PSTR("set_params "), 11) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
435 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
436 cmd_set_params(&readbuf[11]);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
437 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
438 else if (strcmp_P(readbuf, PSTR("awake")) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
439 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
440 cmd_awake();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
441 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
442 else if (strncmp_P(readbuf, PSTR("fridge_setpoint "), 16) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
443 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
444 cmd_set_fridge_setpoint(&readbuf[16]);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
445 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
446 else if (strncmp_P(readbuf, PSTR("fridge_diff "), 12) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
447 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
448 cmd_set_fridge_difference(&readbuf[12]);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
449 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
450 else if (strncmp_P(readbuf, PSTR("fridge_delay "), 13) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
451 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
452 cmd_set_fridge_delay(&readbuf[13]);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
453 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
454 else if (strncmp_P(readbuf, PSTR("overshoot_delay "), 16) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
455 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
456 cmd_set_overshoot_delay(&readbuf[16]);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
457 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
458 else if (strncmp_P(readbuf, PSTR("overshoot_factor "), 17) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
459 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
460 cmd_set_overshoot_factor(&readbuf[17]);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
461 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
462 else if (strcmp_P(readbuf, PSTR("reset")) == 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
463 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
464 cmd_reset();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
465 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
466 else
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
467 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
468 printf_P(PSTR("Bad command '%s'\n"), readbuf);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
469 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
470 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
471
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
472 ISR(INT0_vect)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
473 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
474 button_pressed = 1;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
475 blink();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
476 _delay_ms(100);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
477 blink();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
478 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
479
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
480
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
481 ISR(USART_RX_vect)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
482 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
483 char c = UDR0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
484 #ifdef HAVE_UART_ECHO
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
485 uart_putchar(c, NULL);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
486 #endif
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
487 if (c == '\r' || c == '\n')
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
488 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
489 if (readpos > 0)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
490 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
491 readbuf[readpos] = '\0';
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
492 have_cmd = 1;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
493 readpos = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
494 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
495 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
496 else
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
497 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
498 readbuf[readpos] = c;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
499 readpos++;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
500 if (readpos >= sizeof(readbuf))
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
501 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
502 readpos = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
503 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
504 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
505 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
506
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
507 ISR(TIMER2_COMPA_vect)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
508 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
509 TCNT2 = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
510
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
511 clock_epoch += TICK;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
512
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
513 // watchdogs count up, continuous
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
514 if (watchdog_long_limit > 0) {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
515 watchdog_count += TICK;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
516 if (watchdog_long_count >= watchdog_long_limit)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
517 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
518 watchdog_long_count = 0;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
519 watchdog_long_hit = 1;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
520 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
521 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
522
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
523 if (watchdog_short_limit > 0) {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
524 watchdog_count += TICK;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
525 if (watchdog_short_count >= watchdog_short_limit)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
526 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
527 watchdog_short_count = 0;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
528 watchdog_short_hit = 1;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
529 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
530 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
531
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
532 // newboot counts down, oneshot.
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
533 if (newboot_count > 0)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
534 {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
535 newboot_count--;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
536 if (newboot_count == 0)
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
537 {
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
538 newboot_hit = 1;
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
539 }
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
540 }
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
541
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
542 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
543
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
544 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
545 deep_sleep()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
546 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
547 // p119 of manual
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
548 OCR2A = SLEEP_COMPARE;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
549 loop_until_bit_is_clear(ASSR, OCR2AUB);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
550
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
551 set_sleep_mode(SLEEP_MODE_PWR_SAVE);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
552 sleep_mode();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
553 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
554
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
555 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
556 idle_sleep()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
557 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
558 set_sleep_mode(SLEEP_MODE_IDLE);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
559 sleep_mode();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
560 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
561
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
562 static uint16_t
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
563 adc_vcc()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
564 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
565 PRR &= ~_BV(PRADC);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
566
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
567 // /16 prescaler
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
568 ADCSRA = _BV(ADEN) | _BV(ADPS2);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
569
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
570 // set to measure 1.1 reference
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
571 ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
572 // average a number of samples
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
573 uint16_t sum = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
574 uint8_t num = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
575 for (uint8_t n = 0; n < 20; n++)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
576 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
577 ADCSRA |= _BV(ADSC);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
578 loop_until_bit_is_clear(ADCSRA, ADSC);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
579
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
580 uint8_t low_11 = ADCL;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
581 uint8_t high_11 = ADCH;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
582 uint16_t val = low_11 + (high_11 << 8);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
583
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
584 if (n >= 4)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
585 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
586 sum += val;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
587 num++;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
588 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
589 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
590 ADCSRA = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
591 PRR |= _BV(PRADC);
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 //float res_volts = 1.1 * 1024 * num / sum;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
594 //return 1000 * res_volts;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
595 return ((uint32_t)1100*1024*num) / sum;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
596 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
597
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
598 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
599 do_comms()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
600 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
601 // avoid receiving rubbish, perhaps
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
602 uart_on();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
603
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
604 // write sd card here? same 3.3v regulator...
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
605
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
606 while (1)
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
607 {
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
608 wdt_reset();
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
609 if (have_cmd)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
610 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
611 have_cmd = 0;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
612 read_handler();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
613 continue;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
614 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
615
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
616 // wait for commands from the master
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
617 idle_sleep();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
618 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
619 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
620
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
621 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
622 blink()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
623 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
624 PORT_LED &= ~_BV(PIN_LED);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
625 _delay_ms(1);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
626 PORT_LED |= _BV(PIN_LED);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
627 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
628
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
629 static void
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
630 long_delay(int ms)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
631 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
632 int iter = ms / 100;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
633
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
634 for (int i = 0; i < iter; i++)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
635 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
636 _delay_ms(100);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
637 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
638 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
639
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
640 ISR(BADISR_vect)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
641 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
642 //uart_on();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
643 printf_P(PSTR("Bad interrupt\n"));
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
644 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
645
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
646 int main(void)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
647 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
648 setup_chip();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
649 blink();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
650
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
651 stdout = &mystdout;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
652 uart_on();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
653
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
654 printf(PSTR("Started.\n"));
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
655
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
656 load_params();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
657
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
658 setup_tick_counter();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
659
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
660 sei();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
661
1
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
662 // doesn't return
e23c1b6f6080 few more changes
Matt Johnston <matt@ucc.asn.au>
parents: 0
diff changeset
663 do_comms();
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
664
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
665 return 0; /* never reached */
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
666 }