9#include "esp_heap_caps.h"
12#include "esp_rom_gpio.h"
13#include "soc/spi_periph.h"
16#include "hal/spi_hal.h"
18#define LED_STRIP_SPI_DEFAULT_RESOLUTION (2.5 * 1000 * 1000)
19#define LED_STRIP_SPI_DEFAULT_TRANS_QUEUE_SIZE 4
21#define SPI_BYTES_PER_COLOR_BYTE 3
22#define SPI_BITS_PER_COLOR_BYTE (SPI_BYTES_PER_COLOR_BYTE * 8)
24static const char *
TAG =
"led_strip_spi";
40 *(buf + 2) |=
data & BIT(0) ? BIT(2) | BIT(1) : BIT(2);
41 *(buf + 2) |=
data & BIT(1) ? BIT(5) | BIT(4) : BIT(5);
42 *(buf + 2) |=
data & BIT(2) ? BIT(7) : 0x00;
44 *(buf + 1) |=
data & BIT(3) ? BIT(3) | BIT(2) : BIT(3);
45 *(buf + 1) |=
data & BIT(4) ? BIT(6) | BIT(5) : BIT(6);
46 *(buf + 0) |=
data & BIT(5) ? BIT(1) | BIT(0) : BIT(1);
47 *(buf + 0) |=
data & BIT(6) ? BIT(4) | BIT(3) : BIT(4);
48 *(buf + 0) |=
data & BIT(7) ? BIT(7) | BIT(6) : BIT(7);
54 ESP_RETURN_ON_FALSE(index < spi_strip->strip_len, ESP_ERR_INVALID_ARG,
TAG,
"index out of maximum number of LEDs");
70 ESP_RETURN_ON_FALSE(index < spi_strip->strip_len, ESP_ERR_INVALID_ARG,
TAG,
"index out of maximum number of LEDs");
71 ESP_RETURN_ON_FALSE(spi_strip->
bytes_per_pixel == 4, ESP_ERR_INVALID_ARG,
TAG,
"wrong LED pixel format, expected 4 bytes per pixel");
87 spi_transaction_t tx_conf;
88 memset(&tx_conf, 0,
sizeof(tx_conf));
92 tx_conf.rx_buffer = NULL;
127 ESP_GOTO_ON_FALSE(led_config && spi_config && ret_strip, ESP_ERR_INVALID_ARG, err,
TAG,
"invalid argument");
129 uint8_t bytes_per_pixel = 3;
137 uint32_t mem_caps = MALLOC_CAP_DEFAULT;
140 mem_caps |= MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA;
144 ESP_GOTO_ON_FALSE(spi_strip, ESP_ERR_NO_MEM, err,
TAG,
"no mem for spi strip");
148 spi_clock_source_t clk_src = SPI_CLK_SRC_DEFAULT;
153 spi_bus_config_t spi_bus_cfg = {
165 esp_rom_gpio_connect_out_signal(led_config->
strip_gpio_num, spi_periph_signal[spi_strip->
spi_host].spid_out,
true,
false);
168 spi_device_interface_config_t spi_dev_cfg = {
169 .clock_source = clk_src,
182 esp_rom_delay_us(10);
183 int clock_resolution_khz = 0;
184 spi_device_get_actual_freq(spi_strip->
spi_device, &clock_resolution_khz);
189 TAG,
"unsupported clock resolution:%dKHz", clock_resolution_khz);
199 *ret_strip = &spi_strip->
base;
#define ESP_RETURN_ON_ERROR(x, log_tag, format,...)
#define ESP_GOTO_ON_ERROR(x, goto_tag, log_tag, format,...)
static esp_err_t led_strip_spi_set_pixel(led_strip_t *strip, uint32_t index, uint32_t red, uint32_t green, uint32_t blue)
static esp_err_t led_strip_spi_refresh(led_strip_t *strip)
static 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)
static void __led_strip_spi_bit(uint8_t data, uint8_t *buf)
#define SPI_BITS_PER_COLOR_BYTE
#define SPI_BYTES_PER_COLOR_BYTE
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.
#define LED_STRIP_SPI_DEFAULT_TRANS_QUEUE_SIZE
static esp_err_t led_strip_spi_clear(led_strip_t *strip)
static esp_err_t led_strip_spi_del(led_strip_t *strip)
#define LED_STRIP_SPI_DEFAULT_RESOLUTION
@ LED_PIXEL_FORMAT_INVALID
struct led_strip_t * led_strip_handle_t
LED strip handle.
led_pixel_format_t led_pixel_format
struct led_strip_config_t::@145177150267040163336221331365072267202117177200 flags
LED Strip SPI specific configuration.
spi_host_device_t spi_bus
struct led_strip_spi_config_t::@242107237043306072174300312177366152113164360267 flags
spi_clock_source_t clk_src
spi_device_handle_t spi_device
spi_host_device_t spi_host
LED strip interface definition.
esp_err_t(* refresh)(led_strip_t *strip)
Refresh memory colors to LEDs.
esp_err_t(* set_pixel)(led_strip_t *strip, uint32_t index, uint32_t red, uint32_t green, uint32_t blue)
Set RGB for a specific pixel.
esp_err_t(* del)(led_strip_t *strip)
Free LED strip resources.
esp_err_t(* set_pixel_rgbw)(led_strip_t *strip, uint32_t index, uint32_t red, uint32_t green, uint32_t blue, uint32_t white)
Set RGBW for a specific pixel. Similar to set_pixel but also set the white component.
esp_err_t(* clear)(led_strip_t *strip)
Clear LED strip (turn off all LEDs).
static float data[128 *2]