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