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)
129 ccdbg_debug(CC_DEBUG_BITBANG, "<<<%d bytes>>>", len);
133 for (i = 0; i < indent; i++)
134 ccdbg_debug(CC_DEBUG_BITBANG, " ");
139 ccdbg_debug(CC_DEBUG_BITBANG, "\\r");
143 ccdbg_debug(CC_DEBUG_BITBANG, "\\n\n");
146 if (c < ' ' || c > '~')
147 ccdbg_debug(CC_DEBUG_BITBANG, "\\%02x", c);
149 ccdbg_debug(CC_DEBUG_BITBANG, "%c", c);
155 * Flush pending writes, fill pending reads
159 _cc_usb_sync(struct cc_usb *cc, int wait_for_input)
167 if (cc->hex_count || cc->out_count)
169 else if (wait_for_input && cc->in_pos == cc->in_count)
170 timeout = wait_for_input;
174 /* Move remaining bytes to the start of the input buffer */
176 memmove(cc->in_buf, cc->in_buf + cc->in_pos,
177 cc->in_count - cc->in_pos);
178 cc->in_count -= cc->in_pos;
181 if (cc->in_count < CC_IN_BUF)
182 fds.events |= POLLIN;
184 fds.events |= POLLOUT;
185 ret = poll(&fds, 1, timeout);
195 if (fds.revents & POLLIN) {
196 ret = read(cc->fd, cc->in_buf + cc->in_count,
197 CC_IN_BUF - cc->in_count);
199 cc_usb_dbg(24, cc->in_buf + cc->in_count, ret);
202 cc_handle_hex_read(cc);
206 if (fds.revents & POLLOUT) {
207 ret = write(cc->fd, cc->out_buf,
210 cc_usb_dbg(0, cc->out_buf, ret);
213 cc->out_count - ret);
214 cc->out_count -= ret;
223 cc_usb_sync(struct cc_usb *cc)
225 if (_cc_usb_sync(cc, 0) < 0) {
226 fprintf(stderr, "USB link timeout\n");
232 cc_usb_printf(struct cc_usb *cc, char *format, ...)
238 /* sprintf to a local buffer */
239 va_start(ap, format);
240 ret = vsnprintf(buf, sizeof(buf), format, ap);
242 if (ret > sizeof(buf)) {
243 fprintf(stderr, "printf overflow for format %s\n",
247 /* flush local buffer to the wire */
251 if (this_time > CC_OUT_BUF - cc->out_count)
252 this_time = CC_OUT_BUF - cc->out_count;
253 memcpy(cc->out_buf + cc->out_count, b, this_time);
254 cc->out_count += this_time;
257 while (cc->out_count >= CC_OUT_BUF)
263 cc_usb_getchar_timeout(struct cc_usb *cc, int timeout)
265 while (cc->in_pos == cc->in_count) {
266 if (_cc_usb_sync(cc, timeout) < 0) {
267 fprintf(stderr, "USB link timeout\n");
271 return cc->in_buf[cc->in_pos++];
275 cc_usb_getchar(struct cc_usb *cc)
277 return cc_usb_getchar_timeout(cc, 5000);
281 cc_usb_getline(struct cc_usb *cc, char *line, int max)
285 while ((c = cc_usb_getchar(cc)) != '\n') {
301 cc_usb_send_bytes(struct cc_usb *cc, uint8_t *bytes, int len)
311 cc_usb_printf(cc, "P");
313 cc_usb_printf (cc, " %02x", (*bytes++) & 0xff);
314 cc_usb_printf(cc, "\n");
320 cc_queue_read(struct cc_usb *cc, uint8_t *buf, int len)
322 struct cc_hex_read *hex_buf;
324 /* At the start of a command sequence, flush any pending input */
325 if (cc->hex_count == 0) {
329 while (cc->hex_count >= CC_NUM_HEX_READ)
331 hex_buf = &cc->hex_buf[cc->hex_count++];
337 cc_usb_recv_bytes(struct cc_usb *cc, uint8_t *buf, int len)
339 cc_queue_read(cc, buf, len);
340 cc_usb_printf(cc, "G %x\n", len);
345 cc_usb_write_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
347 cc_usb_printf(cc, "O %x %x\n", len, addr);
349 cc_usb_printf(cc, "%02x", *bytes++);
355 cc_usb_read_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
358 cc_queue_read(cc, bytes, len);
359 cc_usb_printf(cc, "I %x %x\n", len, addr);
361 for (i = 0; i < len; i++) {
364 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
365 ccdbg_debug(CC_DEBUG_MEMORY, "\t%04x", addr + i);
367 ccdbg_debug(CC_DEBUG_MEMORY, " %02x", bytes[i]);
369 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
374 cc_usb_debug_mode(struct cc_usb *cc)
377 cc_usb_printf(cc, "D\n");
382 cc_usb_reset(struct cc_usb *cc)
385 cc_usb_printf(cc, "R\n");
390 cc_usb_open_remote(struct cc_usb *cc, int freq, char *call)
393 fprintf (stderr, "freq %dkHz\n", freq);
394 fprintf (stderr, "call %s\n", call);
395 cc_usb_printf(cc, "\nc F %d\nc c %s\np\nE 0\n", freq, call);
397 cc->in_count = cc->in_pos = 0;
398 _cc_usb_sync(cc, 100);
399 } while (cc->in_count > 0);
405 cc_usb_close_remote(struct cc_usb *cc)
408 cc_usb_printf(cc, "~");
413 static struct termios save_termios;
416 cc_usb_open(char *tty)
419 struct termios termios;
423 cc = calloc (sizeof (struct cc_usb), 1);
426 cc->fd = open(tty, O_RDWR | O_NONBLOCK);
432 tcgetattr(cc->fd, &termios);
433 save_termios = termios;
435 cfsetospeed(&termios, B9600);
436 cfsetispeed(&termios, B9600);
437 tcsetattr(cc->fd, TCSAFLUSH, &termios);
438 cc_usb_printf(cc, "\nE 0\nm 0\n");
440 cc->in_count = cc->in_pos = 0;
441 _cc_usb_sync(cc, 100);
442 } while (cc->in_count > 0);
447 cc_usb_close(struct cc_usb *cc)
449 cc_usb_close_remote(cc);
451 tcsetattr(cc->fd, TCSAFLUSH, &save_termios);