20#include "driver/rmt_tx.h"
22#include "led_strip_interface.h"
23#include "led_strip_rmt_encoder.h"
25#define LED_STRIP_RMT_DEFAULT_RESOLUTION 10000000
26#define LED_STRIP_RMT_DEFAULT_TRANS_QUEUE_SIZE 4
28#ifndef LED_STRIP_RMT_DEFAULT_MEM_BLOCK_SYMBOLS_MULTIPLIER
29#if CONFIG_IDF_TARGET_ESP32C3
31#define LED_STRIP_RMT_DEFAULT_MEM_BLOCK_SYMBOLS_MULTIPLIER 2
33#define LED_STRIP_RMT_DEFAULT_MEM_BLOCK_SYMBOLS_MULTIPLIER 1
38#ifndef LED_STRIP_RMT_DEFAULT_MEM_BLOCK_SYMBOLS
39#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
40#define LED_STRIP_RMT_DEFAULT_MEM_BLOCK_SYMBOLS (LED_STRIP_RMT_DEFAULT_MEM_BLOCK_SYMBOLS_MULTIPLIER * 64)
42#define LED_STRIP_RMT_DEFAULT_MEM_BLOCK_SYMBOLS (LED_STRIP_RMT_DEFAULT_MEM_BLOCK_SYMBOLS_MULTIPLIER * 48)
47static const char *TAG =
"led_strip_rmt";
51 rmt_channel_handle_t rmt_chan;
52 rmt_encoder_handle_t strip_encoder;
54 uint8_t bytes_per_pixel;
59static esp_err_t led_strip_rmt_set_pixel(
led_strip_t *strip, uint32_t index, uint32_t red, uint32_t green, uint32_t blue)
61 led_strip_rmt_obj *rmt_strip = __containerof(strip, led_strip_rmt_obj, base);
62 ESP_RETURN_ON_FALSE(index < rmt_strip->strip_len, ESP_ERR_INVALID_ARG, TAG,
"index out of maximum number of LEDs");
65 uint32_t start = index * rmt_strip->bytes_per_pixel;
66 uint8_t *pixel_buf = rmt_strip->pixel_buf;
68 pixel_buf[start + component_fmt.
format.
r_pos] = red & 0xFF;
69 pixel_buf[start + component_fmt.
format.
g_pos] = green & 0xFF;
70 pixel_buf[start + component_fmt.
format.
b_pos] = blue & 0xFF;
78static esp_err_t led_strip_rmt_set_pixel_rgbw(
led_strip_t *strip, uint32_t index, uint32_t red, uint32_t green, uint32_t blue, uint32_t white)
80 led_strip_rmt_obj *rmt_strip = __containerof(strip, led_strip_rmt_obj, base);
82 ESP_RETURN_ON_FALSE(index < rmt_strip->strip_len, ESP_ERR_INVALID_ARG, TAG,
"index out of maximum number of LEDs");
83 ESP_RETURN_ON_FALSE(component_fmt.
format.
num_components == 4, ESP_ERR_INVALID_ARG, TAG,
"led doesn't have 4 components");
85 uint32_t start = index * rmt_strip->bytes_per_pixel;
86 uint8_t *pixel_buf = rmt_strip->pixel_buf;
88 pixel_buf[start + component_fmt.
format.
r_pos] = red & 0xFF;
89 pixel_buf[start + component_fmt.
format.
g_pos] = green & 0xFF;
90 pixel_buf[start + component_fmt.
format.
b_pos] = blue & 0xFF;
91 pixel_buf[start + component_fmt.
format.
w_pos] = white & 0xFF;
96static esp_err_t led_strip_rmt_refresh_async(
led_strip_t *strip)
98 led_strip_rmt_obj *rmt_strip = __containerof(strip, led_strip_rmt_obj, base);
99 rmt_transmit_config_t tx_conf = {
103 ESP_RETURN_ON_ERROR(rmt_enable(rmt_strip->rmt_chan), TAG,
"enable RMT channel failed");
104 ESP_RETURN_ON_ERROR(rmt_transmit(rmt_strip->rmt_chan, rmt_strip->strip_encoder, rmt_strip->pixel_buf,
105 rmt_strip->strip_len * rmt_strip->bytes_per_pixel, &tx_conf), TAG,
"transmit pixels by RMT failed");
109static esp_err_t led_strip_rmt_wait_for_done(
led_strip_t *strip)
111 led_strip_rmt_obj *rmt_strip = __containerof(strip, led_strip_rmt_obj, base);
112 ESP_RETURN_ON_ERROR(rmt_tx_wait_all_done(rmt_strip->rmt_chan, -1), TAG,
"wait for RMT done failed");
113 ESP_RETURN_ON_ERROR(rmt_disable(rmt_strip->rmt_chan), TAG,
"disable RMT channel failed");
117static esp_err_t led_strip_rmt_refresh(
led_strip_t *strip)
119 ESP_RETURN_ON_ERROR(led_strip_rmt_refresh_async(strip), TAG,
"refresh async failed");
120 ESP_RETURN_ON_ERROR(led_strip_rmt_wait_for_done(strip), TAG,
"wait for done failed");
124static esp_err_t led_strip_rmt_clear(
led_strip_t *strip)
126 led_strip_rmt_obj *rmt_strip = __containerof(strip, led_strip_rmt_obj, base);
128 memset(rmt_strip->pixel_buf, 0, rmt_strip->strip_len * rmt_strip->bytes_per_pixel);
129 return led_strip_rmt_refresh(strip);
132static esp_err_t led_strip_rmt_del(
led_strip_t *strip)
134 led_strip_rmt_obj *rmt_strip = __containerof(strip, led_strip_rmt_obj, base);
135 ESP_RETURN_ON_ERROR(rmt_del_channel(rmt_strip->rmt_chan), TAG,
"delete RMT channel failed");
136 ESP_RETURN_ON_ERROR(rmt_del_encoder(rmt_strip->strip_encoder), TAG,
"delete strip encoder failed");
143 led_strip_rmt_obj *rmt_strip = NULL;
144 esp_err_t ret = ESP_OK;
145 ESP_GOTO_ON_FALSE(led_config && rmt_config && ret_strip, ESP_ERR_INVALID_ARG, err, TAG,
"invalid argument");
149 component_fmt = LED_STRIP_COLOR_COMPONENT_FMT_GRB;
156 ESP_RETURN_ON_FALSE(mask == 0x07, ESP_ERR_INVALID_ARG, TAG,
"invalid order argument");
160 ESP_RETURN_ON_FALSE(mask == 0x0F, ESP_ERR_INVALID_ARG, TAG,
"invalid order argument");
162 ESP_RETURN_ON_FALSE(
false, ESP_ERR_INVALID_ARG, TAG,
"invalid number of color components: %d", component_fmt.
format.
num_components);
166 rmt_strip = calloc(1,
sizeof(led_strip_rmt_obj) + led_config->
max_leds * bytes_per_pixel);
167 ESP_GOTO_ON_FALSE(rmt_strip, ESP_ERR_NO_MEM, err, TAG,
"no mem for rmt strip");
171 rmt_clock_source_t clk_src = RMT_CLK_SRC_DEFAULT;
175 size_t mem_block_symbols = LED_STRIP_RMT_DEFAULT_MEM_BLOCK_SYMBOLS;
180 rmt_tx_channel_config_t rmt_chan_config = {
183 .mem_block_symbols = mem_block_symbols,
184 .resolution_hz = resolution,
185 .trans_queue_depth = LED_STRIP_RMT_DEFAULT_TRANS_QUEUE_SIZE,
189 ESP_GOTO_ON_ERROR(rmt_new_tx_channel(&rmt_chan_config, &rmt_strip->rmt_chan), err, TAG,
"create RMT TX channel failed");
194 .timings = led_config->
timings,
196 ESP_GOTO_ON_ERROR(rmt_new_led_strip_encoder(&strip_encoder_conf, &rmt_strip->strip_encoder), err, TAG,
"create LED strip encoder failed");
198 rmt_strip->component_fmt = component_fmt;
199 rmt_strip->bytes_per_pixel = bytes_per_pixel;
200 rmt_strip->strip_len = led_config->
max_leds;
201 rmt_strip->base.set_pixel = led_strip_rmt_set_pixel;
202 rmt_strip->base.set_pixel_rgbw = led_strip_rmt_set_pixel_rgbw;
203 rmt_strip->base.refresh = led_strip_rmt_refresh;
204 rmt_strip->base.refresh_async = led_strip_rmt_refresh_async;
205 rmt_strip->base.refresh_wait_done = led_strip_rmt_wait_for_done;
206 rmt_strip->base.clear = led_strip_rmt_clear;
207 rmt_strip->base.del = led_strip_rmt_del;
209 *ret_strip = &rmt_strip->base;
213 if (rmt_strip->rmt_chan) {
214 rmt_del_channel(rmt_strip->rmt_chan);
216 if (rmt_strip->strip_encoder) {
217 rmt_del_encoder(rmt_strip->strip_encoder);
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_encoder_timings_t timings
Type of led strip encoder configuration.
LED Strip RMT specific configuration.
struct led_strip_rmt_config_t::led_strip_rmt_extra_config flags
rmt_clock_source_t clk_src
LED strip interface definition.