-
Notifications
You must be signed in to change notification settings - Fork 1
/
digital_rgb_display.c
358 lines (312 loc) · 10 KB
/
digital_rgb_display.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
//
// Digital RGB Display with fx2pipe
// 27-Mar-2021 by Minatsu (@tksm372)
//
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 720
#define DW 640
#define DH 200
#define MGL_IMPLEMENTATION
#include "MGL_dispmanx.h"
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <libusb.h>
#include <assert.h>
#define BIT_VSYNC 4
#define BIT_HSYNC 3
#define BIT_R 2
#define BIT_G 1
#define BIT_B 0
// Built-in firmware hex strings.
static char *firmware[] = {
#include "firmware/slave_sync_8.inc"
NULL};
#define READ() usb_read()
//======================================================================
// USB
//======================================================================
#define VID 0x04b4
#define PID 0x8613
#define IN_EP (LIBUSB_ENDPOINT_IN | 6)
#define RX_SIZE (16 * 1024 * 4)
#define XFR_NUM 64
#define READ_SIZE (RX_SIZE * XFR_NUM)
static uint8_t buf[XFR_NUM][RX_SIZE];
static libusb_device_handle *usb_handle = NULL;
static struct libusb_transfer *xfr[XFR_NUM];
static volatile int usb_run_flag = 1;
//----------------------------------------------------------------------
// USB write RAM
//----------------------------------------------------------------------
#define USB_WRITE_RAM_MAX_SIZE 64
int usb_write_ram(int addr, uint8_t *dat, int size) {
assert(usb_handle != NULL);
for (int i = 0; i < size; i += USB_WRITE_RAM_MAX_SIZE) {
int len = (size - i > USB_WRITE_RAM_MAX_SIZE) ? USB_WRITE_RAM_MAX_SIZE : size - i;
int ret = libusb_control_transfer(usb_handle, LIBUSB_REQUEST_TYPE_VENDOR, 0xa0, addr + i, 0, dat + i, len, 1000);
if (ret < 0) {
fprintf(stderr, "USB: Write Ram at %04x (len %d) failed.\n", addr + i, len);
return -1;
}
}
return 0;
}
//----------------------------------------------------------------------
// USB load firmware
//----------------------------------------------------------------------
#define FIRMWARE_MAX_SIZE_PER_LINE 64
static uint8_t firmware_dat[FIRMWARE_MAX_SIZE_PER_LINE];
int usb_load_firmware(char *firmware[]) {
int ret;
// Take the CPU into RESET
uint8_t dat = 1;
ret = usb_write_ram(0xe600, &dat, sizeof(dat));
if (ret < 0) {
return -1;
}
// Load firmware
int size, addr, record_type, tmp_dat;
for (int i = 0; firmware[i] != NULL; i++) {
char *p = firmware[i] + 1;
// Extract size
ret = sscanf(p, "%2x", &size);
assert(ret != 0);
assert(size <= FIRMWARE_MAX_SIZE_PER_LINE);
p += 2;
// Extract addr
ret = sscanf(p, "%4x", &addr);
assert(ret != 0);
p += 4;
// Extract record type
ret = sscanf(p, "%2x", &record_type);
assert(ret != 0);
p += 2;
// Write program to EZ-USB's RAM (record_type==0).
if (record_type == 0) {
for (int j = 0; j < size; j++) {
ret = sscanf(p, "%2x", &tmp_dat);
firmware_dat[j] = tmp_dat & 0xff;
assert(ret != 0);
p += 2;
}
ret = usb_write_ram(addr, firmware_dat, size);
if (ret < 0) {
return -1;
}
}
}
// Take the CPU out of RESET (run)
dat = 0;
ret = usb_write_ram(0xe600, &dat, sizeof(dat));
if (ret < 0) {
return -1;
}
return 0;
}
//----------------------------------------------------------------------
// Close USB
//----------------------------------------------------------------------
void usb_close() {
for (int i = 0; i < XFR_NUM; i++) {
if (xfr[i] != NULL) {
libusb_cancel_transfer(xfr[i]);
}
}
// Stop USB thread
usb_run_flag = 0;
if (usb_handle != NULL) {
libusb_close(usb_handle);
usb_handle = NULL;
}
for (int i = 0; i < XFR_NUM; i++) {
if (xfr[i] != NULL) {
libusb_free_transfer(xfr[i]);
}
}
}
//----------------------------------------------------------------------
// USB callback for bulk-in transfer
//----------------------------------------------------------------------
static pthread_cond_t usb_cond;
static pthread_mutex_t usb_mtx;
static volatile uint64_t usb_received_size = 0;
static pthread_mutex_t usb_received_size_mtx;
static volatile int usb_trans_pos = 0;
static int usb_closed_flag = 0;
void usb_callback(struct libusb_transfer *xfr) {
switch (xfr->status) {
case LIBUSB_TRANSFER_COMPLETED:
pthread_mutex_lock(&usb_received_size_mtx);
usb_received_size += xfr->actual_length;
pthread_mutex_unlock(&usb_received_size_mtx);
break;
case LIBUSB_TRANSFER_ERROR:
fprintf(stderr, "USB: transfer error.\n");
break;
case LIBUSB_TRANSFER_TIMED_OUT:
fprintf(stderr, "USB: transfer timed out.\n");
break;
case LIBUSB_TRANSFER_OVERFLOW:
fprintf(stderr, "USB: transfer overflow.\n");
break;
case LIBUSB_TRANSFER_CANCELLED:
case LIBUSB_TRANSFER_NO_DEVICE:
default:
return;
}
usb_trans_pos = RX_SIZE * (int)xfr->user_data;
pthread_cond_signal(&usb_cond);
if (usb_run_flag) {
if (libusb_submit_transfer(xfr) < 0) {
fprintf(stderr, "USB: libusb_submit_transfer failed.\n");
MGL_Quit();
}
}
}
//----------------------------------------------------------------------
// USB thread for bulk-in transfer
//----------------------------------------------------------------------
static pthread_t usb_th;
struct timeval tv = {0, 1};
void *usb_run(void *arg) {
puts("USB: Start receiving VH-RGB signals.");
// Submit USB transfers
for (int i = 0; i < XFR_NUM; i++) {
libusb_fill_bulk_transfer(xfr[i], usb_handle,
IN_EP, // Endpoint ID
buf[i], RX_SIZE, usb_callback, (void *)i, 0 /* no timeout */);
if (libusb_submit_transfer(xfr[i]) < 0) {
fprintf(stderr, "USB: libusb_submit_transfer failed.\n");
MGL_Quit();
}
}
// Waiting transfer completion repeatedly
int64_t last = timemillis();
int64_t cur;
int64_t msec;
float avg = 0;
while (usb_run_flag) {
libusb_handle_events_completed(NULL, &usb_closed_flag);
cur = timemillis();
msec = cur - last;
if (msec > 1000) {
pthread_mutex_lock(&usb_received_size_mtx);
int size = usb_received_size;
usb_received_size = 0;
pthread_mutex_unlock(&usb_received_size_mtx);
float mbps = size / ((cur - last) / 1000.0) / 1024.0 / 1024.0;
avg = (!avg) ? mbps : avg * 0.95 + mbps * 0.05;
printf("Receiving at %.3f MBps (Avg. %.3f Mbps)\r", mbps, avg);
last = cur;
}
}
puts("USB: Thread finished.");
}
//----------------------------------------------------------------------
// Read one "000VHRGB" signal byte via USB
//----------------------------------------------------------------------
int read_pos = 0;
inline static uint8_t usb_read() {
while (read_pos == usb_trans_pos) {
pthread_cond_wait(&usb_cond, &usb_mtx);
}
uint8_t dat = buf[0][read_pos++];
read_pos %= READ_SIZE;
return dat;
}
//======================================================================
// Main
//======================================================================
int main(int argc, char *argv[]) {
setvbuf(stdout, (char *)NULL, _IONBF, 0);
int ret;
// Initialize USB
ret = libusb_init(NULL);
assert(ret == 0);
usb_handle = libusb_open_device_with_vid_pid(NULL, VID, PID);
assert(usb_handle != NULL);
ret = libusb_set_auto_detach_kernel_driver(usb_handle, 1);
assert(ret == 0);
ret = libusb_claim_interface(usb_handle, 0);
assert(ret == 0);
ret = libusb_set_interface_alt_setting(usb_handle, 0, 1);
assert(ret == 0);
// load firmware
printf("Main: Firmware download...");
if (usb_load_firmware(firmware) >= 0) {
puts("finished.");
} else {
puts("failed.");
return -1;
}
// Allocating transfer request structures
ZEROFILL(xfr);
for (int i = 0; i < XFR_NUM; i++) {
xfr[i] = libusb_alloc_transfer(0);
if (xfr[i] == NULL) {
usb_close();
return -1;
}
}
if (pthread_create(&usb_th, NULL, usb_run, NULL) != 0) {
perror("Main: Failed to start USB thread");
return -1;
}
// setvbuf(fp, buf, _IOFBF, 10240);
MGL_Start();
uint32_t d = 0;
uint32_t vmask = 1 << BIT_VSYNC;
uint32_t hmask = 1 << BIT_HSYNC;
uint32_t vhmask = vmask | hmask;
col_t col[8] = {0, WEB_RGB(0, 0, 5), WEB_RGB(0, 5, 0), WEB_RGB(0, 5, 5), WEB_RGB(5, 0, 0), WEB_RGB(5, 0, 5), WEB_RGB(5, 5, 0), WEB_RGB(5, 5, 5)};
col_t *p;
while (1) {
// Wait V-Sync
while (READ() & vmask)
; // wait untill low
while (!(READ() & vmask))
; // wait untill hi
// Skip V-Sync back porch
for (int i = 0; i < 36; i++) {
while (READ() & hmask)
; // wait untill low
while (!(READ() & hmask))
; // wait untill hi
}
int x, y;
for (y = 0; y < DH; y++) {
// Wait H-Sync
while (READ() & hmask)
; // wait untill low
while (!(READ() & hmask))
; // wait untill hi
// Skip H-Sync back porch
for (x = 0; x < 132 - 1; x++) {
READ();
}
p = &vram[y * GRP_W];
for (x = 0; x < DW; x++) {
d = READ();
if ((~d) & vhmask) {
y = DH; // Sync is lost, skip this frame
break;
}
*p++ = col[d & 7];
}
}
}
}
void finalize() {
puts("\nMain: Finalizing...");
usb_close();
puts("Main: USB device closed.");
usb_closed_flag = 1;
if (pthread_join(usb_th, NULL) != 0) {
perror("Main: Failed to join USB thread.");
} else {
puts("Main: USB thread joined.");
}
libusb_exit(NULL);
MGL_Quit();
}