summaryrefslogtreecommitdiff
path: root/tool
diff options
context:
space:
mode:
Diffstat (limited to 'tool')
-rw-r--r--tool/con.c106
-rwxr-xr-xtool/connect13
-rwxr-xr-xtool/download.tcl33
-rw-r--r--tool/p8bin2uart.c2
4 files changed, 148 insertions, 6 deletions
diff --git a/tool/con.c b/tool/con.c
new file mode 100644
index 0000000..8522786
--- /dev/null
+++ b/tool/con.c
@@ -0,0 +1,106 @@
1/* con device */
2
3#include <signal.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <fcntl.h>
8#include <termios.h>
9#include <unistd.h>
10
11struct termios save;
12
13void
14host(int fd)
15{
16 int cc;
17 char buf[128];
18
19 for (;;) {
20 cc = read(fd, buf, sizeof buf);
21#ifdef HEX
22 if (cc < 0)
23 break;
24 for (int i = 0; i < cc; ++i) {
25 char hex[3];
26 sprintf(hex, "%02X", (unsigned char) buf[i]);
27 write(1, hex, 2);
28 }
29#else
30 if (cc > 0)
31 write(1, buf, cc);
32 else if (cc < 0)
33 break;
34#endif
35 }
36}
37
38void
39hup(int signo)
40{
41 write(1, "HUP\n", 4);
42 tcsetattr(0, TCSADRAIN, &save);
43 _exit(0);
44}
45
46void
47user(int fd)
48{
49 char c, last = '\r';
50
51 signal(SIGHUP, hup);
52 for (;;)
53 while (read(0, &c, 1) > 0) {
54 if (c == '~' && (last == '\n' || last == '\r'
55 || last == 4)) {
56 read(0, &c, 1);
57 if (c == '.')
58 return;
59 }
60 write(fd, &c, 1);
61 last = c;
62 }
63}
64
65int
66main(int argc, char *argv[])
67{
68 int fd;
69 pid_t pid;
70 struct termios t;
71
72 if (argc != 2)
73 exit(1);
74
75 fd = open(argv[1], O_RDWR | O_NDELAY);
76 if (fd < 0)
77 exit(2);
78 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NDELAY);
79 tcgetattr(fd, &t);
80 t.c_cc[VMIN] = 1;
81 t.c_cc[VTIME] = 0;
82 tcsetattr(fd, TCSADRAIN, &t);
83
84 tcgetattr(0, &t);
85 save = t;
86 t.c_iflag = 0;
87 t.c_oflag = 0;
88 t.c_lflag = 0;
89 t.c_cc[VMIN] = 1;
90 t.c_cc[VTIME] = 0;
91 tcsetattr(0, TCSADRAIN, &t);
92
93 if ((pid = fork()) > 0) {
94 user(fd);
95 } else if (pid == 0) {
96 host(fd);
97 } else {
98 perror("fork");
99 _exit(EXIT_FAILURE);
100 }
101
102 tcsetattr(0, TCSADRAIN, &save);
103 kill(pid, SIGKILL);
104 close(fd);
105 _exit(EXIT_SUCCESS);
106}
diff --git a/tool/connect b/tool/connect
index 536db5b..f900e0c 100755
--- a/tool/connect
+++ b/tool/connect
@@ -1,21 +1,24 @@
1#!/bin/bash 1#!/bin/bash
2 2
3set -eux 3set -eu
4 4
5OLD_SETTINGS="$(stty -F /dev/ttyUSB0 -g)" 5OLD_SETTINGS="$(stty -g -F /dev/ttyUSB0)"
6 6
7stty -F /dev/ttyUSB0 \ 7stty -F /dev/ttyUSB0 \
8 115200 \ 8 115200 \
9 -drain \ 9 -drain \
10 -clocal \ 10 -clocal \
11 cread \ 11 cread \
12 -crtscts \ 12 crtscts \
13 cs8 \ 13 cs8 \
14 cstopb \ 14 cstopb \
15 -hup \ 15 -hup \
16 parenb \ 16 parenb \
17 -parodd \ 17 parodd \
18 -cmspar \
18 -igncr \ 19 -igncr \
20 ignpar \
21 -parmrk \
19 inpck \ 22 inpck \
20 -istrip \ 23 -istrip \
21 -ixany \ 24 -ixany \
@@ -26,4 +29,4 @@ stty -F /dev/ttyUSB0 \
26 29
27con /dev/ttyUSB0 || true 30con /dev/ttyUSB0 || true
28 31
29stty -F "$OLD_SETTINGS" 32stty -drain "$OLD_SETTINGS" -F /dev/ttyUSB0 2> /dev/null
diff --git a/tool/download.tcl b/tool/download.tcl
new file mode 100755
index 0000000..c0dca55
--- /dev/null
+++ b/tool/download.tcl
@@ -0,0 +1,33 @@
1#!/usr/bin/env expect
2
3set timeout 3600
4
5set core 0
6set wordsperline 1
7
8if {$::argc > 1} {
9 set core [lindex $::argv 0]
10 set wordsperline [lindex $::argv 1]
11} elseif {$::argc > 0} {
12 set core [lindex $::argv 0]
13}
14
15spawn tool/connect
16
17send "@[format %x [expr $core * 32768 / $wordsperline]]\n"
18
19while {[gets stdin line] >= 0} {
20 send "$line\n"
21 expect -ex "$line\n"
22}
23
24# It's likely we ended with a very large zero-memory operation. We want to see
25# two command bytes echoed back to us in order to guarantee that the zero
26# operation has completely flushed to memory.
27send "?"
28expect -ex "?"
29send "?"
30expect -ex "?"
31
32send "\n~."
33wait
diff --git a/tool/p8bin2uart.c b/tool/p8bin2uart.c
index de5a328..88c19cd 100644
--- a/tool/p8bin2uart.c
+++ b/tool/p8bin2uart.c
@@ -131,7 +131,7 @@ load(FILE *fp)
131 return v; 131 return v;
132} 132}
133 133
134#define MAX_LINE_SIZE 63 134#define MAX_LINE_SIZE 127
135 135
136char buf[MAX_LINE_SIZE * 2] = {0}; 136char buf[MAX_LINE_SIZE * 2] = {0};
137 137