summaryrefslogtreecommitdiff
path: root/ao-tools/lib/cc-usb.c
blob: 1e023c7ebe8f7b37e99eeef7b044d294a5f531dd (plain) (blame)
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
 * Copyright © 2009 Keith Packard <keithp@keithp.com>
 *
 * 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.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <poll.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include "ccdbg-debug.h"
#include "cc-usb.h"


#define CC_NUM_HEX_READ		64
/*
 * AltOS has different buffer sizes for in/out packets
 */
#define CC_IN_BUF		65536
#define CC_OUT_BUF		64
#define DEFAULT_TTY		"/dev/ttyACM0"

struct cc_hex_read {
	uint8_t	*buf;
	int	len;
};

struct cc_usb {
	int			fd;
	uint8_t			in_buf[CC_IN_BUF];
	int			in_pos;
	int			in_count;
	uint8_t			out_buf[CC_OUT_BUF];
	int			out_count;

	struct cc_hex_read	hex_buf[CC_NUM_HEX_READ];
	int			hex_count;
	int			show_input;

	int			remote;
};

#define NOT_HEX	0xff

static uint8_t
cc_hex_nibble(uint8_t c)
{
	if ('0' <= c && c <= '9')
		return c - '0';
	if ('a' <= c && c <= 'f')
		return c - 'a' + 10;
	if ('A' <= c && c <= 'F')
		return c - 'A' + 10;
	return NOT_HEX;
}

/*
 * Take raw input bytes, parse them as hex
 * and write them to the waiting buffer
 */
static void
cc_handle_hex_read(struct cc_usb *cc)
{
	uint8_t	h, l;
	int	hex_pos;

	hex_pos = 0;
	while (hex_pos < cc->hex_count && cc->in_pos < cc->in_count) {
		/*
		 * Skip to next hex character
		 */
		while (cc->in_pos < cc->in_count &&
		       cc_hex_nibble(cc->in_buf[cc->in_pos]) == NOT_HEX)
			cc->in_pos++;
		/*
		 * Make sure we have two characters left
		 */
		if (cc->in_count - cc->in_pos < 2)
			break;
		/*
		 * Parse hex number
		 */
		h = cc_hex_nibble(cc->in_buf[cc->in_pos]);
		l = cc_hex_nibble(cc->in_buf[cc->in_pos+1]);
		if (h == NOT_HEX || l == NOT_HEX) {
			fprintf(stderr, "hex read error\n");
			break;
		}
		cc->in_pos += 2;
		/*
		 * Store hex number
		 */
		*cc->hex_buf[hex_pos].buf++ = (h << 4) | l;
		if (--cc->hex_buf[hex_pos].len <= 0)
			hex_pos++;
	}

	/* Move pending hex reads to the start of the array */
	if (hex_pos) {
		memmove(cc->hex_buf, cc->hex_buf + hex_pos,
			(cc->hex_count - hex_pos) * sizeof (cc->hex_buf[0]));
		cc->hex_count -= hex_pos;
	}
}

static void
cc_usb_dbg(int indent, uint8_t *bytes, int len)
{
	static int	eol = 1;
	int	i;
	uint8_t	c;
	ccdbg_debug(CC_DEBUG_BITBANG, "<<<%d bytes>>>", len);
	while (len--) {
		c = *bytes++;
		if (eol) {
			for (i = 0; i < indent; i++)
				ccdbg_debug(CC_DEBUG_BITBANG, " ");
			eol = 0;
		}
		switch (c) {
		case '\r':
			ccdbg_debug(CC_DEBUG_BITBANG, "\\r");
			break;
		case '\n':
			eol = 1;
			ccdbg_debug(CC_DEBUG_BITBANG, "\\n\n");
			break;
		default:
			if (c < ' ' || c > '~')
				ccdbg_debug(CC_DEBUG_BITBANG, "\\%02x", c);
			else
				ccdbg_debug(CC_DEBUG_BITBANG, "%c", c);
		}
	}
}

int	cc_default_timeout = 5000;

/*
 * Flush pending writes, fill pending reads
 */

static int
_cc_usb_sync(struct cc_usb *cc, int wait_for_input, int write_timeout)
{
	int		ret;
	struct pollfd	fds;
	int		timeout;

	fds.fd = cc->fd;
	for (;;) {
		if (cc->hex_count || cc->out_count)
			timeout = write_timeout;
		else if (wait_for_input && cc->in_pos == cc->in_count)
			timeout = wait_for_input;
		else
			timeout = 0;
		fds.events = 0;
		/* Move remaining bytes to the start of the input buffer */
		if (cc->in_pos) {
			memmove(cc->in_buf, cc->in_buf + cc->in_pos,
				cc->in_count - cc->in_pos);
			cc->in_count -= cc->in_pos;
			cc->in_pos = 0;
		}
		if (cc->in_count < CC_IN_BUF)
			fds.events |= POLLIN;
		if (cc->out_count)
			fds.events |= POLLOUT;
		ret = poll(&fds, 1, timeout);
		if (ret == 0) {
			if (timeout)
				return -1;
			break;
		}
		if (ret < 0) {
			perror("poll");
			return -1;
		}
		if (fds.revents & POLLIN) {
			ret = read(cc->fd, cc->in_buf + cc->in_count,
				   CC_IN_BUF - cc->in_count);
			if (ret > 0) {
				cc_usb_dbg(24, cc->in_buf + cc->in_count, ret);
				cc->in_count += ret;
				if (cc->hex_count)
					cc_handle_hex_read(cc);
				if (cc->show_input && cc->in_count) {
					write(2, cc->in_buf, cc->in_count);
					cc->in_count = 0;
				}
			} else if (ret <= 0) {
				perror("read");
				return -1;
			}
		}
		if (fds.revents & POLLOUT) {
			ret = write(cc->fd, cc->out_buf,
				    cc->out_count);
			if (ret > 0) {
				cc_usb_dbg(0, cc->out_buf, ret);
				memmove(cc->out_buf,
					cc->out_buf + ret,
					cc->out_count - ret);
				cc->out_count -= ret;
			} else if (ret < 0)
				perror("write");
		}
	}
	return 0;
}

void
cc_usb_sync(struct cc_usb *cc)
{
	if (_cc_usb_sync(cc, 0, cc_default_timeout) < 0) {
		fprintf(stderr, "USB link timeout\n");
		exit(1);
	}
}

void
cc_usb_printf(struct cc_usb *cc, char *format, ...)
{
	char	buf[1024], *b;
	va_list	ap;
	int	ret, this_time;

	/* sprintf to a local buffer */
	va_start(ap, format);
	ret = vsnprintf(buf, sizeof(buf), format, ap);
	va_end(ap);
	if (ret > sizeof(buf)) {
		fprintf(stderr, "printf overflow for format %s\n",
			format);
	}

	/* flush local buffer to the wire */
	b = buf;
	while (ret > 0) {
		this_time = ret;
		if (this_time > CC_OUT_BUF - cc->out_count)
			this_time = CC_OUT_BUF - cc->out_count;
		memcpy(cc->out_buf + cc->out_count, b, this_time);
		cc->out_count += this_time;
		ret -= this_time;
		b += this_time;
		while (cc->out_count >= CC_OUT_BUF)
			cc_usb_sync(cc);
	}
}

int
cc_usb_getchar_timeout(struct cc_usb *cc, int timeout)
{
	while (cc->in_pos == cc->in_count) {
		if (_cc_usb_sync(cc, timeout, cc_default_timeout) < 0) {
			fprintf(stderr, "USB link timeout\n");
			exit(1);
		}
	}
	return cc->in_buf[cc->in_pos++];
}

int
cc_usb_getchar(struct cc_usb *cc)
{
	return cc_usb_getchar_timeout(cc, cc_default_timeout);
}

void
cc_usb_getline(struct cc_usb *cc, char *line, int max)
{
	int	c;

	while ((c = cc_usb_getchar(cc)) != '\n') {
		switch (c) {
		case '\r':
			break;
		default:
			if (max > 1) {
				*line++ = c;
				max--;
			}
			break;
		}
	}
	*line++ = '\0';
}

int
cc_usb_send_bytes(struct cc_usb *cc, uint8_t *bytes, int len)
{
	int	this_len;
	int	ret = len;

	while (len) {
		this_len = len;
		if (this_len > 8)
			this_len = 8;
		len -= this_len;
		cc_usb_printf(cc, "P");
		while (this_len--)
			cc_usb_printf (cc, " %02x", (*bytes++) & 0xff);
		cc_usb_printf(cc, "\n");
	}
	return ret;
}

void
cc_queue_read(struct cc_usb *cc, uint8_t *buf, int len)
{
	struct cc_hex_read	*hex_buf;

	/* At the start of a command sequence, flush any pending input */
	if (cc->hex_count == 0) {
		cc_usb_sync(cc);
		cc->in_count = 0;
	}
	while (cc->hex_count >= CC_NUM_HEX_READ)
		cc_usb_sync(cc);
	hex_buf = &cc->hex_buf[cc->hex_count++];
	hex_buf->buf = buf;
	hex_buf->len = len;
}

int
cc_usb_recv_bytes(struct cc_usb *cc, uint8_t *buf, int len)
{
	cc_queue_read(cc, buf, len);
	cc_usb_printf(cc, "G %x\n", len);
	return len;
}

int
cc_usb_write_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
{
	cc_usb_printf(cc, "O %x %x\n", len, addr);
	while (len--)
		cc_usb_printf(cc, "%02x", *bytes++);
	cc_usb_sync(cc);
	return 0;
}

int
cc_usb_read_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
{
	int	i;
	cc_queue_read(cc, bytes, len);
	cc_usb_printf(cc, "I %x %x\n", len, addr);
	cc_usb_sync(cc);
	for (i = 0; i < len; i++) {
		if ((i & 15) == 0) {
			if (i)
				ccdbg_debug(CC_DEBUG_MEMORY, "\n");
			ccdbg_debug(CC_DEBUG_MEMORY, "\t%04x", addr + i);
		}
		ccdbg_debug(CC_DEBUG_MEMORY, " %02x", bytes[i]);
	}
	ccdbg_debug(CC_DEBUG_MEMORY, "\n");
	return 0;
}

int
cc_usb_debug_mode(struct cc_usb *cc)
{
	cc_usb_sync(cc);
	cc_usb_printf(cc, "D\n");
	return 1;
}

int
cc_usb_reset(struct cc_usb *cc)
{
	cc_usb_sync(cc);
	cc_usb_printf(cc, "R\n");
	return 1;
}

void
cc_usb_open_remote(struct cc_usb *cc, int freq, char *call)
{
	if (!cc->remote) {
		fprintf (stderr, "freq %dkHz\n", freq);
		fprintf (stderr, "call %s\n", call);
		cc_usb_printf(cc, "\nc F %d\nc c %s\np\nE 0\n", freq, call);
		do {
			cc->in_count = cc->in_pos = 0;
			_cc_usb_sync(cc, 100, cc_default_timeout);
		} while (cc->in_count > 0);
		cc->remote = 1;
	}
}

void
cc_usb_close_remote(struct cc_usb *cc)
{
	if (cc->remote) {
		cc_usb_printf(cc, "~");
		cc->remote = 0;
	}
}

static struct termios	save_termios;

#include <errno.h>

struct cc_usb *
cc_usb_open(char *tty)
{
	struct cc_usb	*cc;
	struct termios	termios;
	int		i;

	if (!tty)
		tty = DEFAULT_TTY;
	cc = calloc (sizeof (struct cc_usb), 1);
	if (!cc)
		return NULL;
	i = 0;
	for (;;) {
		cc->fd = open(tty, O_RDWR | O_NONBLOCK);
		if (cc->fd >= 0)
			break;
		i++;
		if (errno == EBUSY || errno == EPERM || errno == EACCES) {
			fprintf(stderr, "open failed, pausing");
			perror(tty);
			if (i < 20) {
				sleep(3);
				continue;
			}
		}

		perror(tty);
		free (cc);
		return NULL;
	}
	tcgetattr(cc->fd, &termios);
	save_termios = termios;
	cfmakeraw(&termios);
	cfsetospeed(&termios, B9600);
	cfsetispeed(&termios, B9600);
	tcsetattr(cc->fd, TCSAFLUSH, &termios);
	cc_usb_printf(cc, "\nE 0\nm 0\n");
	do {
		cc->in_count = cc->in_pos = 0;
		_cc_usb_sync(cc, 100, cc_default_timeout);
	} while (cc->in_count > 0);
	return cc;
}

void
cc_usb_close(struct cc_usb *cc)
{
	cc_usb_close_remote(cc);
	cc_usb_sync(cc);
	tcsetattr(cc->fd, TCSAFLUSH, &save_termios);
	close (cc->fd);
	free (cc);
}

int
cc_usb_write(struct cc_usb *cc, void *buf, int c)
{
	uint8_t	*b;
	int this_time;

	b = buf;
	cc->show_input = 1;
	while (c > 0) {
		this_time = c;
		if (this_time > CC_OUT_BUF - cc->out_count)
			this_time = CC_OUT_BUF - cc->out_count;
		memcpy(cc->out_buf + cc->out_count, b, this_time);
		cc->out_count += this_time;
		c -= this_time;
		b += this_time;
		while (cc->out_count >= CC_OUT_BUF) {
			_cc_usb_sync(cc, 0, -1);
		}
	}
	return 1;
}