Mercurial > templog
annotate main.c @ 26:d3e5934fe55c
- Move crc16 to utils and fix it
- Fix main.c crc calculation
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 07 Jun 2012 20:26:50 +0800 |
parents | 2943f62c8e62 |
children | dbbd503119ba |
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]; |
88 | |
17 | 89 static uint8_t measure_count; |
90 static uint16_t comms_count; | |
91 | |
92 static uint32_t clock_epoch; | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
93 |
13 | 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; | |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
105 uint8_t sensor_id[MAX_SENSORS][ID_LEN]; |
13 | 106 }; |
107 | |
8 | 108 #define DEBUG(str) printf_P(PSTR(str)) |
109 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
110 static void deep_sleep(); |
1 | 111 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
112 // Very first setup |
18 | 113 static void |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
114 setup_chip() |
18 | 115 { |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
116 // Set clock to 2mhz |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
117 cli(); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
118 CLKPR = _BV(CLKPCE); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
119 // divide by 4 |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
120 CLKPR = _BV(CLKPS1); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
121 sei(); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
122 |
19 | 123 // 3.3v power for bluetooth and SD |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
124 DDR_LED |= _BV(PIN_LED); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
125 DDR_SHDN |= _BV(PIN_SHDN); |
19 | 126 |
18 | 127 // INT0 setup |
128 EIMSK = _BV(INT0); | |
129 // set pullup | |
130 PORTD |= _BV(PD2); | |
131 } | |
132 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
133 static void |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
134 set_aux_power(uint8_t on) |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
135 { |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
136 if (on) |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
137 { |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
138 PORT_SHDN &= ~_BV(PIN_SHDN); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
139 } |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
140 else |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
141 { |
24
44c5ab5ea879
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
22
diff
changeset
|
142 //PORT_SHDN |= _BV(PIN_SHDN); |
16
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 |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
146 static void |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
147 setup_tick_counter() |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
148 { |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
149 // set up counter2. |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
150 // COM21 COM20 Set OC2 on Compare Match (p116) |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
151 // WGM21 Clear counter on compare |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
152 //TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
153 // toggle on match |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
154 TCCR2A = _BV(COM2A0); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
155 // CS22 CS21 CS20 clk/1024 |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
156 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
157 // set async mode |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
158 ASSR |= _BV(AS2); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
159 TCNT2 = 0; |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
160 OCR2A = SLEEP_COMPARE; |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
161 // interrupt |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
162 TIMSK2 = _BV(OCIE2A); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
163 } |
18 | 164 |
0 | 165 static void |
7 | 166 uart_on() |
0 | 167 { |
8 | 168 // Power reduction register |
169 //PRR &= ~_BV(PRUSART0); | |
170 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
171 // All of this needs to be done each time after turning off the PRR |
0 | 172 // baud rate |
7 | 173 UBRR0H = (unsigned char)(UBRR >> 8); |
174 UBRR0L = (unsigned char)UBRR; | |
175 // set 2x clock, improves accuracy of UBRR | |
176 UCSR0A |= _BV(U2X0); | |
6 | 177 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); |
0 | 178 //8N1 |
7 | 179 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); |
6 | 180 } |
181 | |
182 static void | |
183 uart_off() | |
184 { | |
24
44c5ab5ea879
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
22
diff
changeset
|
185 #if 0 |
6 | 186 // Turn of interrupts and disable tx/rx |
187 UCSR0B = 0; | |
188 | |
189 // Power reduction register | |
8 | 190 //PRR |= _BV(PRUSART0); |
24
44c5ab5ea879
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
22
diff
changeset
|
191 #endif |
0 | 192 } |
193 | |
10 | 194 int |
0 | 195 uart_putchar(char c, FILE *stream) |
196 { | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
197 // XXX could perhaps sleep in the loop for power. |
8 | 198 if (c == '\n') |
199 { | |
10 | 200 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
|
201 UDR0 = '\r'; |
8 | 202 } |
2 | 203 loop_until_bit_is_set(UCSR0A, UDRE0); |
204 UDR0 = c; | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
205 if (stream == crc_stdout) |
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
206 { |
26
d3e5934fe55c
- Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents:
25
diff
changeset
|
207 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
|
208 } |
10 | 209 if (c == '\r') |
210 { | |
211 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
|
212 UDR0 = '\n'; |
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
213 if (stream == crc_stdout) |
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
214 { |
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
215 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
|
216 } |
10 | 217 } |
0 | 218 return 0; |
219 } | |
220 | |
221 static void | |
222 cmd_fetch() | |
223 { | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
224 crc_out = 0; |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
225 uint8_t n_sensors; |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
226 eeprom_read(n_sensors, n_sensors); |
13 | 227 |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
228 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
|
229 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
|
230 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
|
231 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
|
232 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
|
233 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
|
234 { |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
235 uint8_t id[ID_LEN]; |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
236 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
|
237 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
|
238 printhex(id, ID_LEN, crc_stdout); |
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
239 fputc('\n', crc_stdout); |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
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("measurements=%d\n"), n_measurements); |
14 | 242 for (uint16_t n = 0; n < n_measurements; n++) |
0 | 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("meas%3d="), n); |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
245 for (uint8_t s = 0; s < n_sensors; s++) |
13 | 246 { |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
247 fprintf_P(crc_stdout, PSTR(" %6d"), measurements[n][s]); |
13 | 248 } |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
249 fputc('\n', crc_stdout); |
0 | 250 } |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
251 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
|
252 fprintf_P(stdout, PSTR("CRC=%hu\n"), crc_out); |
0 | 253 } |
254 | |
255 static void | |
256 cmd_clear() | |
257 { | |
7 | 258 n_measurements = 0; |
259 printf_P(PSTR("Cleared\n")); | |
0 | 260 } |
261 | |
262 static void | |
263 cmd_btoff() | |
264 { | |
22
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
265 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
|
266 printf_P(PSTR("off:%d\n"), next_wake); |
7 | 267 _delay_ms(50); |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
268 comms_timeout = 0; |
0 | 269 } |
270 | |
271 static void | |
12 | 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); | |
14 | 282 printf_P(PSTR("All sensors, ret %d, waiting...\n"), ret); |
283 long_delay(DS18B20_TCONV_12BIT); | |
12 | 284 simple_ds18b20_read_all(); |
285 } | |
286 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
287 #if 0 |
13 | 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 } | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
341 #endif |
13 | 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(); | |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
351 eeprom_write_from(id, sensor_id[n], ID_LEN); |
13 | 352 n++; |
353 eeprom_write(n, n_sensors); | |
354 sei(); | |
355 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
|
356 printhex(id, ID_LEN, stdout); |
13 | 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 | |
17 | 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 | |
13 | 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")); | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
413 // 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
|
414 long_delay(2); |
13 | 415 cmd_init(); |
416 cmd_add_all(); | |
14 | 417 cli(); |
418 magic = EXPECT_MAGIC; | |
419 eeprom_write(magic, magic); | |
420 sei(); | |
13 | 421 } |
422 } | |
423 | |
12 | 424 static void |
0 | 425 read_handler() |
426 { | |
6 | 427 if (strcmp_P(readbuf, PSTR("fetch")) == 0) |
0 | 428 { |
429 cmd_fetch(); | |
430 } | |
6 | 431 else if (strcmp_P(readbuf, PSTR("clear")) == 0) |
0 | 432 { |
433 cmd_clear(); | |
434 } | |
6 | 435 else if (strcmp_P(readbuf, PSTR("btoff")) == 0) |
0 | 436 { |
437 cmd_btoff(); | |
438 } | |
12 | 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 } | |
13 | 447 else if (strcmp_P(readbuf, PSTR("addall"))== 0) |
448 { | |
449 cmd_add_all(); | |
450 } | |
17 | 451 else if (strncmp_P(readbuf, PSTR("settime "), |
452 strlen("settime ") == 0)) | |
453 { | |
454 cmd_settime(&readbuf[strlen("settime ")]); | |
455 } | |
13 | 456 else if (strcmp_P(readbuf, PSTR("init")) == 0) |
457 { | |
458 cmd_init(); | |
459 } | |
0 | 460 else |
461 { | |
6 | 462 printf_P(PSTR("Bad command\n")); |
0 | 463 } |
464 } | |
465 | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
466 ISR(INT0_vect) |
18 | 467 { |
468 need_comms = 1; | |
24
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); |
44c5ab5ea879
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
22
diff
changeset
|
477 blink(); |
44c5ab5ea879
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
22
diff
changeset
|
478 _delay_ms(100); |
44c5ab5ea879
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
22
diff
changeset
|
479 blink(); |
44c5ab5ea879
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
22
diff
changeset
|
480 _delay_ms(100); |
44c5ab5ea879
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
22
diff
changeset
|
481 blink(); |
44c5ab5ea879
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
22
diff
changeset
|
482 _delay_ms(100); |
18 | 483 } |
484 | |
485 | |
2 | 486 ISR(USART_RX_vect) |
0 | 487 { |
2 | 488 char c = UDR0; |
12 | 489 uart_putchar(c, NULL); |
22
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
490 // 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
|
491 if (c == '\r' || c == '\n') |
0 | 492 { |
22
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
493 if (readpos > 0) |
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
494 { |
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
495 readbuf[readpos] = '\0'; |
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
496 read_handler(); |
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
497 readpos = 0; |
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
498 } |
0 | 499 } |
500 else | |
501 { | |
502 readbuf[readpos] = c; | |
503 readpos++; | |
504 if (readpos >= sizeof(readbuf)) | |
505 { | |
506 readpos = 0; | |
507 } | |
508 } | |
509 } | |
510 | |
2 | 511 ISR(TIMER2_COMPA_vect) |
1 | 512 { |
14 | 513 TCNT2 = 0; |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
514 measure_count ++; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
515 comms_count ++; |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
516 |
17 | 517 clock_epoch ++; |
518 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
519 if (comms_timeout != 0) |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
520 { |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
521 comms_timeout--; |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
522 } |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
523 |
14 | 524 if (measure_count >= MEASURE_WAKE) |
1 | 525 { |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
526 measure_count = 0; |
1 | 527 need_measurement = 1; |
528 } | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
529 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
530 if (comms_count >= COMMS_WAKE) |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
531 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
532 comms_count = 0; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
533 need_comms = 1; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
534 } |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
535 |
1 | 536 } |
537 | |
3 | 538 DWORD get_fattime (void) |
539 { | |
540 return 0; | |
541 } | |
542 | |
1 | 543 static void |
544 deep_sleep() | |
545 { | |
546 // p119 of manual | |
2 | 547 OCR2A = SLEEP_COMPARE; |
548 loop_until_bit_is_clear(ASSR, OCR2AUB); | |
1 | 549 |
550 set_sleep_mode(SLEEP_MODE_PWR_SAVE); | |
551 sleep_mode(); | |
552 } | |
553 | |
554 static void | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
555 idle_sleep() |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
556 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
557 set_sleep_mode(SLEEP_MODE_IDLE); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
558 sleep_mode(); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
559 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
560 |
14 | 561 #if 0 |
562 // untested | |
6 | 563 static void |
564 do_adc_335() | |
565 { | |
8 | 566 //PRR &= ~_BV(PRADC); |
6 | 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; | |
13 | 593 // XXX fixme |
594 //measurements[n_measurements] = temp; | |
6 | 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; | |
13 | 608 // XXX fixme |
609 //internal_measurements[n_measurements] = internal_temp; | |
6 | 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++; | |
8 | 615 //PRR |= _BV(PRADC); |
6 | 616 } |
14 | 617 #endif |
6 | 618 |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
619 static void |
1 | 620 do_measurement() |
621 { | |
13 | 622 uint8_t n_sensors; |
14 | 623 printf("do_measurement\n"); |
13 | 624 eeprom_read(n_sensors, n_sensors); |
14 | 625 printf("do_measurement sensors %d\n", n_sensors); |
13 | 626 |
627 uint8_t ret = simple_ds18b20_start_meas(NULL); | |
14 | 628 printf_P(PSTR("Read all sensors, ret %d, waiting...\n"), ret); |
13 | 629 _delay_ms(DS18B20_TCONV_12BIT); |
630 | |
631 if (n_measurements == NUM_MEASUREMENTS) | |
632 { | |
14 | 633 printf_P(PSTR("Measurements .overflow\n")); |
13 | 634 n_measurements = 0; |
635 } | |
6 | 636 |
14 | 637 for (uint8_t s = 0; s < MAX_SENSORS; s++) |
13 | 638 { |
639 int16_t decicelsius; | |
14 | 640 if (s >= n_sensors) |
13 | 641 { |
642 decicelsius = VALUE_NOSENSOR; | |
643 } | |
644 else | |
645 { | |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
646 uint8_t id[ID_LEN]; |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
647 eeprom_read_to(id, sensor_id[s], ID_LEN); |
13 | 648 |
649 uint8_t ret = simple_ds18b20_read_decicelsius(id, &decicelsius); | |
650 if (ret != DS18X20_OK) | |
651 { | |
652 decicelsius = VALUE_BROKEN; | |
653 } | |
654 } | |
14 | 655 measurements[n_measurements][s] = decicelsius; |
13 | 656 } |
22
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
657 |
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
658 if (n_measurements == 0) |
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
659 { |
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
660 first_measurement_clock = clock_epoch; |
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
661 } |
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
662 last_measurement_clock = clock_epoch; |
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
663 |
14 | 664 n_measurements++; |
12 | 665 //do_adc_335(); |
1 | 666 } |
667 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
668 static void |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
669 do_comms() |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
670 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
671 // turn on bluetooth |
24
44c5ab5ea879
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
22
diff
changeset
|
672 set_aux_power(1); |
7 | 673 uart_on(); |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
674 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
675 // 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
|
676 |
12 | 677 printf("ready> \n"); |
678 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
679 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
|
680 { |
6 | 681 if (need_measurement) |
682 { | |
14 | 683 need_measurement = 0; |
684 printf("measure from do_comms\n"); | |
6 | 685 do_measurement(); |
686 } | |
687 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
688 // wait for commands from the master |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
689 idle_sleep(); |
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 |
6 | 692 uart_off(); |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
693 set_aux_power(0); |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
694 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
695 |
7 | 696 static void |
697 blink() | |
698 { | |
699 PORT_LED &= ~_BV(PIN_LED); | |
9 | 700 _delay_ms(1); |
7 | 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 | |
8 | 715 ISR(BADISR_vect) |
716 { | |
14 | 717 //uart_on(); |
8 | 718 printf_P(PSTR("Bad interrupt\n")); |
719 } | |
720 | |
9 | 721 |
0 | 722 int main(void) |
723 { | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
724 setup_chip(); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
725 blink(); |
9 | 726 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
727 set_aux_power(0); |
7 | 728 |
6 | 729 stdout = &mystdout; |
7 | 730 uart_on(); |
731 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
732 printf(PSTR("Started.\n")); |
13 | 733 |
734 check_first_startup(); | |
735 | |
8 | 736 uart_off(); |
0 | 737 |
6 | 738 // turn off everything except timer2 |
8 | 739 //PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC); |
740 | |
741 // for testing | |
742 uart_on(); | |
743 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
744 setup_tick_counter(); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
745 |
12 | 746 sei(); |
8 | 747 |
25
2943f62c8e62
- Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents:
24
diff
changeset
|
748 need_comms = 1; |
2943f62c8e62
- Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents:
24
diff
changeset
|
749 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
750 #if 0 |
6 | 751 for (;;) |
752 { | |
12 | 753 do_comms(); |
6 | 754 } |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
755 #endif |
12 | 756 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
757 for(;;) |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
758 { |
1 | 759 if (need_measurement) |
760 { | |
14 | 761 need_measurement = 0; |
1 | 762 do_measurement(); |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
763 continue; |
1 | 764 } |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
765 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
766 if (need_comms) |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
767 { |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
768 need_comms = 0; |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
769 do_comms(); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
770 continue; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
771 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
772 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
773 deep_sleep(); |
9 | 774 blink(); |
10 | 775 printf("."); |
0 | 776 } |
12 | 777 |
0 | 778 return 0; /* never reached */ |
779 } |