This repository has been archived by the owner on Jul 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bridge.c
404 lines (364 loc) · 11.6 KB
/
bridge.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Compile: /usr/local/musl/bin/musl-gcc --static bridge.c -Wall -o bridge
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
int spike_version = 1; // Default to Spike 1
int set_interface_attribs(int fd, int speed)
{
struct termios tty;
if (tcgetattr(fd, &tty) < 0) {
printf("Error from tcgetattr: %s\n", strerror(errno));
return -1;
}
cfsetospeed(&tty, (speed_t)speed);
cfsetispeed(&tty, (speed_t)speed);
tty.c_cflag |= (CLOCAL | CREAD); /* ignore modem controls */
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8; /* 8-bit characters */
tty.c_cflag &= ~PARENB; /* no parity bit */
tty.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
tty.c_cflag &= ~CRTSCTS; /* no hardware flowcontrol */
/* setup for non-canonical mode */
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tty.c_oflag &= ~OPOST;
/* fetch bytes as they become available */
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 1;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
printf("Error from tcsetattr: %s\n", strerror(errno));
return -1;
}
return 0;
}
int get_gpio_state(int gpio) {
if (spike_version == 2) {
// Not supported yet
return 0;
}
int fd = open("/dev/gpio", O_RDWR);
int result = ioctl(fd, 0x3C02, gpio);
close(fd);
return result;
}
void set_gpio_on(int gpio) {
if (spike_version == 2) {
// Not supported yet
return;
}
int fd = open("/dev/gpio", O_RDWR);
ioctl(fd, 0x3C03, gpio);
close(fd);
}
void set_gpio_off(int gpio) {
if (spike_version == 2) {
// Not supported yet
return;
}
int fd = open("/dev/gpio", O_RDWR);
ioctl(fd, 0x3C04, gpio);
close(fd);
}
void set_backlight(int brightness) {
if (spike_version == 2) {
// Not supported yet
return;
}
int fd = open("/dev/backlight", O_RDWR);
ioctl(fd, 0x4001, &brightness);
close(fd);
}
int message_position = 0;
char message[2052]; // 2048 + 3 (node, length, response_len)
int fd;
int spi_fd;
int local_activity = 0;
char local_switches[8] = {0,0,0,0,0,0,0,0};
void process_message() {
if (message[0] == 0x00) {
//printf("Poll\n");
// Intercept poll if we have local activity
if (local_activity) {
// Tell MPF that there is local activity
printf("F0 ");
} else {
// Or forward to bus
write(fd, &message, 1);
// Also check local inputs
char tmp_inputs[8];
if (spike_version == 1) {
read(spi_fd, tmp_inputs, sizeof tmp_inputs);
if (memcmp(tmp_inputs, local_switches, 8)) {
local_activity = 1;
}
}
}
} else if (message[0] == 0x01) {
// Sleep after certain messages to keep bus in sync. MPF specific
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = message[1] * 1000000;
nanosleep(&ts, NULL);
} else if ((message[0] & 0xF0) == 0x80) {
//printf("Bus message received\n");
if ((message[0] & 0x0F) == 0x00 && (message[2] & 0xFF) == 0x11) {
// Intercept input reads to board 0
local_activity = 0;
//printf("Read local switches\n");
int j;
if (spike_version == 1) {
read(spi_fd, local_switches, sizeof local_switches);
}
char checksum = 0;
for (j = 0; j < 8; j++)
{
printf("%02X ", local_switches[j]);
checksum += local_switches[j];
}
checksum = 256 - checksum;
printf("%02X 00 ", checksum & 0xFF);
} else if ((message[0] & 0x0F) == 0x00 && (message[1] & 0xFF) == 0x04 && (message[2] & 0xFF) == 0x80) {
// Intercept LED sets to backlight
int brightness = ((message[4] & 0xFF) << 8);
set_backlight(brightness);
} else if ((message[0] & 0xFF) == 0x80 && (message[1] & 0xFF) == 0x00 && (message[2] & 0xFF) == 0x90) {
// Send DMD frames
//printf("Sending DMD frame\n");
// Open dmd port
char *dmd_portname = "/dev/spi0";
int dmd_fd = open (dmd_portname, O_RDWR | O_NOCTTY | O_SYNC);
if (dmd_fd < 0)
{
printf("Error opening %s: %s\n", dmd_portname, strerror(errno));
return;
}
write(dmd_fd, &message[3], 2048);
close(dmd_fd);
} else {
// Send message to nodebus
//printf("Sending to Bus\n");
write(fd, &message, message[1] + 3);
}
} else {
/*printf("Invalid message received");
int j;
for (j = 0; j < message[1] + 3; j++)
{
printf("%02X ", message[j]);
}*/
}
}
void process_byte(char bus_byte)
{
//printf("Processing %02X %i\n", bus_byte, message_position);
if (message_position < 0 || message_position >= 2051) {
message_position = 0;
memset(message, 0, sizeof message);
}
if (message_position == 0) {
// First byte - type + node
if (bus_byte == 0x00) {
// Poll
message[0] = bus_byte;
process_message();
message_position = -1;
} else if (bus_byte == 0x01) {
// Custom wait command to get bus sync
message[0] = bus_byte;
message_position = 1;
} else if ((bus_byte & 0xF0) == 0x80) {
// Node bus message
message_position = 1;
message[0] = bus_byte;
} else {
// Invalid. We probably lost sync.
message_position = -1;
}
} else if (message_position == 1 && message[0] == 0x01) {
message[1] = bus_byte;
process_message();
message_position = -1;
} else if (message_position == 1) {
// Second byte - length
message[1] = bus_byte;
message_position = 2;
} else {
message[message_position] = bus_byte;
message_position += 1;
// Data + readback
if (message[0] == 0x80 && message[1] == 0x00) {
if (message_position - 3 >= 2048) {
// Message complete
process_message();
message_position = -1;
}
} else if (message_position - 3 == message[1]) {
// Message complete
process_message();
message_position = -1;
}
}
}
int stop_bridge(const struct termios old)
{
// Disable backlight
set_backlight(0);
// Disable Nodebus power
set_gpio_off(0x6B);
// Reset terminal mode
if (tcsetattr (fileno (stdin), TCSAFLUSH, &old) != 0)
{
printf("Error setting stdin attributes");
exit(-1);
}
printf("Resetting terminal mode and quitting.");
exit(0);
}
int main(int argc, char* argv[])
{
printf("MPF Spike Bridge!\n");
// Evaluate spike version from cmd line
if (argc >= 3 && strcmp(argv[2], "SPIKE2") == 0) {
spike_version = 2;
}
// Disable backlight
set_backlight(0);
// Enable Nodebus power
set_gpio_on(0x6B);
// Enable Amp
//set_gpio_on(0x6A);
// Open Spike bus
char *portname = "/dev/ttyS4";
if (spike_version == 2) {
portname = "/dev/ttymxc1";
}
fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
{
printf("Error opening %s: %s\n", portname, strerror(errno));
return -1;
}
// Set RTS and DTS on Node bus
int flags;
flags = TIOCM_RTS | TIOCM_DTR;
ioctl(fd, TIOCMBIS, &flags);
// Set speed of nodebus
set_interface_attribs (fd, B460800);
// Open local ports on cpu
char *spi_portname = "/dev/spi1";
if (spike_version == 2) {
spi_fd = fd; // HACK: needed for select
} else {
spi_fd = open (spi_portname, O_RDWR | O_NOCTTY | O_SYNC);
if (spi_fd < 0)
{
printf("Error opening %s: %s\n", spi_portname, strerror(errno));
return -1;
}
}
// Open stdin
int stdin_fd = 0;
setbuf(stdout, NULL);
setbuf(stdin, NULL);
speed_t serial_speed = (speed_t)B921600;
if (argc >= 2) {
if (strcmp(argv[1], "230400") == 0) {
serial_speed = (speed_t)B230400;
} else if (strcmp(argv[1], "460800") == 0) {
serial_speed = (speed_t)B460800;
} else if (strcmp(argv[1], "576000") == 0) {
serial_speed = (speed_t)B576000;
} else if (strcmp(argv[1], "921600") == 0) {
serial_speed = (speed_t)B921600;
} else if (strcmp(argv[1], "1000000") == 0) {
serial_speed = (speed_t)B1000000;
} else if (strcmp(argv[1], "1152000") == 0) {
serial_speed = (speed_t)B1152000;
} else if (strcmp(argv[1], "1500000") == 0) {
serial_speed = (speed_t)B1500000;
} else if (strcmp(argv[1], "2000000") == 0) {
serial_speed = (speed_t)B2000000;
} else if (strcmp(argv[1], "2500000") == 0) {
serial_speed = (speed_t)B2500000;
} else if (strcmp(argv[1], "3000000") == 0) {
serial_speed = (speed_t)B3000000;
} else if (strcmp(argv[1], "3500000") == 0) {
serial_speed = (speed_t)B3500000;
} else if (strcmp(argv[1], "4000000") == 0) {
serial_speed = (speed_t)B4000000;
} else {
serial_speed = 0;
}
}
struct termios old, new;
if (tcgetattr (fileno (stdin), &old) != 0)
return -1;
new = old;
// raw mode according to terminos(3) man page
new.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
new.c_oflag &= ~OPOST;
new.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
new.c_cflag &= ~(CSIZE | PARENB);
new.c_cflag |= CS8;
if (serial_speed) {
cfsetospeed(&new, serial_speed);
cfsetispeed(&new, serial_speed);
}
if (tcsetattr (fileno (stdin), TCSAFLUSH, &new) != 0)
{
printf("Error setting stdin attributes");
return -1;
}
// Loop variables
char buffer[1024];
int select_return;
unsigned int last_byte_received = time(NULL);
while (1) {
struct timeval tv = {2, 0};
fd_set set;
/* Initialize the file descriptor set. */
FD_ZERO (&set);
FD_SET (stdin_fd, &set);
FD_SET (fd, &set);
select_return = select(spi_fd + 1, &set, NULL, NULL, &tv);
int n;
// In case we ran into a timeout do exit.
if (select_return == 0) {
stop_bridge(old);
}
if (FD_ISSET(stdin_fd, &set)) {
last_byte_received = time(NULL);
n = read(stdin_fd, buffer, sizeof buffer);
// printf("Got %02X bytes from stdin\n", n);
int j = 0;
for (j = 0; j < n; j += 1)
{
process_byte(buffer[j]);
// printf("Sending: %02X\n", b);
}
} else if (FD_ISSET(fd, &set)) {
n = read(fd, buffer, sizeof buffer);
// printf("Got %02X bytes from ttyS4\n", n);
int j = 0;
for (j = 0; j < n; j++)
{
printf("%02X ", buffer[j]);
}
} else {
printf("Select failure\n");
}
if (last_byte_received + 5 < time(NULL)) {
stop_bridge(old);
}
}
return 0;
}