ao-dump-up: Add --wait option to make testing µPusb easier
[fw/altos] / ao-tools / lib / cc-usb.c
index 9f07e6624d2c849f7469c07cbc80dc018648f383..1e023c7ebe8f7b37e99eeef7b044d294a5f531dd 100644 (file)
@@ -53,6 +53,7 @@ struct cc_usb {
 
        struct cc_hex_read      hex_buf[CC_NUM_HEX_READ];
        int                     hex_count;
+       int                     show_input;
 
        int                     remote;
 };
@@ -123,9 +124,10 @@ cc_handle_hex_read(struct cc_usb *cc)
 static void
 cc_usb_dbg(int indent, uint8_t *bytes, int len)
 {
-       int     eol = 1;
+       static int      eol = 1;
        int     i;
        uint8_t c;
+       ccdbg_debug(CC_DEBUG_BITBANG, "<<<%d bytes>>>", len);
        while (len--) {
                c = *bytes++;
                if (eol) {
@@ -135,22 +137,29 @@ cc_usb_dbg(int indent, uint8_t *bytes, int len)
                }
                switch (c) {
                case '\r':
-                       ccdbg_debug(CC_DEBUG_BITBANG, "^M");
+                       ccdbg_debug(CC_DEBUG_BITBANG, "\\r");
                        break;
                case '\n':
                        eol = 1;
+                       ccdbg_debug(CC_DEBUG_BITBANG, "\\n\n");
+                       break;
                default:
-                       ccdbg_debug(CC_DEBUG_BITBANG, "%c", c);
+                       if (c < ' ' || c > '~')
+                               ccdbg_debug(CC_DEBUG_BITBANG, "\\%02x", c);
+                       else
+                               ccdbg_debug(CC_DEBUG_BITBANG, "%c", c);
                }
        }
 }
 
+int    cc_default_timeout = 5000;
+
 /*
  * Flush pending writes, fill pending reads
  */
 
 static int
-_cc_usb_sync(struct cc_usb *cc, int wait_for_input)
+_cc_usb_sync(struct cc_usb *cc, int wait_for_input, int write_timeout)
 {
        int             ret;
        struct pollfd   fds;
@@ -159,7 +168,7 @@ _cc_usb_sync(struct cc_usb *cc, int wait_for_input)
        fds.fd = cc->fd;
        for (;;) {
                if (cc->hex_count || cc->out_count)
-                       timeout = 5000;
+                       timeout = write_timeout;
                else if (wait_for_input && cc->in_pos == cc->in_count)
                        timeout = wait_for_input;
                else
@@ -194,8 +203,14 @@ _cc_usb_sync(struct cc_usb *cc, int wait_for_input)
                                cc->in_count += ret;
                                if (cc->hex_count)
                                        cc_handle_hex_read(cc);
-                       } else if (ret < 0)
+                               if (cc->show_input && cc->in_count) {
+                                       write(2, cc->in_buf, cc->in_count);
+                                       cc->in_count = 0;
+                               }
+                       } else if (ret <= 0) {
                                perror("read");
+                               return -1;
+                       }
                }
                if (fds.revents & POLLOUT) {
                        ret = write(cc->fd, cc->out_buf,
@@ -216,7 +231,7 @@ _cc_usb_sync(struct cc_usb *cc, int wait_for_input)
 void
 cc_usb_sync(struct cc_usb *cc)
 {
-       if (_cc_usb_sync(cc, 0) < 0) {
+       if (_cc_usb_sync(cc, 0, cc_default_timeout) < 0) {
                fprintf(stderr, "USB link timeout\n");
                exit(1);
        }
@@ -254,10 +269,10 @@ cc_usb_printf(struct cc_usb *cc, char *format, ...)
 }
 
 int
-cc_usb_getchar(struct cc_usb *cc)
+cc_usb_getchar_timeout(struct cc_usb *cc, int timeout)
 {
        while (cc->in_pos == cc->in_count) {
-               if (_cc_usb_sync(cc, 5000) < 0) {
+               if (_cc_usb_sync(cc, timeout, cc_default_timeout) < 0) {
                        fprintf(stderr, "USB link timeout\n");
                        exit(1);
                }
@@ -265,6 +280,12 @@ cc_usb_getchar(struct cc_usb *cc)
        return cc->in_buf[cc->in_pos++];
 }
 
+int
+cc_usb_getchar(struct cc_usb *cc)
+{
+       return cc_usb_getchar_timeout(cc, cc_default_timeout);
+}
+
 void
 cc_usb_getline(struct cc_usb *cc, char *line, int max)
 {
@@ -383,7 +404,7 @@ cc_usb_open_remote(struct cc_usb *cc, int freq, char *call)
                cc_usb_printf(cc, "\nc F %d\nc c %s\np\nE 0\n", freq, call);
                do {
                        cc->in_count = cc->in_pos = 0;
-                       _cc_usb_sync(cc, 100);
+                       _cc_usb_sync(cc, 100, cc_default_timeout);
                } while (cc->in_count > 0);
                cc->remote = 1;
        }
@@ -400,19 +421,35 @@ cc_usb_close_remote(struct cc_usb *cc)
 
 static struct termios  save_termios;
 
+#include <errno.h>
+
 struct cc_usb *
 cc_usb_open(char *tty)
 {
        struct cc_usb   *cc;
        struct termios  termios;
+       int             i;
 
        if (!tty)
                tty = DEFAULT_TTY;
        cc = calloc (sizeof (struct cc_usb), 1);
        if (!cc)
                return NULL;
-       cc->fd = open(tty, O_RDWR | O_NONBLOCK);
-       if (cc->fd < 0) {
+       i = 0;
+       for (;;) {
+               cc->fd = open(tty, O_RDWR | O_NONBLOCK);
+               if (cc->fd >= 0)
+                       break;
+               i++;
+               if (errno == EBUSY || errno == EPERM || errno == EACCES) {
+                       fprintf(stderr, "open failed, pausing");
+                       perror(tty);
+                       if (i < 20) {
+                               sleep(3);
+                               continue;
+                       }
+               }
+
                perror(tty);
                free (cc);
                return NULL;
@@ -420,11 +457,13 @@ cc_usb_open(char *tty)
        tcgetattr(cc->fd, &termios);
        save_termios = termios;
        cfmakeraw(&termios);
+       cfsetospeed(&termios, B9600);
+       cfsetispeed(&termios, B9600);
        tcsetattr(cc->fd, TCSAFLUSH, &termios);
        cc_usb_printf(cc, "\nE 0\nm 0\n");
        do {
                cc->in_count = cc->in_pos = 0;
-               _cc_usb_sync(cc, 100);
+               _cc_usb_sync(cc, 100, cc_default_timeout);
        } while (cc->in_count > 0);
        return cc;
 }
@@ -438,3 +477,26 @@ cc_usb_close(struct cc_usb *cc)
        close (cc->fd);
        free (cc);
 }
+
+int
+cc_usb_write(struct cc_usb *cc, void *buf, int c)
+{
+       uint8_t *b;
+       int this_time;
+
+       b = buf;
+       cc->show_input = 1;
+       while (c > 0) {
+               this_time = c;
+               if (this_time > CC_OUT_BUF - cc->out_count)
+                       this_time = CC_OUT_BUF - cc->out_count;
+               memcpy(cc->out_buf + cc->out_count, b, this_time);
+               cc->out_count += this_time;
+               c -= this_time;
+               b += this_time;
+               while (cc->out_count >= CC_OUT_BUF) {
+                       _cc_usb_sync(cc, 0, -1);
+               }
+       }
+       return 1;
+}