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