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];
63 cc_hex_nibble(uint8_t c)
65 if ('0' <= c && c <= '9')
67 if ('a' <= c && c <= 'f')
69 if ('A' <= c && c <= 'F')
75 * Take raw input bytes, parse them as hex
76 * and write them to the waiting buffer
79 cc_handle_hex_read(struct cc_usb *cc)
85 while (hex_pos < cc->hex_count && cc->in_pos < cc->in_count) {
87 * Skip to next hex character
89 while (cc->in_pos < cc->in_count &&
90 cc_hex_nibble(cc->in_buf[cc->in_pos]) == NOT_HEX)
93 * Make sure we have two characters left
95 if (cc->in_count - cc->in_pos < 2)
100 h = cc_hex_nibble(cc->in_buf[cc->in_pos]);
101 l = cc_hex_nibble(cc->in_buf[cc->in_pos+1]);
102 if (h == NOT_HEX || l == NOT_HEX) {
103 fprintf(stderr, "hex read error\n");
110 *cc->hex_buf[hex_pos].buf++ = (h << 4) | l;
111 if (--cc->hex_buf[hex_pos].len <= 0)
115 /* Move pending hex reads to the start of the array */
117 memmove(cc->hex_buf, cc->hex_buf + hex_pos,
118 (cc->hex_count - hex_pos) * sizeof (cc->hex_buf[0]));
119 cc->hex_count -= hex_pos;
124 cc_usb_dbg(int indent, uint8_t *bytes, int len)
132 for (i = 0; i < indent; i++)
133 ccdbg_debug(CC_DEBUG_BITBANG, " ");
138 ccdbg_debug(CC_DEBUG_BITBANG, "^M");
143 ccdbg_debug(CC_DEBUG_BITBANG, "%c", c);
149 * Flush pending writes, fill pending reads
153 _cc_usb_sync(struct cc_usb *cc, int wait_for_input)
161 if (cc->hex_count || cc->out_count)
163 else if (wait_for_input && cc->in_pos == cc->in_count)
164 timeout = wait_for_input;
168 /* Move remaining bytes to the start of the input buffer */
170 memmove(cc->in_buf, cc->in_buf + cc->in_pos,
171 cc->in_count - cc->in_pos);
172 cc->in_count -= cc->in_pos;
175 if (cc->in_count < CC_IN_BUF)
176 fds.events |= POLLIN;
178 fds.events |= POLLOUT;
179 ret = poll(&fds, 1, timeout);
189 if (fds.revents & POLLIN) {
190 ret = read(cc->fd, cc->in_buf + cc->in_count,
191 CC_IN_BUF - cc->in_count);
193 cc_usb_dbg(24, cc->in_buf + cc->in_count, ret);
196 cc_handle_hex_read(cc);
200 if (fds.revents & POLLOUT) {
201 ret = write(cc->fd, cc->out_buf,
204 cc_usb_dbg(0, cc->out_buf, ret);
207 cc->out_count - ret);
208 cc->out_count -= ret;
217 cc_usb_sync(struct cc_usb *cc)
219 if (_cc_usb_sync(cc, 0) < 0) {
220 fprintf(stderr, "USB link timeout\n");
226 cc_usb_printf(struct cc_usb *cc, char *format, ...)
232 /* sprintf to a local buffer */
233 va_start(ap, format);
234 ret = vsnprintf(buf, sizeof(buf), format, ap);
236 if (ret > sizeof(buf)) {
237 fprintf(stderr, "printf overflow for format %s\n",
241 /* flush local buffer to the wire */
245 if (this_time > CC_OUT_BUF - cc->out_count)
246 this_time = CC_OUT_BUF - cc->out_count;
247 memcpy(cc->out_buf + cc->out_count, b, this_time);
248 cc->out_count += this_time;
251 while (cc->out_count >= CC_OUT_BUF)
257 cc_usb_getchar_timeout(struct cc_usb *cc, int timeout)
259 while (cc->in_pos == cc->in_count) {
260 if (_cc_usb_sync(cc, timeout) < 0) {
261 fprintf(stderr, "USB link timeout\n");
265 return cc->in_buf[cc->in_pos++];
269 cc_usb_getchar(struct cc_usb *cc)
271 return cc_usb_getchar_timeout(cc, 5000);
275 cc_usb_getline(struct cc_usb *cc, char *line, int max)
279 while ((c = cc_usb_getchar(cc)) != '\n') {
295 cc_usb_send_bytes(struct cc_usb *cc, uint8_t *bytes, int len)
305 cc_usb_printf(cc, "P");
307 cc_usb_printf (cc, " %02x", (*bytes++) & 0xff);
308 cc_usb_printf(cc, "\n");
314 cc_queue_read(struct cc_usb *cc, uint8_t *buf, int len)
316 struct cc_hex_read *hex_buf;
318 /* At the start of a command sequence, flush any pending input */
319 if (cc->hex_count == 0) {
323 while (cc->hex_count >= CC_NUM_HEX_READ)
325 hex_buf = &cc->hex_buf[cc->hex_count++];
331 cc_usb_recv_bytes(struct cc_usb *cc, uint8_t *buf, int len)
333 cc_queue_read(cc, buf, len);
334 cc_usb_printf(cc, "G %x\n", len);
339 cc_usb_write_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
341 cc_usb_printf(cc, "O %x %x\n", len, addr);
343 cc_usb_printf(cc, "%02x", *bytes++);
349 cc_usb_read_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
352 cc_queue_read(cc, bytes, len);
353 cc_usb_printf(cc, "I %x %x\n", len, addr);
355 for (i = 0; i < len; i++) {
358 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
359 ccdbg_debug(CC_DEBUG_MEMORY, "\t%04x", addr + i);
361 ccdbg_debug(CC_DEBUG_MEMORY, " %02x", bytes[i]);
363 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
368 cc_usb_debug_mode(struct cc_usb *cc)
371 cc_usb_printf(cc, "D\n");
376 cc_usb_reset(struct cc_usb *cc)
379 cc_usb_printf(cc, "R\n");
384 cc_usb_open_remote(struct cc_usb *cc, int freq, char *call)
387 fprintf (stderr, "freq %dkHz\n", freq);
388 fprintf (stderr, "call %s\n", call);
389 cc_usb_printf(cc, "\nc F %d\nc c %s\np\nE 0\n", freq, call);
391 cc->in_count = cc->in_pos = 0;
392 _cc_usb_sync(cc, 100);
393 } while (cc->in_count > 0);
399 cc_usb_close_remote(struct cc_usb *cc)
402 cc_usb_printf(cc, "~");
407 static struct termios save_termios;
410 cc_usb_open(char *tty)
413 struct termios termios;
417 cc = calloc (sizeof (struct cc_usb), 1);
420 cc->fd = open(tty, O_RDWR | O_NONBLOCK);
426 tcgetattr(cc->fd, &termios);
427 save_termios = termios;
429 cfsetospeed(&termios, B9600);
430 cfsetispeed(&termios, B9600);
431 tcsetattr(cc->fd, TCSAFLUSH, &termios);
432 cc_usb_printf(cc, "\nE 0\nm 0\n");
434 cc->in_count = cc->in_pos = 0;
435 _cc_usb_sync(cc, 100);
436 } while (cc->in_count > 0);
441 cc_usb_close(struct cc_usb *cc)
443 cc_usb_close_remote(cc);
445 tcsetattr(cc->fd, TCSAFLUSH, &save_termios);