comparison sprink.c @ 1:be930b34fcd3

Make it compile, no testing yet.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 26 Jan 2015 23:04:09 +1030
parents 93d4ddff7dd0
children 1188042ddc2f
comparison
equal deleted inserted replaced
0:93d4ddff7dd0 1:be930b34fcd3
38 38
39 #include "bitstring.h" 39 #include "bitstring.h"
40 #include "cons.h" 40 #include "cons.h"
41 #include "ds1307.h" 41 #include "ds1307.h"
42 #include "1wire.h" 42 #include "1wire.h"
43 #include "water.h"
43 44
44 /* 45 /*
45 ** Fuse bits should be set as follows 46 ** Fuse bits should be set as follows
46 ** 47 **
47 ** EFUSE - BOD 4.3V -> xxxxx100 -> 0xfc 48 ** EFUSE - BOD 4.3V -> xxxxx100 -> 0xfc
56 ** HFUSE - 0x99 - 10011001 - OCD disabled, JTAG enabled, SPI enabled, WDT disabled, 57 ** HFUSE - 0x99 - 10011001 - OCD disabled, JTAG enabled, SPI enabled, WDT disabled,
57 ** - EESAVE off, BOOTSZ0/1, BOOTRST off 58 ** - EESAVE off, BOOTSZ0/1, BOOTRST off
58 ** LFUSE - 0x62 - 01100010 - CLKDIV8, no CKOUT, long SUT, CKSEL3/2/0 (internal 8Mhz) 59 ** LFUSE - 0x62 - 01100010 - CLKDIV8, no CKOUT, long SUT, CKSEL3/2/0 (internal 8Mhz)
59 */ 60 */
60 61
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 /* 62 /*
96 * Mirror of the MCUCSR register, taken early during startup. 63 * Mirror of the MCUCSR register, taken early during startup.
97 */ 64 */
98 uint8_t mcucsr __attribute__((section(".noinit"))); 65 uint8_t mcucsr __attribute__((section(".noinit")));
99 66
130 ds1307_init(); 97 ds1307_init();
131 98
132 /* Set up the one wire stuff */ 99 /* Set up the one wire stuff */
133 OWInit(); 100 OWInit();
134 101
102 /* Init water control state machine */
103 water_init();
104
135 /* Analogue input is PA0:7 */ 105 /* Analogue input is PA0:7 */
136 DDRA = 0x00; 106 DDRA = 0x00;
137 PORTA = 0x00; 107 PORTA = 0x00;
138 DIDR0 = 0xff; /* Disable digital input buffers */ 108 DIDR0 = 0xff; /* Disable digital input buffers */
139 #ifdef PRR 109 #ifdef PRR
140 PRR &= ~_BV(PRADC); /* Power ADV on - note that the 110 PRR &= ~_BV(PRADC); /* Power ADC on - note that the
141 * datasheet says this is already 0 at 111 * datasheet says this is already 0 at
142 * power on.. */ 112 * power on.. */
143 #endif 113 #endif
144 114
145 /* PB0 Used for 1-wire bus 115 /* PB0 Used for 1-wire bus
191 161
192 /* Wait for user input or an "interrupt" */ 162 /* Wait for user input or an "interrupt" */
193 while (1) { 163 while (1) {
194 wdt_reset(); 164 wdt_reset();
195 165
166 water_update();
167
196 if (cmd.state == 255) { 168 if (cmd.state == 255) {
197 process_cmd(); 169 process_cmd();
198 printf_P(PSTR("> ")); 170 printf_P(PSTR("> "));
199 /* Allow new characters to be processed */ 171 /* Allow new characters to be processed */
200 cmd.state = 0; 172 cmd.state = 0;
208 if (cmd.len == 0) 180 if (cmd.len == 0)
209 return; 181 return;
210 182
211 if (!strcasecmp_P((char *)cmd.buf, PSTR("?")) || 183 if (!strcasecmp_P((char *)cmd.buf, PSTR("?")) ||
212 !strcasecmp_P((char *)cmd.buf, PSTR("help"))) { 184 !strcasecmp_P((char *)cmd.buf, PSTR("help"))) {
213 printf_P(PSTR("help This help\r\n" 185 puts_P(PSTR("help This help\r\n"
214 "gc Get time of day\r\n" 186 "wa dly time Water for time minutes after dly minutes\r\n"
215 "sc time Set time of day (time is YYYY/MM/DD HH:MM:SS)\r\n" 187 "gc Get time of day\r\n"
216 "in port Read from a port\r\n" 188 "sc time Set time of day (time is YYYY/MM/DD HH:MM:SS)\r\n"
217 "ou port val Write to a port (val in hex)\r\n" 189 "in port Read from a port\r\n"
218 "dd port val Set DDR on port\r\n" 190 "ou port val Write to a port (val in hex)\r\n"
219 "an pin Sample from pin\r\n" 191 "dd port val Set DDR on port\r\n"
220 "sr Search for 1-wire devices\r\n" 192 "an pin Sample from pin\r\n"
221 "te ID Sample temperature from ID\r\n" 193 "sr Search for 1-wire devices\r\n"
222 "re Reset 1-wire bus\r\n" 194 "te ID Sample temperature from ID\r\n"
223 "rb Read bit from 1-wire bus\r\n" 195 "re Reset 1-wire bus\r\n"
224 "rc Read byte from 1-wire bus\r\n" 196 "rb Read bit from 1-wire bus\r\n"
225 "wb bit Write bit to 1-wire bus\r\n" 197 "rc Read byte from 1-wire bus\r\n"
226 "wc byte Write byte to 1-wire bus\r\n" 198 "wb bit Write bit to 1-wire bus\r\n"
227 "zz Reset micro\r\n")); 199 "wc byte Write byte to 1-wire bus\r\n"
200 "zz Reset micro\r\n"));
228 return; 201 return;
202 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("wa"), 2)) {
203 water_cmd((char *)cmd.buf);
229 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("gc"), 2)) { 204 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("gc"), 2)) {
230 ds1307_printtime(PSTR(""), PSTR("\r\n")); 205 ds1307_printnow(PSTR(""), PSTR("\r\n"));
231 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("sc"), 2)) { 206 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("sc"), 2)) {
232 if (cmd.len < 17) { 207 if (cmd.len < 17) {
233 printf_P(PSTR("Invalid TOD\r\n")); 208 printf_P(PSTR("Invalid TOD\r\n"));
234 } else { 209 } else {
235 if (ds1307_settod((char *)cmd.buf + 3) != 1) 210 if (ds1307_settod((char *)cmd.buf + 3) != 1)
271 if (pin < 0 || pin > 7) { 246 if (pin < 0 || pin > 7) {
272 printf_P(PSTR("Unknown pin\r\n")); 247 printf_P(PSTR("Unknown pin\r\n"));
273 return; 248 return;
274 } 249 }
275 250
276 /* Select desired pin, use VCC reference */ 251 /* Select desired pin, use AVREF */
277 ADMUX = _BV(REFS0) | pin; 252 ADMUX = _BV(pin);
278 253
279 /* Enable ADC, start conversion, set divisor to 64 254 /* Enable ADC, start conversion, set divisor to 64
280 * (8e6 / 64 => 125kHz (max is 200kHz) 255 * (8e6 / 64 => 125kHz (max is 200kHz)
281 */ 256 */
282 ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADPS2) | _BV(ADPS1); 257 ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADPS2) | _BV(ADPS1);
283 258