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