forked from PaulStoffregen/OctoWS2811
-
Notifications
You must be signed in to change notification settings - Fork 5
/
OctoSK6812.cpp
executable file
·467 lines (429 loc) · 15 KB
/
OctoSK6812.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
OctoSK6812 is an update to allow for usage with SK6812 LEDs by Tenkai Kariya 2/12/17
Original modificaitons by made by Mackenzie Hauck nocduro.ca
https://hackaday.io/project/11850-led-light-rod/log/39905-modifying-octows2811-to-work-with-rgbw-led-strips
OctoWS2811 - High Performance WS2811 LED Display Library
http://www.pjrc.com/teensy/td_libs_OctoWS2811.html
Copyright (c) 2013 Paul Stoffregen, PJRC.COM, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <string.h>
#include "OctoSK6812.h"
uint16_t OctoSK6812::stripLen;
uint8_t OctoSK6812::pixelBits;
void * OctoSK6812::frameBuffer;
void * OctoSK6812::drawBuffer;
uint8_t OctoSK6812::params;
DMAChannel OctoSK6812::dma1;
DMAChannel OctoSK6812::dma2;
DMAChannel OctoSK6812::dma3;
static uint8_t ones = 0xFF;
static volatile uint8_t update_in_progress = 0;
static uint32_t update_completed_at = 0;
OctoSK6812::OctoSK6812(uint32_t numPerStrip, void *frameBuf, void *drawBuf, uint8_t config)
{
stripLen = numPerStrip;
frameBuffer = frameBuf;
drawBuffer = drawBuf;
params = config;
uint8_t stripType = config & 0x07;
switch (stripType) {
case SK6812_RGBW:
case SK6812_GRBW:
// if RGBW strips we need 32 bits per led
pixelBits = 32;
break;
default:
pixelBits = 24;
break;
}
}
// Waveform timing: these set the high time for a 0 and 1 bit, as a fraction of
// the total 800 kHz or 400 kHz clock cycle. The scale is 0 to 255. The Worldsemi
// datasheet seems T1H should be 600 ns of a 1250 ns cycle, or 48%. That may
// erroneous information? Other sources reason the chip actually samples the
// line close to the center of each bit time, so T1H should be 80% if TOH is 20%.
// The chips appear to work based on a simple one-shot delay triggered by the
// rising edge. At least 1 chip tested retransmits 0 as a 330 ns pulse (26%) and
// a 1 as a 660 ns pulse (53%). Perhaps it's actually sampling near 500 ns?
// There doesn't seem to be any advantage to making T1H less, as long as there
// is sufficient low time before the end of the cycle, so the next rising edge
// can be detected. T0H has been lengthened slightly, because the pulse can
// narrow if the DMA controller has extra latency during bus arbitration. If you
// have an insight about tuning these parameters AND you have actually tested on
// real LED strips, please contact [email protected]. Please do not email based only
// on reading the datasheets and purely theoretical analysis.
#define SK6812_TIMING_T0H 60
#define SK6812_TIMING_T1H 176
// Discussion about timing and flicker & color shift issues:
// http://forum.pjrc.com/threads/23877-WS2812B-compatible-with-OctoWS2811-library?p=38190&viewfull=1#post38190
void OctoSK6812::begin(void)
{
uint32_t bufsize, frequency;
bufsize = stripLen * pixelBits;
// set up the buffers
memset(frameBuffer, 0, bufsize);
if (drawBuffer) {
memset(drawBuffer, 0, bufsize);
} else {
drawBuffer = frameBuffer;
}
// configure the 8 output pins
GPIOD_PCOR = 0xFF;
pinMode(2, OUTPUT); // strip #1
pinMode(14, OUTPUT); // strip #2
pinMode(7, OUTPUT); // strip #3
pinMode(8, OUTPUT); // strip #4
pinMode(6, OUTPUT); // strip #5
pinMode(20, OUTPUT); // strip #6
pinMode(21, OUTPUT); // strip #7
pinMode(5, OUTPUT); // strip #8
// create the two waveforms for WS2811 low and high bits
switch (params & 0xF0) {
case WS2811_400kHz:
frequency = 400000;
break;
case WS2811_800kHz:
frequency = 800000;
break;
case WS2813_800kHz:
frequency = 800000;
break;
default:
frequency = 800000;
}
#if defined(__MK20DX128__)
FTM1_SC = 0;
FTM1_CNT = 0;
uint32_t mod = (F_BUS + frequency / 2) / frequency;
FTM1_MOD = mod - 1;
FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0);
FTM1_C0SC = 0x69;
FTM1_C1SC = 0x69;
FTM1_C0V = (mod * SK6812_TIMING_T0H) >> 8;
FTM1_C1V = (mod * SK6812_TIMING_T1H) >> 8;
// pin 16 triggers DMA(port B) on rising edge
CORE_PIN16_CONFIG = PORT_PCR_IRQC(1)|PORT_PCR_MUX(3);
//CORE_PIN4_CONFIG = PORT_PCR_MUX(3); // testing only
#elif defined(__MK20DX256__)
FTM2_SC = 0;
FTM2_CNT = 0;
uint32_t mod = (F_BUS + frequency / 2) / frequency;
FTM2_MOD = mod - 1;
FTM2_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0);
FTM2_C0SC = 0x69;
FTM2_C1SC = 0x69;
FTM2_C0V = (mod * SK6812_TIMING_T0H) >> 8;
FTM2_C1V = (mod * SK6812_TIMING_T1H) >> 8;
// pin 32 is FTM2_CH0, PTB18, triggers DMA(port B) on rising edge
// pin 25 is FTM2_CH1, PTB19
CORE_PIN32_CONFIG = PORT_PCR_IRQC(1)|PORT_PCR_MUX(3);
//CORE_PIN25_CONFIG = PORT_PCR_MUX(3); // testing only
#elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
FTM2_SC = 0;
FTM2_CNT = 0;
uint32_t mod = (F_BUS + frequency / 2) / frequency;
FTM2_MOD = mod - 1;
FTM2_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0);
FTM2_C0SC = 0x69;
FTM2_C1SC = 0x69;
FTM2_C0V = (mod * SK6812_TIMING_T0H) >> 8;
FTM2_C1V = (mod * SK6812_TIMING_T1H) >> 8;
// FTM2_CH0, PTA10 (not connected), triggers DMA(port A) on rising edge
PORTA_PCR10 = PORT_PCR_IRQC(1)|PORT_PCR_MUX(3);
#elif defined(__MKL26Z64__)
FTM2_SC = 0;
FTM2_CNT = 0;
uint32_t mod = F_CPU / frequency;
FTM2_MOD = mod - 1;
FTM2_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0);
FTM2_C0SC = FTM_CSC_CHF | FTM_CSC_MSB | FTM_CSC_ELSB;
FTM2_C1SC = FTM_CSC_CHF | FTM_CSC_MSB | FTM_CSC_ELSB;
TPM2_C0V = mod - ((mod * SK6812_TIMING_T1H) >> 8);
TPM2_C1V = mod - ((mod * SK6812_TIMING_T1H) >> 8) + ((mod * SK6812_TIMING_T0H) >> 8);
#endif
// DMA channel #1 sets WS2811 high at the beginning of each cycle
dma1.source(ones);
dma1.destination(GPIOD_PSOR);
dma1.transferSize(1);
dma1.transferCount(bufsize);
dma1.disableOnCompletion();
// DMA channel #2 writes the pixel data at 23% of the cycle
dma2.sourceBuffer((uint8_t *)frameBuffer, bufsize);
dma2.destination(GPIOD_PDOR);
dma2.transferSize(1);
dma2.transferCount(bufsize);
dma2.disableOnCompletion();
// DMA channel #3 clear all the pins low at 69% of the cycle
dma3.source(ones);
dma3.destination(GPIOD_PCOR);
dma3.transferSize(1);
dma3.transferCount(bufsize);
dma3.disableOnCompletion();
dma3.interruptAtCompletion();
#if defined(__MK20DX128__)
// route the edge detect interrupts to trigger the 3 channels
dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_PORTB);
dma2.triggerAtHardwareEvent(DMAMUX_SOURCE_FTM1_CH0);
dma3.triggerAtHardwareEvent(DMAMUX_SOURCE_FTM1_CH1);
DMAPriorityOrder(dma3, dma2, dma1);
#elif defined(__MK20DX256__)
// route the edge detect interrupts to trigger the 3 channels
dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_PORTB);
dma2.triggerAtHardwareEvent(DMAMUX_SOURCE_FTM2_CH0);
dma3.triggerAtHardwareEvent(DMAMUX_SOURCE_FTM2_CH1);
DMAPriorityOrder(dma3, dma2, dma1);
#elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
// route the edge detect interrupts to trigger the 3 channels
dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_PORTA);
dma2.triggerAtHardwareEvent(DMAMUX_SOURCE_FTM2_CH0);
dma3.triggerAtHardwareEvent(DMAMUX_SOURCE_FTM2_CH1);
DMAPriorityOrder(dma3, dma2, dma1);
#elif defined(__MKL26Z64__)
// route the timer interrupts to trigger the 3 channels
dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_TPM2_CH0);
dma2.triggerAtHardwareEvent(DMAMUX_SOURCE_TPM2_CH1);
dma3.triggerAtHardwareEvent(DMAMUX_SOURCE_FTM2_OV);
#endif
// enable a done interrupts when channel #3 completes
dma3.attachInterrupt(isr);
//pinMode(9, OUTPUT); // testing: oscilloscope trigger
}
void OctoSK6812::isr(void)
{
//digitalWriteFast(9, HIGH);
//Serial1.print(".");
//Serial1.println(dma3.CFG->DCR, HEX);
//Serial1.print(dma3.CFG->DSR_BCR > 24, HEX);
dma3.clearInterrupt();
#if defined(__MKL26Z64__)
GPIOD_PCOR = 0xFF;
#endif
//Serial1.print("*");
update_completed_at = micros();
update_in_progress = 0;
//digitalWriteFast(9, LOW);
}
int OctoSK6812::busy(void)
{
if (update_in_progress) return 1;
// busy for 50 (or 300 for ws2813) us after the done interrupt, for WS2811 reset
if (micros() - update_completed_at < 300) return 1;
return 0;
}
void OctoSK6812::show(void)
{
// wait for any prior DMA operation
//Serial1.print("1");
while (update_in_progress) ;
//Serial1.print("2");
// it's ok to copy the drawing buffer to the frame buffer
// during the 50us WS2811 reset time
if (drawBuffer != frameBuffer) {
// TODO: this could be faster with DMA, especially if the
// buffers are 32 bit aligned... but does it matter?
memcpy(frameBuffer, drawBuffer, stripLen * pixelBits);
}
// wait for WS2811 reset
while (micros() - update_completed_at < 300) ;
// ok to start, but we must be very careful to begin
// without any prior 3 x 800kHz DMA requests pending
#if defined(__MK20DX128__)
uint32_t cv = FTM1_C0V;
noInterrupts();
// CAUTION: this code is timing critical.
while (FTM1_CNT <= cv) ;
while (FTM1_CNT > cv) ; // wait for beginning of an 800 kHz cycle
while (FTM1_CNT < cv) ;
FTM1_SC = 0; // stop FTM1 timer (hopefully before it rolls over)
FTM1_CNT = 0;
update_in_progress = 1;
//digitalWriteFast(9, HIGH); // oscilloscope trigger
PORTB_ISFR = (1<<0); // clear any prior rising edge
uint32_t tmp __attribute__((unused));
FTM1_C0SC = 0x28;
tmp = FTM1_C0SC; // clear any prior timer DMA triggers
FTM1_C0SC = 0x69;
FTM1_C1SC = 0x28;
tmp = FTM1_C1SC;
FTM1_C1SC = 0x69;
dma1.enable();
dma2.enable(); // enable all 3 DMA channels
dma3.enable();
FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0); // restart FTM1 timer
//digitalWriteFast(9, LOW);
#elif defined(__MK20DX256__)
FTM2_C0SC = 0x28;
FTM2_C1SC = 0x28;
delay(1);
uint32_t cv = FTM2_C0V;
noInterrupts();
// CAUTION: this code is timing critical.
while (FTM2_CNT <= cv) ;
while (FTM2_CNT > cv) ; // wait for beginning of an 800 kHz cycle
while (FTM2_CNT < cv) ;
FTM2_SC = 0; // stop FTM2 timer (hopefully before it rolls over)
FTM2_CNT = 0;
update_in_progress = 1;
//digitalWriteFast(9, HIGH); // oscilloscope trigger
PORTB_ISFR = (1<<18); // clear any prior rising edge
uint32_t tmp __attribute__((unused));
FTM2_C0SC = 0x28;
tmp = FTM2_C0SC; // clear any prior timer DMA triggers
FTM2_C0SC = 0x69;
FTM2_C1SC = 0x28;
tmp = FTM2_C1SC;
FTM2_C1SC = 0x69;
dma1.enable();
dma2.enable(); // enable all 3 DMA channels
dma3.enable();
FTM2_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0); // restart FTM2 timer
//digitalWriteFast(9, LOW);
#elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
FTM2_C0SC = 0x28;
FTM2_C1SC = 0x28;
delay(1);
uint32_t cv = FTM2_C1V;
noInterrupts();
// CAUTION: this code is timing critical.
while (FTM2_CNT <= cv) ;
while (FTM2_CNT > cv) ; // wait for beginning of an 800 kHz cycle
while (FTM2_CNT < cv) ;
FTM2_SC = 0; // stop FTM2 timer (hopefully before it rolls over)
update_in_progress = 1;
//digitalWriteFast(9, HIGH); // oscilloscope trigger
PORTA_ISFR = (1<<10); // clear any prior rising edge
uint32_t tmp __attribute__((unused));
FTM2_C0SC = 0x28;
tmp = FTM2_C0SC; // clear any prior timer DMA triggers
FTM2_C0SC = 0x69;
FTM2_C1SC = 0x28;
tmp = FTM2_C1SC;
FTM2_C1SC = 0x69;
dma1.enable();
dma2.enable(); // enable all 3 DMA channels
dma3.enable();
FTM2_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0); // restart FTM2 timer
//digitalWriteFast(9, LOW);
#elif defined(__MKL26Z64__)
uint32_t sc __attribute__((unused)) = FTM2_SC;
uint32_t cv = FTM2_C1V;
noInterrupts();
while (FTM2_CNT <= cv) ;
while (FTM2_CNT > cv) ; // wait for beginning of an 800 kHz cycle
while (FTM2_CNT < cv) ;
FTM2_SC = 0; // stop FTM2 timer (hopefully before it rolls over)
update_in_progress = 1;
//digitalWriteFast(9, HIGH); // oscilloscope trigger
dma1.clearComplete();
dma2.clearComplete();
dma3.clearComplete();
uint32_t bufsize = stripLen*24;
dma1.transferCount(bufsize);
dma2.transferCount(bufsize);
dma3.transferCount(bufsize);
dma2.sourceBuffer((uint8_t *)frameBuffer, bufsize);
// clear any pending event flags
FTM2_SC = FTM_SC_TOF;
FTM2_C0SC = FTM_CSC_CHF | FTM_CSC_MSB | FTM_CSC_ELSB | FTM_CSC_DMA;
FTM2_C1SC = FTM_CSC_CHF | FTM_CSC_MSB | FTM_CSC_ELSB | FTM_CSC_DMA;
// clear any prior pending DMA requests
dma1.enable();
dma2.enable(); // enable all 3 DMA channels
dma3.enable();
FTM2_CNT = 0; // writing any value resets counter
FTM2_SC = FTM_SC_DMA | FTM_SC_CLKS(1) | FTM_SC_PS(0);
//digitalWriteFast(9, LOW);
#endif
//Serial1.print("3");
interrupts();
//Serial1.print("4");
}
void OctoSK6812::setPixel(uint32_t num, int color)
{
uint32_t strip, offset, mask;
uint8_t bit, *p;
switch (params & 7) {
case SK6812_RBG:
color = (color&0xFF0000) | ((color<<8)&0x00FF00) | ((color>>8)&0x0000FF);
break;
case SK6812_GRB:
color = ((color<<8)&0xFF0000) | ((color>>8)&0x00FF00) | (color&0x0000FF);
break;
case SK6812_GBR:
color = ((color<<16)&0xFFFF00) | ((color>>8)&0x0000FF);
break;
case SK6812_BRG:
color = ((color<<8)&0xFFFF00) | ((color>>16)&0x0000FF);
break;
case SK6812_BGR:
color = ((color<<16)&0xFF0000) | (color&0x00FF00) | ((color>>16)&0x0000FF);
break;
case SK6812_GRBW:
color = ((color<<8)&0xFF000000) | ((color>>8)&0x00FF0000) | (color&0x0000FFFF);
break;
default:
break;
}
strip = num / stripLen; // Cortex-M4 has 2 cycle unsigned divide :-)
offset = num % stripLen;
bit = (1<<strip);
p = ((uint8_t *)drawBuffer) + offset * pixelBits;
for (mask = (1<<(pixelBits-1)) ; mask ; mask >>= 1) {
if (color & mask) {
*p++ |= bit;
} else {
*p++ &= ~bit;
}
}
}
int OctoSK6812::getPixel(uint32_t num)
{
uint32_t strip, offset, mask;
uint8_t bit, *p;
int color=0;
strip = num / stripLen;
offset = num % stripLen;
bit = (1<<strip);
p = ((uint8_t *)drawBuffer) + offset * pixelBits;
for (mask = (1<<(pixelBits-1)) ; mask ; mask >>= 1) {
if (*p++ & bit) color |= mask;
}
switch (params & 7) {
case SK6812_RBG:
color = (color&0xFF0000) | ((color<<8)&0x00FF00) | ((color>>8)&0x0000FF);
break;
case SK6812_GRB:
color = ((color<<8)&0xFF0000) | ((color>>8)&0x00FF00) | (color&0x0000FF);
break;
case SK6812_GBR:
color = ((color<<16)&0xFFFF00) | ((color>>8)&0x0000FF);
break;
case SK6812_BRG:
color = ((color<<8)&0xFFFF00) | ((color>>16)&0x0000FF);
break;
case SK6812_BGR:
color = ((color<<16)&0xFF0000) | (color&0x00FF00) | ((color>>16)&0x0000FF);
break;
case SK6812_GRBW:
color = ((color<<8)&0xFF000000) | ((color>>8)&0x00FF0000) | (color&0x0000FFFF);
break;
default:
break;
}
return color;
}