-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhub75.cpp
384 lines (329 loc) · 13.5 KB
/
hub75.cpp
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
#include "hub75.h"
/**
* PIO programs and rough architecture are based on hub75 example from pico-examples
* repo. To improve performance, DMA is used and redrawing is done while waiting
* for DMA or bit timings.
*
* Architecture / Flow of the display driver:
*
* Nested loops:
* outer is per frame and infinite
* inner per row and DISPLAY_SCAN iterations
* innermost per bit for color
*
*
* Per row loop:
* Per bit loop:
* Setup and trigger DMA to send pixel data for row to hub75_data PIO SM
*
* Check if simulation step is done by checking SIO FIFO for a magic number
* If so, set flag display_redraw
*
* Wait until DMA / PIO shifting is done
* Push out alignment pixels
*
* Pulse LAT and OE using hub75_row PIO SM
*
* Until hub75_row PIO SM is done, call display update routine
*
* Next bit, until full depth is done
* Next row, until frame is done
*
* If frame is done and display_flip is set:
* Swap addresses of front and back buffers
* Write magic number to SIO FIFO to signal next simulation step
*
* Next frame, forever
*
* Display update routine:
* Restartable, e.g. state is saved between runs
* Clear backbuf by copying over background image
* Draw particles
* Set display_flip flag
*
* Framebuffers are interleaved to maximize performance
* Given this Display:
* x->
* y 1234
* | 4567
* v 89AB
* CDEF
*
* The framebuffer would look something like this:
* 18293A4B
* 4C5D6E7F
*
* The DMA would first push out the 182... row and then the 4C5... row
* Even numbered Framebuffer indices are rows 0-15 and odd numbered indices are rows 16-31
*
* y-Coordinates are rows, x are columns
*
* Panel Colors:
*
* BGR -> 0x00BBGGRR
*/
static inline uint32_t gamma_correct_565_888(uint16_t pix) {
uint32_t r_gamma = pix & 0xf800u;
r_gamma *= r_gamma;
uint32_t g_gamma = pix & 0x07e0u;
g_gamma *= g_gamma;
uint32_t b_gamma = pix & 0x001fu;
b_gamma *= b_gamma;
return (b_gamma >> 2 << 16) | (g_gamma >> 14 << 8) | (r_gamma >> 24 << 0);
}
// Double-buffering
uint32_t display_buffers[2][DISPLAY_FRAMEBUFFER_SIZE];
uint32_t* display_front_buf = &display_buffers[0][0];
uint32_t* display_back_buf = &display_buffers[1][0];
const uint32_t* display_background = nullptr;
uint32_t display_wait = DISPLAY_WAIT_US;
uint32_t display_particlecount;
particle_t display_particles[SIM_MAX_PARTICLECOUNT];
PIO display_pio = pio0;
uint display_sm_data, display_sm_row;
uint display_offset_data, display_offset_row;
int display_dma_chan;
bool display_redraw = false;
bool display_flip = false;
int display_redraw_curidx = 0;
uint8_t display_framenum = 0;
void hub75_init() {
// Information for picotool
bi_decl(bi_3pins_with_names(
DISPLAY_DATAPINS_BASE, "HUB75 R0 Pin",
DISPLAY_DATAPINS_BASE+1, "HUB75 G0 Pin",
DISPLAY_DATAPINS_BASE+2, "HUB75 B0 Pin"));
bi_decl(bi_3pins_with_names(
DISPLAY_DATAPINS_BASE+3, "HUB75 R1 Pin",
DISPLAY_DATAPINS_BASE+4, "HUB75 G1 Pin",
DISPLAY_DATAPINS_BASE+5, "HUB75 B1 Pin"));
bi_decl(bi_4pins_with_names(
DISPLAY_ROWSEL_BASE, "HUB75 A Pin",
DISPLAY_ROWSEL_BASE+1, "HUB75 B Pin",
DISPLAY_ROWSEL_BASE+2, "HUB75 C Pin",
DISPLAY_ROWSEL_BASE+3, "HUB75 D Pin"
));
bi_decl(bi_3pins_with_names(
DISPLAY_CLKPIN, "HUB75 CLK Pin",
DISPLAY_STROBEPIN, "HUB75 LAT / STB Pin",
DISPLAY_OENPIN, "HUB75 OEn Pin"
));
// Initialize PIO
display_sm_data = pio_claim_unused_sm(display_pio, true);
display_sm_row = pio_claim_unused_sm(display_pio, true);
display_offset_data = pio_add_program(display_pio, &hub75_data_program);
display_offset_row = pio_add_program(display_pio, &hub75_row_program);
hub75_data_program_init(
display_pio,
display_sm_data,
display_offset_data,
DISPLAY_DATAPINS_BASE,
DISPLAY_CLKPIN
);
hub75_row_program_init(
display_pio,
display_sm_row,
display_offset_row,
DISPLAY_ROWSEL_BASE, DISPLAY_ROWSEL_COUNT,
DISPLAY_STROBEPIN
);
// Initialize DMA
display_dma_chan = dma_claim_unused_channel(true);
dma_channel_config c = dma_channel_get_default_config(display_dma_chan);
channel_config_set_transfer_data_size(&c, DMA_SIZE_32);
channel_config_set_read_increment(&c, true);
channel_config_set_write_increment(&c, false);
channel_config_set_dreq(&c, DREQ_PIO0_TX0+display_sm_data);
dma_channel_configure(
display_dma_chan,
&c,
&pio0_hw->txf[display_sm_data],
NULL, // Will be set later for each transfer
DISPLAY_SIZE*2, // Because we shift out two rows at once
false
);
}
[[noreturn]] void __not_in_flash_func(hub75_main)() {
puts("Hello from HUB75!");
DISPLAY_REDRAWSTATE redrawstate = DISPLAY_REDRAWSTATE_IDLE;
// Fill both framebuffers with a default pattern
for (int x = 0; x < DISPLAY_SIZE; ++x) {
for (int y = 0; y < DISPLAY_SIZE; ++y) {
uint32_t c = (x*8) << 16 | (y*8) << 8 | 16 << 0;
hub75_draw_pixel(display_front_buf, x, y, c);
hub75_draw_pixel(display_back_buf, x, y, c);
}
}
puts("HUB75 is done initializing framebuffers");
// Simulation waits until we are ready
multicore_fifo_push_blocking(DISPLAY_TRIGGER_SIMULATION_MAGIC_NUMBER);
while (true) {
// per-Frame loop
for (int row = 0; row < DISPLAY_SCAN; ++row) {
// per-Row loop
for (int bit = 8-DISPLAY_BITDEPTH; bit < 8; ++bit) {
// per-Bit level loop
// Set correct bit offset in data SM
hub75_data_set_shift(display_pio, display_sm_data, display_offset_data, bit);
// Start DMA to push out pixels
dma_channel_set_read_addr(display_dma_chan,
&display_front_buf[row*DISPLAY_SIZE*2],
true);
// Check SIO FIFO if new simulation data is available
if ( !display_redraw
&& !display_flip
&& multicore_fifo_rvalid()
&& multicore_fifo_pop_blocking() == DISPLAY_TRIGGER_REDRAW_MAGIC_NUMBER) {
// New data available, set flag
display_redraw = true;
}
// Wait for DMA completion to prevent dummy pixels being inserted at the wrong position
// We could try and redraw here as well, but the dma (and shifting) itself is quite fast
dma_channel_wait_for_finish_blocking(display_dma_chan);
// Push two dummy pixels to properly synchronize
// Also acts as a synchronization between row and data
pio_sm_put_blocking(display_pio, display_sm_data, 0);
pio_sm_put_blocking(display_pio, display_sm_data, 0);
// Just in case the flags were still set
hub75_pio_sm_clearstall();
// Continue redrawing if necessary
// Only try to redraw if the bit level is high enough, not worth it otherwise
if (bit > 4 && display_redraw) {
redrawstate = hub75_update(redrawstate);
if (redrawstate == DISPLAY_REDRAWSTATE_IDLE) {
// Done redrawing, mark readiness for flipping at the end of the frame
display_redraw = false;
display_flip = true;
}
}
// Finish waiting if redraw was quick or not necessary
// Also clears FIFO stall flags
hub75_wait_tx_stall(display_pio, display_sm_data);
hub75_wait_tx_stall(display_pio, display_sm_row);
// Pulse LAT and OE using PIO
// TODO: check scaling of delay
pio_sm_put_blocking(display_pio,
display_sm_row,
row | ((100u * (1u << bit)) << DISPLAY_ROWSEL_COUNT)
);
}
}
//if (display_redraw) {
// printf("REDRAW Carry\n");
//}
// Ready to flip
if (display_flip) {
//printf("Flip\n");
display_flip = false;
// Swap buffers
uint32_t* tmp = display_back_buf;
display_back_buf = display_front_buf;
display_front_buf = tmp;
display_framenum++;
// Send magic number back over FIFO to signal that the simulation buffer
// can be reused
// Keeps both cores synced
if (!multicore_fifo_wready()) {
// Should never happen, panic
panic("Tried to signal finished redraw, but FIFO was full!\n");
}
multicore_fifo_push_blocking(DISPLAY_TRIGGER_SIMULATION_MAGIC_NUMBER);
}
if (display_wait > 0) {
sleep_us(display_wait);
}
}
}
DISPLAY_REDRAWSTATE __not_in_flash_func(hub75_update)(DISPLAY_REDRAWSTATE state) {
if (state == DISPLAY_REDRAWSTATE_IDLE) {
// Fresh start, start by clearing
state = DISPLAY_REDRAWSTATE_CLEAR;
display_redraw_curidx = 0;
}
while (hub75_pio_sm_stalled() && state != DISPLAY_REDRAWSTATE_IDLE) {
// Redraw something as long as we would have to wait anyway
// Requires that the loop contents be somewhat fast to minimize overshoot
if (state == DISPLAY_REDRAWSTATE_CLEAR) {
// Copy one row per iteration
if (display_background == nullptr) {
state = DISPLAY_REDRAWSTATE_PARTICLES;
display_redraw_curidx = 0;
continue;
}
// Note that while we could use hub75_draw_pixel() here, it would be
// far less efficient. Since we always copy a row at a time, we can
// pre-calculate the starting address and thus save us a branch on every pixel
int startaddr = display_redraw_curidx*DISPLAY_SIZE*2;
if (display_redraw_curidx >= DISPLAY_SCAN) {
// Interleave-shifted row
startaddr = (display_redraw_curidx-DISPLAY_SCAN)*DISPLAY_SIZE*2+1;
}
for (int x = 0; x < DISPLAY_SIZE; x++) {
display_back_buf[startaddr+2*x] = display_background[display_redraw_curidx*DISPLAY_SIZE+x];
}
display_redraw_curidx++;
if (display_redraw_curidx >= DISPLAY_SIZE) {
if (display_particlecount > 0) {
// Only proceed to draw particles if there are any
state = DISPLAY_REDRAWSTATE_PARTICLES;
} else {
state = DISPLAY_REDRAWSTATE_IDLE;
}
display_redraw_curidx = 0;
}
} else if (state == DISPLAY_REDRAWSTATE_PARTICLES) {
// Draw one or eight particles per iteration
if (display_redraw_curidx + 8 < display_particlecount) {
// If there are more than eight particles remaining, draw them at once
// Reduces overhead from loop, since the drawing itself is quite fast
for (int i = 0; i < 8; ++i) {
hub75_draw_pixel(display_back_buf,
display_particles[display_redraw_curidx].x / 256,
display_particles[display_redraw_curidx].y / 256,
display_particles[display_redraw_curidx].color
);
display_redraw_curidx++;
}
} else {
// Not enough particles remaining, draw them one by one
hub75_draw_pixel(display_back_buf,
display_particles[display_redraw_curidx].x / 256,
display_particles[display_redraw_curidx].y / 256,
display_particles[display_redraw_curidx].color
);
display_redraw_curidx++;
}
if (display_redraw_curidx >= display_particlecount) {
// We're done, loop will break due to state
state = DISPLAY_REDRAWSTATE_IDLE;
display_redraw_curidx = 0;
}
} else {
// Fallback, should not normally happen
state = DISPLAY_REDRAWSTATE_IDLE;
}
}
return state;
}
bool hub75_pio_sm_stalled() {
// Checks whether the state machines are stalled
// We currently only check the row SM, since it will take longer for higher
// bit levels
uint32_t txstall_mask = 1u << (PIO_FDEBUG_TXSTALL_LSB + display_sm_row);
//txstall_mask |= 1u << (PIO_FDEBUG_TXSTALL_LSB + display_sm_data);
return !(display_pio->fdebug & txstall_mask);
}
void hub75_pio_sm_clearstall() {
uint32_t txstall_mask = 1u << (PIO_FDEBUG_TXSTALL_LSB + display_sm_row);
txstall_mask |= 1u << (PIO_FDEBUG_TXSTALL_LSB + display_sm_data);
display_pio->fdebug = txstall_mask;
}
static inline void __not_in_flash_func(hub75_draw_pixel)(uint32_t* buf, uint32_t x, uint32_t y, uint32_t color) {
if (y<DISPLAY_SCAN) {
// Normal, store pixel
buf[y*DISPLAY_SIZE*2+2*x] = color;
} else {
// Above display scan, store as interleaved
buf[(y-DISPLAY_SCAN)*DISPLAY_SIZE*2+2*x+1] = color;
}
}