some recording support, abort cmd on exit
authornotaz <notasas@gmail.com>
Wed, 8 Oct 2014 00:45:29 +0000 (03:45 +0300)
committernotaz <notasas@gmail.com>
Wed, 22 Oct 2014 20:39:11 +0000 (23:39 +0300)
host/main.c
main.c
pkts.h

index 58fbcb9..a21e419 100644 (file)
@@ -559,6 +559,44 @@ unhandled_line:
   return out;
 }
 
+static int write_bkm_frame(FILE *f, const uint8_t *data)
+{
+  /* ?0SA 00DU, ?1CB RLDU */
+  static const char ref[]  = "UDLRABCSXYZM";
+  static const char bits[] = { 0,1,2,3, 12,4,5,13, 16,16,16,16 };
+  uint32_t idata[2];
+  int p, i;
+
+  if (f == NULL) {
+    fprintf(stderr, "%s called without outfile\n", __func__);
+    goto out;
+  }
+
+  idata[0] = 0x10000 | (data[0] << 8) | data[1];
+  idata[1] = ~0;
+
+  fprintf(f, "|.|");
+  for (p = 0; p < 2; p++) {
+    for (i = 0; i < 12; i++)
+      fprintf(f, "%c", (idata[p] & (1 << bits[i])) ? '.' : ref[i]);
+    fprintf(f, "|");
+  }
+  fprintf(f, "|\n");
+
+out:
+  return 2;
+}
+
+static int tas_data_to_teensy(uint8_t b, uint8_t *data)
+{
+  /* SCBA RLDU */
+  /*     v     */
+  /* ?0SA 00DU, ?1CB RLDU */
+  data[0] = (b & 0x13) | ((b >> 2) & 0x20);
+  data[1] = (b & 0x0f) | ((b >> 1) & 0x30);
+  return 2;
+}
+
 static int submit_urb(int fd, struct usbdevfs_urb *urb, int ep,
   void *buf, size_t buf_size)
 {
@@ -597,10 +635,12 @@ int main(int argc, char *argv[])
   int pending_urbs = 0;
   fd_set rfds, wfds;
   const char *tasfn = NULL;
+  const char *outfn = NULL;
   uint8_t *tas_data = NULL;
   int use_readinc = 0; // frame increment on read
   int tas_skip = 0;
   int enable_sent = 0;
+  int abort_sent = 0;
   int frame_count = 0;
   int frames_sent = 0;
   char buf_dbg[64 + 1];
@@ -608,6 +648,7 @@ int main(int argc, char *argv[])
   struct tas_pkt pkt_out;
   struct timeval *timeout = NULL;
   struct timeval tout;
+  FILE *outf = NULL;
   int i, ret = -1;
   int fd;
 
@@ -620,6 +661,12 @@ int main(int argc, char *argv[])
           missing_arg(i);
         tasfn = argv[i];
         continue;
+      case 'w':
+        i++;
+        if (argv[i] == NULL)
+          missing_arg(i);
+        outfn = argv[i];
+        continue;
       case 's':
         i++;
         if (argv[i] == NULL)
@@ -634,6 +681,8 @@ int main(int argc, char *argv[])
         return 1;
       }
     }
+
+    /* remaining args are evdev filenames */
     if (evdev_fd_cnt >= ARRAY_SIZE(evdev_fds)) {
       fprintf(stderr, "too many evdevs\n");
       break;
@@ -723,12 +772,21 @@ int main(int argc, char *argv[])
     }
   }
 
+  if (outfn != NULL) {
+    outf = fopen(outfn, "w");
+    if (outf == NULL) {
+      fprintf(stderr, "fopen %s: ", tasfn);
+      perror("");
+      return 1;
+    }
+  }
+
   enable_echo(0);
   signal(SIGINT, signal_handler);
 
   dev.fd = -1;
 
-  while (!g_exit)
+  while (!g_exit || (pending_urbs & (1 << URB_DATA_OUT)))
   {
     if (dev.fd == -1) {
       ret = find_device(&dev, 0x16C0, 0x0486);
@@ -794,7 +852,6 @@ int main(int argc, char *argv[])
     timeout = NULL;
 
     /* sometihng form stdin? */
-    /* something from input devices? */
     if (FD_ISSET(STDIN_FILENO, &rfds)) {
       char c = 0;
       ret = read(STDIN_FILENO, &c, 1);
@@ -810,6 +867,7 @@ int main(int argc, char *argv[])
       }
     }
 
+    /* something from input devices? */
     fixed_input_changed = 0;
     for (i = 0; i < evdev_fd_cnt; i++) {
       if (FD_ISSET(evdev_fds[i], &rfds)) {
@@ -852,33 +910,24 @@ int main(int argc, char *argv[])
       else if (reaped_urb == &urb[URB_DATA_IN])
       {
         /* some request from teensy */
-        int count;
-        uint8_t b;
-
         switch (pkt_in.type) {
         case PKT_STREAM_REQ:
           printf("req: %d/%d/%d\n", pkt_in.req.frame,
             frames_sent, frame_count);
 
-          for (i = 0; i < sizeof(pkt_out.data) / 2; i++) {
-            pkt_out.data[i * 2 + 0] = 0x33;
-            pkt_out.data[i * 2 + 1] = 0x3f;
-          }
+          pkt_out.size = 0;
           if (frames_sent < frame_count) {
-            pkt_out.type = PKT_STREAM_DATA;
-
-            count = frame_count - frames_sent;
-            if (count > sizeof(pkt_out.data) / 2)
-              count = sizeof(pkt_out.data) / 2;
-            for (i = 0; i < count; i++) {
-              /* SCBA RLDU */
-              b = tas_data[frames_sent];
-
-              /* ?0SA 00DU, ?1CB RLDU */
-              pkt_out.data[i * 2 + 0] = (b & 0x13) | ((b >> 2) & 0x20);
-              pkt_out.data[i * 2 + 1] = (b & 0x0f) | ((b >> 1) & 0x30);
+            pkt_out.type = PKT_STREAM_DATA_TO;
+
+            for (i = 0; i < sizeof(pkt_out.data); ) {
+              i += tas_data_to_teensy(tas_data[frames_sent],
+                     pkt_out.data + i);
+
               frames_sent++;
+              if (frames_sent >= frame_count)
+                break;
             }
+            pkt_out.size = i;
           }
           else
             pkt_out.type = PKT_STREAM_END;
@@ -886,7 +935,19 @@ int main(int argc, char *argv[])
           ret = submit_urb(dev.fd, &urb[URB_DATA_OUT],
                   dev.ifaces[0].ep_out, &pkt_out, sizeof(pkt_out));
           if (ret != 0)
-            perror("USBDEVFS_SUBMITURB URB_DATA_OUT PKT_STREAM_DATA");
+            perror("USBDEVFS_SUBMITURB PKT_STREAM_DATA_TO");
+          break;
+
+        case PKT_STREAM_DATA_FROM:
+          printf("f: %d\n", frame_count);
+          if (pkt_in.size == 0 || pkt_in.size > sizeof(pkt_out.data)) {
+            printf("host: got bad DATA_FROM size: %u\n", pkt_in.size);
+            break;
+          }
+          for (i = 0; i < pkt_in.size; ) {
+            i += write_bkm_frame(outf, pkt_in.data + i);
+            frame_count++;
+          }
           break;
 
         default:
@@ -914,10 +975,12 @@ int main(int argc, char *argv[])
       // can't do that yet
       continue;
 
-    if (tas_data != NULL && !enable_sent) {
+    if ((tas_data != NULL || outf != NULL) && !enable_sent) {
       memset(&pkt_out, 0, sizeof(pkt_out));
       pkt_out.type = PKT_STREAM_ENABLE;
-      pkt_out.start.use_readinc = use_readinc;
+      pkt_out.enable.stream_to = (tas_data != NULL);
+      pkt_out.enable.stream_from = (outf != NULL);
+      pkt_out.enable.use_readinc = use_readinc;
 
       ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
                        &pkt_out, sizeof(pkt_out));
@@ -925,8 +988,10 @@ int main(int argc, char *argv[])
         perror("USBDEVFS_SUBMITURB PKT_STREAM_ENABLE");
         continue;
       }
+      pending_urbs |= 1 << URB_DATA_OUT;
       enable_sent = 1;
       frames_sent = 0;
+      continue;
     }
     if (tas_data == NULL && fixed_input_changed) {
       memset(&pkt_out, 0, sizeof(pkt_out));
@@ -936,10 +1001,24 @@ int main(int argc, char *argv[])
       ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
                        &pkt_out, sizeof(pkt_out));
       if (ret != 0) {
-        perror("USBDEVFS_SUBMITURB URB_DATA_OUT");
+        perror("USBDEVFS_SUBMITURB PKT_FIXED_STATE");
+        break;
+      }
+      pending_urbs |= 1 << URB_DATA_OUT;
+      continue;
+    }
+    if (g_exit && !abort_sent) {
+      memset(&pkt_out, 0, sizeof(pkt_out));
+      pkt_out.type = PKT_STREAM_ABORT;
+
+      ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
+                       &pkt_out, sizeof(pkt_out));
+      if (ret != 0) {
+        perror("USBDEVFS_SUBMITURB PKT_STREAM_ABORT");
         break;
       }
       pending_urbs |= 1 << URB_DATA_OUT;
+      abort_sent = 1;
       continue;
     }
 
@@ -952,6 +1031,9 @@ dev_close:
 
   enable_echo(1);
 
+  if (outf != NULL)
+    fclose(outf);
+
   if (dev.fd != -1) {
     /* deal with pending URBs */
     if (pending_urbs & (1 << URB_DATA_IN))
diff --git a/main.c b/main.c
index a3062c2..0cae7e6 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,26 +6,35 @@
 #include "teensy3/usb_rawhid.h"
 #include "pkts.h"
 
+#define noinline __attribute__((noinline))
+
 // use power of 2
 #define STREAM_BUF_SIZE 512
 #define STREAM_BUF_MASK (512 - 1)
 
 /* ?0SA 00DU, ?1CB RLDU */
+#define STREAM_EL_SZ 2
+
 static struct {
-       uint8_t stream[STREAM_BUF_SIZE][2];
-       uint8_t fixed_state[4];
-       uint32_t stream_enable:1;
+       uint8_t stream_to[STREAM_BUF_SIZE][STREAM_EL_SZ];
+       uint8_t stream_from[STREAM_BUF_SIZE][STREAM_EL_SZ];
+       union {
+               uint8_t fixed_state[4];
+               uint32_t fixed_state32;
+       };
+       uint32_t stream_enable_to:1;
+       uint32_t stream_enable_from:1;
        uint32_t stream_started:1;
-       uint32_t stream_received:1;
+       uint32_t stream_ended:1;
        uint32_t use_readinc:1;
        uint32_t frame_cnt;
        uint32_t edge_cnt;
-       uint32_t i;
-       uint32_t o;
+       uint32_t t_i;
+       uint32_t t_o;
+       uint32_t f_i;
+       uint32_t f_o;
 } g;
 
-#define STREAM_EL_SZ sizeof(g.stream[0])
-
 ssize_t _write(int fd, const void *buf, size_t nbyte)
 {
        char tbuf[64];
@@ -56,7 +65,7 @@ static void portb_isr_fixed(void)
        g.edge_cnt++;
 }
 
-static void portb_isr_readinc(void)
+static void portb_isr_do_to_inc(void)
 {
        uint32_t isfr, th;
 
@@ -64,10 +73,10 @@ static void portb_isr_readinc(void)
        PORTB_ISFR = isfr;
        th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
 
-       GPIOD_PDOR = g.stream[g.o][th];
+       GPIOD_PDOR = g.stream_to[g.t_o][th];
        if (th) {
-               g.o = (g.o + 1) & STREAM_BUF_MASK;
-               if (g.o == g.i)
+               g.t_o = (g.t_o + 1) & STREAM_BUF_MASK;
+               if (g.t_o == g.t_i)
                        // done
                        attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
                g.frame_cnt++;
@@ -75,7 +84,7 @@ static void portb_isr_readinc(void)
        g.edge_cnt++;
 }
 
-static void portb_isr_read(void)
+static void portb_isr_do_to(void)
 {
        uint32_t isfr, th;
 
@@ -83,7 +92,7 @@ static void portb_isr_read(void)
        PORTB_ISFR = isfr;
        th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
 
-       GPIOD_PDOR = g.stream[g.o][th];
+       GPIOD_PDOR = g.stream_to[g.t_o][th];
        g.edge_cnt++;
 }
 
@@ -104,14 +113,51 @@ static void portc_isr_frameinc(void)
        isfr = PORTC_ISFR;
        PORTC_ISFR = isfr;
 
-       g.o = (g.o + 1) & STREAM_BUF_MASK;
-       if (g.o == g.i) {
+       g.t_o = (g.t_o + 1) & STREAM_BUF_MASK;
+       if (g.t_o == g.t_i) {
                attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
                attachInterruptVector(IRQ_PORTC, portc_isr_nop);
        }
        g.frame_cnt++;
 }
 
+/* "recording" data */
+static noinline void do_from_step(void)
+{
+       uint32_t s;
+
+       // should hopefully give atomic fixed_state read..
+       s = g.fixed_state32;
+       g.stream_from[g.f_i][0] = s;
+       g.stream_from[g.f_i][1] = s >> 8;
+       g.f_i = (g.f_i + 1) & STREAM_BUF_MASK;
+}
+
+static void portb_isr_fixed_do_from(void)
+{
+       uint32_t isfr, th;
+
+       isfr = PORTB_ISFR;
+       PORTB_ISFR = isfr;
+       th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
+
+       GPIOD_PDOR = g.fixed_state[th];
+       if (th)
+               do_from_step();
+       g.edge_cnt++;
+}
+
+static void portc_isr_frameinc_do_from(void)
+{
+       uint32_t isfr;
+
+       isfr = PORTC_ISFR;
+       PORTC_ISFR = isfr;
+
+       do_from_step();
+       g.frame_cnt++;
+}
+
 static void udelay(uint32_t us)
 {
        uint32_t start = micros();
@@ -178,38 +224,80 @@ static void do_start_seq(void)
                return;
        }
 
-       if (!g.stream_enable) {
+       if (!g.stream_enable_to && !g.stream_enable_from) {
                printf("got start_seq, without enable from USB\n");
                return;
        }
 
-       if (g.i == g.o) {
-               printf("got start_seq while buffer is empty\n");
+       if (g.stream_enable_to && g.t_i == g.t_o) {
+               printf("got start_seq while stream_to is empty\n");
+               return;
+       }
+
+       if (g.stream_enable_from && g.f_i != g.f_o) {
+               printf("got start_seq while stream_from is not empty\n");
                return;
        }
 
        __disable_irq();
        g.stream_started = 1;
-       if (g.use_readinc) {
-               attachInterruptVector(IRQ_PORTB, portb_isr_readinc);
-               attachInterruptVector(IRQ_PORTC, portc_isr_nop);
+       if (g.stream_enable_to) {
+               if (g.use_readinc) {
+                       attachInterruptVector(IRQ_PORTB, portb_isr_do_to_inc);
+                       attachInterruptVector(IRQ_PORTC, portc_isr_nop);
+               }
+               else {
+                       attachInterruptVector(IRQ_PORTB, portb_isr_do_to);
+                       attachInterruptVector(IRQ_PORTC, portc_isr_frameinc);
+               }
        }
-       else {
-               attachInterruptVector(IRQ_PORTB, portb_isr_read);
-               attachInterruptVector(IRQ_PORTC, portc_isr_frameinc);
+       else if (g.stream_enable_from) {
+               if (g.use_readinc) {
+                       attachInterruptVector(IRQ_PORTB,
+                                               portb_isr_fixed_do_from);
+                       attachInterruptVector(IRQ_PORTC, portc_isr_nop);
+               }
+               else {
+                       attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
+                       attachInterruptVector(IRQ_PORTC,
+                                             portc_isr_frameinc_do_from);
+               }
+
+               // prep first frame already
+               do_from_step();
        }
        __enable_irq();
 }
 
-static int get_space(void)
+// callers must disable IRQs
+static void clear_state(void)
+{
+       g.stream_enable_to = 0;
+       g.stream_enable_from = 0;
+       g.use_readinc = 0;
+       g.stream_started = 0;
+       g.stream_ended = 0;
+       g.t_i = g.t_o = 0;
+       g.f_i = g.f_o = 0;
+       g.frame_cnt = 0;
+       attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
+       attachInterruptVector(IRQ_PORTC, portc_isr_nop);
+}
+
+static int get_space_to(void)
 {
-       return STREAM_BUF_SIZE - ((g.i - g.o) & STREAM_BUF_MASK);
+       return STREAM_BUF_SIZE - ((g.t_i - g.t_o) & STREAM_BUF_MASK);
+}
+
+static int get_used_from(void)
+{
+       return (g.f_i - g.f_o) & STREAM_BUF_MASK;
 }
 
 static void do_usb(void *buf)
 {
        struct tas_pkt *pkt = buf;
-       uint32_t i, g_i;
+       uint32_t t_i, i;
        int space;
 
        switch (pkt->type) {
@@ -218,35 +306,36 @@ static void do_usb(void *buf)
                break;
        case PKT_STREAM_ENABLE:
                __disable_irq();
+               clear_state();
                /* wait for start from MD */
-               g.stream_enable = 1;
-               g.stream_started = 0;
-               g.stream_received = 0;
-               g.use_readinc = pkt->start.use_readinc;
-               g.i = g.o = 0;
-               g.frame_cnt = 0;
-               attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
-               attachInterruptVector(IRQ_PORTC, portc_isr_nop);
+               g.stream_enable_to = pkt->enable.stream_to;
+               g.stream_enable_from = pkt->enable.stream_from;
+               g.use_readinc = pkt->enable.use_readinc;
+               __enable_irq();
+               break;
+       case PKT_STREAM_ABORT:
+               __disable_irq();
+               clear_state();
                __enable_irq();
                break;
        case PKT_STREAM_END:
-               g.stream_received = 1;
+               g.stream_ended = 1;
                printf("end of stream\n");
                break;
-       case PKT_STREAM_DATA:
-               g_i = g.i;
-               space = get_space();
-               if (space <= sizeof(pkt->data) / STREAM_EL_SZ) {
+       case PKT_STREAM_DATA_TO:
+               t_i = g.t_i;
+               space = get_space_to();
+               if (space <= pkt->size / STREAM_EL_SZ) {
                        printf("got data pkt while space=%d\n", space);
                        return;
                }
-               for (i = 0; i < sizeof(pkt->data) / STREAM_EL_SZ; i++) {
-                       memcpy(&g.stream[g_i++],
+               for (i = 0; i < pkt->size / STREAM_EL_SZ; i++) {
+                       memcpy(&g.stream_to[t_i++],
                               pkt->data + i * STREAM_EL_SZ,
                               STREAM_EL_SZ);
-                       g_i &= STREAM_BUF_MASK;
+                       t_i &= STREAM_BUF_MASK;
                }
-               g.i = g_i;
+               g.t_i = t_i;
                break;
        default:
                printf("got unknown pkt type: %04x\n", pkt->type);
@@ -310,8 +399,9 @@ int main(void)
 
        while (1) {
                struct tas_pkt pkt;
-               while (g.stream_enable && !g.stream_received
-                      && get_space() > sizeof(pkt.data) / STREAM_EL_SZ)
+
+               while (g.stream_enable_to && !g.stream_ended
+                 && get_space_to() > sizeof(pkt.data) / STREAM_EL_SZ)
                {
                        pkt.type = PKT_STREAM_REQ;
                        pkt.req.frame = g.frame_cnt;
@@ -329,6 +419,30 @@ int main(void)
                                do_usb(buf);
                }
 
+               while (g.stream_enable_from && !g.stream_ended
+                 && get_used_from() >= sizeof(pkt.data) / STREAM_EL_SZ)
+               {
+                       uint32_t f_o;
+                       int i;
+
+                       f_o = g.f_o;
+                       for (i = 0; i < sizeof(pkt.data); i += STREAM_EL_SZ) {
+                               memcpy(pkt.data + i, &g.stream_from[f_o++],
+                                       STREAM_EL_SZ);
+                               f_o &= STREAM_BUF_MASK;
+                       }
+                       g.f_o = f_o;
+
+                       pkt.type = PKT_STREAM_DATA_FROM;
+                       pkt.size = i;
+
+                       ret = usb_rawhid_send(&pkt, 1000);
+                       if (ret != sizeof(pkt)) {
+                               printf("send DATA_FROM: %d\n", ret);
+                               break;
+                       }
+               }
+
                if (timeout == 1000) {
                        edge_cnt = g.edge_cnt;
                        //printf("e: %d th: %d\n", edge_cnt - edge_cnt_last,
diff --git a/pkts.h b/pkts.h
index 86e24b8..a51b0d0 100644 (file)
--- a/pkts.h
+++ b/pkts.h
@@ -3,7 +3,8 @@ enum tas_pkt_type {
        PKT_FIXED_STATE      = 0xef01,
        PKT_STREAM_ENABLE    = 0xef02,
        PKT_STREAM_REQ       = 0xef03,
-       PKT_STREAM_DATA      = 0xef04,
+       PKT_STREAM_DATA_TO   = 0xef04,
+       PKT_STREAM_DATA_FROM = 0xef05,
        PKT_STREAM_END       = 0xef06,
        PKT_STREAM_ABORT     = 0xef07,
 };