forked from osresearch/LEDscape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledscape.c
390 lines (335 loc) · 8.37 KB
/
ledscape.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
/** \file
* Userspace interface to the WS281x LED strip driver.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
#include <errno.h>
#include <unistd.h>
#include "ledscape.h"
#include "pru.h"
#define CONFIG_LED_MATRIX
/** GPIO pins used by the LEDscape.
*
* The device tree should handle this configuration for us, but it
* seems horribly broken and won't configure these pins as outputs.
* So instead we have to repeat them here as well.
*
* If these are changed, be sure to check the mappings in
* ws281x.p!
*
* The RGB matrix uses a subset of these pins, although with
* the HDMI disabled it might use quite a few more for the four
* output version.
*
* \todo: Find a way to unify this with the defines in the .p file
*/
static const uint8_t gpios0[] = {
23, 27, 22, 10, 9, 8, 26, 11, 30, 31, 5, 3, 20, 4, 2, 14, 7
};
static const uint8_t gpios1[] = {
13, 15, 12, 14, 29, 16, 17, 28, 18, 19,
};
static const uint8_t gpios2[] = {
2, 5, 22, 23, 14, 12, 10, 8, 6, 3, 4, 1, 24, 25, 17, 16, 15, 13, 11, 9, 7,
};
static const uint8_t gpios3[] = {
21, 19, 15, 14, 17, 16
};
#define ARRAY_COUNT(a) ((sizeof(a) / sizeof(*a)))
/** Command structure shared with the PRU.
*
* This is mapped into the PRU data RAM and points to the
* frame buffer in the shared DDR segment.
*
* Changing this requires changes in ws281x.p
*/
typedef struct
{
// in the DDR shared with the PRU
uintptr_t pixels_dma;
// Length in pixels of the longest LED strip.
unsigned num_pixels;
// write 1 to start, 0xFF to abort. will be cleared when started
volatile unsigned command;
// will have a non-zero response written when done
volatile unsigned response;
} __attribute__((__packed__)) ws281x_command_t;
typedef struct
{
uint32_t x_offset;
uint32_t y_offset;
} led_matrix_t;
#define NUM_MATRIX 16
typedef struct
{
uint32_t matrix_width; // of a full chain
uint32_t matrix_height; // number of rows per-output (8 or 16)
led_matrix_t matrix[NUM_MATRIX];
} led_matrix_config_t;
struct ledscape
{
ws281x_command_t * ws281x;
pru_t * pru;
unsigned width;
unsigned height;
unsigned frame_size;
led_matrix_config_t * matrix;
};
#if 0
/** Retrieve one of the two frame buffers. */
ledscape_frame_t *
ledscape_frame(
ledscape_t * const leds,
unsigned int frame
)
{
if (frame >= 2)
return NULL;
return (ledscape_frame_t*)((uint8_t*) leds->pru->ddr + leds->frame_size * frame);
}
#endif
static uint8_t
bright_map(
uint8_t val
)
{
return val;
}
static uint8_t *
ledscape_remap(
ledscape_t * const leds,
uint8_t * const frame,
unsigned x,
unsigned y
)
{
#define CONFIG_ZIGZAG
#ifdef CONFIG_ZIGZAG
(void) leds;
// each panel is 16x8
// vertical panel number is y % 8 (which output line)
// horizontal panel number is y % (16*8)
// if y % 2 == 1, map backwards
const unsigned panel_width = 16;
const unsigned panel_height = 8;
unsigned panel_num = x / panel_width;
unsigned output_line = y / panel_height;
unsigned panel_x = x % panel_width;
unsigned panel_y = y % panel_height;
unsigned panel_offset = panel_y * panel_width;
// the even lines are forwards, the odd lines go backwards
if (panel_y % 2 == 0)
{
panel_offset += panel_x;
} else {
panel_offset += panel_width - panel_x - 1;
}
return &frame[(panel_num*128 + panel_offset)*48*3 + output_line];
#else
return &frame[x*48*3 + y];
#endif
}
/** Translate the RGBA buffer to the correct output type and
* initiate the transfer of a frame to the LED strips.
*
* Matrix drivers shuffle to have consecutive bits, ws281x do bit slicing.
*/
void
ledscape_draw(
ledscape_t * const leds,
const void * const buffer
)
{
static unsigned frame = 0;
const uint32_t * const in = buffer;
uint8_t * const out = leds->pru->ddr + leds->frame_size * frame;
#ifdef CONFIG_LED_MATRIX
// matrix packed is:
// this way the PRU can read all sixteen output pixels in
// one LBBO and clock them out.
// there is an array of NUM_MATRIX output coordinates (one for each of
// the sixteen drivers).
for (unsigned i = 0 ; i < NUM_MATRIX ; i++)
{
const led_matrix_t * const m = &leds->matrix->matrix[i];
for (uint32_t y = 0 ; y < leds->matrix->matrix_height ; y++)
{
const uint32_t * const in_row
= &in[(y+m->y_offset) * leds->width];
uint8_t * const out_row
= &out[y * leds->matrix->matrix_width * 3 * NUM_MATRIX];
for (uint32_t x = 0 ; x < leds->matrix->matrix_width ; x++)
{
const uint8_t * const rgb = (const void*) &in_row[x + m->x_offset];
uint8_t * const out_rgb = &out_row[(i + x * NUM_MATRIX)*3];
out_rgb[0] = bright_map(rgb[0]);
out_rgb[1] = bright_map(rgb[1]);
out_rgb[2] = bright_map(rgb[2]);
}
}
}
leds->ws281x->pixels_dma = leds->pru->ddr_addr + leds->frame_size * frame;
frame = (frame + 1) & 1;
#else
// Translate the RGBA frame into G R B, sliced by color
// only 48 outputs currently supported
const unsigned pru_stride = 48;
for (unsigned y = 0 ; y < leds->height ; y++)
{
const uint32_t * const row_in = &in[y*leds->width];
for (unsigned x = 0 ; x < leds->width ; x++)
{
uint8_t * const row_out
= ledscape_remap(leds, out, x, y);
const uint32_t p = row_in[x];
row_out[0*pru_stride] = (p >> 8) & 0xFF; // green
row_out[1*pru_stride] = (p >> 0) & 0xFF; // red
row_out[2*pru_stride] = (p >> 16) & 0xFF; // blue
}
}
// Wait for any current command to have been acknowledged
while (leds->ws281x->command)
;
// Update the pixel data and send the start
leds->ws281x->pixels_dma
= leds->pru->ddr_addr + leds->frame_size * frame;
frame = (frame + 1) & 1;
// Send the start command
leds->ws281x->command = 1;
#endif
}
/** Wait for the current frame to finish transfering to the strips.
* \returns a token indicating the response code.
*/
uint32_t
ledscape_wait(
ledscape_t * const leds
)
{
while (1)
{
uint32_t response = leds->ws281x->response;
if (!response)
continue;
leds->ws281x->response = 0;
return response;
}
}
ledscape_t *
ledscape_init(
unsigned width,
unsigned height
)
{
pru_t * const pru = pru_init(0);
#ifdef CONFIG_LED_MATRIX
const size_t frame_size = 16 * 8 * width * 3; //LEDSCAPE_NUM_STRIPS * 4;
#else
const size_t frame_size = 48 * width * 8 * 3;
#endif
#if 0
if (2 *frame_size > pru->ddr_size)
die("Pixel data needs at least 2 * %zu, only %zu in DDR\n",
frame_size,
pru->ddr_size
);
#endif
ledscape_t * const leds = calloc(1, sizeof(*leds));
*leds = (ledscape_t) {
.pru = pru,
.width = width,
.height = height,
.ws281x = pru->data_ram,
.frame_size = frame_size,
.matrix = calloc(sizeof(*leds->matrix), 1),
};
#ifdef CONFIG_LED_MATRIX
*(leds->matrix) = (led_matrix_config_t) {
.matrix_width = 128,
.matrix_height = 8,
.matrix = {
{ 0, 0 },
{ 0, 8 },
{ 0, 16 },
{ 0, 24 },
{ 0, 32 },
{ 0, 40 },
{ 0, 48 },
{ 0, 56 },
{ 128, 0 },
{ 128, 8 },
{ 128, 16 },
{ 128, 24 },
{ 128, 32 },
{ 128, 40 },
{ 128, 48 },
{ 128, 56 },
},
};
*(leds->ws281x) = (ws281x_command_t) {
.pixels_dma = 0, // will be set in draw routine
.num_pixels = (leds->matrix->matrix_width * 3) * 16,
.command = 0,
.response = 0,
};
#else
// LED strips, not matrix output
*(leds->ws281x) = (ws281x_command_t) {
.pixels_dma = 0, // will be set in draw routine
.num_pixels = width * 8, // panel height
.command = 0,
.response = 0,
};
#endif
printf("%d\n", leds->ws281x->num_pixels);
// Configure all of our output pins.
for (unsigned i = 0 ; i < ARRAY_COUNT(gpios0) ; i++)
pru_gpio(0, gpios0[i], 1, 0);
for (unsigned i = 0 ; i < ARRAY_COUNT(gpios1) ; i++)
pru_gpio(1, gpios1[i], 1, 0);
for (unsigned i = 0 ; i < ARRAY_COUNT(gpios2) ; i++)
pru_gpio(2, gpios2[i], 1, 0);
for (unsigned i = 0 ; i < ARRAY_COUNT(gpios3) ; i++)
pru_gpio(3, gpios3[i], 1, 0);
// Initiate the PRU program
#ifdef CONFIG_LED_MATRIX
pru_exec(pru, "./matrix.bin");
#else
pru_exec(pru, "./ws281x.bin");
#endif
// Watch for a done response that indicates a proper startup
// \todo timeout if it fails
printf("waiting for response\n");
while (!leds->ws281x->response)
;
printf("got response\n");
return leds;
}
void
ledscape_close(
ledscape_t * const leds
)
{
// Signal a halt command
leds->ws281x->command = 0xFF;
pru_close(leds->pru);
}
void
ledscape_set_color(
ledscape_frame_t * const frame,
uint8_t strip,
uint8_t pixel,
uint8_t r,
uint8_t g,
uint8_t b
)
{
ledscape_pixel_t * const p = &frame[pixel].strip[strip];
p->r = r;
p->g = g;
p->b = b;
}