Skip to content

Commit

Permalink
drivers/ws281x: improve timing for ESP32x
Browse files Browse the repository at this point in the history
If overhead like the loop control or the calculation of the waiting times for the next bit are performed while waiting for the end of the LOW phase, the time required for such operations is included in the LOW phase. This makes both the LOW phase and the period more precise.
  • Loading branch information
gschorcht committed Mar 25, 2023
1 parent 71e5b2c commit c40e015
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions drivers/ws281x/esp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,23 @@ void ws281x_write_buffer(ws281x_t *dev, const void *buf, size_t size)
on_wait = zero_on;
off_wait = zero_off;
}
data <<= 1;
while (cpu_hal_get_cycle_count() < current_wait) { }
/* end of LOW phase and start of HIGH phase */
start = cpu_hal_get_cycle_count();
gpio_set(dev->params.pin);
current_wait = start + on_wait;
while (cpu_hal_get_cycle_count() < current_wait) { }
gpio_clear(dev->params.pin);
/* end of HIGH phase and start of HIGH phase */
start = cpu_hal_get_cycle_count();
gpio_clear(dev->params.pin);
current_wait = start + off_wait;
while (cpu_hal_get_cycle_count() < current_wait) { }
data <<= 1;
}
pos++;
}
/* final LOW phase */
current_wait = cpu_hal_get_cycle_count();
/* end of final LOW phase */
}

int ws281x_init(ws281x_t *dev, const ws281x_params_t *params)
Expand Down

0 comments on commit c40e015

Please sign in to comment.