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_READ 16
35 * AltOS has different buffer sizes for in/out packets
39 #define DEFAULT_TTY "/dev/ttyACM0"
48 uint8_t in_buf[CC_IN_BUF];
50 uint8_t out_buf[CC_OUT_BUF];
52 struct cc_read read_buf[CC_NUM_READ];
59 cc_hex_nibble(uint8_t c)
61 if ('0' <= c && c <= '9')
63 if ('a' <= c && c <= 'f')
65 if ('A' <= c && c <= 'F')
71 * Take raw input bytes, parse them as hex
72 * and write them to the waiting buffer
75 cc_handle_in(struct cc_usb *cc)
83 while (read_pos < cc->read_count && in_pos < cc->in_count) {
85 * Skip to next hex character
87 while (in_pos < cc->in_count &&
88 cc_hex_nibble(cc->in_buf[in_pos]) == NOT_HEX)
91 * Make sure we have two characters left
93 if (cc->in_count - in_pos < 2)
98 h = cc_hex_nibble(cc->in_buf[in_pos]);
99 l = cc_hex_nibble(cc->in_buf[in_pos+1]);
100 if (h == NOT_HEX || l == NOT_HEX) {
101 fprintf(stderr, "hex read error\n");
108 *cc->read_buf[read_pos].buf++ = (h << 4) | l;
109 if (--cc->read_buf[read_pos].len <= 0)
113 /* Move remaining bytes to the start of the input buffer */
115 memmove(cc->in_buf, cc->in_buf + in_pos,
116 cc->in_count - in_pos);
117 cc->in_count -= in_pos;
120 /* Move pending reads to the start of the array */
122 memmove(cc->read_buf, cc->read_buf + read_pos,
123 (cc->read_count - read_pos) * sizeof (cc->read_buf[0]));
124 cc->read_count -= read_pos;
127 /* Once we're done reading, flush any pending input */
128 if (cc->read_count == 0)
133 cc_usb_dbg(int indent, uint8_t *bytes, int len)
141 for (i = 0; i < indent; i++)
142 ccdbg_debug(CC_DEBUG_BITBANG, " ");
147 ccdbg_debug(CC_DEBUG_BITBANG, "^M");
152 ccdbg_debug(CC_DEBUG_BITBANG, "%c", c);
158 * Flush pending writes, fill pending reads
162 cc_usb_sync(struct cc_usb *cc)
170 if (cc->read_count || cc->out_count)
175 if (cc->in_count < CC_IN_BUF)
176 fds.events |= POLLIN;
178 fds.events |= POLLOUT;
179 ret = poll(&fds, 1, timeout);
182 fprintf(stderr, "USB link timeout\n");
191 if (fds.revents & POLLIN) {
192 ret = read(cc->fd, cc->in_buf + cc->in_count,
193 CC_IN_BUF - cc->in_count);
195 cc_usb_dbg(24, cc->in_buf + cc->in_count, ret);
201 if (fds.revents & POLLOUT) {
202 ret = write(cc->fd, cc->out_buf,
205 cc_usb_dbg(0, cc->out_buf, ret);
208 cc->out_count - ret);
209 cc->out_count -= ret;
217 cc_usb_printf(struct cc_usb *cc, char *format, ...)
223 /* sprintf to a local buffer */
224 va_start(ap, format);
225 ret = vsnprintf(buf, sizeof(buf), format, ap);
227 if (ret > sizeof(buf)) {
228 fprintf(stderr, "printf overflow for format %s\n",
232 /* flush local buffer to the wire */
236 if (this_time > CC_OUT_BUF - cc->out_count)
237 this_time = CC_OUT_BUF - cc->out_count;
238 memcpy(cc->out_buf + cc->out_count, b, this_time);
239 cc->out_count += this_time;
242 while (cc->out_count >= CC_OUT_BUF)
248 cc_usb_send_bytes(struct cc_usb *cc, uint8_t *bytes, int len)
258 cc_usb_printf(cc, "P");
260 cc_usb_printf (cc, " %02x", (*bytes++) & 0xff);
261 cc_usb_printf(cc, "\n");
267 cc_queue_read(struct cc_usb *cc, uint8_t *buf, int len)
269 struct cc_read *read_buf;
270 while (cc->read_count >= CC_NUM_READ)
272 read_buf = &cc->read_buf[cc->read_count++];
278 cc_usb_recv_bytes(struct cc_usb *cc, uint8_t *buf, int len)
280 cc_queue_read(cc, buf, len);
281 cc_usb_printf(cc, "G %x\n", len);
286 cc_usb_write_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
288 cc_usb_printf(cc, "O %x %x\n", len, addr);
290 cc_usb_printf(cc, "%02x", *bytes++);
296 cc_usb_read_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
299 cc_queue_read(cc, bytes, len);
300 cc_usb_printf(cc, "I %x %x\n", len, addr);
302 for (i = 0; i < len; i++) {
305 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
306 ccdbg_debug(CC_DEBUG_MEMORY, "\t%04x", addr + i);
308 ccdbg_debug(CC_DEBUG_MEMORY, " %02x", bytes[i]);
310 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
315 cc_usb_debug_mode(struct cc_usb *cc)
318 cc_usb_printf(cc, "D\n");
323 cc_usb_reset(struct cc_usb *cc)
326 cc_usb_printf(cc, "R\n");
330 static struct termios save_termios;
333 cc_usb_open(char *tty)
336 struct termios termios;
340 cc = calloc (sizeof (struct cc_usb), 1);
343 cc->fd = open(tty, O_RDWR | O_NONBLOCK);
349 tcgetattr(cc->fd, &termios);
350 save_termios = termios;
352 tcsetattr(cc->fd, TCSAFLUSH, &termios);
353 cc_usb_printf(cc, "E 0\nm 0\n");
361 cc_usb_close(struct cc_usb *cc)
363 tcsetattr(cc->fd, TCSAFLUSH, &save_termios);