-
Notifications
You must be signed in to change notification settings - Fork 28
/
io_ledpixel.c
394 lines (318 loc) · 8.75 KB
/
io_ledpixel.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
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
#include "io_ledpixel.h"
#include "util.h"
#include "uart.h"
#include "i2s.h"
#include "io_gpio.h"
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <attribute.h>
typedef struct attr_packed
{
unsigned int enabled:1;
unsigned int extended:1;
unsigned int grb:1;
unsigned int fill8:1;
unsigned int value:32;
} ledpixel_data_pin_t;
assert_size(ledpixel_data_pin_t, 5);
typedef union attr_packed
{
struct
{
unsigned int b:5;
unsigned int g:5;
unsigned int r:5;
unsigned int flag:1;
unsigned int pinmask:16;
};
unsigned int value;
} ledpixel_mask_value_t;
assert_size(ledpixel_mask_value_t, 4);
roflash static const unsigned int lut_5_8[32] =
{
0, // 0 0x00
1, // 1 0x01
2, // 2 0x02
3, // 3 0x03
4, // 4 0x04
5, // 5 0x05
6, // 6 0x06
7, // 7 0x07
8, // 8 0x08
10, // 9 0x0a
12, // 10 0x0c
15, // 11 0x0f
18, // 12 0x12
21, // 13 0x15
25, // 14 0x19
28, // 15 0x1c
32, // 16 0x20
40, // 17 0x28
48, // 18 0x30
56, // 19 0x38
64, // 20 0x40
72, // 21 0x48
80, // 22 0x50
88, // 23 0x58
96, // 24 0x60
112, // 25 0x70
128, // 26 0x80
144, // 27 0x90
160, // 28 0xa0
192, // 29 0xc0
224, // 30 0xe0
255, // 31 0xff
};
static bool use_uart_0 = false;
static bool use_uart_1 = false;
static bool use_i2s = false;
static bool use_i2s_invert = false;
static ledpixel_data_pin_t ledpixel_data_pin[max_pins_per_io];
static unsigned int lookup_5_to_8(unsigned int entry)
{
if(entry >= sizeof(lut_5_8))
return(0xff);
return(lut_5_8[entry]);
}
static void value_mask_to_rgb(unsigned int value, unsigned int *pinmask, unsigned int *flag, unsigned int *rgb)
{
ledpixel_mask_value_t mask_value;
unsigned int r, g, b;
mask_value.value = value;
*pinmask = mask_value.pinmask;
*flag = mask_value.flag;
r = lookup_5_to_8(mask_value.r);
g = lookup_5_to_8(mask_value.g);
b = lookup_5_to_8(mask_value.b);
*rgb = (r << 16) | (g << 8) | (b << 0);
}
static void send_byte_uart(unsigned int byte)
{
// from an idea by nodemcu coders: https://github.com/nodemcu/nodemcu-firmware/blob/master/app/modules/ws2812.c
static const unsigned int bit_pattern[4] =
{ // mirror add start/stop negate
0b110111, // 00 111-011 [0]111-011[1] 1000-1000
0b000111, // 01 111-000 [0]111-000[1] 1000-1110
0b110100, // 10 001-011 [0]001-011[1] 1110-1000
0b000100, // 11 001-000 [0]001-000[1] 1110-1110
};
unsigned int byte_bit_index;
unsigned int by_six = 0;
for(byte_bit_index = 0; byte_bit_index < 4; byte_bit_index++)
{
by_six = bit_pattern[(byte & 0b11000000) >> 6];
byte <<= 2;
if(use_uart_0)
uart_send(0, by_six);
if(use_uart_1)
uart_send(1, by_six);
}
}
static void send_byte_i2s(unsigned int byte_value_in)
{
static const unsigned int off_pattern_normal = 0b1000;
static const unsigned int on_pattern_normal = 0b1110;
static const unsigned int off_pattern_invert = 0b0111;
static const unsigned int on_pattern_invert = 0b0001;
uint8_t buffer[4];
unsigned int bit_in;
unsigned int byte_out;
unsigned int pattern;
for(bit_in = 0, byte_out = 0; (bit_in < 8) && (byte_out < 4); bit_in++)
{
pattern = (byte_value_in & (1 << (7 - bit_in))) ? (use_i2s_invert ? on_pattern_invert : on_pattern_normal) : (use_i2s_invert ? off_pattern_invert : off_pattern_normal);
if(bit_in & 0x01) // after every second input bit the output nibble is full
{
buffer[byte_out] <<= 4;
buffer[byte_out] |= pattern;
byte_out++;
}
else
buffer[byte_out] = pattern;
}
// always send at least four bytes at once,
// to prevent padding zero bytes from being
i2s_send(sizeof(buffer), buffer); // inserted as i2s uses a fifo of 32 bit words
}
static void send_byte(unsigned int byte)
{
if(use_i2s)
send_byte_i2s(byte);
if(use_uart_0 || use_uart_1)
send_byte_uart(byte);
}
static void send_all(bool force)
{
static const uint8_t zero_sample_normal[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
static const uint8_t zero_sample_invert[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
unsigned int pin, fill;
for(pin = 0; pin < max_pins_per_io; pin++)
{
if(!force && !ledpixel_data_pin[pin].enabled)
break;
for(fill = ledpixel_data_pin[pin].fill8 ? 8 : 1; fill > 0; fill--)
{
if(ledpixel_data_pin[pin].grb)
{
send_byte((ledpixel_data_pin[pin].value & 0x0000ff00) >> 8);
send_byte((ledpixel_data_pin[pin].value & 0x00ff0000) >> 16);
}
else
{
send_byte((ledpixel_data_pin[pin].value & 0x00ff0000) >> 16);
send_byte((ledpixel_data_pin[pin].value & 0x0000ff00) >> 8);
}
send_byte((ledpixel_data_pin[pin].value & 0x000000ff) >> 0);
// some ws2812's have four leds (including a white one) and need an extra byte to be sent for it
if(ledpixel_data_pin[pin].extended)
send_byte((ledpixel_data_pin[pin].value & 0xff000000) >> 24);
}
if(use_uart_0)
uart_flush(0);
if(use_uart_1)
uart_flush(1);
}
if(use_i2s)
{
if(use_i2s_invert)
i2s_send(sizeof(zero_sample_invert), zero_sample_invert); // the last "sample" (4 bytes) get repeated until the transmitter is stopped
else
i2s_send(sizeof(zero_sample_normal), zero_sample_normal); // the last "sample" (4 bytes) get repeated until the transmitter is stopped
i2s_flush();
}
}
io_ledpixel_mode_t io_ledpixel_mode(unsigned int io, unsigned int pin)
{
unsigned int uart;
if(io != io_id_gpio)
return(ledpixel_invalid);
if(io_gpio_get_i2s_from_pin(pin) == gpio_i2s_pin_output_data)
return(ledpixel_i2s);
uart = io_gpio_get_uart_from_pin(pin);
if(uart == 0)
return(ledpixel_uart_0);
if(uart == 1)
return(ledpixel_uart_1);
return(ledpixel_invalid);
}
bool io_ledpixel_pre_init(unsigned int io, unsigned int pin)
{
io_config_pin_entry_t *pin_config;
switch(io_ledpixel_mode(io, pin))
{
case(ledpixel_i2s):
{
use_i2s = true;
pin_config = &io_config[io][pin];
if(pin_config->static_flags & io_flag_static_invert)
use_i2s_invert = true;
break;
}
case(ledpixel_uart_0):
{
use_uart_0 = true;
break;
}
case(ledpixel_uart_1):
{
use_uart_1 = true;
break;
}
default:
{
return(false);
}
}
return(true);
}
static io_error_t init(const struct io_info_entry_T *info)
{
if(!use_uart_0 && !use_uart_1 && !use_i2s)
return(io_error);
if(use_uart_0)
{
uart_baudrate(0, 3200000);
uart_data_bits(0, 6);
uart_stop_bits(0, 1);
uart_parity(0, parity_none);
}
if(use_uart_1)
{
uart_baudrate(1, 3200000);
uart_data_bits(1, 6);
uart_stop_bits(1, 1);
uart_parity(1, parity_none);
}
if(use_i2s && !i2s_init())
return(io_error);
return(io_ok);
}
static void post_init(const struct io_info_entry_T *info)
{
send_all(true);
}
static attr_pure unsigned int pin_max_value(const struct io_info_entry_T *info, io_data_pin_entry_t *data, const io_config_pin_entry_t *pin_config, unsigned int pin)
{
unsigned int value = 0;
if(pin_config->llmode == io_pin_ll_output_pwm1)
value = ledpixel_data_pin[pin].extended ? 0xffffffff : 0x00ffffff;
return(value);
}
static io_error_t init_pin_mode(string_t *error_message, const struct io_info_entry_T *info, io_data_pin_entry_t *pin_data, const io_config_pin_entry_t *pin_config, int pin)
{
ledpixel_data_pin[pin].enabled = pin_config->llmode == io_pin_ll_output_pwm1;
ledpixel_data_pin[pin].extended = !!(pin_config->static_flags & io_flag_static_extended);
ledpixel_data_pin[pin].grb = !!(pin_config->static_flags & io_flag_static_grb);
ledpixel_data_pin[pin].fill8 = !!(pin_config->static_flags & io_flag_static_fill8);
ledpixel_data_pin[pin].value = 0;
return(io_ok);
}
static io_error_t read_pin(string_t *error_message, const struct io_info_entry_T *info, io_data_pin_entry_t *pin_data, const io_config_pin_entry_t *pin_config, int pin, unsigned int *value)
{
*value = ledpixel_data_pin[pin].value;
return(io_ok);
}
static io_error_t write_pin(string_t *error_message, const struct io_info_entry_T *info, io_data_pin_entry_t *pin_data, const io_config_pin_entry_t *pin_config, int pin, unsigned int value)
{
ledpixel_data_pin[pin].value = value;
send_all(false);
return(io_ok);
}
io_error_t io_ledpixel_pinmask(unsigned int mask)
{
unsigned int pin;
unsigned int pinmask, flag, rgb;
if(!use_uart_0 && !use_uart_1 && !use_i2s)
return(io_error);
value_mask_to_rgb(mask, &pinmask, &flag, &rgb);
for(pin = 0; pin < max_pins_per_io; pin++)
{
if(!ledpixel_data_pin[pin].enabled)
break;
if(pinmask & (1 << pin))
ledpixel_data_pin[pin].value = rgb;
}
send_all(false);
return(io_ok);
}
roflash const io_info_entry_t io_info_entry_ledpixel =
{
io_id_ledpixel, /* = 6 */
0x00,
0,
16,
caps_output_pwm1,
"led string",
init,
post_init,
pin_max_value,
(void *)0, // periodic slow
(void *)0, // periodic fast
(void *)0, // pin change handler
init_pin_mode,
(void *)0, // get pin info
read_pin,
write_pin,
(void *)0, // set_mask // FIXME this can be implemented, but may not be very useful
};