summaryrefslogtreecommitdiffstats
path: root/pressure_control/firmware/remote.c
blob: cb646a7e58bf5d27902f1b544d6757beff8f20fa (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
/*
 *  Pneumatic pressure controller.
 *  Remote control.
 *
 *  Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>
 *
 *  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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "remote.h"
#include "util.h"
#include "calibration.h"
#include "main.h"
#include "valves.h"

#include <string.h>

#include <avr/io.h>


#define BAUDRATE	38400 /* Error = 0.2% */
#define USE_2X		0


static struct remote_message rx_msg;
static uint8_t rx_msg_count;
static bool rx_msg_valid;
static bool rx_softirq;
static uint16_t rx_timeout;


static inline void usart_tx(uint8_t data)
{
	while (!(UCSRA & (1 << UDRE)))
		;
	UDR = data;
}

static void usart_tx_buf(const void *_buf, uint8_t size)
{
	const uint8_t *buf = _buf;

	while (size) {
		usart_tx(*buf);
		buf++;
		size--;
	}
}

#define ERXFE		1 /* USART RX frame error */
#define ERXPE		2 /* USART RX parity error */
#define ERXOV		3 /* USART RX hardware buffer overflow */
#define ENODATA		4 /* No data available */

static inline int8_t usart_rx(uint8_t *data)
{
	uint8_t status;
	int8_t err = 0;

	status = UCSRA;
	if (!(status & (1 << RXC)))
		return -ENODATA;
	if (unlikely(status & ((1 << FE) | (1 << PE) | (1 << DOR)))) {
		if (status & (1 << FE))
			err = -ERXFE;
		if (status & (1 << PE))
			err = -ERXPE;
		if (status & (1 << DOR))
			err = -ERXOV;
	}
	*data = UDR;

	return err;
}

static void send_message(struct remote_message *msg)
{
	/* Calculate the CRC. */
	msg->crc = crc16_block_update(0xFFFF, msg,
				      sizeof(*msg) - sizeof(msg->crc));
	msg->crc ^= 0xFFFF;
	/* And transmit the bits. */
	usart_tx_buf(msg, sizeof(*msg));
}

static void handle_received_message(void)
{
	struct remote_message reply;
	uint16_t calc_crc;
	uint8_t err = MSG_ERR_NONE;

	calc_crc = crc16_block_update(0xFFFF, &rx_msg,
				      sizeof(rx_msg) - sizeof(rx_msg.crc));
	calc_crc ^= 0xFFFF;
	if (calc_crc != rx_msg.crc) {
		/* CRC mismatch. */
		err = MSG_ERR_CHKSUM;
		goto out;
	}

	memset(&reply, 0, sizeof(reply));

	switch (rx_msg.id) {
	case MSG_PING:
		reply.id = MSG_PONG;
		send_message(&reply);
		break;
	case MSG_GET_CURRENT_PRESSURE: {
		struct pressure_state state;

		get_pressure_state(&state);
		reply.id = MSG_CURRENT_PRESSURE;
		reply.pressure.mbar = state.mbar;
		send_message(&reply);
		break;
	}
	case MSG_GET_DESIRED_PRESSURE: {
		struct pressure_config conf;

		get_pressure_config(&conf);
		reply.id = MSG_DESIRED_PRESSURE;
		reply.pressure.mbar = conf.desired;
		send_message(&reply);
		break;
	}
	case MSG_GET_HYSTERESIS: {
		struct pressure_config conf;

		get_pressure_config(&conf);
		reply.id = MSG_HYSTERESIS;
		reply.pressure.mbar = conf.hysteresis;
		send_message(&reply);
		break;
	}
	case MSG_GET_CONFIG_FLAGS: {
		struct pressure_config conf;

		get_pressure_config(&conf);
		reply.id = MSG_CONFIG_FLAGS;
		if (conf.autoadjust_enable)
			reply.config.flags |= (1 << CFG_FLAG_AUTOADJUST_ENABLE);
		send_message(&reply);
		break;
	}
	case MSG_SET_DESIRED_PRESSURE: {
		struct pressure_config conf;

		cli();
		get_pressure_config(&conf);
		conf.desired = rx_msg.pressure.mbar;
		set_pressure_config(&conf);
		sei();
		break;
	}
	case MSG_SET_HYSTERESIS: {
		struct pressure_config conf;

		cli();
		get_pressure_config(&conf);
		conf.hysteresis = rx_msg.pressure.mbar;
		set_pressure_config(&conf);
		sei();
		break;
	}
	case MSG_SET_CONFIG_FLAGS: {
		struct pressure_config conf;
		bool flag;

		cli();
		get_pressure_config(&conf);
		flag = !!(rx_msg.config.flags & (1 << CFG_FLAG_AUTOADJUST_ENABLE));
		if (conf.autoadjust_enable != flag) {
			conf.autoadjust_enable = flag;
			/* Make sure the values are idle. */
			valves_global_switch(VALVES_IDLE);
		}
		set_pressure_config(&conf);
		sei();
		break;
	}
	case MSG_SET_VALVE: {
		struct pressure_config conf;

		get_pressure_config(&conf);
		if (conf.autoadjust_enable) {
			err = MSG_ERR_BUSY;
			break;
		}
		if (rx_msg.valve.nr == 0) {
			valve0_switch(rx_msg.valve.state == 0 ?
				      VALVE_STATE_12 : VALVE_STATE_14);
			valve_wait_toggle();
			valve0_switch(VALVE_STATE_IDLE);
		} else if (rx_msg.valve.nr == 1) {
			valve1_switch(rx_msg.valve.state == 0 ?
				      VALVE_STATE_12 : VALVE_STATE_14);
			valve_wait_toggle();
			valve0_switch(VALVE_STATE_IDLE);
		} else
			err = MSG_ERR_INVAL;
		break;
	}
	case MSG_INVALID:
		break;
	default:
		err = MSG_ERR_NOCMD;
		break;
	}

out:
	if (rx_msg.flags & (1 << MSG_FLAG_REQ_ERRCODE)) {
		memset(&reply, 0, sizeof(reply));
		reply.id = MSG_ERROR;
		reply.error.code = err;
		send_message(&reply);
	}
}

static void usart_handle_rx_irq(void)
{
	uint8_t *rxbuf = (uint8_t *)&rx_msg;
	int8_t err;
	uint8_t data;

	while (1) {
		err = usart_rx(&data);
		if (err == -ENODATA)
			break;
		/* Ignore other errors. The CRC check will detect them later. */

		rxbuf[rx_msg_count++] = data;
		if (rx_msg_count == sizeof(struct remote_message)) {
			rx_msg_count = 0;
			rx_timeout = 0;
			mb();
			rx_msg_valid = 1;
			break;
		}
	}
}

/* RX interrupt */
ISR(USART_RXC_vect)
{
	if (rx_msg_valid) {
		/* We're busy. Schedule a software IRQ for later. */
		rx_softirq = 1;
		return;
	}
	usart_handle_rx_irq();
}

/* Called with IRQs disabled. */
static void usart_rx_timeout_check()
{
	if (rx_msg_count > 0)
		rx_timeout++;
	if (rx_timeout > 100 /* milliseconds */) {
		/* Timeout! Reset the RX buffer. */
		rx_msg_count = 0;
		rx_timeout = 0;
	}
	mb();
}

void print_sram(const char *str)
{
	struct remote_message msg;
	uint8_t c, i;

	do {
		memset(&msg, 0, sizeof(msg));
		msg.id = MSG_LOGMESSAGE;

		for (i = 0; i < sizeof(msg.logmessage.str); i++) {
			c = *str;
			if (c == '\0')
				break;
			str++;
			msg.logmessage.str[i] = c;
		}

		send_message(&msg);
	} while (c != '\0');
}

void print_pgm(const prog_char *str)
{
	struct remote_message msg;
	uint8_t c, i;

	do {
		memset(&msg, 0, sizeof(msg));
		msg.id = MSG_LOGMESSAGE;

		for (i = 0; i < sizeof(msg.logmessage.str); i++) {
			c = pgm_read_byte(str);
			if (c == '\0')
				break;
			str++;
			msg.logmessage.str[i] = c;
		}

		send_message(&msg);
	} while (c != '\0');
}

static void __print_dec(char prefix, uint16_t number)
{
	uint8_t num[NUM16_NR_DIGITS + sizeof(prefix) + 1];

	num[0] = prefix;
	num16_to_ascii(num + 1, number);
	if (prefix)
		print_sram((char *)num);
	else
		print_sram((char *)(num + 1));
}

void print_dec(uint16_t number)
{
	__print_dec(0, number);
}

void print_dec_signed(int16_t number)
{
	if (number < 0)
		__print_dec('-', -number);
	else
		__print_dec(0, number);
}

void print_hex(uint8_t number)
{
	char hex[3];

	hex[0] = hexdigit_to_ascii(number >> 4);
	hex[1] = hexdigit_to_ascii(number & 0xF);
	hex[2] = '\0';

	print_sram(hex);
}

/* Maintanance work. Called with IRQs enabled. */
void remote_work(void)
{
	if (rx_msg_valid) {
		handle_received_message();
		mb();
		rx_msg_valid = 0;
	}
	cli();
	if (rx_softirq) {
		rx_softirq = 0;
		usart_handle_rx_irq();
	}
	sei();
}

/* Maintanance work. Called at a frequency of 1KHz with IRQs disabled. */
void remote_1khz_work(void)
{
	usart_rx_timeout_check();
}

void remote_pressure_change_notification(uint16_t mbar)
{
	struct remote_message msg;

	memset(&msg, 0, sizeof(msg));
	msg.id = MSG_CURRENT_PRESSURE;
	msg.pressure.mbar = mbar;
	send_message(&msg);
}

#if USE_2X
# define UBRR_FACTOR	2
#else
# define UBRR_FACTOR	1
#endif

static void usart_init(void)
{
	uint8_t dummy;

	/* Set baud rate */
	UBRRL = lo8((CPU_HZ / 16 / BAUDRATE) * UBRR_FACTOR);
	UBRRH = hi8((CPU_HZ / 16 / BAUDRATE) * UBRR_FACTOR) & ~(1 << URSEL);
#if USE_2X
	UCSRA = (1 << U2X);
#endif
	/* 8 Data bits, 1 Stop bit, No parity */
	UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
	/* Enable transceiver and RX IRQs */
	UCSRB = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE);
	/* Drain the RX buffer */
	while (usart_rx(&dummy) != -ENODATA)
		mb();
}

void remote_init(void)
{
	/* The remote tool depends on the exact size (and layout). */
	BUILD_BUG_ON(sizeof(struct remote_message) != 12);

	usart_init();
}
bues.ch cgit interface