1616#include "esp_heap_caps.h"
1717
1818#define LED_STRIP_SPI_DEFAULT_RESOLUTION (2.5 * 1000 * 1000) // 2.5MHz resolution
19+ #define LED_STRIP_SPI_WS2816_RESOLUTION (2.7 * 1000 * 1000) // 2.7MHz resolution
1920#define LED_STRIP_SPI_DEFAULT_TRANS_QUEUE_SIZE 4
2021
2122#define SPI_BYTES_PER_COLOR_BYTE 3
@@ -154,12 +155,14 @@ esp_err_t led_strip_new_spi_device(const led_strip_config_t *led_config, const l
154155 esp_err_t ret = ESP_OK ;
155156 ESP_GOTO_ON_FALSE (led_config && spi_config && ret_strip , ESP_ERR_INVALID_ARG , err , TAG , "invalid argument" );
156157 led_color_component_format_t component_fmt = led_config -> color_component_format ;
158+ uint32_t clock_speed_hz = LED_STRIP_SPI_DEFAULT_RESOLUTION ;
157159 // If R/G/B order is not specified, set default GRB order as fallback
158160 if (component_fmt .format_id == 0 ) {
159161 component_fmt = LED_STRIP_COLOR_COMPONENT_FMT_GRB ;
160162 }
161163 if (led_config -> led_model == LED_MODEL_WS2816 ) {
162164 component_fmt .format .bytes_per_color = 2 ;
165+ clock_speed_hz = LED_STRIP_SPI_WS2816_RESOLUTION ;
163166 }
164167 if (component_fmt .format .bytes_per_color == 0 ) {
165168 component_fmt .format .bytes_per_color = 1 ;
@@ -216,7 +219,7 @@ esp_err_t led_strip_new_spi_device(const led_strip_config_t *led_config, const l
216219 .command_bits = 0 ,
217220 .address_bits = 0 ,
218221 .dummy_bits = 0 ,
219- .clock_speed_hz = LED_STRIP_SPI_DEFAULT_RESOLUTION ,
222+ .clock_speed_hz = clock_speed_hz ,
220223 .mode = 0 ,
221224 //set -1 when CS is not used
222225 .spics_io_num = -1 ,
@@ -229,13 +232,12 @@ esp_err_t led_strip_new_spi_device(const led_strip_config_t *led_config, const l
229232 int clock_resolution_khz = 0 ;
230233 spi_device_get_actual_freq (spi_strip -> spi_device , & clock_resolution_khz );
231234 // TODO: ideally we should decide the SPI_BYTES_PER_COLOR_BYTE by the real clock resolution
232- // 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
233- // clock_resolution between 2.2MHz to 2.8MHz is supported
234- 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 ,
235+ // But now, let's fixed the resolution
236+ ESP_GOTO_ON_FALSE ((clock_resolution_khz < clock_speed_hz / 1000 + 300 ) && (clock_resolution_khz > clock_speed_hz / 1000 - 300 ), ESP_ERR_NOT_SUPPORTED , err ,
235237 TAG , "unsupported clock resolution:%dKHz" , clock_resolution_khz );
236238
237- if (led_config -> led_model != LED_MODEL_WS2812 ) {
238- ESP_LOGW (TAG , "Only support WS2812. The timing requirements for other models may not be met" );
239+ if (led_config -> led_model != LED_MODEL_WS2812 && led_config -> led_model != LED_MODEL_WS2816 ) {
240+ ESP_LOGW (TAG , "Only support WS2812 and WS2816 . The timing requirements for other models may not be met" );
239241 }
240242
241243 spi_strip -> component_fmt = component_fmt ;
0 commit comments