2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #include <sys/types.h>
29 #include "ccdbg-debug.h"
33 #define CC_NUM_HEX_READ 64
35 * AltOS has different buffer sizes for in/out packets
37 #define CC_IN_BUF 65536
39 #define DEFAULT_TTY "/dev/ttyACM0"
48 uint8_t in_buf[CC_IN_BUF];
51 uint8_t out_buf[CC_OUT_BUF];
54 struct cc_hex_read hex_buf[CC_NUM_HEX_READ];
64 cc_hex_nibble(uint8_t c)
66 if ('0' <= c && c <= '9')
68 if ('a' <= c && c <= 'f')
70 if ('A' <= c && c <= 'F')
76 * Take raw input bytes, parse them as hex
77 * and write them to the waiting buffer
80 cc_handle_hex_read(struct cc_usb *cc)
86 while (hex_pos < cc->hex_count && cc->in_pos < cc->in_count) {
88 * Skip to next hex character
90 while (cc->in_pos < cc->in_count &&
91 cc_hex_nibble(cc->in_buf[cc->in_pos]) == NOT_HEX)
94 * Make sure we have two characters left
96 if (cc->in_count - cc->in_pos < 2)
101 h = cc_hex_nibble(cc->in_buf[cc->in_pos]);
102 l = cc_hex_nibble(cc->in_buf[cc->in_pos+1]);
103 if (h == NOT_HEX || l == NOT_HEX) {
104 fprintf(stderr, "hex read error\n");
111 *cc->hex_buf[hex_pos].buf++ = (h << 4) | l;
112 if (--cc->hex_buf[hex_pos].len <= 0)
116 /* Move pending hex reads to the start of the array */
118 memmove(cc->hex_buf, cc->hex_buf + hex_pos,
119 (cc->hex_count - hex_pos) * sizeof (cc->hex_buf[0]));
120 cc->hex_count -= hex_pos;
125 cc_usb_dbg(int indent, uint8_t *bytes, int len)
130 ccdbg_debug(CC_DEBUG_BITBANG, "<<<%d bytes>>>", len);
134 for (i = 0; i < indent; i++)
135 ccdbg_debug(CC_DEBUG_BITBANG, " ");
140 ccdbg_debug(CC_DEBUG_BITBANG, "\\r");
144 ccdbg_debug(CC_DEBUG_BITBANG, "\\n\n");
147 if (c < ' ' || c > '~')
148 ccdbg_debug(CC_DEBUG_BITBANG, "\\%02x", c);
150 ccdbg_debug(CC_DEBUG_BITBANG, "%c", c);
156 * Flush pending writes, fill pending reads
160 _cc_usb_sync(struct cc_usb *cc, int wait_for_input, int write_timeout)
168 if (cc->hex_count || cc->out_count)
169 timeout = write_timeout;
170 else if (wait_for_input && cc->in_pos == cc->in_count)
171 timeout = wait_for_input;
175 /* Move remaining bytes to the start of the input buffer */
177 memmove(cc->in_buf, cc->in_buf + cc->in_pos,
178 cc->in_count - cc->in_pos);
179 cc->in_count -= cc->in_pos;
182 if (cc->in_count < CC_IN_BUF)
183 fds.events |= POLLIN;
185 fds.events |= POLLOUT;
186 ret = poll(&fds, 1, timeout);
196 if (fds.revents & POLLIN) {
197 ret = read(cc->fd, cc->in_buf + cc->in_count,
198 CC_IN_BUF - cc->in_count);
200 cc_usb_dbg(24, cc->in_buf + cc->in_count, ret);
203 cc_handle_hex_read(cc);
204 if (cc->show_input && cc->in_count) {
205 write(2, cc->in_buf, cc->in_count);
211 if (fds.revents & POLLOUT) {
212 ret = write(cc->fd, cc->out_buf,
215 cc_usb_dbg(0, cc->out_buf, ret);
218 cc->out_count - ret);
219 cc->out_count -= ret;
228 cc_usb_sync(struct cc_usb *cc)
230 if (_cc_usb_sync(cc, 0, 5000) < 0) {
231 fprintf(stderr, "USB link timeout\n");
237 cc_usb_printf(struct cc_usb *cc, char *format, ...)
243 /* sprintf to a local buffer */
244 va_start(ap, format);
245 ret = vsnprintf(buf, sizeof(buf), format, ap);
247 if (ret > sizeof(buf)) {
248 fprintf(stderr, "printf overflow for format %s\n",
252 /* flush local buffer to the wire */
256 if (this_time > CC_OUT_BUF - cc->out_count)
257 this_time = CC_OUT_BUF - cc->out_count;
258 memcpy(cc->out_buf + cc->out_count, b, this_time);
259 cc->out_count += this_time;
262 while (cc->out_count >= CC_OUT_BUF)
268 cc_usb_getchar_timeout(struct cc_usb *cc, int timeout)
270 while (cc->in_pos == cc->in_count) {
271 if (_cc_usb_sync(cc, timeout, 5000) < 0) {
272 fprintf(stderr, "USB link timeout\n");
276 return cc->in_buf[cc->in_pos++];
280 cc_usb_getchar(struct cc_usb *cc)
282 return cc_usb_getchar_timeout(cc, 5000);
286 cc_usb_getline(struct cc_usb *cc, char *line, int max)
290 while ((c = cc_usb_getchar(cc)) != '\n') {
306 cc_usb_send_bytes(struct cc_usb *cc, uint8_t *bytes, int len)
316 cc_usb_printf(cc, "P");
318 cc_usb_printf (cc, " %02x", (*bytes++) & 0xff);
319 cc_usb_printf(cc, "\n");
325 cc_queue_read(struct cc_usb *cc, uint8_t *buf, int len)
327 struct cc_hex_read *hex_buf;
329 /* At the start of a command sequence, flush any pending input */
330 if (cc->hex_count == 0) {
334 while (cc->hex_count >= CC_NUM_HEX_READ)
336 hex_buf = &cc->hex_buf[cc->hex_count++];
342 cc_usb_recv_bytes(struct cc_usb *cc, uint8_t *buf, int len)
344 cc_queue_read(cc, buf, len);
345 cc_usb_printf(cc, "G %x\n", len);
350 cc_usb_write_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
352 cc_usb_printf(cc, "O %x %x\n", len, addr);
354 cc_usb_printf(cc, "%02x", *bytes++);
360 cc_usb_read_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
363 cc_queue_read(cc, bytes, len);
364 cc_usb_printf(cc, "I %x %x\n", len, addr);
366 for (i = 0; i < len; i++) {
369 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
370 ccdbg_debug(CC_DEBUG_MEMORY, "\t%04x", addr + i);
372 ccdbg_debug(CC_DEBUG_MEMORY, " %02x", bytes[i]);
374 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
379 cc_usb_debug_mode(struct cc_usb *cc)
382 cc_usb_printf(cc, "D\n");
387 cc_usb_reset(struct cc_usb *cc)
390 cc_usb_printf(cc, "R\n");
395 cc_usb_open_remote(struct cc_usb *cc, int freq, char *call)
398 fprintf (stderr, "freq %dkHz\n", freq);
399 fprintf (stderr, "call %s\n", call);
400 cc_usb_printf(cc, "\nc F %d\nc c %s\np\nE 0\n", freq, call);
402 cc->in_count = cc->in_pos = 0;
403 _cc_usb_sync(cc, 100, 5000);
404 } while (cc->in_count > 0);
410 cc_usb_close_remote(struct cc_usb *cc)
413 cc_usb_printf(cc, "~");
418 static struct termios save_termios;
423 cc_usb_open(char *tty)
426 struct termios termios;
431 cc = calloc (sizeof (struct cc_usb), 1);
436 cc->fd = open(tty, O_RDWR | O_NONBLOCK);
440 if (errno == EBUSY || errno == EPERM || errno == EACCES) {
441 fprintf(stderr, "open failed, pausing");
453 tcgetattr(cc->fd, &termios);
454 save_termios = termios;
456 cfsetospeed(&termios, B9600);
457 cfsetispeed(&termios, B9600);
458 tcsetattr(cc->fd, TCSAFLUSH, &termios);
459 cc_usb_printf(cc, "\nE 0\nm 0\n");
461 cc->in_count = cc->in_pos = 0;
462 _cc_usb_sync(cc, 100, 5000);
463 } while (cc->in_count > 0);
468 cc_usb_close(struct cc_usb *cc)
470 cc_usb_close_remote(cc);
472 tcsetattr(cc->fd, TCSAFLUSH, &save_termios);
478 cc_usb_write(struct cc_usb *cc, void *buf, int c)
487 if (this_time > CC_OUT_BUF - cc->out_count)
488 this_time = CC_OUT_BUF - cc->out_count;
489 memcpy(cc->out_buf + cc->out_count, b, this_time);
490 cc->out_count += this_time;
493 while (cc->out_count >= CC_OUT_BUF) {
494 _cc_usb_sync(cc, 0, -1);