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
161 cc_usb_sync(struct cc_usb *cc)
169 if (cc->read_count || cc->out_count)
174 if (cc->in_count < CC_IN_BUF)
175 fds.events |= POLLIN;
177 fds.events |= POLLOUT;
178 ret = poll(&fds, 1, timeout);
185 if (fds.revents & POLLIN) {
186 ret = read(cc->fd, cc->in_buf + cc->in_count,
187 CC_IN_BUF - cc->in_count);
189 cc_usb_dbg(24, cc->in_buf + cc->in_count, ret);
195 if (fds.revents & POLLOUT) {
196 ret = write(cc->fd, cc->out_buf,
199 cc_usb_dbg(0, cc->out_buf, ret);
202 cc->out_count - ret);
203 cc->out_count -= ret;
211 cc_usb_printf(struct cc_usb *cc, char *format, ...)
217 /* sprintf to a local buffer */
218 va_start(ap, format);
219 ret = vsnprintf(buf, sizeof(buf), format, ap);
221 if (ret > sizeof(buf)) {
222 fprintf(stderr, "printf overflow for format %s\n",
226 /* flush local buffer to the wire */
230 if (this_time > CC_OUT_BUF - cc->out_count)
231 this_time = CC_OUT_BUF - cc->out_count;
232 memcpy(cc->out_buf + cc->out_count, b, this_time);
233 cc->out_count += this_time;
236 while (cc->out_count >= CC_OUT_BUF)
242 cc_usb_send_bytes(struct cc_usb *cc, uint8_t *bytes, int len)
252 cc_usb_printf(cc, "P");
254 cc_usb_printf (cc, " %02x", (*bytes++) & 0xff);
255 cc_usb_printf(cc, "\n");
261 cc_queue_read(struct cc_usb *cc, uint8_t *buf, int len)
263 struct cc_read *read_buf;
264 while (cc->read_count >= CC_NUM_READ)
266 read_buf = &cc->read_buf[cc->read_count++];
272 cc_usb_recv_bytes(struct cc_usb *cc, uint8_t *buf, int len)
274 cc_queue_read(cc, buf, len);
275 cc_usb_printf(cc, "G %x\n", len);
280 cc_usb_write_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
282 cc_usb_printf(cc, "O %x %x\n", len, addr);
284 cc_usb_printf(cc, "%02x", *bytes++);
290 cc_usb_read_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
293 cc_queue_read(cc, bytes, len);
294 cc_usb_printf(cc, "I %x %x\n", len, addr);
296 for (i = 0; i < len; i++) {
299 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
300 ccdbg_debug(CC_DEBUG_MEMORY, "\t%04x", addr + i);
302 ccdbg_debug(CC_DEBUG_MEMORY, " %02x", bytes[i]);
304 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
309 cc_usb_debug_mode(struct cc_usb *cc)
312 cc_usb_printf(cc, "D\n");
317 cc_usb_reset(struct cc_usb *cc)
320 cc_usb_printf(cc, "R\n");
324 static struct termios save_termios;
327 cc_usb_open(char *tty)
330 struct termios termios;
334 cc = calloc (sizeof (struct cc_usb), 1);
337 cc->fd = open(tty, O_RDWR | O_NONBLOCK);
343 tcgetattr(cc->fd, &termios);
344 save_termios = termios;
346 tcsetattr(cc->fd, TCSAFLUSH, &termios);
347 cc_usb_printf(cc, "E 0\nm 0\n");
355 cc_usb_close(struct cc_usb *cc)
357 tcsetattr(cc->fd, TCSAFLUSH, &save_termios);