pico-sdk icon indicating copy to clipboard operation
pico-sdk copied to clipboard

pio_calculate_clkdiv_from_float is not rounding

Open lkesteloot opened this issue 1 year ago • 0 comments

The pio_calculate_clkdiv_from_float() function is truncating the fractional result:

*div_frac = (uint8_t)((div - (float)*div_int) * (1u << 8u));

It should be rounding. One way to do this is to add half of 1/256 up front, as the very first line of the function, before the range check:

// Bump div by 1/512 so that we're rounding instead of truncating.
div += 0.001953125f;

This error caused an NTSC color burst to be far enough off in frequency to not be recognized by two different monitors.

lkesteloot avatar Sep 15 '24 04:09 lkesteloot