-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathlight_ws2811strip.c
More file actions
329 lines (267 loc) · 10.4 KB
/
Copy pathlight_ws2811strip.c
File metadata and controls
329 lines (267 loc) · 10.4 KB
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
/*
* This file is part of Cleanflight.
*
* Cleanflight is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cleanflight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* "Note that the timing on the WS2812/WS2812B LEDs has changed as of batches from WorldSemi
* manufactured made in October 2013, and timing tolerance for approx 10-30% of parts is very small.
* Recommendation from WorldSemi is now: 0 = 400ns high/850ns low, and 1 = 850ns high, 400ns low"
*
* Currently the timings are 0 = 350ns high/800ns and 1 = 700ns high/650ns low.
*/
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <platform.h>
#ifdef USE_LED_STRIP
#include "build/build_config.h"
#include "build/debug.h"
#include "common/color.h"
#include "common/colorconversion.h"
#include "common/maths.h"
#include "common/time.h"
#include "drivers/dma.h"
#include "drivers/io.h"
#include "drivers/time.h"
#include "drivers/timer.h"
#include "drivers/timer_impl.h"
#include "drivers/light_ws2811strip.h"
#include "fc/runtime_config.h"
#define WS2811_PERIOD (WS2811_TIMER_HZ / WS2811_CARRIER_HZ)
#define WS2811_BIT_COMPARE_1 ((WS2811_PERIOD * 2) / 3)
#define WS2811_BIT_COMPARE_0 (WS2811_PERIOD / 3)
// Circular DMA buffer: 2 halves, 1 LED group each. ws2811DMARefillCallback
// refills whichever half DMA just finished sending, so this only needs to
// hold a couple of LEDs regardless of strip length.
#define WS2811_LEDS_PER_GROUP 4
#define WS2811_GROUP_BITS (WS2811_LEDS_PER_GROUP * WS2811_BITS_PER_LED)
#define WS2811_CHUNK_BUFFER_SIZE (2 * WS2811_GROUP_BITS)
// WS2812 reset/latch is a *minimum* low duration with no upper bound, so
// this is a threshold to check against, not a preamble to budget for.
#define WS2811_RESET_US 60
// timerDMASafeType_t, not a narrower type: DMA writes to CCR must be
// word-width on every supported platform.
static DMA_RAM timerDMASafeType_t ledStripDMABuffer[WS2811_CHUNK_BUFFER_SIZE];
static IO_t ws2811IO = IO_NONE;
static TCH_t * ws2811TCH = NULL;
static bool ws2811Initialised = false;
static hsvColor_t ledColorBuffer[WS2811_LED_STRIP_LENGTH];
void setLedHsv(uint16_t index, const hsvColor_t *color)
{
ledColorBuffer[index] = *color;
}
void getLedHsv(uint16_t index, hsvColor_t *color)
{
*color = ledColorBuffer[index];
}
void setLedValue(uint16_t index, const uint8_t value)
{
ledColorBuffer[index].v = value;
}
void scaleLedValue(uint16_t index, const uint8_t scalePercent)
{
ledColorBuffer[index].v = ((uint16_t)ledColorBuffer[index].v * scalePercent / 100);
}
void setStripColor(const hsvColor_t *color)
{
uint16_t index;
for (index = 0; index < WS2811_LED_STRIP_LENGTH; index++) {
setLedHsv(index, color);
}
}
void setStripColors(const hsvColor_t *colors)
{
uint16_t index;
for (index = 0; index < WS2811_LED_STRIP_LENGTH; index++) {
setLedHsv(index, colors++);
}
}
static void ws2811DMARefillCallback(TCH_t * tch, bool transferComplete);
bool ledConfigureDMA(void) {
/* Compute the prescaler value */
uint8_t period = WS2811_TIMER_HZ / WS2811_CARRIER_HZ;
timerConfigBase(ws2811TCH, period, WS2811_TIMER_HZ);
timerPWMConfigChannel(ws2811TCH, 0);
return timerPWMConfigChannelDMA(ws2811TCH, ledStripDMABuffer, sizeof(ledStripDMABuffer[0]), WS2811_CHUNK_BUFFER_SIZE);
}
// Written from both task context and the DMA refill ISR (never truly
// concurrently — the ISR only runs while a transfer is active, and task
// context only touches these once it isn't — but volatile documents that
// and guards against the compiler assuming otherwise).
// Number of LEDs in the most recent transfer; bounds the DMA transfer (and
// ws2811SetIdleHigh's target) to what's actually configured.
static volatile uint16_t activeLedCount = WS2811_LED_STRIP_LENGTH;
// groupInHalf[i]: group index currently in half i, so the refill callback
// can tell when the group it just finished sending was the last one.
static volatile uint16_t totalGroups;
static volatile uint16_t nextGroupToAssign;
static volatile uint16_t groupInHalf[2];
// Shared between normal transfer completion and ws2811SetIdleHigh (PINIO).
static volatile bool lineIdleLow = true;
static volatile timeUs_t lastLowAtUs = 0;
// Idle level PINIO last asked for; persists across transfers so
// ws2811StopTransfer() knows what to restore the line to.
static volatile bool idleHighRequested = false;
void ws2811LedStripInit(void)
{
const timerHardware_t * timHw = timerGetByTag(IO_TAG(WS2811_PIN), TIM_USE_ANY);
if (!(timHw->usageFlags & TIM_USE_LED)) { // Check if it has not been reassigned
timHw = timerGetByUsageFlag(TIM_USE_LED); // Get first pin marked as LED
}
if (timHw == NULL) {
return;
}
ws2811TCH = timerGetTCH(timHw);
if (ws2811TCH == NULL) {
return;
}
impl_timerPWMSetDMARefillCallback(ws2811TCH, ws2811DMARefillCallback);
ws2811IO = IOGetByTag(timHw->tag); //IOGetByTag(IO_TAG(WS2811_PIN));
IOInit(ws2811IO, OWNER_LED_STRIP, RESOURCE_OUTPUT, 0);
IOConfigGPIOAF(ws2811IO, IOCFG_AF_PP_FAST, timHw->alternateFunction);
if (!ledConfigureDMA()) {
// If DMA failed - abort
ws2811Initialised = false;
return;
}
// Zero out DMA buffer — LED pin idles LOW between WS2812 bursts
memset(&ledStripDMABuffer, 0, sizeof(ledStripDMABuffer));
lineIdleLow = true;
lastLowAtUs = micros();
ws2811Initialised = true;
ws2811UpdateStrip(WS2811_LED_STRIP_LENGTH);
}
bool isWS2811LedStripReady(void)
{
return !timerPWMDMAInProgress(ws2811TCH);
}
static void writeLedBits(timerDMASafeType_t *dest, const rgbColor24bpp_t *color)
{
uint32_t grb = (color->rgb.g << 16) | (color->rgb.r << 8) | (color->rgb.b);
for (int8_t index = 23; index >= 0; index--) {
*dest++ = (grb & (1 << index)) ? WS2811_BIT_COMPARE_1 : WS2811_BIT_COMPARE_0;
}
}
// Slots at or beyond activeLedCount get a direct "off" write instead of a
// color lookup, so a group is self-contained regardless of where the
// configured strip actually ends.
static void ws2811FillGroup(uint8_t halfIndex, uint16_t groupIndex)
{
timerDMASafeType_t *half = &ledStripDMABuffer[halfIndex * WS2811_GROUP_BITS];
uint16_t baseLed = groupIndex * WS2811_LEDS_PER_GROUP;
for (uint8_t slot = 0; slot < WS2811_LEDS_PER_GROUP; slot++) {
timerDMASafeType_t *dest = &half[slot * WS2811_BITS_PER_LED];
uint16_t ledIdx = baseLed + slot;
if (ledIdx < activeLedCount) {
writeLedBits(dest, hsvToRgb24(&ledColorBuffer[ledIdx]));
} else {
for (uint8_t bit = 0; bit < WS2811_BITS_PER_LED; bit++) {
dest[bit] = WS2811_BIT_COMPARE_0;
}
}
}
}
static void ws2811RefillHalf(uint8_t halfIndex)
{
ws2811FillGroup(halfIndex, nextGroupToAssign);
groupInHalf[halfIndex] = nextGroupToAssign;
nextGroupToAssign++;
}
// CCR is preload/shadow-buffered, so the direct write below takes effect
// cleanly at the next period boundary without needing further DMA. Restores
// whatever idle level PINIO last asked for, rather than always going low —
// a transfer finishing shouldn't silently override that.
static void ws2811StopTransfer(void)
{
timerPWMStopDMA(ws2811TCH);
if (idleHighRequested) {
*timerCCR(ws2811TCH) = 255;
lineIdleLow = false;
} else {
*timerCCR(ws2811TCH) = 0;
lineIdleLow = true;
lastLowAtUs = micros();
}
}
// transferComplete: true = half 1 just finished (DMA wrapped to half 0),
// false = half 0 just finished (DMA moved on to half 1).
static void ws2811DMARefillCallback(TCH_t * tch, bool transferComplete)
{
(void)tch;
uint8_t finishedHalf = transferComplete ? 1 : 0;
// Bounds-check nextGroupToAssign itself, not just the group that just
// finished — the other half may already hold the true last group, so
// checking groupInHalf[finishedHalf] alone lags by one refill and can
// assign a group index past totalGroups-1.
if (nextGroupToAssign < totalGroups) {
ws2811RefillHalf(finishedHalf);
} else if (groupInHalf[finishedHalf] == totalGroups - 1) {
ws2811StopTransfer();
}
}
static void ws2811EnsureResetGap(void)
{
// A gap before real data is always safe regardless of length — only a
// mid-frame gap (prevented by true circular DMA) risks looking like a
// premature reset — so usually this is just a compare, not a wait.
if (!lineIdleLow || cmpTimeUs(micros(), lastLowAtUs) < WS2811_RESET_US) {
*timerCCR(ws2811TCH) = 0;
lineIdleLow = true;
delayMicroseconds(WS2811_RESET_US);
lastLowAtUs = micros();
}
}
// Non-blocking except when the line was left idle-high by PINIO or updates
// are requested faster than the reset window allows. LEDs are transmitted
// in the background via the DMA refill callback.
void ws2811UpdateStrip(uint16_t usedLedCount)
{
if (!ws2811Initialised || !ws2811TCH) {
return;
}
// don't wait - risk of infinite block, just get an update next time round
if (timerPWMDMAInProgress(ws2811TCH)) {
return;
}
activeLedCount = MIN(usedLedCount, (uint16_t)WS2811_LED_STRIP_LENGTH);
if (activeLedCount == 0) {
return;
}
ws2811EnsureResetGap();
totalGroups = (activeLedCount + WS2811_LEDS_PER_GROUP - 1) / WS2811_LEDS_PER_GROUP;
nextGroupToAssign = 0;
ws2811RefillHalf(0);
ws2811RefillHalf(1);
impl_timerPWMSetDMACircular(ws2811TCH, true, WS2811_CHUNK_BUFFER_SIZE);
}
void ws2811SetIdleHigh(bool high)
{
idleHighRequested = high; // record even if not initialised yet
if (!ws2811Initialised || !ws2811TCH) {
return;
}
// DMA drives CCR directly during an active transfer; don't race it with
// a CPU write here. ws2811StopTransfer() applies idleHighRequested once
// the in-flight transfer finishes.
if (timerPWMDMAInProgress(ws2811TCH)) {
return;
}
lineIdleLow = !high;
*timerCCR(ws2811TCH) = high ? 255 : 0;
if (!high) {
lastLowAtUs = micros();
}
}
#endif