]> Joshua Wise's Git repositories - fpgaboy.git/blobdiff - binwire.c
Fix the UART, and set it to 19k2 for more stable operation. Make the downloader a...
[fpgaboy.git] / binwire.c
index 9322ef1b710eb9484bb477938b13d3f7c0c6ef38..ed6b98c90a7aac056b2e4807a679e022cbf4c286 100644 (file)
--- a/binwire.c
+++ b/binwire.c
@@ -1,4 +1,9 @@
 #include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <poll.h>
 
 void dowrite(char *s, int len)
 {
@@ -9,20 +14,81 @@ void dowrite(char *s, int len)
   }
 }
 
-void main()
+int waitchar(int timeout)
 {
-  char buf[259];
+  struct pollfd pfd;
+  
+  pfd.fd = 0;
+  pfd.events = POLLIN;
+  return poll(&pfd, 1, timeout) == 1;
+}
+
+void expect(char *s, int len)
+{
+  int i;
+  char c;
+  for (i=0; i < len; i++)
+  {
+    if (waitchar(100) == 0)
+    {
+      fprintf(stderr, "Timeout reached in expect (expected %c)\n", s[i]);
+      return;
+    }
+    while (read(0, &c, 1) == 0)
+      fprintf(stderr, "Short read...\n");
+    if (c != s[i])
+      fprintf(stderr, "Expect failed: expected %d, got %d (pos %d)\n", s[i], c, i);
+  }
+}
+
+void expect_no_chars()
+{
+  int cs = 0;
+  
+  while (waitchar(10) == 1)
+  {
+    char c;
+    if (read(0, &c, 1) == 0)
+      fprintf(stderr, "enc Short read...\n");
+    cs++;
+    fprintf(stderr, "Warning: expected no chars, got %d\n", c);
+  }
+  if (cs)
+    fprintf(stderr, "Expect no chars failed: got %d chars\n", cs);
+  
+  
+}
+
+void main(int argc, char **argv)
+{
+  unsigned char buf[259];
   int sz;
+  int rfd;
+  
+  if (argc < 2)
+  {
+    fprintf(stderr, "Usage: %s [filename]\n", argv[0]);
+    exit(1);
+  }
+  rfd = open(argv[1], O_RDONLY);
+  if (rfd < 0)
+  {
+    perror("open");
+    exit(1);
+  }
+  
   dowrite("\x1B" "A\x00\x00\x00...", 8);
   fprintf(stderr, "Address sent\n");
-  while ((sz = read(0, buf+3, 128)) > 0)
+  expect("A...", 4);
+  while ((sz = read(rfd, buf+3, 128)) > 0)
   {
     buf[0] = 0x1B;
     buf[1] = 'D';
-    buf[2] = sz - 1;
+    buf[2] = sz+1;
     dowrite(buf, sz + 3);
-    dowrite(".", 1);
     fprintf(stderr, "Data sent\n");
-    usleep(100000);
+    expect("D", 1);
+    expect_no_chars();
   }
+  exit(0);
 }
\ No newline at end of file
This page took 0.026372 seconds and 4 git commands to generate.