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(struct cc_usb *cc)
259 while (cc->in_pos == cc->in_count) {
260 if (_cc_usb_sync(cc, 5000) < 0) {
261 fprintf(stderr, "USB link timeout\n");
265 return cc->in_buf[cc->in_pos++];
269 cc_usb_getline(struct cc_usb *cc, char *line, int max)
273 while ((c = cc_usb_getchar(cc)) != '\n') {
289 cc_usb_send_bytes(struct cc_usb *cc, uint8_t *bytes, int len)
299 cc_usb_printf(cc, "P");
301 cc_usb_printf (cc, " %02x", (*bytes++) & 0xff);
302 cc_usb_printf(cc, "\n");
308 cc_queue_read(struct cc_usb *cc, uint8_t *buf, int len)
310 struct cc_hex_read *hex_buf;
312 /* At the start of a command sequence, flush any pending input */
313 if (cc->hex_count == 0) {
317 while (cc->hex_count >= CC_NUM_HEX_READ)
319 hex_buf = &cc->hex_buf[cc->hex_count++];
325 cc_usb_recv_bytes(struct cc_usb *cc, uint8_t *buf, int len)
327 cc_queue_read(cc, buf, len);
328 cc_usb_printf(cc, "G %x\n", len);
333 cc_usb_write_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
335 cc_usb_printf(cc, "O %x %x\n", len, addr);
337 cc_usb_printf(cc, "%02x", *bytes++);
343 cc_usb_read_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
346 cc_queue_read(cc, bytes, len);
347 cc_usb_printf(cc, "I %x %x\n", len, addr);
349 for (i = 0; i < len; i++) {
352 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
353 ccdbg_debug(CC_DEBUG_MEMORY, "\t%04x", addr + i);
355 ccdbg_debug(CC_DEBUG_MEMORY, " %02x", bytes[i]);
357 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
362 cc_usb_debug_mode(struct cc_usb *cc)
365 cc_usb_printf(cc, "D\n");
370 cc_usb_reset(struct cc_usb *cc)
373 cc_usb_printf(cc, "R\n");
378 cc_usb_open_remote(struct cc_usb *cc)
381 cc_usb_printf(cc, "\np\nE 0\n");
383 cc->in_count = cc->in_pos = 0;
384 _cc_usb_sync(cc, 100);
385 } while (cc->in_count > 0);
391 cc_usb_close_remote(struct cc_usb *cc)
394 cc_usb_printf(cc, "~");
399 static struct termios save_termios;
402 cc_usb_open(char *tty)
405 struct termios termios;
409 cc = calloc (sizeof (struct cc_usb), 1);
412 cc->fd = open(tty, O_RDWR | O_NONBLOCK);
418 tcgetattr(cc->fd, &termios);
419 save_termios = termios;
421 tcsetattr(cc->fd, TCSAFLUSH, &termios);
422 cc_usb_printf(cc, "\nE 0\nm 0\n");
424 cc->in_count = cc->in_pos = 0;
425 _cc_usb_sync(cc, 100);
426 } while (cc->in_count > 0);
431 cc_usb_close(struct cc_usb *cc)
433 cc_usb_close_remote(cc);
435 tcsetattr(cc->fd, TCSAFLUSH, &save_termios);