summaryrefslogtreecommitdiffstats
path: root/firmware/main.c
blob: 1eb83282d1034b3e75413b5e91217db13d85048f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
 * Moistcontrol
 *
 * Copyright (c) 2013 Michael Buesch <m@bues.ch>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "main.h"
#include "comm.h"
#include "util.h"
#include "sensor.h"
#include "controller.h"
#include "log.h"
#include "twi_master.h"
#include "pcf8574.h"
#include "rv3029.h"
#include "notify_led.h"
#include "onoffswitch.h"

#include <avr/io.h>
#include <avr/wdt.h>


/* RTC time fetch interval, in milliseconds. */
#define RTC_FETCH_INTERVAL_MS		1000


/* Message IDs of control messages transferred to and from
 * the host over serial wire. */
enum user_message_id {
	MSG_LOG,			/* Log message */
	MSG_LOG_FETCH,			/* Log message request */
	MSG_RTC,			/* RTC time */
	MSG_RTC_FETCH,			/* RTC time request */
	MSG_CONTR_CONF,			/* Global configuration */
	MSG_CONTR_CONF_FETCH,		/* Global configuration request */
	MSG_CONTR_POT_CONF,		/* Pot configuration */
	MSG_CONTR_POT_CONF_FETCH,	/* Pot configuration request */
	MSG_CONTR_POT_STATE,		/* Pot state */
	MSG_CONTR_POT_STATE_FETCH,	/* Pot state request */
	MSG_CONTR_POT_REM_STATE,	/* Pot remanent state */
	MSG_CONTR_POT_REM_STATE_FETCH,	/* Pot remanent state request */
	MSG_MAN_MODE,			/* Manual mode settings */
	MSG_MAN_MODE_FETCH,		/* Manual mode settings request */
};

enum man_mode_flags {
	MANFLG_FREEZE_CHANGE	= 1 << 0, /* Freeze change request */
	MANFLG_FREEZE_ENABLE	= 1 << 1, /* Freeze on/off */
	MANFLG_NOTIFY_CHANGE	= 1 << 2, /* LED-status change request */
	MANFLG_NOTIFY_ENABLE	= 1 << 3, /* LED-status on/off */
};

/* Payload of host communication messages. */
struct msg_payload {
	/* The ID number. (enum user_message_id) */
	uint8_t id;

	union {
		/* Log message. */
		struct {
			struct log_item item;
		} _packed log;

		/* RTC time. */
		struct {
			struct rtc_time time;
		} _packed rtc;

		/* Global controller configuration. */
		struct {
			struct controller_global_config conf;
		} _packed contr_conf;

		/* Controller flower pot configuration. */
		struct {
			uint8_t pot_number;
			struct flowerpot_config conf;
		} _packed contr_pot_conf;

		/* Controller flower pot state. */
		struct {
			uint8_t pot_number;
			struct flowerpot_state state;
		} _packed contr_pot_state;

		/* Controller flower pot remanent state. */
		struct {
			uint8_t pot_number;
			struct flowerpot_remanent_state rem_state;
		} _packed contr_pot_rem_state;

		/* Manual mode settings. */
		struct {
			uint8_t force_stop_watering_mask;
			uint8_t valve_manual_mask;
			uint8_t valve_manual_state;
			uint8_t flags;
		} _packed manual_mode;
	} _packed;
} _packed;
// TODO: add global state message with LED state
// TODO: add global state message with hw switch state


/* The current timekeeping count. */
static jiffies_t jiffies_count;
/* Serial communication timer. */
static jiffies_t comm_timer;
/* Timestamp for the next RTC time fetch. */
static jiffies_t next_rtc_fetch;


/* Host message handler.
 * Handle all received control messages sent by the host.
 */
bool comm_handle_rx_message(const struct comm_message *msg,
			    void *reply_payload)
{
	const struct msg_payload *pl = comm_payload(const struct msg_payload *, msg);
	struct msg_payload *reply = reply_payload;
	bool ok;

	if (msg->fc & COMM_FC_ACK) {
		/* This is just an acknowledge. Ignore. */
		return 1;
	}

	switch (pl->id) {
	case MSG_LOG_FETCH: {
		/* Fetch of the first log item. */

		/* Fill the reply message. */
		reply->id = MSG_LOG;
		/* Get the first item from the log stack. */
		ok = log_pop(&reply->log.item);
		if (!ok) {
			/* No log available.
			 * Signal an error to the host. */
			return 0;
		}
		break;
	}
	case MSG_RTC: {
		/* RTC time adjustment. */

		/* Write the new time to the RTC hardware. */
		rv3029_write_time(&pl->rtc.time);
		break;
	}
	case MSG_RTC_FETCH: {
		/* RTC time fetch. */

		/* Fill the reply message. */
		reply->id = MSG_RTC;
		rv3029_get_time(&reply->rtc.time);
		break;
	}
	case MSG_CONTR_CONF: {
		/* Set controller config. */

		struct controller_config conf;

		/* Get the current configuration. */
		controller_get_config(&conf);

		/* Write the new configuration. */
		conf.global = pl->contr_conf.conf;
		controller_update_config(&conf);
		break;
	}
	case MSG_CONTR_CONF_FETCH: {
		/* Fetch controller config. */

		struct controller_config conf;

		/* Get the current configuration. */
		controller_get_config(&conf);

		/* Fill the reply message. */
		reply->id = MSG_CONTR_CONF;
		reply->contr_conf.conf = conf.global;
		break;
	}
	case MSG_CONTR_POT_CONF: {
		/* Set flower pot config. */

		uint8_t pot_number = pl->contr_pot_conf.pot_number;
		struct controller_config conf;

		if (pot_number >= MAX_NR_FLOWERPOTS) {
			/* Invalid pot number. */
			return 0;
		}

		/* Get the current configuration. */
		controller_get_config(&conf);

		/* Write the new configuration. */
		conf.pots[pot_number] = pl->contr_pot_conf.conf;
		controller_update_config(&conf);
		break;
	}
	case MSG_CONTR_POT_CONF_FETCH: {
		/* Fetch flower pot config. */

		uint8_t pot_number = pl->contr_pot_conf.pot_number;
		struct controller_config conf;

		if (pot_number >= MAX_NR_FLOWERPOTS) {
			/* Invalid pot number. */
			return 0;
		}

		/* Get the current configuration. */
		controller_get_config(&conf);

		/* Fill the reply message. */
		reply->id = MSG_CONTR_POT_CONF;
		reply->contr_pot_conf.pot_number = pot_number;
		reply->contr_pot_conf.conf = conf.pots[pot_number];
		break;
	}
	case MSG_CONTR_POT_STATE_FETCH: {
		/* Fetch flower pot state. */

		uint8_t pot_number = pl->contr_pot_state.pot_number;

		if (pot_number >= MAX_NR_FLOWERPOTS) {
			/* Invalid pot number. */
			return 0;
		}

		/* Fill the reply message. */
		reply->id = MSG_CONTR_POT_STATE;
		reply->contr_pot_state.pot_number = pot_number;
		controller_get_pot_state(pot_number,
					 &reply->contr_pot_state.state,
					 NULL);
		break;
	}
	case MSG_CONTR_POT_REM_STATE: {
		/* Set the flower pot remanent state. */

		uint8_t pot_number = pl->contr_pot_rem_state.pot_number;

		if (pot_number >= MAX_NR_FLOWERPOTS) {
			/* Invalid pot number. */
			return 0;
		}

		/* Write the new rememanent state. */
		controller_update_pot_rem_state(pot_number,
						&pl->contr_pot_rem_state.rem_state);
		break;
	}
	case MSG_CONTR_POT_REM_STATE_FETCH: {
		/* Fetch flower pot remanent state. */

		uint8_t pot_number = pl->contr_pot_rem_state.pot_number;

		if (pot_number >= MAX_NR_FLOWERPOTS) {
			/* Invalid pot number. */
			return 0;
		}

		/* Fill the reply message. */
		reply->id = MSG_CONTR_POT_REM_STATE;
		reply->contr_pot_rem_state.pot_number = pot_number;
		controller_get_pot_state(pot_number,
					 NULL,
					 &reply->contr_pot_rem_state.rem_state);
		break;
	}
	case MSG_MAN_MODE: {
		/* Set controller manual mode state. */

		controller_manual_mode(pl->manual_mode.force_stop_watering_mask,
				       pl->manual_mode.valve_manual_mask,
				       pl->manual_mode.valve_manual_state);

		if (pl->manual_mode.flags & MANFLG_FREEZE_CHANGE)
			controller_freeze(!!(pl->manual_mode.flags & MANFLG_FREEZE_ENABLE));

		if (pl->manual_mode.flags & MANFLG_NOTIFY_CHANGE)
			notify_led_set(!!(pl->manual_mode.flags & MANFLG_NOTIFY_ENABLE));

		break;
	}
	default:
		/* Unsupported message. Return failure. */
		return 0;
	}

	return 1;
}

/* 200 Hz system timer. */
ISR(TIMER1_COMPA_vect)
{
	mb();
	/* Increment the system time counter. */
	jiffies_count++;
	mb();
}

/* Get the current system time counter. */
jiffies_t jiffies_get(void)
{
	uint8_t sreg;
	jiffies_t j;

	/* Fetch system time counter with interrupts disabled. */
	sreg = irq_disable_save();
	j = jiffies_count;
	irq_restore(sreg);

	return j;
}

/* Initialize the system timer. */
static void systimer_init(void)
{
	/* Initialize timer-1 to 200 Hz interrupt frequency. */

	/* Set OC value for 16 Mhz CPU clock. */
	build_assert(F_CPU == 16000000ul);
	OCR1A = 1250;
	TCNT1 = 0;
	TCCR1A = 0;
	/* CTC mode, prescaler 64 */
	TCCR1B = (1 << WGM12) | (1 << CS10) | (1 << CS11);
	/* Enable OC interrupt. */
	TIMSK |= (1 << OCIE1A);
}

/* Handle realtime clock work. */
static void handle_rtc(jiffies_t now)
{
	/* Only, if the RTC fetch timer expired. */
	if (time_before(now, next_rtc_fetch))
		return;
	/* Re-trigger the RTC fetch timer. */
	next_rtc_fetch = now + msec_to_jiffies(RTC_FETCH_INTERVAL_MS);

	/* Read the current time from RTC. */
	rv3029_read_time();
}

/* Handle changes on the hardware on/off-switch state. */
static enum onoff_state handle_onoffswitch(void)
{
	enum onoff_state hw_switch;

	hw_switch = onoffswitch_get_state();

	if (hw_switch == ONOFF_IS_OFF ||
	    hw_switch == ONOFF_SWITCHED_OFF) {
		/* The controller is off.
		 * Enable notification LED permanently. */
		notify_led_set(1);
	}

	if (hw_switch == ONOFF_SWITCHED_OFF) {
		/* Log the 'off'-event. */
		log_info(LOG_INFO_HWONOFF, 0);
	} else if (hw_switch == ONOFF_SWITCHED_ON) {
		/* Log the 'on'-event. */
		log_info(LOG_INFO_HWONOFF, 1);

		/* Disable notification LED.
		 * This will also clear notification messages from other
		 * sources (e.g. controller). */
		notify_led_set(0);
	}

	return hw_switch;
}

/* Program entry point. */
int main(void) _mainfunc;
int main(void)
{
	jiffies_t now;
	enum onoff_state hw_switch;

	irq_disable();

	wdt_enable(WDTO_1S);

	/* Initialize the system. */
	onoffswitch_init();
	notify_led_init();
	twi_init();
	systimer_init();
	rv3029_init();
	sensor_init();
	controller_init();
	comm_init();

	/* Sanity checks. */
	build_assert(sizeof(struct msg_payload) <= COMM_PAYLOAD_LEN);

	/* Enable interrupts and enter the mainloop. */
	irq_enable();
	while (1) {
		/* Poke the watchdog. */
		wdt_reset();

		/* Get the current timestamp. */
		now = jiffies_get();

		/* Handle the state of the hardware on/off-switch. */
		hw_switch = handle_onoffswitch();

		/* Handle serial host communication. */
		comm_work();
		if (!time_before(now, comm_timer)) {
			comm_timer = now + msec_to_jiffies(10);
			comm_centisecond_tick();
		}

		/* Handle realtime clock work. */
		handle_rtc(now);

		/* Run the controller state machine. */
		controller_work(hw_switch);

		/* Handle notification LED state. */
		notify_led_work();
	}
}
bues.ch cgit interface