forked from aurimasniekis/fatboy-vesc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws2811.c
253 lines (205 loc) · 6.47 KB
/
ws2811.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
/*
Copyright 2016 Benjamin Vedder [email protected]
This file is part of the VESC firmware.
The VESC firmware 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.
The VESC firmware 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include "ws2811.h"
#include "stm32f4xx_conf.h"
#include "ch.h"
#include "hal.h"
// Settings
#define TIM_PERIOD (((168000000 / 2 / WS2811_CLK_HZ) - 1))
#define LED_BUFFER_LEN (WS2811_LED_NUM + 1)
#define BITBUFFER_PAD 50
#define BITBUFFER_LEN (24 * LED_BUFFER_LEN + BITBUFFER_PAD)
#define WS2811_ZERO (TIM_PERIOD * 0.2)
#define WS2811_ONE (TIM_PERIOD * 0.8)
// Private variables
static uint16_t bitbuffer[BITBUFFER_LEN];
static uint32_t RGBdata[LED_BUFFER_LEN];
static uint8_t gamma_table[256];
static uint32_t brightness;
// Private function prototypes
static uint32_t rgb_to_local(uint32_t color);
void ws2811_init(void) {
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
DMA_InitTypeDef DMA_InitStructure;
brightness = 100;
// Default LED values
int i, bit;
for (i = 0;i < LED_BUFFER_LEN;i++) {
RGBdata[i] = 0;
}
for (i = 0;i < LED_BUFFER_LEN;i++) {
uint32_t tmp_color = rgb_to_local(RGBdata[i]);
for (bit = 0;bit < 24;bit++) {
if(tmp_color & (1 << 23)) {
bitbuffer[bit + i * 24] = WS2811_ONE;
} else {
bitbuffer[bit + i * 24] = WS2811_ZERO;
}
tmp_color <<= 1;
}
}
// Fill the rest of the buffer with zeros to give the LEDs a chance to update
// after sending all bits
for (i = 0;i < BITBUFFER_PAD;i++) {
bitbuffer[BITBUFFER_LEN - BITBUFFER_PAD - 1 + i] = 0;
}
// Generate gamma correction table
for (int i = 0;i < 256;i++) {
gamma_table[i] = (int)roundf(powf((float)i / 255.0, 1.0 / 0.45) * 255.0);
}
#if WS2811_USE_CH2
palSetPadMode(GPIOB, 7,
PAL_MODE_ALTERNATE(GPIO_AF_TIM4) |
PAL_STM32_OTYPE_OPENDRAIN |
PAL_STM32_OSPEED_MID1);
#else
palSetPadMode(GPIOB, 6,
PAL_MODE_ALTERNATE(GPIO_AF_TIM4) |
PAL_STM32_OTYPE_OPENDRAIN |
PAL_STM32_OSPEED_MID1);
#endif
// DMA clock enable
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 , ENABLE);
#if WS2811_USE_CH2
DMA_DeInit(DMA1_Stream3);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&TIM4->CCR2;
#else
DMA_DeInit(DMA1_Stream0);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&TIM4->CCR1;
#endif
DMA_InitStructure.DMA_Channel = DMA_Channel_2;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)bitbuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = BITBUFFER_LEN;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
#if WS2811_USE_CH2
DMA_Init(DMA1_Stream3, &DMA_InitStructure);
#else
DMA_Init(DMA1_Stream0, &DMA_InitStructure);
#endif
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
// Time Base configuration
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = TIM_PERIOD;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
// Channel 1 Configuration in PWM mode
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = bitbuffer[0];
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
#if WS2811_USE_CH2
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
#else
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
#endif
// TIM4 counter enable
TIM_Cmd(TIM4, ENABLE);
// DMA enable
#if WS2811_USE_CH2
DMA_Cmd(DMA1_Stream3, ENABLE);
#else
DMA_Cmd(DMA1_Stream0, ENABLE);
#endif
// TIM4 Update DMA Request enable
#if WS2811_USE_CH2
TIM_DMACmd(TIM4, TIM_DMA_CC2, ENABLE);
#else
TIM_DMACmd(TIM4, TIM_DMA_CC1, ENABLE);
#endif
// Main Output Enable
TIM_CtrlPWMOutputs(TIM4, ENABLE);
}
void ws2811_set_led_color(int led, uint32_t color) {
if (led >= 0 && led < WS2811_LED_NUM) {
RGBdata[led] = color;
color = rgb_to_local(color);
int bit;
for (bit = 0;bit < 24;bit++) {
if(color & (1 << 23)) {
bitbuffer[bit + led * 24] = WS2811_ONE;
} else {
bitbuffer[bit + led * 24] = WS2811_ZERO;
}
color <<= 1;
}
}
}
uint32_t ws2811_get_led_color(int led) {
if (led >= 0 && led < WS2811_LED_NUM) {
return RGBdata[led];
}
return 0;
}
void ws2811_all_off(void) {
int i;
for (i = 0;i < WS2811_LED_NUM;i++) {
RGBdata[i] = 0;
}
for (i = 0;i < (WS2811_LED_NUM * 24);i++) {
bitbuffer[i] = WS2811_ZERO;
}
}
void ws2811_set_all(uint32_t color) {
int i, bit;
for (i = 0;i < WS2811_LED_NUM;i++) {
RGBdata[i] = color;
uint32_t tmp_color = rgb_to_local(color);
for (bit = 0;bit < 24;bit++) {
if(tmp_color & (1 << 23)) {
bitbuffer[bit + i * 24] = WS2811_ONE;
} else {
bitbuffer[bit + i * 24] = WS2811_ZERO;
}
tmp_color <<= 1;
}
}
}
void ws2811_set_brightness(uint32_t br) {
brightness = br;
for (int i = 0;i < WS2811_LED_NUM;i++) {
ws2811_set_led_color(i, ws2811_get_led_color(i));
}
}
uint32_t ws2811_get_brightness(void) {
return brightness;
}
static uint32_t rgb_to_local(uint32_t color) {
uint32_t r = (color >> 16) & 0xFF;
uint32_t g = (color >> 8) & 0xFF;
uint32_t b = color & 0xFF;
r = (r * brightness) / 100;
g = (g * brightness) / 100;
b = (b * brightness) / 100;
r = gamma_table[r];
g = gamma_table[g];
b = gamma_table[b];
return (g << 16) | (r << 8) | b;
}