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
];
60 #if defined(STM32F1) || defined(STM32F3)
61 uint8_t ledStripDMABuffer
[WS2811_DMA_BUFFER_SIZE
];
62 #elif defined(STM32F7)
63 FAST_DATA_ZERO_INIT
uint32_t ledStripDMABuffer
[WS2811_DMA_BUFFER_SIZE
];
64 #elif defined(STM32H7)
65 DMA_RAM
uint32_t ledStripDMABuffer
[WS2811_DMA_BUFFER_SIZE
];
67 uint32_t ledStripDMABuffer
[WS2811_DMA_BUFFER_SIZE
];
71 static ioTag_t ledStripIoTag
;
72 static bool ws2811Initialised
= false;
73 volatile bool ws2811LedDataTransferInProgress
= false;
74 static unsigned usedLedCount
= 0;
75 static bool needsFullRefresh
= true;
77 uint16_t BIT_COMPARE_1
= 0;
78 uint16_t BIT_COMPARE_0
= 0;
80 static hsvColor_t ledColorBuffer
[WS2811_DATA_BUFFER_SIZE
];
82 #if !defined(USE_WS2811_SINGLE_COLOUR)
83 void setLedHsv(uint16_t index
, const hsvColor_t
*color
)
85 ledColorBuffer
[index
] = *color
;
88 void getLedHsv(uint16_t index
, hsvColor_t
*color
)
90 *color
= ledColorBuffer
[index
];
93 void setLedValue(uint16_t index
, const uint8_t value
)
95 ledColorBuffer
[index
].v
= value
;
98 void scaleLedValue(uint16_t index
, const uint8_t scalePercent
)
100 ledColorBuffer
[index
].v
= ((uint16_t)ledColorBuffer
[index
].v
* scalePercent
/ 100);
104 void setStripColor(const hsvColor_t
*color
)
106 for (unsigned index
= 0; index
< usedLedCount
; index
++) {
107 ledColorBuffer
[index
] = *color
;
111 void setStripColors(const hsvColor_t
*colors
)
113 for (unsigned index
= 0; index
< usedLedCount
; index
++) {
114 setLedHsv(index
, colors
++);
118 void setUsedLedCount(unsigned ledCount
)
120 usedLedCount
= (ledCount
< WS2811_DATA_BUFFER_SIZE
) ? ledCount
: WS2811_DATA_BUFFER_SIZE
;
122 // Update all possible positions on the next update in case the count
123 // decreased otherwise LEDs on the end could be left in their previous state
124 needsFullRefresh
= true;
127 void ws2811LedStripInit(ioTag_t ioTag
)
129 memset(ledStripDMABuffer
, 0, sizeof(ledStripDMABuffer
));
131 ledStripIoTag
= ioTag
;
134 void ws2811LedStripEnable(void)
136 if (!ws2811Initialised
) {
137 if (!ws2811LedStripHardwareInit(ledStripIoTag
)) {
141 const hsvColor_t hsv_black
= { 0, 0, 0 };
142 setStripColor(&hsv_black
);
143 // RGB or GRB ordering doesn't matter for black, use 4-channel LED configuraton to make sure all channels are zero
144 ws2811UpdateStrip(LED_GRBW
, 100);
146 ws2811Initialised
= true;
150 bool isWS2811LedStripReady(void)
152 return ws2811Initialised
&& !ws2811LedDataTransferInProgress
;
155 STATIC_UNIT_TESTED
void updateLEDDMABuffer(ledStripFormatRGB_e ledFormat
, rgbColor24bpp_t
*color
, unsigned ledIndex
)
157 uint32_t bits_per_led
;
158 uint32_t packed_colour
;
161 case LED_RGB
: // WS2811 drivers use RGB format
162 packed_colour
= (color
->rgb
.r
<< 16) | (color
->rgb
.g
<< 8) | (color
->rgb
.b
);
166 case LED_GRBW
: // SK6812 drivers use this
168 /* reconstruct white channel from RGB, making the intensity a bit nonlinear, but thats fine for this use case */
169 uint8_t white
= MIN(MIN(color
->rgb
.r
, color
->rgb
.g
), color
->rgb
.b
);
170 packed_colour
= (color
->rgb
.g
<< 24) | (color
->rgb
.r
<< 16) | (color
->rgb
.b
<< 8) | (white
);
175 case LED_GRB
: // WS2812 drivers use GRB format
177 packed_colour
= (color
->rgb
.g
<< 16) | (color
->rgb
.r
<< 8) | (color
->rgb
.b
);
182 unsigned dmaBufferOffset
= 0;
183 for (int index
= bits_per_led
-1; index
>= 0; index
--) {
184 ledStripDMABuffer
[ledIndex
* bits_per_led
+ dmaBufferOffset
++] = (packed_colour
& (1 << index
)) ? BIT_COMPARE_1
: BIT_COMPARE_0
;
189 * This method is non-blocking unless an existing LED update is in progress.
190 * it does not wait until all the LEDs have been updated, that happens in the background.
192 void ws2811UpdateStrip(ledStripFormatRGB_e ledFormat
, uint8_t brightness
)
194 // don't wait - risk of infinite block, just get an update next time round
195 if (!ws2811Initialised
|| ws2811LedDataTransferInProgress
) {
196 schedulerIgnoreTaskStateTime();
200 unsigned ledIndex
= 0; // reset led index
202 // fill transmit buffer with correct compare values to achieve
203 // correct pulse widths according to color values
204 const unsigned ledUpdateCount
= needsFullRefresh
? WS2811_DATA_BUFFER_SIZE
: usedLedCount
;
205 const hsvColor_t hsvBlack
= { 0, 0, 0 };
206 while (ledIndex
< ledUpdateCount
) {
207 hsvColor_t scaledLed
= ledIndex
< usedLedCount
? ledColorBuffer
[ledIndex
] : hsvBlack
;
208 // Scale the LED brightness
209 scaledLed
.v
= scaledLed
.v
* brightness
/ 100;
211 rgbColor24bpp_t
*rgb24
= hsvToRgb24(&scaledLed
);
213 updateLEDDMABuffer(ledFormat
, rgb24
, ledIndex
++);
215 needsFullRefresh
= false;
217 #ifdef USE_LEDSTRIP_CACHE_MGMT
218 SCB_CleanDCache_by_Addr(ledStripDMABuffer
, WS2811_DMA_BUF_CACHE_ALIGN_BYTES
);
221 ws2811LedDataTransferInProgress
= true;
222 ws2811LedStripDMAEnable();