FastLED 3.9.15
Loading...
Searching...
No Matches
led_strip_spi_dev.c
Go to the documentation of this file.
1#ifdef ESP32
2
3#include "enabled.h"
4
5#if FASTLED_ESP32_HAS_CLOCKLESS_SPI
6/*
7 * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
8 *
9 * SPDX-License-Identifier: Apache-2.0
10 */
11
12#include <stdlib.h>
13#include <string.h>
14#include <sys/cdefs.h>
15#include "esp_log.h"
16#include "esp_check.h"
17#include "esp_rom_gpio.h"
18#include "soc/spi_periph.h"
19#include "led_strip.h"
20#include "led_strip_interface.h"
21#include "fl/unused.h"
22
23#define LED_STRIP_SPI_DEFAULT_RESOLUTION (2.5 * 1000 * 1000) // 2.5MHz resolution
24#define LED_STRIP_SPI_DEFAULT_TRANS_QUEUE_SIZE 4
25
26#define SPI_BYTES_PER_COLOR_BYTE 3
27#define SPI_BITS_PER_COLOR_BYTE (SPI_BYTES_PER_COLOR_BYTE * 8)
28
29static const char *TAG = "led_strip_spi";
30
31typedef struct {
32 led_strip_t base;
33 spi_host_device_t spi_host;
34 spi_device_handle_t spi_device;
35 uint32_t strip_len;
36 uint8_t bytes_per_pixel;
37 led_color_component_format_t component_fmt;
38 uint8_t pixel_buf[];
39} led_strip_spi_obj;
40
41// please make sure to zero-initialize the buf before calling this function
42static void __led_strip_spi_bit(uint8_t data, uint8_t *buf)
43{
44 // Each color of 1 bit is represented by 3 bits of SPI, low_level:100 ,high_level:110
45 // So a color byte occupies 3 bytes of SPI.
46 *(buf + 2) |= data & BIT(0) ? BIT(2) | BIT(1) : BIT(2);
47 *(buf + 2) |= data & BIT(1) ? BIT(5) | BIT(4) : BIT(5);
48 *(buf + 2) |= data & BIT(2) ? BIT(7) : 0x00;
49 *(buf + 1) |= BIT(0);
50 *(buf + 1) |= data & BIT(3) ? BIT(3) | BIT(2) : BIT(3);
51 *(buf + 1) |= data & BIT(4) ? BIT(6) | BIT(5) : BIT(6);
52 *(buf + 0) |= data & BIT(5) ? BIT(1) | BIT(0) : BIT(1);
53 *(buf + 0) |= data & BIT(6) ? BIT(4) | BIT(3) : BIT(4);
54 *(buf + 0) |= data & BIT(7) ? BIT(7) | BIT(6) : BIT(7);
55}
56
57static esp_err_t led_strip_spi_set_pixel(led_strip_t *strip, uint32_t index, uint32_t red, uint32_t green, uint32_t blue)
58{
59 led_strip_spi_obj *spi_strip = __containerof(strip, led_strip_spi_obj, base);
60 ESP_RETURN_ON_FALSE(index < spi_strip->strip_len, ESP_ERR_INVALID_ARG, TAG, "index out of maximum number of LEDs");
61 // 3 pixels take 72bits(9bytes)
62 uint32_t start = index * spi_strip->bytes_per_pixel * SPI_BYTES_PER_COLOR_BYTE;
63 uint8_t *pixel_buf = spi_strip->pixel_buf;
64 led_color_component_format_t component_fmt = spi_strip->component_fmt;
65 memset(pixel_buf + start, 0, spi_strip->bytes_per_pixel * SPI_BYTES_PER_COLOR_BYTE);
66
67 __led_strip_spi_bit(red, &pixel_buf[start + SPI_BYTES_PER_COLOR_BYTE * component_fmt.format.r_pos]);
68 __led_strip_spi_bit(green, &pixel_buf[start + SPI_BYTES_PER_COLOR_BYTE * component_fmt.format.g_pos]);
69 __led_strip_spi_bit(blue, &pixel_buf[start + SPI_BYTES_PER_COLOR_BYTE * component_fmt.format.b_pos]);
70 if (component_fmt.format.num_components > 3) {
71 __led_strip_spi_bit(0, &pixel_buf[start + SPI_BYTES_PER_COLOR_BYTE * component_fmt.format.w_pos]);
72 }
73
74 return ESP_OK;
75}
76
77static esp_err_t led_strip_spi_set_pixel_rgbw(led_strip_t *strip, uint32_t index, uint32_t red, uint32_t green, uint32_t blue, uint32_t white)
78{
79 led_strip_spi_obj *spi_strip = __containerof(strip, led_strip_spi_obj, base);
80 led_color_component_format_t component_fmt = spi_strip->component_fmt;
81 ESP_RETURN_ON_FALSE(index < spi_strip->strip_len, ESP_ERR_INVALID_ARG, TAG, "index out of maximum number of LEDs");
82 ESP_RETURN_ON_FALSE(component_fmt.format.num_components == 4, ESP_ERR_INVALID_ARG, TAG, "led doesn't have 4 components");
83
84 // LED_PIXEL_FORMAT_GRBW takes 96bits(12bytes)
85 uint32_t start = index * spi_strip->bytes_per_pixel * SPI_BYTES_PER_COLOR_BYTE;
86 uint8_t *pixel_buf = spi_strip->pixel_buf;
87 memset(pixel_buf + start, 0, spi_strip->bytes_per_pixel * SPI_BYTES_PER_COLOR_BYTE);
88
89 __led_strip_spi_bit(red, &pixel_buf[start + SPI_BYTES_PER_COLOR_BYTE * component_fmt.format.r_pos]);
90 __led_strip_spi_bit(green, &pixel_buf[start + SPI_BYTES_PER_COLOR_BYTE * component_fmt.format.g_pos]);
91 __led_strip_spi_bit(blue, &pixel_buf[start + SPI_BYTES_PER_COLOR_BYTE * component_fmt.format.b_pos]);
92 __led_strip_spi_bit(white, &pixel_buf[start + SPI_BYTES_PER_COLOR_BYTE * component_fmt.format.w_pos]);
93
94 return ESP_OK;
95}
96
97
98static esp_err_t spi_led_strip_refresh_async(led_strip_t *strip)
99{
100 led_strip_spi_obj *spi_strip = __containerof(strip, led_strip_spi_obj, base);
101 spi_transaction_t tx_conf;
102 memset(&tx_conf, 0, sizeof(tx_conf));
103
104 tx_conf.length = spi_strip->strip_len * spi_strip->bytes_per_pixel * SPI_BITS_PER_COLOR_BYTE;
105 tx_conf.tx_buffer = spi_strip->pixel_buf;
106 tx_conf.rx_buffer = NULL;
107 spi_device_queue_trans(spi_strip->spi_device, &tx_conf, portMAX_DELAY);
108 return ESP_OK;
109}
110
111static esp_err_t spi_led_strip_refresh_wait_done(led_strip_t *strip)
112{
113 led_strip_spi_obj *spi_strip = __containerof(strip, led_strip_spi_obj, base);
114 spi_transaction_t* tx_conf = 0;
115 spi_device_get_trans_result(spi_strip->spi_device, &tx_conf, portMAX_DELAY);
116 return ESP_OK;
117}
118
119static esp_err_t led_strip_spi_refresh(led_strip_t *strip)
120{
121 ESP_RETURN_ON_ERROR(spi_led_strip_refresh_async(strip), TAG, "refresh async failed");
122 ESP_RETURN_ON_ERROR(spi_led_strip_refresh_wait_done(strip), TAG, "wait for done failed");
123 return ESP_OK;
124}
125
126
127static esp_err_t led_strip_spi_clear(led_strip_t *strip)
128{
129 led_strip_spi_obj *spi_strip = __containerof(strip, led_strip_spi_obj, base);
130 //Write zero to turn off all leds
131 memset(spi_strip->pixel_buf, 0, spi_strip->strip_len * spi_strip->bytes_per_pixel * SPI_BYTES_PER_COLOR_BYTE);
132 uint8_t *buf = spi_strip->pixel_buf;
133 for (int index = 0; index < spi_strip->strip_len * spi_strip->bytes_per_pixel; index++) {
134 __led_strip_spi_bit(0, buf);
135 buf += SPI_BYTES_PER_COLOR_BYTE;
136 }
137
138 return led_strip_spi_refresh(strip);
139}
140
141static esp_err_t led_strip_spi_del(led_strip_t *strip)
142{
143 led_strip_spi_obj *spi_strip = __containerof(strip, led_strip_spi_obj, base);
144
145 ESP_RETURN_ON_ERROR(spi_bus_remove_device(spi_strip->spi_device), TAG, "delete spi device failed");
146 ESP_RETURN_ON_ERROR(spi_bus_free(spi_strip->spi_host), TAG, "free spi bus failed");
147
148 free(spi_strip);
149 return ESP_OK;
150}
151
152esp_err_t led_strip_new_spi_device(const led_strip_config_t *led_config, const led_strip_spi_config_t *spi_config, led_strip_handle_t *ret_strip)
153{
154 led_strip_spi_obj *spi_strip = NULL;
155 esp_err_t ret = ESP_OK;
156 FASTLED_UNUSED(ret);
157 ESP_GOTO_ON_FALSE(led_config && spi_config && ret_strip, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
158 led_color_component_format_t component_fmt = led_config->color_component_format;
159 // If R/G/B order is not specified, set default GRB order as fallback
160 if (component_fmt.format_id == 0) {
161 component_fmt = LED_STRIP_COLOR_COMPONENT_FMT_GRB;
162 }
163 // check the validation of the color component format
164 uint8_t mask = 0;
165 if (component_fmt.format.num_components == 3) {
166 mask = BIT(component_fmt.format.r_pos) | BIT(component_fmt.format.g_pos) | BIT(component_fmt.format.b_pos);
167 // Check for invalid values
168 ESP_RETURN_ON_FALSE(mask == 0x07, ESP_ERR_INVALID_ARG, TAG, "invalid order argument");
169 } else if (component_fmt.format.num_components == 4) {
170 mask = BIT(component_fmt.format.r_pos) | BIT(component_fmt.format.g_pos) | BIT(component_fmt.format.b_pos) | BIT(component_fmt.format.w_pos);
171 // Check for invalid values
172 ESP_RETURN_ON_FALSE(mask == 0x0F, ESP_ERR_INVALID_ARG, TAG, "invalid order argument");
173 } else {
174 ESP_RETURN_ON_FALSE(false, ESP_ERR_INVALID_ARG, TAG, "invalid number of color components: %d", component_fmt.format.num_components);
175 }
176 // TODO: we assume each color component is 8 bits, may need to support other configurations in the future, e.g. 10bits per color component?
177 uint8_t bytes_per_pixel = component_fmt.format.num_components;
178 uint32_t mem_caps = MALLOC_CAP_DEFAULT;
179 if (spi_config->flags.with_dma) {
180 // DMA buffer must be placed in internal SRAM
181 mem_caps |= MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA;
182 }
183 spi_strip = heap_caps_calloc(1, sizeof(led_strip_spi_obj) + led_config->max_leds * bytes_per_pixel * SPI_BYTES_PER_COLOR_BYTE, mem_caps);
184
185 ESP_GOTO_ON_FALSE(spi_strip, ESP_ERR_NO_MEM, err, TAG, "no mem for spi strip");
186
187 spi_strip->spi_host = spi_config->spi_bus;
188 // for backward compatibility, if the user does not set the clk_src, use the default value
189 spi_clock_source_t clk_src = SPI_CLK_SRC_DEFAULT;
190 if (spi_config->clk_src) {
191 clk_src = spi_config->clk_src;
192 }
193
194 spi_bus_config_t spi_bus_cfg = {
195 .mosi_io_num = led_config->strip_gpio_num,
196 //Only use MOSI to generate the signal, set -1 when other pins are not used.
197 .miso_io_num = -1,
198 .sclk_io_num = -1,
199 .quadwp_io_num = -1,
200 .quadhd_io_num = -1,
201 .max_transfer_sz = led_config->max_leds * bytes_per_pixel * SPI_BYTES_PER_COLOR_BYTE,
202 };
203 ESP_GOTO_ON_ERROR(spi_bus_initialize(spi_strip->spi_host, &spi_bus_cfg, spi_config->flags.with_dma ? SPI_DMA_CH_AUTO : SPI_DMA_DISABLED), err, TAG, "create SPI bus failed");
204
205 if (led_config->flags.invert_out == true) {
206 esp_rom_gpio_connect_out_signal(led_config->strip_gpio_num, spi_periph_signal[spi_strip->spi_host].spid_out, true, false);
207 }
208
209 spi_device_interface_config_t spi_dev_cfg = {
210 .clock_source = clk_src,
211 .command_bits = 0,
212 .address_bits = 0,
213 .dummy_bits = 0,
214 .clock_speed_hz = LED_STRIP_SPI_DEFAULT_RESOLUTION,
215 .mode = 0,
216 //set -1 when CS is not used
217 .spics_io_num = -1,
218 .queue_size = LED_STRIP_SPI_DEFAULT_TRANS_QUEUE_SIZE,
219 };
220
221 ESP_GOTO_ON_ERROR(spi_bus_add_device(spi_strip->spi_host, &spi_dev_cfg, &spi_strip->spi_device), err, TAG, "Failed to add spi device");
222 //ensure the reset time is enough
223 esp_rom_delay_us(10);
224 int clock_resolution_khz = 0;
225 spi_device_get_actual_freq(spi_strip->spi_device, &clock_resolution_khz);
226 // TODO: ideally we should decide the SPI_BYTES_PER_COLOR_BYTE by the real clock resolution
227 // But now, let's fixed the resolution, the downside is, we don't support a clock source whose frequency is not multiple of LED_STRIP_SPI_DEFAULT_RESOLUTION
228 // clock_resolution between 2.2MHz to 2.8MHz is supported
229 ESP_GOTO_ON_FALSE((clock_resolution_khz < LED_STRIP_SPI_DEFAULT_RESOLUTION / 1000 + 300) && (clock_resolution_khz > LED_STRIP_SPI_DEFAULT_RESOLUTION / 1000 - 300), ESP_ERR_NOT_SUPPORTED, err,
230 TAG, "unsupported clock resolution:%dKHz", clock_resolution_khz);
231
232 spi_strip->component_fmt = component_fmt;
233 spi_strip->bytes_per_pixel = bytes_per_pixel;
234 spi_strip->strip_len = led_config->max_leds;
235 spi_strip->base.set_pixel = led_strip_spi_set_pixel;
236 spi_strip->base.set_pixel_rgbw = led_strip_spi_set_pixel_rgbw;
237 spi_strip->base.refresh = led_strip_spi_refresh;
238 spi_strip->base.refresh_async = spi_led_strip_refresh_async;
239 spi_strip->base.refresh_wait_done = spi_led_strip_refresh_wait_done;
240 spi_strip->base.clear = led_strip_spi_clear;
241 spi_strip->base.del = led_strip_spi_del;
242
243 *ret_strip = &spi_strip->base;
244 return ESP_OK;
245err:
246 if (spi_strip) {
247 if (spi_strip->spi_device) {
248 spi_bus_remove_device(spi_strip->spi_device);
249 }
250 if (spi_strip->spi_host) {
251 spi_bus_free(spi_strip->spi_host);
252 }
253 free(spi_strip);
254 }
255 return ret;
256}
257
258
259#endif // FASTLED_ESP_HAS_CLOCKLESS_SPI
260
261#endif // ESP32
LED strip interface definition.
esp_err_t led_strip_new_spi_device(const led_strip_config_t *led_config, const led_strip_spi_config_t *spi_config, led_strip_handle_t *ret_strip)
Create LED strip based on SPI MOSI channel.
spi_host_device_t spi_bus
spi_clock_source_t clk_src
struct led_strip_spi_config_t::@276143246045101136044262336246252357033024237362 flags
LED Strip SPI specific configuration.
#define LED_STRIP_COLOR_COMPONENT_FMT_GRB
Helper macros to set the color component format.
struct led_strip_t * led_strip_handle_t
Type of LED strip handle.
led_color_component_format_t color_component_format
struct led_strip_config_t::led_strip_extra_flags flags
struct led_color_component_format_t::format_layout format
LED color component format.
LED Strip common configurations The common configurations are not specific to any backend peripheral.
#define FASTLED_UNUSED(x)
Definition unused.h:3