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