Try to increase NCO phase precision

This commit is contained in:
Jan Hamal Dvořák 2024-08-10 12:12:50 +02:00
parent f10f8d5ceb
commit 758488c054

View file

@ -132,13 +132,23 @@ static void init_lo()
pio_sm_exec_wait_blocking(PIO, SM_LO, pio_encode_set(pio_pins, 0)); pio_sm_exec_wait_blocking(PIO, SM_LO, pio_encode_set(pio_pins, 0));
} }
inline static uint32_t phase_bit(uint32_t phase, uint32_t step)
{
uint32_t next = phase + step;
if ((next & 0x7fffffff) > (step >> 1))
return next >> 31;
return phase >> 31;
}
static void nco_generate_phase(uint32_t *buf, size_t len, uint32_t step, uint32_t phase) static void nco_generate_phase(uint32_t *buf, size_t len, uint32_t step, uint32_t phase)
{ {
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
uint32_t bits = 0; uint32_t bits = 0;
for (int j = 0; j < 32; j++) { for (int j = 0; j < 32; j++) {
bits |= phase >> 31; bits |= phase_bit(phase, step);
bits <<= 1; bits <<= 1;
phase += step; phase += step;
} }