5#if FASTLED_ESP32_HAS_CLOCKLESS_SPI
17#include "esp_rom_gpio.h"
18#include "soc/spi_periph.h"
23#define LED_STRIP_SPI_DEFAULT_RESOLUTION (2.5 * 1000 * 1000)
24#define LED_STRIP_SPI_DEFAULT_TRANS_QUEUE_SIZE 4
26#define SPI_BYTES_PER_COLOR_BYTE 3
27#define SPI_BITS_PER_COLOR_BYTE (SPI_BYTES_PER_COLOR_BYTE * 8)
35#ifndef FASTLED_ESP32_SPI_HACK_NO_TRANSACTION_WAIT
36#define FASTLED_ESP32_SPI_HACK_NO_TRANSACTION_WAIT 0
39static const char *TAG =
"led_strip_spi";
43 spi_host_device_t spi_host;
44 spi_device_handle_t spi_device;
46 uint8_t bytes_per_pixel;
48 spi_transaction_t tx_trans;
54static void __led_strip_spi_bit(uint8_t data, uint8_t *buf)
58 *(buf + 2) |= data & BIT(0) ? BIT(2) | BIT(1) : BIT(2);
59 *(buf + 2) |= data & BIT(1) ? BIT(5) | BIT(4) : BIT(5);
60 *(buf + 2) |= data & BIT(2) ? BIT(7) : 0x00;
62 *(buf + 1) |= data & BIT(3) ? BIT(3) | BIT(2) : BIT(3);
63 *(buf + 1) |= data & BIT(4) ? BIT(6) | BIT(5) : BIT(6);
64 *(buf + 0) |= data & BIT(5) ? BIT(1) | BIT(0) : BIT(1);
65 *(buf + 0) |= data & BIT(6) ? BIT(4) | BIT(3) : BIT(4);
66 *(buf + 0) |= data & BIT(7) ? BIT(7) | BIT(6) : BIT(7);
69static esp_err_t led_strip_spi_set_pixel(
led_strip_t *strip, uint32_t index, uint32_t red, uint32_t green, uint32_t blue)
71 led_strip_spi_obj *spi_strip = __containerof(strip, led_strip_spi_obj, base);
72 ESP_RETURN_ON_FALSE(index < spi_strip->strip_len, ESP_ERR_INVALID_ARG, TAG,
"index out of maximum number of LEDs");
74 uint32_t start = index * spi_strip->bytes_per_pixel * SPI_BYTES_PER_COLOR_BYTE;
75 uint8_t *pixel_buf = spi_strip->pixel_buf;
77 memset(pixel_buf + start, 0, spi_strip->bytes_per_pixel * SPI_BYTES_PER_COLOR_BYTE);
79 __led_strip_spi_bit(red, &pixel_buf[start + SPI_BYTES_PER_COLOR_BYTE * component_fmt.
format.
r_pos]);
80 __led_strip_spi_bit(green, &pixel_buf[start + SPI_BYTES_PER_COLOR_BYTE * component_fmt.
format.
g_pos]);
81 __led_strip_spi_bit(blue, &pixel_buf[start + SPI_BYTES_PER_COLOR_BYTE * component_fmt.
format.
b_pos]);
83 __led_strip_spi_bit(0, &pixel_buf[start + SPI_BYTES_PER_COLOR_BYTE * component_fmt.
format.
w_pos]);
89static 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)
91 led_strip_spi_obj *spi_strip = __containerof(strip, led_strip_spi_obj, base);
93 ESP_RETURN_ON_FALSE(index < spi_strip->strip_len, ESP_ERR_INVALID_ARG, TAG,
"index out of maximum number of LEDs");
94 ESP_RETURN_ON_FALSE(component_fmt.
format.
num_components == 4, ESP_ERR_INVALID_ARG, TAG,
"led doesn't have 4 components");
97 uint32_t start = index * spi_strip->bytes_per_pixel * SPI_BYTES_PER_COLOR_BYTE;
98 uint8_t *pixel_buf = spi_strip->pixel_buf;
99 memset(pixel_buf + start, 0, spi_strip->bytes_per_pixel * SPI_BYTES_PER_COLOR_BYTE);
101 __led_strip_spi_bit(red, &pixel_buf[start + SPI_BYTES_PER_COLOR_BYTE * component_fmt.
format.
r_pos]);
102 __led_strip_spi_bit(green, &pixel_buf[start + SPI_BYTES_PER_COLOR_BYTE * component_fmt.
format.
g_pos]);
103 __led_strip_spi_bit(blue, &pixel_buf[start + SPI_BYTES_PER_COLOR_BYTE * component_fmt.
format.
b_pos]);
104 __led_strip_spi_bit(white, &pixel_buf[start + SPI_BYTES_PER_COLOR_BYTE * component_fmt.
format.
w_pos]);
109static esp_err_t spi_led_strip_refresh_async(
led_strip_t *strip)
111 led_strip_spi_obj *spi_strip = __containerof(strip, led_strip_spi_obj, base);
114 if (spi_strip->trans_pending) {
115 spi_transaction_t* trans_ptr;
116 spi_device_get_trans_result(spi_strip->spi_device, &trans_ptr, pdMS_TO_TICKS(1000));
117 spi_strip->trans_pending =
false;
121 memset(&spi_strip->tx_trans, 0,
sizeof(spi_strip->tx_trans));
122 spi_strip->tx_trans.length = spi_strip->strip_len * spi_strip->bytes_per_pixel * SPI_BITS_PER_COLOR_BYTE;
123 spi_strip->tx_trans.tx_buffer = spi_strip->pixel_buf;
124 spi_strip->tx_trans.rx_buffer = NULL;
127 esp_err_t ret = spi_device_queue_trans(spi_strip->spi_device, &spi_strip->tx_trans, portMAX_DELAY);
129 spi_strip->trans_pending =
true;
135static esp_err_t spi_led_strip_refresh_wait_done(
led_strip_t *strip)
137 led_strip_spi_obj *spi_strip = __containerof(strip, led_strip_spi_obj, base);
140 if (spi_strip->trans_pending) {
141 spi_transaction_t* trans_ptr;
142 esp_err_t ret = spi_device_get_trans_result(spi_strip->spi_device, &trans_ptr, pdMS_TO_TICKS(1000));
144 spi_strip->trans_pending =
false;
152static esp_err_t led_strip_spi_refresh(
led_strip_t *strip)
154 ESP_RETURN_ON_ERROR(spi_led_strip_refresh_async(strip), TAG,
"refresh async failed");
155 ESP_RETURN_ON_ERROR(spi_led_strip_refresh_wait_done(strip), TAG,
"wait for done failed");
160static esp_err_t led_strip_spi_clear(
led_strip_t *strip)
162 led_strip_spi_obj *spi_strip = __containerof(strip, led_strip_spi_obj, base);
164 memset(spi_strip->pixel_buf, 0, spi_strip->strip_len * spi_strip->bytes_per_pixel * SPI_BYTES_PER_COLOR_BYTE);
165 uint8_t *buf = spi_strip->pixel_buf;
166 for (
int index = 0; index < spi_strip->strip_len * spi_strip->bytes_per_pixel; index++) {
167 __led_strip_spi_bit(0, buf);
168 buf += SPI_BYTES_PER_COLOR_BYTE;
171 return led_strip_spi_refresh(strip);
174static esp_err_t led_strip_spi_del(
led_strip_t *strip)
176 led_strip_spi_obj *spi_strip = __containerof(strip, led_strip_spi_obj, base);
179 if (spi_strip->trans_pending) {
180 spi_transaction_t* trans_ptr;
181 spi_device_get_trans_result(spi_strip->spi_device, &trans_ptr, pdMS_TO_TICKS(1000));
182 spi_strip->trans_pending =
false;
185 ESP_RETURN_ON_ERROR(spi_bus_remove_device(spi_strip->spi_device), TAG,
"delete spi device failed");
186 ESP_RETURN_ON_ERROR(spi_bus_free(spi_strip->spi_host), TAG,
"free spi bus failed");
194 led_strip_spi_obj *spi_strip = NULL;
195 esp_err_t ret = ESP_OK;
197 ESP_GOTO_ON_FALSE(led_config && spi_config && ret_strip, ESP_ERR_INVALID_ARG, err, TAG,
"invalid argument");
208 ESP_RETURN_ON_FALSE(mask == 0x07, ESP_ERR_INVALID_ARG, TAG,
"invalid order argument");
212 ESP_RETURN_ON_FALSE(mask == 0x0F, ESP_ERR_INVALID_ARG, TAG,
"invalid order argument");
214 ESP_RETURN_ON_FALSE(
false, ESP_ERR_INVALID_ARG, TAG,
"invalid number of color components: %d", component_fmt.
format.
num_components);
218 uint32_t mem_caps = MALLOC_CAP_DEFAULT;
219 if (spi_config->
flags.with_dma) {
221 mem_caps |= MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA;
223 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);
225 ESP_GOTO_ON_FALSE(spi_strip, ESP_ERR_NO_MEM, err, TAG,
"no mem for spi strip");
227 spi_strip->spi_host = spi_config->
spi_bus;
229 spi_clock_source_t clk_src = SPI_CLK_SRC_DEFAULT;
234 spi_bus_config_t spi_bus_cfg = {
241 .max_transfer_sz = led_config->
max_leds * bytes_per_pixel * SPI_BYTES_PER_COLOR_BYTE,
243 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");
246 esp_rom_gpio_connect_out_signal(led_config->
strip_gpio_num, spi_periph_signal[spi_strip->spi_host].spid_out,
true,
false);
249 spi_device_interface_config_t spi_dev_cfg = {
250 .clock_source = clk_src,
254 .clock_speed_hz = LED_STRIP_SPI_DEFAULT_RESOLUTION,
258 .queue_size = LED_STRIP_SPI_DEFAULT_TRANS_QUEUE_SIZE,
261 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");
263 esp_rom_delay_us(10);
264 int clock_resolution_khz = 0;
265 spi_device_get_actual_freq(spi_strip->spi_device, &clock_resolution_khz);
269 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,
270 TAG,
"unsupported clock resolution:%dKHz", clock_resolution_khz);
272 spi_strip->component_fmt = component_fmt;
273 spi_strip->bytes_per_pixel = bytes_per_pixel;
274 spi_strip->strip_len = led_config->
max_leds;
275 spi_strip->trans_pending =
false;
276 spi_strip->base.set_pixel = led_strip_spi_set_pixel;
277 spi_strip->base.set_pixel_rgbw = led_strip_spi_set_pixel_rgbw;
278 spi_strip->base.refresh = led_strip_spi_refresh;
279 spi_strip->base.refresh_async = spi_led_strip_refresh_async;
280 spi_strip->base.refresh_wait_done = spi_led_strip_refresh_wait_done;
281 spi_strip->base.clear = led_strip_spi_clear;
282 spi_strip->base.del = led_strip_spi_del;
284 *ret_strip = &spi_strip->base;
288 if (spi_strip->spi_device) {
289 spi_bus_remove_device(spi_strip->spi_device);
291 if (spi_strip->spi_host) {
292 spi_bus_free(spi_strip->spi_host);
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 Strip common configurations The common configurations are not specific to any backend peripheral.
#define FASTLED_UNUSED(x)