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