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