2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
22 * "Note that the timing on the WS2812/WS2812B LEDs has changed as of batches from WorldSemi
23 * manufactured made in October 2013, and timing tolerance for approx 10-30% of parts is very small.
24 * Recommendation from WorldSemi is now: 0 = 400ns high/850ns low, and 1 = 850ns high, 400ns low"
26 * Currently the timings are 0 = 350ns high/800ns and 1 = 700ns high/650ns low.
34 #include "common/maths.h"
38 #include "build/build_config.h"
40 #include "common/color.h"
41 #include "common/colorconversion.h"
43 #include "drivers/dma.h"
44 #include "drivers/io.h"
46 #include "light_ws2811strip.h"
48 #include "scheduler/scheduler.h"
50 #ifdef USE_LEDSTRIP_CACHE_MGMT
51 // WS2811_DMA_BUFFER_SIZE is multiples of uint32_t
52 // Number of bytes required for buffer
53 #define WS2811_DMA_BUF_BYTES (WS2811_DMA_BUFFER_SIZE * sizeof(uint32_t))
54 // Number of bytes required to cache align buffer
55 #define WS2811_DMA_BUF_CACHE_ALIGN_BYTES ((WS2811_DMA_BUF_BYTES + 0x20) & ~0x1f)
56 // Size of array to create a cache aligned buffer
57 #define WS2811_DMA_BUF_CACHE_ALIGN_LENGTH (WS2811_DMA_BUF_CACHE_ALIGN_BYTES / sizeof(uint32_t))
58 DMA_RW_AXI
__attribute__((aligned(32))) uint32_t ledStripDMABuffer
[WS2811_DMA_BUF_CACHE_ALIGN_LENGTH
];
61 FAST_DATA_ZERO_INIT
uint32_t ledStripDMABuffer
[WS2811_DMA_BUFFER_SIZE
];
62 #elif defined(STM32H7)
63 DMA_RAM
uint32_t ledStripDMABuffer
[WS2811_DMA_BUFFER_SIZE
];
65 uint32_t ledStripDMABuffer
[WS2811_DMA_BUFFER_SIZE
];
69 static ioTag_t ledStripIoTag
;
70 static bool ws2811Initialised
= false;
71 volatile bool ws2811LedDataTransferInProgress
= false;
72 static unsigned usedLedCount
= 0;
73 static bool needsFullRefresh
= true;
75 uint16_t BIT_COMPARE_1
= 0;
76 uint16_t BIT_COMPARE_0
= 0;
78 static hsvColor_t ledColorBuffer
[WS2811_DATA_BUFFER_SIZE
];
80 #if !defined(USE_WS2811_SINGLE_COLOUR)
81 void setLedHsv(uint16_t index
, const hsvColor_t
*color
)
83 ledColorBuffer
[index
] = *color
;
86 void getLedHsv(uint16_t index
, hsvColor_t
*color
)
88 *color
= ledColorBuffer
[index
];
91 void setLedValue(uint16_t index
, const uint8_t value
)
93 ledColorBuffer
[index
].v
= value
;
96 void scaleLedValue(uint16_t index
, const uint8_t scalePercent
)
98 ledColorBuffer
[index
].v
= ((uint16_t)ledColorBuffer
[index
].v
* scalePercent
/ 100);
102 void setStripColor(const hsvColor_t
*color
)
104 for (unsigned index
= 0; index
< usedLedCount
; index
++) {
105 ledColorBuffer
[index
] = *color
;
109 void setStripColors(const hsvColor_t
*colors
)
111 for (unsigned index
= 0; index
< usedLedCount
; index
++) {
112 setLedHsv(index
, colors
++);
116 void setUsedLedCount(unsigned ledCount
)
118 usedLedCount
= (ledCount
< WS2811_DATA_BUFFER_SIZE
) ? ledCount
: WS2811_DATA_BUFFER_SIZE
;
120 // Update all possible positions on the next update in case the count
121 // decreased otherwise LEDs on the end could be left in their previous state
122 needsFullRefresh
= true;
125 void ws2811LedStripInit(ioTag_t ioTag
)
127 memset(ledStripDMABuffer
, 0, sizeof(ledStripDMABuffer
));
129 ledStripIoTag
= ioTag
;
132 void ws2811LedStripEnable(void)
134 if (!ws2811Initialised
) {
135 if (!ws2811LedStripHardwareInit(ledStripIoTag
)) {
139 const hsvColor_t hsv_black
= { 0, 0, 0 };
140 setStripColor(&hsv_black
);
141 // RGB or GRB ordering doesn't matter for black, use 4-channel LED configuraton to make sure all channels are zero
142 ws2811UpdateStrip(LED_GRBW
, 100);
144 ws2811Initialised
= true;
148 bool isWS2811LedStripReady(void)
150 return ws2811Initialised
&& !ws2811LedDataTransferInProgress
;
153 STATIC_UNIT_TESTED
void updateLEDDMABuffer(ledStripFormatRGB_e ledFormat
, rgbColor24bpp_t
*color
, unsigned ledIndex
)
155 uint32_t bits_per_led
;
156 uint32_t packed_colour
;
159 case LED_RGB
: // WS2811 drivers use RGB format
160 packed_colour
= (color
->rgb
.r
<< 16) | (color
->rgb
.g
<< 8) | (color
->rgb
.b
);
164 case LED_GRBW
: // SK6812 drivers use this
166 /* reconstruct white channel from RGB, making the intensity a bit nonlinear, but thats fine for this use case */
167 uint8_t white
= MIN(MIN(color
->rgb
.r
, color
->rgb
.g
), color
->rgb
.b
);
168 packed_colour
= (color
->rgb
.g
<< 24) | (color
->rgb
.r
<< 16) | (color
->rgb
.b
<< 8) | (white
);
173 case LED_GRB
: // WS2812 drivers use GRB format
175 packed_colour
= (color
->rgb
.g
<< 16) | (color
->rgb
.r
<< 8) | (color
->rgb
.b
);
180 unsigned dmaBufferOffset
= 0;
181 for (int index
= bits_per_led
-1; index
>= 0; index
--) {
182 ledStripDMABuffer
[ledIndex
* bits_per_led
+ dmaBufferOffset
++] = (packed_colour
& (1 << index
)) ? BIT_COMPARE_1
: BIT_COMPARE_0
;
187 * This method is non-blocking unless an existing LED update is in progress.
188 * it does not wait until all the LEDs have been updated, that happens in the background.
190 void ws2811UpdateStrip(ledStripFormatRGB_e ledFormat
, uint8_t brightness
)
192 // don't wait - risk of infinite block, just get an update next time round
193 if (!ws2811Initialised
|| ws2811LedDataTransferInProgress
) {
194 schedulerIgnoreTaskStateTime();
198 unsigned ledIndex
= 0; // reset led index
200 // fill transmit buffer with correct compare values to achieve
201 // correct pulse widths according to color values
202 const unsigned ledUpdateCount
= needsFullRefresh
? WS2811_DATA_BUFFER_SIZE
: usedLedCount
;
203 const hsvColor_t hsvBlack
= { 0, 0, 0 };
204 while (ledIndex
< ledUpdateCount
) {
205 hsvColor_t scaledLed
= ledIndex
< usedLedCount
? ledColorBuffer
[ledIndex
] : hsvBlack
;
206 // Scale the LED brightness
207 scaledLed
.v
= scaledLed
.v
* brightness
/ 100;
209 rgbColor24bpp_t
*rgb24
= hsvToRgb24(&scaledLed
);
211 updateLEDDMABuffer(ledFormat
, rgb24
, ledIndex
++);
213 needsFullRefresh
= false;
215 #ifdef USE_LEDSTRIP_CACHE_MGMT
216 SCB_CleanDCache_by_Addr(ledStripDMABuffer
, WS2811_DMA_BUF_CACHE_ALIGN_BYTES
);
219 ws2811LedDataTransferInProgress
= true;
220 ws2811LedStripDMAEnable();