Mercurial > templog
annotate main.c @ 17:a5e3b363675d
Add clock_epoch
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 22 May 2012 21:24:04 +0800 |
parents | 5075d8c428bd |
children | f1016b151689 |
rev | line source |
---|---|
0 | 1 /* Name: main.c |
2 * Author: <insert your name here> | |
3 * Copyright: <insert your copyright message here> | |
4 * License: <insert your license reference here> | |
5 */ | |
6 | |
7 #include <stdio.h> | |
8 #include <string.h> | |
13 | 9 #include <stddef.h> |
1 | 10 #include <avr/io.h> |
11 #include <avr/interrupt.h> | |
12 #include <avr/sleep.h> | |
7 | 13 #include <util/delay.h> |
6 | 14 #include <avr/pgmspace.h> |
13 | 15 #include <avr/eeprom.h> |
0 | 16 #include <util/crc16.h> |
17 | |
12 | 18 // for DWORD of get_fattime() |
3 | 19 #include "integer.h" |
12 | 20 |
21 #include "simple_ds18b20.h" | |
13 | 22 #include "onewire.h" |
3 | 23 |
0 | 24 // configuration params |
25 // - measurement interval | |
26 // - transmit interval | |
27 // - bluetooth params | |
28 // - number of sensors (and range?) | |
29 | |
1 | 30 // 1 second. we have 1024 prescaler, 32768 crystal. |
2 | 31 #define SLEEP_COMPARE 32 |
14 | 32 #define MEASURE_WAKE 10 |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
33 |
13 | 34 #define VALUE_NOSENSOR -9000 |
35 #define VALUE_BROKEN -8000 | |
36 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
37 #define COMMS_WAKE 3600 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
38 #define WAKE_SECS 30 |
2 | 39 |
10 | 40 #define BAUD 19200 |
7 | 41 #define UBRR ((F_CPU)/8/(BAUD)-1) |
42 | |
43 #define PORT_LED PORTC | |
44 #define DDR_LED DDRC | |
45 #define PIN_LED PC4 | |
1 | 46 |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
47 #define PORT_SHDN PORTD |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
48 #define DDR_SHDN DDRD |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
49 #define PIN_SHDN PD7 |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
50 |
13 | 51 #define NUM_MEASUREMENTS 100 |
52 #define MAX_SENSORS 5 | |
6 | 53 |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
54 // fixed at 8, have a shorter name |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
55 #define ID_LEN OW_ROMCODE_SIZE |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
56 |
10 | 57 int uart_putchar(char c, FILE *stream); |
14 | 58 static void long_delay(int ms); |
59 | |
0 | 60 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, |
61 _FDEV_SETUP_WRITE); | |
62 | |
17 | 63 static uint16_t n_measurements; |
13 | 64 // stored as decidegrees |
14 | 65 static int16_t measurements[NUM_MEASUREMENTS][MAX_SENSORS]; |
0 | 66 |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
67 // boolean flags |
17 | 68 static uint8_t need_measurement; |
69 static uint8_t need_comms; | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
70 |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
71 // counts down from WAKE_SECS to 0, goes to deep sleep when hits 0 |
17 | 72 static uint8_t comms_timeout; |
1 | 73 |
17 | 74 static uint8_t readpos; |
0 | 75 static char readbuf[30]; |
76 | |
17 | 77 static uint8_t measure_count; |
78 static uint16_t comms_count; | |
79 | |
80 static uint32_t clock_epoch; | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
81 |
13 | 82 // thanks to http://projectgus.com/2010/07/eeprom-access-with-arduino/ |
83 #define eeprom_read_to(dst_p, eeprom_field, dst_size) eeprom_read_block((dst_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (dst_size)) | |
84 #define eeprom_read(dst, eeprom_field) eeprom_read_to((&dst), eeprom_field, sizeof(dst)) | |
85 #define eeprom_write_from(src_p, eeprom_field, src_size) eeprom_write_block((src_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (src_size)) | |
86 #define eeprom_write(src, eeprom_field) { eeprom_write_from(&src, eeprom_field, sizeof(src)); } | |
87 | |
88 #define EXPECT_MAGIC 0x67c9 | |
89 | |
90 struct __attribute__ ((__packed__)) __eeprom_data { | |
91 uint16_t magic; | |
92 uint8_t n_sensors; | |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
93 uint8_t sensor_id[MAX_SENSORS][ID_LEN]; |
13 | 94 }; |
95 | |
8 | 96 #define DEBUG(str) printf_P(PSTR(str)) |
97 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
98 static void deep_sleep(); |
1 | 99 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
100 // Very first setup |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
101 static void |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
102 setup_chip() |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
103 { |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
104 // Set clock to 2mhz |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
105 cli(); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
106 CLKPR = _BV(CLKPCE); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
107 // divide by 4 |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
108 CLKPR = _BV(CLKPS1); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
109 sei(); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
110 |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
111 DDR_LED |= _BV(PIN_LED); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
112 DDR_SHDN |= _BV(PIN_SHDN); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
113 } |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
114 |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
115 static void |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
116 set_aux_power(uint8_t on) |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
117 { |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
118 if (on) |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
119 { |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
120 PORT_SHDN &= ~_BV(PIN_SHDN); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
121 } |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
122 else |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
123 { |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
124 PORT_SHDN |= _BV(PIN_SHDN); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
125 } |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
126 } |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
127 |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
128 static void |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
129 setup_tick_counter() |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
130 { |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
131 // set up counter2. |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
132 // COM21 COM20 Set OC2 on Compare Match (p116) |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
133 // WGM21 Clear counter on compare |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
134 //TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
135 // toggle on match |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
136 TCCR2A = _BV(COM2A0); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
137 // CS22 CS21 CS20 clk/1024 |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
138 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
139 // set async mode |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
140 ASSR |= _BV(AS2); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
141 TCNT2 = 0; |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
142 OCR2A = SLEEP_COMPARE; |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
143 // interrupt |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
144 TIMSK2 = _BV(OCIE2A); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
145 } |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
146 |
0 | 147 static void |
7 | 148 uart_on() |
0 | 149 { |
8 | 150 // Power reduction register |
151 //PRR &= ~_BV(PRUSART0); | |
152 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
153 // All of this needs to be done each time after turning off the PRR |
0 | 154 // baud rate |
7 | 155 UBRR0H = (unsigned char)(UBRR >> 8); |
156 UBRR0L = (unsigned char)UBRR; | |
157 // set 2x clock, improves accuracy of UBRR | |
158 UCSR0A |= _BV(U2X0); | |
6 | 159 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); |
0 | 160 //8N1 |
7 | 161 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); |
6 | 162 } |
163 | |
164 static void | |
165 uart_off() | |
166 { | |
167 // Turn of interrupts and disable tx/rx | |
168 UCSR0B = 0; | |
169 | |
170 // Power reduction register | |
8 | 171 //PRR |= _BV(PRUSART0); |
0 | 172 } |
173 | |
10 | 174 int |
0 | 175 uart_putchar(char c, FILE *stream) |
176 { | |
10 | 177 // XXX should sleep in the loop for power. |
8 | 178 if (c == '\n') |
179 { | |
10 | 180 loop_until_bit_is_set(UCSR0A, UDRE0); |
181 UDR0 = '\r';; | |
8 | 182 } |
2 | 183 loop_until_bit_is_set(UCSR0A, UDRE0); |
184 UDR0 = c; | |
10 | 185 if (c == '\r') |
186 { | |
187 loop_until_bit_is_set(UCSR0A, UDRE0); | |
188 UDR0 = '\n';; | |
189 } | |
0 | 190 return 0; |
191 } | |
192 | |
193 static void | |
17 | 194 update_crc(uint16_t *crc, const void *data, uint8_t len) |
195 { | |
196 for (uint8_t i = 0; i < len; i++) | |
197 { | |
198 *crc = _crc_ccitt_update(*crc, ((const uint8_t*)data)[i]); | |
199 } | |
200 } | |
201 | |
202 static void | |
0 | 203 cmd_fetch() |
204 { | |
205 uint16_t crc = 0; | |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
206 uint8_t n_sensors; |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
207 eeprom_read(n_sensors, n_sensors); |
13 | 208 |
17 | 209 printf_P(PSTR("Time %lu\n"), clock_epoch); |
210 update_crc(&crc, &clock_epoch, sizeof(clock_epoch)); | |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
211 printf_P(PSTR("%d sensors\n"), n_measurements); |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
212 for (uint8_t s = 0; s < n_sensors; s++) |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
213 { |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
214 uint8_t id[ID_LEN]; |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
215 printf_P(PSTR("%d : "), s); |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
216 eeprom_read_to(id, sensor_id[s], ID_LEN); |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
217 printhex(id, ID_LEN); |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
218 putchar('\n'); |
17 | 219 update_crc(&crc, id, ID_LEN); |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
220 } |
6 | 221 printf_P(PSTR("%d measurements\n"), n_measurements); |
14 | 222 for (uint16_t n = 0; n < n_measurements; n++) |
0 | 223 { |
14 | 224 printf_P(PSTR("%3d :"), n); |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
225 for (uint8_t s = 0; s < n_sensors; s++) |
13 | 226 { |
14 | 227 printf_P(PSTR(" %6d"), measurements[n][s]); |
17 | 228 update_crc(&crc, &measurements[n][s], sizeof(measurements[n][s])); |
13 | 229 } |
230 putchar('\n'); | |
0 | 231 } |
6 | 232 printf_P(PSTR("CRC : %d\n"), crc); |
0 | 233 } |
234 | |
235 static void | |
236 cmd_clear() | |
237 { | |
7 | 238 n_measurements = 0; |
239 printf_P(PSTR("Cleared\n")); | |
0 | 240 } |
241 | |
242 static void | |
243 cmd_btoff() | |
244 { | |
7 | 245 printf_P(PSTR("Turning off\n")); |
246 _delay_ms(50); | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
247 comms_timeout = 0; |
0 | 248 } |
249 | |
250 static void | |
12 | 251 cmd_measure() |
252 { | |
253 printf_P(PSTR("Measuring\n")); | |
254 need_measurement = 1; | |
255 } | |
256 | |
257 static void | |
258 cmd_sensors() | |
259 { | |
260 uint8_t ret = simple_ds18b20_start_meas(NULL); | |
14 | 261 printf_P(PSTR("All sensors, ret %d, waiting...\n"), ret); |
262 long_delay(DS18B20_TCONV_12BIT); | |
12 | 263 simple_ds18b20_read_all(); |
264 } | |
265 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
266 #if 0 |
13 | 267 // 0 on success |
268 static uint8_t | |
269 get_hex_string(const char *hex, uint8_t *out, uint8_t size) | |
270 { | |
271 uint8_t upper; | |
272 uint8_t o; | |
273 for (uint8_t i = 0, z = 0; o < size; i++) | |
274 { | |
275 uint8_t h = hex[i]; | |
276 if (h >= 'A' && h <= 'F') | |
277 { | |
278 // lower case | |
279 h += 0x20; | |
280 } | |
281 uint8_t nibble; | |
282 if (h >= '0' && h <= '9') | |
283 { | |
284 nibble = h - '0'; | |
285 } | |
286 else if (h >= 'a' && h <= 'f') | |
287 { | |
288 nibble = 10 + h - 'a'; | |
289 } | |
290 else if (h == ' ' || h == ':') | |
291 { | |
292 continue; | |
293 } | |
294 else | |
295 { | |
296 printf_P(PSTR("Bad hex 0x%x '%c'\n"), hex[i], hex[i]); | |
297 return 1; | |
298 } | |
299 | |
300 if (z % 2 == 0) | |
301 { | |
302 upper = nibble << 4; | |
303 } | |
304 else | |
305 { | |
306 out[o] = upper | nibble; | |
307 o++; | |
308 } | |
309 | |
310 z++; | |
311 } | |
312 | |
313 if (o != size) | |
314 { | |
315 printf_P(PSTR("Short hex\n")); | |
316 return 1; | |
317 } | |
318 return 0; | |
319 } | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
320 #endif |
13 | 321 |
322 static void | |
323 add_sensor(uint8_t *id) | |
324 { | |
325 uint8_t n; | |
326 eeprom_read(n, n_sensors); | |
327 if (n < MAX_SENSORS) | |
328 { | |
329 cli(); | |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
330 eeprom_write_from(id, sensor_id[n], ID_LEN); |
13 | 331 n++; |
332 eeprom_write(n, n_sensors); | |
333 sei(); | |
334 printf_P(PSTR("Added sensor %d : "), n); | |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
335 printhex(id, ID_LEN); |
13 | 336 putchar('\n'); |
337 } | |
338 else | |
339 { | |
340 printf_P(PSTR("Too many sensors\n")); | |
341 } | |
342 } | |
343 | |
344 static void | |
345 cmd_add_all() | |
346 { | |
347 uint8_t id[OW_ROMCODE_SIZE]; | |
348 printf_P("Adding all\n"); | |
349 ow_reset(); | |
350 for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; ) | |
351 { | |
352 diff = ow_rom_search( diff, &id[0] ); | |
353 if( diff == OW_PRESENCE_ERR ) { | |
354 printf_P( PSTR("No Sensor found\r") ); | |
355 return; | |
356 } | |
357 | |
358 if( diff == OW_DATA_ERR ) { | |
359 printf_P( PSTR("Bus Error\r") ); | |
360 return; | |
361 } | |
362 add_sensor(id); | |
363 } | |
364 } | |
365 | |
366 static void | |
367 cmd_init() | |
368 { | |
369 printf_P(PSTR("Resetting sensor list\n")); | |
370 uint8_t zero = 0; | |
371 cli(); | |
372 eeprom_write(zero, n_sensors); | |
373 sei(); | |
374 printf_P(PSTR("Done.\n")); | |
375 } | |
376 | |
377 static void | |
17 | 378 cmd_settime(const char *str) |
379 { | |
380 clock_epoch = strtoul(str, NULL, 10); | |
381 printf_P(PSTR("Time set to %lu\n"), clock_epoch); | |
382 } | |
383 | |
384 static void | |
13 | 385 check_first_startup() |
386 { | |
387 uint16_t magic; | |
388 eeprom_read(magic, magic); | |
389 if (magic != EXPECT_MAGIC) | |
390 { | |
391 printf_P(PSTR("First boot, looking for sensors...\n")); | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
392 // in case of power fumbles don't want to reset during eeprom write, |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
393 long_delay(2); |
13 | 394 cmd_init(); |
395 cmd_add_all(); | |
14 | 396 cli(); |
397 magic = EXPECT_MAGIC; | |
398 eeprom_write(magic, magic); | |
399 sei(); | |
13 | 400 } |
401 } | |
402 | |
12 | 403 static void |
0 | 404 read_handler() |
405 { | |
6 | 406 if (strcmp_P(readbuf, PSTR("fetch")) == 0) |
0 | 407 { |
408 cmd_fetch(); | |
409 } | |
6 | 410 else if (strcmp_P(readbuf, PSTR("clear")) == 0) |
0 | 411 { |
412 cmd_clear(); | |
413 } | |
6 | 414 else if (strcmp_P(readbuf, PSTR("btoff")) == 0) |
0 | 415 { |
416 cmd_btoff(); | |
417 } | |
12 | 418 else if (strcmp_P(readbuf, PSTR("measure")) == 0) |
419 { | |
420 cmd_measure(); | |
421 } | |
422 else if (strcmp_P(readbuf, PSTR("sensors")) == 0) | |
423 { | |
424 cmd_sensors(); | |
425 } | |
13 | 426 else if (strcmp_P(readbuf, PSTR("addall"))== 0) |
427 { | |
428 cmd_add_all(); | |
429 } | |
17 | 430 else if (strncmp_P(readbuf, PSTR("settime "), |
431 strlen("settime ") == 0)) | |
432 { | |
433 cmd_settime(&readbuf[strlen("settime ")]); | |
434 } | |
13 | 435 else if (strcmp_P(readbuf, PSTR("init")) == 0) |
436 { | |
437 cmd_init(); | |
438 } | |
0 | 439 else |
440 { | |
6 | 441 printf_P(PSTR("Bad command\n")); |
0 | 442 } |
443 } | |
444 | |
2 | 445 ISR(USART_RX_vect) |
0 | 446 { |
2 | 447 char c = UDR0; |
12 | 448 uart_putchar(c, NULL); |
449 if (c == '\r') | |
0 | 450 { |
451 readbuf[readpos] = '\0'; | |
452 read_handler(); | |
453 readpos = 0; | |
454 } | |
455 else | |
456 { | |
457 readbuf[readpos] = c; | |
458 readpos++; | |
459 if (readpos >= sizeof(readbuf)) | |
460 { | |
461 readpos = 0; | |
462 } | |
463 } | |
464 } | |
465 | |
2 | 466 ISR(TIMER2_COMPA_vect) |
1 | 467 { |
14 | 468 TCNT2 = 0; |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
469 measure_count ++; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
470 comms_count ++; |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
471 |
17 | 472 clock_epoch ++; |
473 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
474 if (comms_timeout != 0) |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
475 { |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
476 comms_timeout--; |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
477 } |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
478 |
14 | 479 if (measure_count >= MEASURE_WAKE) |
1 | 480 { |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
481 measure_count = 0; |
1 | 482 need_measurement = 1; |
483 } | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
484 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
485 if (comms_count >= COMMS_WAKE) |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
486 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
487 comms_count = 0; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
488 need_comms = 1; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
489 } |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
490 |
1 | 491 } |
492 | |
3 | 493 DWORD get_fattime (void) |
494 { | |
495 return 0; | |
496 } | |
497 | |
1 | 498 static void |
499 deep_sleep() | |
500 { | |
501 // p119 of manual | |
2 | 502 OCR2A = SLEEP_COMPARE; |
503 loop_until_bit_is_clear(ASSR, OCR2AUB); | |
1 | 504 |
505 set_sleep_mode(SLEEP_MODE_PWR_SAVE); | |
506 sleep_mode(); | |
507 } | |
508 | |
509 static void | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
510 idle_sleep() |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
511 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
512 set_sleep_mode(SLEEP_MODE_IDLE); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
513 sleep_mode(); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
514 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
515 |
14 | 516 #if 0 |
517 // untested | |
6 | 518 static void |
519 do_adc_335() | |
520 { | |
8 | 521 //PRR &= ~_BV(PRADC); |
6 | 522 |
523 ADMUX = _BV(ADLAR); | |
524 | |
525 // ADPS2 = /16 prescaler, 62khz at 1mhz clock | |
526 ADCSRA = _BV(ADEN) | _BV(ADPS2); | |
527 | |
528 // measure value | |
529 ADCSRA |= _BV(ADSC); | |
530 loop_until_bit_is_clear(ADCSRA, ADSC); | |
531 uint8_t low = ADCL; | |
532 uint8_t high = ADCH; | |
533 uint16_t f_measure = low + (high << 8); | |
534 | |
535 // set to measure 1.1 reference | |
536 ADMUX = _BV(ADLAR) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); | |
537 ADCSRA |= _BV(ADSC); | |
538 loop_until_bit_is_clear(ADCSRA, ADSC); | |
539 uint8_t low_11 = ADCL; | |
540 uint8_t high_11 = ADCH; | |
541 uint16_t f_11 = low_11 + (high_11 << 8); | |
542 | |
543 float res_volts = 1.1 * f_measure / f_11; | |
544 | |
545 // 10mV/degree | |
546 // scale to 1/5 degree units above 0C | |
547 int temp = (res_volts - 2.73) * 500; | |
13 | 548 // XXX fixme |
549 //measurements[n_measurements] = temp; | |
6 | 550 // XXX something if it hits the limit |
551 | |
552 // measure AVR internal temperature against 1.1 ref. | |
553 ADMUX = _BV(ADLAR) | _BV(MUX3) | _BV(REFS1) | _BV(REFS0); | |
554 ADCSRA |= _BV(ADSC); | |
555 loop_until_bit_is_clear(ADCSRA, ADSC); | |
556 uint16_t res_internal = ADCL; | |
557 res_internal |= ADCH << 8; | |
558 | |
559 float internal_volts = res_internal * (1.1 / 1024.0); | |
560 | |
561 // 1mV/degree | |
562 int internal_temp = (internal_volts - 2.73) * 5000; | |
13 | 563 // XXX fixme |
564 //internal_measurements[n_measurements] = internal_temp; | |
6 | 565 |
566 printf_P("measure %d: external %d, internal %d, 1.1 %d\n", | |
567 n_measurements, temp, internal_temp, f_11); | |
568 | |
569 n_measurements++; | |
8 | 570 //PRR |= _BV(PRADC); |
6 | 571 } |
14 | 572 #endif |
6 | 573 |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
574 static void |
1 | 575 do_measurement() |
576 { | |
13 | 577 uint8_t n_sensors; |
14 | 578 printf("do_measurement\n"); |
13 | 579 eeprom_read(n_sensors, n_sensors); |
14 | 580 printf("do_measurement sensors %d\n", n_sensors); |
13 | 581 |
582 uint8_t ret = simple_ds18b20_start_meas(NULL); | |
14 | 583 printf_P(PSTR("Read all sensors, ret %d, waiting...\n"), ret); |
13 | 584 _delay_ms(DS18B20_TCONV_12BIT); |
585 | |
586 if (n_measurements == NUM_MEASUREMENTS) | |
587 { | |
14 | 588 printf_P(PSTR("Measurements .overflow\n")); |
13 | 589 n_measurements = 0; |
590 } | |
6 | 591 |
14 | 592 for (uint8_t s = 0; s < MAX_SENSORS; s++) |
13 | 593 { |
594 int16_t decicelsius; | |
14 | 595 if (s >= n_sensors) |
13 | 596 { |
597 decicelsius = VALUE_NOSENSOR; | |
598 } | |
599 else | |
600 { | |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
601 uint8_t id[ID_LEN]; |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
602 eeprom_read_to(id, sensor_id[s], ID_LEN); |
13 | 603 |
604 uint8_t ret = simple_ds18b20_read_decicelsius(id, &decicelsius); | |
605 if (ret != DS18X20_OK) | |
606 { | |
607 decicelsius = VALUE_BROKEN; | |
608 } | |
609 } | |
14 | 610 measurements[n_measurements][s] = decicelsius; |
13 | 611 } |
14 | 612 n_measurements++; |
12 | 613 //do_adc_335(); |
1 | 614 } |
615 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
616 static void |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
617 do_comms() |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
618 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
619 // turn on bluetooth |
7 | 620 uart_on(); |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
621 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
622 // write sd card here? same 3.3v regulator... |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
623 |
12 | 624 printf("ready> \n"); |
625 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
626 for (comms_timeout = WAKE_SECS; comms_timeout > 0; ) |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
627 { |
6 | 628 if (need_measurement) |
629 { | |
14 | 630 need_measurement = 0; |
631 printf("measure from do_comms\n"); | |
6 | 632 do_measurement(); |
633 } | |
634 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
635 // wait for commands from the master |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
636 idle_sleep(); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
637 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
638 |
6 | 639 uart_off(); |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
640 set_aux_power(0); |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
641 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
642 |
7 | 643 static void |
644 blink() | |
645 { | |
646 PORT_LED &= ~_BV(PIN_LED); | |
9 | 647 _delay_ms(1); |
7 | 648 PORT_LED |= _BV(PIN_LED); |
649 } | |
650 | |
651 static void | |
652 long_delay(int ms) | |
653 { | |
654 int iter = ms / 100; | |
655 | |
656 for (int i = 0; i < iter; i++) | |
657 { | |
658 _delay_ms(100); | |
659 } | |
660 } | |
661 | |
8 | 662 ISR(BADISR_vect) |
663 { | |
14 | 664 //uart_on(); |
8 | 665 printf_P(PSTR("Bad interrupt\n")); |
666 } | |
667 | |
9 | 668 |
0 | 669 int main(void) |
670 { | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
671 setup_chip(); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
672 blink(); |
9 | 673 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
674 set_aux_power(0); |
7 | 675 |
6 | 676 stdout = &mystdout; |
7 | 677 uart_on(); |
678 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
679 printf(PSTR("Started.\n")); |
13 | 680 |
681 check_first_startup(); | |
682 | |
8 | 683 uart_off(); |
0 | 684 |
6 | 685 // turn off everything except timer2 |
8 | 686 //PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC); |
687 | |
688 // for testing | |
689 uart_on(); | |
690 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
691 setup_tick_counter(); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
692 |
12 | 693 sei(); |
8 | 694 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
695 #if 0 |
6 | 696 for (;;) |
697 { | |
12 | 698 do_comms(); |
6 | 699 } |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
700 #endif |
12 | 701 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
702 for(;;) |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
703 { |
1 | 704 if (need_measurement) |
705 { | |
14 | 706 need_measurement = 0; |
1 | 707 do_measurement(); |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
708 continue; |
1 | 709 } |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
710 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
711 if (need_comms) |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
712 { |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
713 need_comms = 0; |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
714 do_comms(); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
715 continue; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
716 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
717 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
718 deep_sleep(); |
9 | 719 blink(); |
10 | 720 printf("."); |
0 | 721 } |
12 | 722 |
0 | 723 return 0; /* never reached */ |
724 } |