comparison sprink.c @ 0:93d4ddff7dd0

Jumbo commit since I appear to have forgotten to do this before..
author Daniel O'Connor <darius@dons.net.au>
date Wed, 04 Jan 2012 23:19:12 +1030
parents
children be930b34fcd3
comparison
equal deleted inserted replaced
-1:000000000000 0:93d4ddff7dd0
1 /*
2 * Sprinkler relay control
3 *
4 * Copyright (c) 2009
5 * Daniel O'Connor <darius@dons.net.au>. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <avr/io.h>
30 #include <avr/interrupt.h>
31 #include <avr/pgmspace.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <util/delay.h>
37 #include <avr/wdt.h>
38
39 #include "bitstring.h"
40 #include "cons.h"
41 #include "ds1307.h"
42 #include "1wire.h"
43
44 /*
45 ** Fuse bits should be set as follows
46 **
47 ** EFUSE - BOD 4.3V -> xxxxx100 -> 0xfc
48 ** HFUSE - SPIEN, EESAVE
49 ** BOOTSZ1/0 -> 11010001 -> 0xd1
50 ** LFUSE - SUT1, CKSEL1 (low XTAL) -> 11011101 -> 0xdd
51 **
52 ** http://www.engbedded.com/fusecalc/
53 **
54 ** From Ateml the fuses were..
55 ** EFUSE - 0xff - 11111111 - BOD disabled
56 ** HFUSE - 0x99 - 10011001 - OCD disabled, JTAG enabled, SPI enabled, WDT disabled,
57 ** - EESAVE off, BOOTSZ0/1, BOOTRST off
58 ** LFUSE - 0x62 - 01100010 - CLKDIV8, no CKOUT, long SUT, CKSEL3/2/0 (internal 8Mhz)
59 */
60
61 #define NUMSPRINKS 6
62
63 /* Holds all the settings needed */
64 typedef struct {
65 bitstr_t bit_decl(mon[NUMSPRINKS], 24);
66 bitstr_t bit_decl(tue[NUMSPRINKS], 24);
67 bitstr_t bit_decl(wed[NUMSPRINKS], 24);
68 bitstr_t bit_decl(thu[NUMSPRINKS], 24);
69 bitstr_t bit_decl(fri[NUMSPRINKS], 24);
70 bitstr_t bit_decl(sat[NUMSPRINKS], 24);
71 bitstr_t bit_decl(sun[NUMSPRINKS], 24);
72
73 } __attribute__((packed)) settings_t;
74
75 /* Current settings in RAM */
76 static settings_t settings;
77
78 /* Our map of EEPROM */
79 struct {
80 settings_t settings;
81 uint16_t crc;
82 } ee_area __attribute__((section(".eeprom")));
83
84 /* Defaults that are shoved into EEPROM if it fails checksum */
85 const PROGMEM settings_t default_settings = {
86 /* XXX: no handy macro for this */
87 .mon[0] = {0, 0, 0},
88 .mon[1] = {0, 0, 0},
89 .mon[2] = {0, 0, 0},
90 .mon[3] = {0, 0, 0},
91 .mon[4] = {0, 0, 0},
92 .mon[5] = {0, 0, 0}
93 };
94
95 /*
96 * Mirror of the MCUCSR register, taken early during startup.
97 */
98 uint8_t mcucsr __attribute__((section(".noinit")));
99
100 void process_cmd(void);
101
102 /*
103 * Read out and reset MCUCSR early during startup.
104 */
105 void handle_mcucsr(void)
106 __attribute__((section(".init3")))
107 __attribute__((naked));
108 void handle_mcucsr(void) {
109 wdt_disable();
110 #ifdef MCUSR
111 mcucsr = MCUSR;
112 MCUSR = 0;
113 #else
114 mcucsr = MCUCSR;
115 MCUCSR = 0;
116 #endif
117 }
118
119 consbuf_t cmd;
120
121 int
122 main(void) {
123 /* Disable interrupts while we frob stuff */
124 cli();
125
126 /* Init UART */
127 cons_init();
128
129 /* Init TWI etc */
130 ds1307_init();
131
132 /* Set up the one wire stuff */
133 OWInit();
134
135 /* Analogue input is PA0:7 */
136 DDRA = 0x00;
137 PORTA = 0x00;
138 DIDR0 = 0xff; /* Disable digital input buffers */
139 #ifdef PRR
140 PRR &= ~_BV(PRADC); /* Power ADV on - note that the
141 * datasheet says this is already 0 at
142 * power on.. */
143 #endif
144
145 /* PB0 Used for 1-wire bus
146 * PB4:7 Used for relay board */
147 DDRB = 0xf0;
148 PORTB = 0x00;
149
150 /* TWI (0:1) */
151 DDRC = 0x03;
152 PORTC = 0x03;
153
154 /* USART (0:1) */
155 DDRD = 0x03;
156 PORTD = 0x03;
157
158 printf_P(PSTR("\r\n\r\n===============\r\n"
159 "Inited!\r\n\r\n"));
160
161 if ((mcucsr & _BV(PORF)) == _BV(PORF))
162 printf_P(PSTR("Power on\r\n"));
163
164 if ((mcucsr & _BV(EXTRF)) == _BV(EXTRF))
165 printf_P(PSTR("External\r\n"));
166
167 if ((mcucsr & _BV(BORF)) == _BV(BORF))
168 printf_P(PSTR("Brown-out\r\n"));
169
170 if ((mcucsr & _BV(WDRF)) == _BV(WDRF))
171 printf_P(PSTR("Watchdog\r\n"));
172
173 #ifdef JTRF
174 if ((mcucsr & _BV(JTRF)) == _BV(JTRF))
175 printf_P(PSTR("JTAG\r\n"));
176 #endif
177
178 /* Ready to go! */
179 sei();
180
181 /*
182 * Enable the watchdog with the largest prescaler. Will cause a
183 * watchdog reset after approximately 2 s @ Vcc = 5 V
184 *
185 * Gets reset in the loop below and in the tempctrl.c timer IRQ
186 */
187 wdt_enable(WDTO_2S);
188
189 printf_P(PSTR("> "));
190 cmd.state = 0;
191
192 /* Wait for user input or an "interrupt" */
193 while (1) {
194 wdt_reset();
195
196 if (cmd.state == 255) {
197 process_cmd();
198 printf_P(PSTR("> "));
199 /* Allow new characters to be processed */
200 cmd.state = 0;
201 }
202 }
203 }
204
205 void
206 process_cmd(void) {
207 /* User just pressed enter */
208 if (cmd.len == 0)
209 return;
210
211 if (!strcasecmp_P((char *)cmd.buf, PSTR("?")) ||
212 !strcasecmp_P((char *)cmd.buf, PSTR("help"))) {
213 printf_P(PSTR("help This help\r\n"
214 "gc Get time of day\r\n"
215 "sc time Set time of day (time is YYYY/MM/DD HH:MM:SS)\r\n"
216 "in port Read from a port\r\n"
217 "ou port val Write to a port (val in hex)\r\n"
218 "dd port val Set DDR on port\r\n"
219 "an pin Sample from pin\r\n"
220 "sr Search for 1-wire devices\r\n"
221 "te ID Sample temperature from ID\r\n"
222 "re Reset 1-wire bus\r\n"
223 "rb Read bit from 1-wire bus\r\n"
224 "rc Read byte from 1-wire bus\r\n"
225 "wb bit Write bit to 1-wire bus\r\n"
226 "wc byte Write byte to 1-wire bus\r\n"
227 "zz Reset micro\r\n"));
228 return;
229 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("gc"), 2)) {
230 ds1307_printtime(PSTR(""), PSTR("\r\n"));
231 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("sc"), 2)) {
232 if (cmd.len < 17) {
233 printf_P(PSTR("Invalid TOD\r\n"));
234 } else {
235 if (ds1307_settod((char *)cmd.buf + 3) != 1)
236 printf_P(PSTR("Unable to set TOD\r\n"));
237 }
238 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("in"), 2)) {
239 uint8_t inp;
240
241 switch (tolower(cmd.buf[3])) {
242 case 'a':
243 inp = PINA;
244 break;
245
246 case 'b':
247 inp = PINB;
248 break;
249
250 case 'c':
251 inp = PINC;
252 break;
253
254 case 'd':
255 inp = PIND;
256 break;
257
258 default:
259 printf_P(PSTR("Unknown port\r\n"));
260 return;
261 }
262 printf_P(PSTR("PORT %c <= 0x%02x\r\n"), toupper(cmd.buf[3]), inp);
263 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("an"), 2)) {
264 uint16_t res;
265 int pin;
266
267 if (sscanf_P((char *)cmd.buf, PSTR("an %d"), &pin) != 1) {
268 printf_P(PSTR("Unable to parse\r\n"));
269 return;
270 }
271 if (pin < 0 || pin > 7) {
272 printf_P(PSTR("Unknown pin\r\n"));
273 return;
274 }
275
276 /* Select desired pin, use VCC reference */
277 ADMUX = _BV(REFS0) | pin;
278
279 /* Enable ADC, start conversion, set divisor to 64
280 * (8e6 / 64 => 125kHz (max is 200kHz)
281 */
282 ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADPS2) | _BV(ADPS1);
283
284 /* Spin waiting for result */
285 while ((ADCSRA & _BV(ADIF)) == 0)
286 ;
287 res = ADCL | (ADCH << 8);
288 printf_P(PSTR("Pin %d <= %hhd\r\n"), pin, res);
289
290 /* Disable ADC */
291 ADCSRA = 0;
292 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("ou"), 2)) {
293 char port;
294 int val;
295
296 if (sscanf_P((char *)cmd.buf, PSTR("ou %c %x"), &port, &val) != 2) {
297 printf_P(PSTR("Unable to parse\r\n"));
298 return;
299 }
300
301 switch (port) {
302 case 'a':
303 PORTA = val & 0xff;
304 break;
305
306 case 'b':
307 PORTB = val & 0xff;
308 break;
309
310 case 'c':
311 PORTC = val & 0xff;
312 break;
313
314 case 'd':
315 PORTD = val & 0xff;
316 break;
317
318 default:
319 printf_P(PSTR("Unknown port\r\n"));
320 return;
321 }
322 printf_P(PSTR("PORT%c <= 0x%02x\r\n"), toupper(port), val);
323 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("dd"), 2)) {
324 char port;
325 uint8_t val;
326 int num;
327
328 num = sscanf_P((char *)cmd.buf, PSTR("dd %c %x"), &port, &val);
329
330 if (num != 1 && num != 2) {
331 printf_P(PSTR("Unable to parse dd arguments\r\n"));
332 return;
333 }
334
335 if (num == 1) {
336 switch (port) {
337 case 'a':
338 DDRA = val;
339 break;
340
341 case 'b':
342 DDRB = val;
343 break;
344
345 case 'c':
346 DDRC = val;
347 break;
348
349 case 'd':
350 DDRD = val;
351 break;
352
353 default:
354 printf_P(PSTR("Unknown port\r\n"));
355 return;
356 }
357 printf_P(PSTR("DDR%c => 0x%02x\r\n"), toupper(port), val);
358 } else {
359 switch (port) {
360 case 'a':
361 DDRA = val & 0xff;
362 break;
363
364 case 'b':
365 DDRB = val & 0xff;
366 break;
367
368 case 'c':
369 DDRC = val & 0xff;
370 break;
371
372 case 'd':
373 DDRD = val & 0xff;
374 break;
375
376 default:
377 printf_P(PSTR("Unknown port\r\n"));
378 return;
379 }
380 printf_P(PSTR("DDR%c <= 0x%02x\r\n"), toupper(port), val);
381 }
382 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("zz"), 2)) {
383 cli();
384 wdt_enable(WDTO_15MS);
385 for (;;)
386 ;
387 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("sr"), 2)) {
388 uint8_t ROM[8];
389 int8_t i;
390
391 memset(ROM, 0, 8);
392
393 i = OWFirst(ROM, 1, 0);
394 do {
395 switch (i) {
396 case OW_NOMODULES:
397 case OW_FOUND:
398 break;
399
400 case OW_BADWIRE:
401 case OW_NOPRESENCE:
402 case OW_BADCRC:
403 default:
404 printf_P(PSTR("Err %d\r\n"), i);
405 break;
406 }
407
408 if (i != OW_FOUND)
409 break;
410
411 for (i = 0; i < 8; i++)
412 printf_P(PSTR("%02x%S"), ROM[i], i == 7 ? PSTR("\r\n") : PSTR(":"));
413
414 i = OWNext(ROM, 1, 0);
415 } while (1);
416 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("te"), 2)) {
417 uint8_t ROM[8];
418 int16_t t;
419
420 if (sscanf_P((char *)cmd.buf, PSTR("te %hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx"),
421 &ROM[0], &ROM[1], &ROM[2], &ROM[3],
422 &ROM[4], &ROM[5], &ROM[6], &ROM[7]) != 8) {
423 printf_P(PSTR("Unable to parse ROM ID\r\n"));
424 return;
425 }
426
427 t = OWGetTemp(ROM);
428 switch (t) {
429 case OW_TEMP_WRONG_FAM:
430 printf_P(PSTR("ROM specified isn't a temperature sensor\r\n"));
431 break;
432
433 case OW_TEMP_CRC_ERR:
434 printf_P(PSTR("CRC mismatch\r\n"));
435 break;
436
437 case OW_TEMP_NO_ROM:
438 printf_P(PSTR("No ROM found\r\n"));
439 break;
440
441 default:
442 printf_P(PSTR("%d.%02d\r\n"), GETWHOLE(t), GETFRAC(t));
443 break;
444 }
445 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("re"), 2)) {
446 printf_P(PSTR("Reset = %d\r\n"), OWTouchReset());
447 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("rb"), 2)) {
448 printf_P(PSTR("Read %d\r\n"), OWReadBit());
449 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("rc"), 2)) {
450 printf_P(PSTR("Read 0x%02x\r\n"), OWReadByte());
451 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("wb"), 2)) {
452 uint8_t d;
453
454 if (sscanf_P((char *)cmd.buf, PSTR("wb %hhu"), &d) != 1) {
455 printf_P(PSTR("Can't parse bit\r\n"));
456 return;
457 }
458 OWWriteBit(d);
459 printf_P(PSTR("Wrote %d\r\n"), d);
460 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("wc"), 2)) {
461 uint8_t d;
462
463 if (sscanf_P((char *)cmd.buf, PSTR("wc %hhx"), &d) != 1) {
464 printf_P(PSTR("Can't parse byte\r\n"));
465 return;
466 }
467
468 OWWriteByte(d);
469 printf_P(PSTR("Wrote 0x%02x\r\n"), d);
470 } else {
471 printf_P(PSTR("Unknown command, help for a list\r\n"));
472 }
473 }
474