Skip to content

Commit

Permalink
Merge pull request #21068 from IsikcanYilmaz/drivers/ws281x/esp32_rmt…
Browse files Browse the repository at this point in the history
…_symbols_at_init

drivers/ws281x: esp32 neopixel driver to set the rmt symbol high/low lengths at init instead of every write
  • Loading branch information
maribu authored Dec 13, 2024
2 parents d394f62 + 53a4a8a commit a0327ae
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions drivers/ws281x/esp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ static uint32_t _ws281x_one_on;
static uint32_t _ws281x_one_off;
static uint32_t _ws281x_zero_on;
static uint32_t _ws281x_zero_off;
static uint8_t channel;

static uint8_t _rmt_channel(ws281x_t *dev)
{
Expand All @@ -71,23 +72,6 @@ void ws281x_write_buffer(ws281x_t *dev, const void *buf, size_t size)
assert(dev);

#ifdef MODULE_WS281X_ESP32_HW
/* determine used RMT channel from configured GPIO */
uint8_t channel = _rmt_channel(dev);

/* determine the current clock frequency */
uint32_t freq;
if (rmt_get_counter_clock(channel, &freq) != ESP_OK) {
LOG_ERROR("[ws281x_esp32] Could not get RMT counter clock\n");
return;
}

/* compute phase times used in ws2812_rmt_adapter once rmt_write_sample is called */
uint32_t total_cycles = freq / (NS_PER_SEC / WS281X_T_DATA_NS);
_ws281x_one_on = freq / (NS_PER_SEC / WS281X_T_DATA_ONE_NS);
_ws281x_one_off = total_cycles - _ws281x_one_on;
_ws281x_zero_on = freq / (NS_PER_SEC / WS281X_T_DATA_ZERO_NS);
_ws281x_zero_off = total_cycles - _ws281x_zero_on;

rmt_write_sample(channel, (const uint8_t *)buf, size, false);
#else
const uint8_t *pos = buf;
Expand Down Expand Up @@ -192,7 +176,7 @@ int ws281x_init(ws281x_t *dev, const ws281x_params_t *params)
"[ws281x_esp32] RMT configuration problem");

/* determine used RMT channel from configured GPIO */
uint8_t channel = _rmt_channel(dev);
channel = _rmt_channel(dev);

rmt_config_t cfg = RMT_DEFAULT_CONFIG_TX(params->pin, channel);
cfg.clk_div = 2;
Expand All @@ -203,6 +187,21 @@ int ws281x_init(ws281x_t *dev, const ws281x_params_t *params)
LOG_ERROR("[ws281x_esp32] RMT initialization failed\n");
return -EIO;
}

/* determine the current clock frequency */
uint32_t freq;
if (rmt_get_counter_clock(channel, &freq) != ESP_OK) {
LOG_ERROR("[ws281x_esp32] Could not get RMT counter clock\n");
return -EIO;
}

/* compute phase times used in ws2812_rmt_adapter */
uint32_t total_cycles = freq / (NS_PER_SEC / WS281X_T_DATA_NS);
_ws281x_one_on = freq / (NS_PER_SEC / WS281X_T_DATA_ONE_NS);
_ws281x_one_off = total_cycles - _ws281x_one_on;
_ws281x_zero_on = freq / (NS_PER_SEC / WS281X_T_DATA_ZERO_NS);
_ws281x_zero_off = total_cycles - _ws281x_zero_on;

#else
if (gpio_init(dev->params.pin, GPIO_OUT)) {
return -EIO;
Expand Down

0 comments on commit a0327ae

Please sign in to comment.