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];
61 cc_hex_nibble(uint8_t c)
63 if ('0' <= c && c <= '9')
65 if ('a' <= c && c <= 'f')
67 if ('A' <= c && c <= 'F')
73 * Take raw input bytes, parse them as hex
74 * and write them to the waiting buffer
77 cc_handle_hex_read(struct cc_usb *cc)
83 while (hex_pos < cc->hex_count && cc->in_pos < cc->in_count) {
85 * Skip to next hex character
87 while (cc->in_pos < cc->in_count &&
88 cc_hex_nibble(cc->in_buf[cc->in_pos]) == NOT_HEX)
91 * Make sure we have two characters left
93 if (cc->in_count - cc->in_pos < 2)
98 h = cc_hex_nibble(cc->in_buf[cc->in_pos]);
99 l = cc_hex_nibble(cc->in_buf[cc->in_pos+1]);
100 if (h == NOT_HEX || l == NOT_HEX) {
101 fprintf(stderr, "hex read error\n");
108 *cc->hex_buf[hex_pos].buf++ = (h << 4) | l;
109 if (--cc->hex_buf[hex_pos].len <= 0)
113 /* Move pending hex reads to the start of the array */
115 memmove(cc->hex_buf, cc->hex_buf + hex_pos,
116 (cc->hex_count - hex_pos) * sizeof (cc->hex_buf[0]));
117 cc->hex_count -= hex_pos;
122 cc_usb_dbg(int indent, uint8_t *bytes, int len)
130 for (i = 0; i < indent; i++)
131 ccdbg_debug(CC_DEBUG_BITBANG, " ");
136 ccdbg_debug(CC_DEBUG_BITBANG, "^M");
141 ccdbg_debug(CC_DEBUG_BITBANG, "%c", c);
147 * Flush pending writes, fill pending reads
151 _cc_usb_sync(struct cc_usb *cc, int wait_for_input)
159 if (cc->hex_count || cc->out_count)
161 else if (wait_for_input && cc->in_pos == cc->in_count)
162 timeout = wait_for_input;
166 /* Move remaining bytes to the start of the input buffer */
168 memmove(cc->in_buf, cc->in_buf + cc->in_pos,
169 cc->in_count - cc->in_pos);
170 cc->in_count -= cc->in_pos;
173 if (cc->in_count < CC_IN_BUF)
174 fds.events |= POLLIN;
176 fds.events |= POLLOUT;
177 ret = poll(&fds, 1, timeout);
187 if (fds.revents & POLLIN) {
188 ret = read(cc->fd, cc->in_buf + cc->in_count,
189 CC_IN_BUF - cc->in_count);
191 cc_usb_dbg(24, cc->in_buf + cc->in_count, ret);
194 cc_handle_hex_read(cc);
198 if (fds.revents & POLLOUT) {
199 ret = write(cc->fd, cc->out_buf,
202 cc_usb_dbg(0, cc->out_buf, ret);
205 cc->out_count - ret);
206 cc->out_count -= ret;
215 cc_usb_sync(struct cc_usb *cc)
217 if (_cc_usb_sync(cc, 0) < 0) {
218 fprintf(stderr, "USB link timeout\n");
224 cc_usb_printf(struct cc_usb *cc, char *format, ...)
230 /* sprintf to a local buffer */
231 va_start(ap, format);
232 ret = vsnprintf(buf, sizeof(buf), format, ap);
234 if (ret > sizeof(buf)) {
235 fprintf(stderr, "printf overflow for format %s\n",
239 /* flush local buffer to the wire */
243 if (this_time > CC_OUT_BUF - cc->out_count)
244 this_time = CC_OUT_BUF - cc->out_count;
245 memcpy(cc->out_buf + cc->out_count, b, this_time);
246 cc->out_count += this_time;
249 while (cc->out_count >= CC_OUT_BUF)
255 cc_usb_getchar(struct cc_usb *cc)
257 while (cc->in_pos == cc->in_count) {
258 if (_cc_usb_sync(cc, 5000) < 0) {
259 fprintf(stderr, "USB link timeout\n");
263 return cc->in_buf[cc->in_pos++];
267 cc_usb_getline(struct cc_usb *cc, char *line, int max)
271 while ((c = cc_usb_getchar(cc)) != '\n') {
287 cc_usb_send_bytes(struct cc_usb *cc, uint8_t *bytes, int len)
297 cc_usb_printf(cc, "P");
299 cc_usb_printf (cc, " %02x", (*bytes++) & 0xff);
300 cc_usb_printf(cc, "\n");
306 cc_queue_read(struct cc_usb *cc, uint8_t *buf, int len)
308 struct cc_hex_read *hex_buf;
310 /* At the start of a command sequence, flush any pending input */
311 if (cc->hex_count == 0) {
315 while (cc->hex_count >= CC_NUM_HEX_READ)
317 hex_buf = &cc->hex_buf[cc->hex_count++];
323 cc_usb_recv_bytes(struct cc_usb *cc, uint8_t *buf, int len)
325 cc_queue_read(cc, buf, len);
326 cc_usb_printf(cc, "G %x\n", len);
331 cc_usb_write_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
333 cc_usb_printf(cc, "O %x %x\n", len, addr);
335 cc_usb_printf(cc, "%02x", *bytes++);
341 cc_usb_read_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
344 cc_queue_read(cc, bytes, len);
345 cc_usb_printf(cc, "I %x %x\n", len, addr);
347 for (i = 0; i < len; i++) {
350 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
351 ccdbg_debug(CC_DEBUG_MEMORY, "\t%04x", addr + i);
353 ccdbg_debug(CC_DEBUG_MEMORY, " %02x", bytes[i]);
355 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
360 cc_usb_debug_mode(struct cc_usb *cc)
363 cc_usb_printf(cc, "D\n");
368 cc_usb_reset(struct cc_usb *cc)
371 cc_usb_printf(cc, "R\n");
375 static struct termios save_termios;
378 cc_usb_open(char *tty)
381 struct termios termios;
385 cc = calloc (sizeof (struct cc_usb), 1);
388 cc->fd = open(tty, O_RDWR | O_NONBLOCK);
394 tcgetattr(cc->fd, &termios);
395 save_termios = termios;
397 tcsetattr(cc->fd, TCSAFLUSH, &termios);
398 cc_usb_printf(cc, "E 0\nm 0\n");
400 cc->in_count = cc->in_pos = 0;
401 _cc_usb_sync(cc, 100);
402 } while (cc->in_count > 0);
407 cc_usb_close(struct cc_usb *cc)
409 tcsetattr(cc->fd, TCSAFLUSH, &save_termios);