aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/coproc-firmware/main.c
blob: 83c45162855742406405bf6c7c315c0b353e10e1 (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
/*
 *   CNC-remote-control
 *   Button processor
 *
 *   Copyright (C) 2009-2016 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
 *   version 2 as published by the Free Software Foundation.
 *
 *   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.
 */

#include "util.h"
#include "spi_interface.h"

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

#include <stdint.h>


typedef uint16_t jiffies_t;


#define BUTTON_DEBOUNCE		msec2jiffies(40)
#define ENC_DEBOUNCE		usec2jiffies(3500)


/* Hardware state of a button */
struct button_hwstate {
	bool state;			/* 1 = pressed, 0 = released */
	bool synchronized;		/* Is synchronized with software state? */
	jiffies_t sync_deadline;	/* Deadline for sync */
};

/* Hardware state of a torque encoder */
struct encoder_hwstate {
	uint8_t gray;			/* The graycode state */
	uint8_t prev_gray;
	bool synchronized;		/* Is synchronized with software state? */
	jiffies_t sync_deadline;	/* Deadline for sync */
};

/* Software state of a torque encoder */
struct encoder_swstate {
	int8_t state;
};

static struct button_hwstate hwstates[14];
static uint16_t swstates;
static struct encoder_hwstate enc_hwstates[1];
static struct encoder_swstate enc_swstates[1];


/* Convert 2bit graycode to binary */
static inline uint8_t gray2bin_2bit(uint8_t graycode)
{
	if (graycode & 2)
		graycode ^= 1;
	return graycode;
}

static void jiffies_init(void)
{
#define JPS			31250 /* jiffies per second */

	/* Initialize the timer to 8M/256=31250 */
	TCNT1 = 0;
	OCR1A = 0;
	TIMSK = 0;
	TCCR1A = 0;
	TCCR1B = (0 << CS10) | (0 << CS11) | (1 << CS12);
}

#define msec2jiffies(ms)	((jiffies_t)((uint32_t)(ms) * JPS / (uint32_t)1000))
#define usec2jiffies(us)	((jiffies_t)((uint32_t)(us) * JPS / (uint32_t)1000000))

#define time_after(a, b)	((int16_t)(b) - (int16_t)(a) < 0)
#define time_before(a, b)	time_after(b, a)

static inline jiffies_t jiffies_get(void)
{
	return TCNT1;
}

static inline void do_button_read(struct button_hwstate *hw,
				  bool state,
				  jiffies_t timestamp)
{
	if (state != hw->state) {
		hw->state = state;
		hw->synchronized = 0;
		hw->sync_deadline = timestamp + BUTTON_DEBOUNCE;
	}
}

static inline void do_encoder_read(struct encoder_hwstate *hw,
				   bool a, bool b,
				   jiffies_t timestamp)
{
	uint8_t gray;

	gray = (uint8_t)((uint8_t)a | ((uint8_t)b << 1u));

	if (gray != hw->gray) {
		hw->gray = gray;
		hw->synchronized = 0;
		hw->sync_deadline = timestamp + ENC_DEBOUNCE;
	}
}

/* Read the hardware states of the buttons */
static void buttons_read(void)
{
	uint8_t b, c, d;
	jiffies_t now;

	b = PINB;
	c = PINC;
	d = PIND;
	now = jiffies_get();

	/* Interpret the buttons */
	do_button_read(&hwstates[0], !(b & (1 << 0)), now);
	do_button_read(&hwstates[1], !(b & (1 << 1)), now);
	do_button_read(&hwstates[2], !(c & (1 << 0)), now);
	do_button_read(&hwstates[3], !(c & (1 << 1)), now);
	do_button_read(&hwstates[4], !(c & (1 << 2)), now);
	do_button_read(&hwstates[5], !(c & (1 << 3)), now);
	do_button_read(&hwstates[6], !(c & (1 << 4)), now);
	do_button_read(&hwstates[7], !(c & (1 << 5)), now);
	do_button_read(&hwstates[8], !(d & (1 << 0)), now);
	do_button_read(&hwstates[9], !(d & (1 << 1)), now);
	do_button_read(&hwstates[10], !(d & (1 << 2)), now);
	do_button_read(&hwstates[11], !(d & (1 << 3)), now);
	do_button_read(&hwstates[12], !(d & (1 << 4)), now);
	do_button_read(&hwstates[13], !(d & (1 << 5)), now);
	BUILD_BUG_ON(ARRAY_SIZE(hwstates) != 14);
	BUILD_BUG_ON(ARRAY_SIZE(hwstates) > sizeof(swstates) * 8);

	/* Interpret the torque encoders */
	do_encoder_read(&enc_hwstates[0], !(d & (1 << 6)), !(d & (1 << 7)), now);
	BUILD_BUG_ON(ARRAY_SIZE(enc_hwstates) != 1);
	BUILD_BUG_ON(ARRAY_SIZE(enc_hwstates) != ARRAY_SIZE(enc_swstates));
}

static void buttons_init(void)
{
	uint8_t i;

	/* Configure inputs and pullups */
	DDRB = (uint8_t)(DDRB & ~0x03u);
	PORTB = (uint8_t)(PORTB | 0x03u);

	DDRC = (uint8_t)(DDRC & ~0x3Fu);
	PORTC = (uint8_t)(PORTC | 0x3Fu);

	DDRD = (uint8_t)(DDRD & ~0xFFu);
	PORTD = (uint8_t)(PORTD | 0xFFu);

	buttons_read();
	for (i = 0; i < ARRAY_SIZE(enc_hwstates); i++)
		enc_hwstates[i].prev_gray = enc_hwstates[i].gray;
}

static void trigger_trans_interrupt(void)
{
	SPI_SLAVE_TRANSIRQ_PORT = (uint8_t)(SPI_SLAVE_TRANSIRQ_PORT &
					    ~(1u << SPI_SLAVE_TRANSIRQ_BIT));
	nop();
	nop();
	SPI_SLAVE_TRANSIRQ_PORT = (uint8_t)(SPI_SLAVE_TRANSIRQ_PORT |
					    (1u << SPI_SLAVE_TRANSIRQ_BIT));
}

static inline uint8_t do_sync_button(struct button_hwstate *hw,
				     uint8_t swstate_bit,
				     jiffies_t now)
{
	bool state;

	if (!hw->synchronized) {
		if (time_after(now, hw->sync_deadline)) {
			state = hw->state;
			irq_disable();
			if (state)
				swstates |= (1u << swstate_bit);
			else
				swstates &= ~(1u << swstate_bit);
			irq_enable();
			hw->synchronized = 1;

			return 1;
		}
	}

	return 0;
}

static inline uint8_t do_sync_encoder(struct encoder_hwstate *hw,
				      struct encoder_swstate *sw,
				      jiffies_t now)
{
	uint8_t cur, prev;

	if (!hw->synchronized) {
		if (time_after(now, hw->sync_deadline)) {
			cur = gray2bin_2bit(hw->gray);
			prev = gray2bin_2bit(hw->prev_gray);
			hw->prev_gray = hw->gray;
			hw->synchronized = 1;
			if (cur == ((prev + 1) & 3)) {
				irq_disable();
				sw->state--;
				irq_enable();

				return 1;
			}
			if (cur == ((prev - 1) & 3)) {
				irq_disable();
				sw->state++;
				irq_enable();

				return 1;
			}
		}
	}

	return 0;
}

/* Synchronize the software state of the buttons */
static void buttons_synchronize(void)
{
	uint8_t i, one_state_changed = 0;
	jiffies_t now;

	now = jiffies_get();

	/* Sync buttons */
	for (i = 0; i < ARRAY_SIZE(hwstates); i++) {
		one_state_changed |= do_sync_button(&hwstates[i], i,
						    now);
	}

	/* Sync encoders */
	for (i = 0; i < ARRAY_SIZE(enc_hwstates); i++) {
		one_state_changed |= do_sync_encoder(&enc_hwstates[i],
						     &enc_swstates[i],
						     now);
	}

	if (one_state_changed)
		trigger_trans_interrupt();
}

static noreturn void enter_bootloader(void)
{
	irq_disable();
	wdt_reset();

	/* Jump to bootloader code */
	__asm__ __volatile__(
	"ijmp\n"
	: /* None */
	: [_Z]		"z" (BOOT_OFFSET / 2)
	);
	unreachable();
}

ISR(SPI_STC_vect)
{
	uint8_t data;
	static uint8_t checksum;
	static bool enterboot_first_stage_done;

	data = SPDR;

	switch (data) {
	case SPI_CONTROL_ENTERBOOT:
		data = SPI_RESULT_OK;
		checksum = 0;
		enterboot_first_stage_done = 1;
		goto out;
	case SPI_CONTROL_ENTERBOOT2:
		if (enterboot_first_stage_done)
			enter_bootloader();
		data = SPI_RESULT_FAIL;
		checksum = 0;
		goto out;
	default:
		enterboot_first_stage_done = 0;
	}

	switch (data) {
	case SPI_CONTROL_GETLOW:
		data = swstates & 0xFF;
		checksum ^= data;
		break;
	case SPI_CONTROL_GETHIGH:
		data = (uint8_t)((swstates >> 8) & 0xFFu);
		checksum ^= data;
		break;
	case SPI_CONTROL_GETENC:
		data = (uint8_t)(enc_swstates[0].state);
		enc_swstates[0].state = 0;
		checksum ^= data;
		break;
	case SPI_CONTROL_GETSUM:
		data = checksum ^ 0xFF;
		checksum = 0;
		break;
	case SPI_CONTROL_TESTAPP:
		data = SPI_RESULT_OK;
		checksum = 0;
		break;
	case SPI_CONTROL_ENTERAPP:
	case SPI_CONTROL_NOP:
	default:
		data = 0;
		checksum = 0;
	}

out:
	SPDR = data;
}

static void spi_init(void)
{
	/* SPI slave mode 0 with IRQ enabled. */
	DDRB = (uint8_t)(DDRB | (1u << 4/*MISO*/));
	DDRB = (uint8_t)(DDRB & ~((1u << 5/*SCK*/) | (1u << 3/*MOSI*/) |
				  (1u << 2/*SS*/)));
	SPI_SLAVE_TRANSIRQ_PORT = (uint8_t)(SPI_SLAVE_TRANSIRQ_PORT |
					    (1u << SPI_SLAVE_TRANSIRQ_BIT));
	SPI_SLAVE_TRANSIRQ_DDR = (uint8_t)(SPI_SLAVE_TRANSIRQ_DDR | 
					   (1u << SPI_SLAVE_TRANSIRQ_BIT));

	SPCR = (1u << SPE) | (1u << SPIE) | (0u << CPOL) | (0u << CPHA);
	(void)SPSR; /* clear state */
	(void)SPDR; /* clear state */
}

int main(void) _mainfunc;
int main(void)
{
	irq_disable();
	wdt_enable(WDTO_500MS);

	jiffies_init();
	buttons_init();
	spi_init();

	irq_enable();
	while (1) {
		buttons_read();
		buttons_synchronize();
		wdt_reset();
	}
}
bues.ch cgit interface