mirror of https://github.com/joan2937/pigpio
V71+: rewrite chooseBestClock to fix errors
This commit is contained in:
parent
6b770ad810
commit
9b9ca9455d
54
pigpio.c
54
pigpio.c
|
@ -5087,51 +5087,33 @@ int serDataAvailable(unsigned handle)
|
||||||
static int chooseBestClock
|
static int chooseBestClock
|
||||||
(clkInf_t *clkInf, unsigned f, unsigned numc, unsigned *cf)
|
(clkInf_t *clkInf, unsigned f, unsigned numc, unsigned *cf)
|
||||||
{
|
{
|
||||||
int c, valid, offby, offby2, best_offby;
|
int c, valid;
|
||||||
uint32_t div;
|
double fdiv, offby, best_offby;
|
||||||
uint64_t frac;
|
unsigned div, frac;
|
||||||
|
|
||||||
valid = 0;
|
valid = 0;
|
||||||
best_offby = 0;
|
best_offby = 0;
|
||||||
|
|
||||||
for (c=0; c<numc; c++)
|
for (c=0; c<numc; c++)
|
||||||
{
|
{
|
||||||
div = cf[c] / f;
|
fdiv = (double)cf[c] / (double)f;
|
||||||
|
if (f < PI_MASH_MAX_FREQ)
|
||||||
|
{
|
||||||
|
fdiv += (0.5 / 4096.0);
|
||||||
|
div = fdiv;
|
||||||
|
frac = (fdiv - div) * 4096.0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fdiv += 0.5;
|
||||||
|
div = fdiv;
|
||||||
|
frac = 0;
|
||||||
|
}
|
||||||
|
|
||||||
if ((div > 1) && (div < 4096))
|
if ((div > 1) && (div < 4096))
|
||||||
{
|
{
|
||||||
if (f < PI_MASH_MAX_FREQ)
|
offby = f - (cf[c] / (div + (frac / 4096.0)));
|
||||||
{
|
if (offby < 0) offby = - offby;
|
||||||
frac = cf[c] - (div * f);
|
|
||||||
frac = (frac * 4096) / f;
|
|
||||||
offby = cf[c] - (div * f) - ((frac * f) / 4096);
|
|
||||||
if (frac < 4095)
|
|
||||||
{
|
|
||||||
offby2 = cf[c] - (div * f) - (((frac+1) * f) / 4096);
|
|
||||||
if (offby2 < 0) offby2 = -offby2;
|
|
||||||
if (offby2 < offby)
|
|
||||||
{
|
|
||||||
offby = offby2;
|
|
||||||
frac++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
frac = 0;
|
|
||||||
offby = cf[c] - (div * f);
|
|
||||||
if (div < 4095)
|
|
||||||
{
|
|
||||||
offby2 = cf[c] - ((div+1) * f);
|
|
||||||
if (offby2 < 0) offby2 = -offby2;
|
|
||||||
if (offby2 < offby)
|
|
||||||
{
|
|
||||||
offby = offby2;
|
|
||||||
div++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((!valid) || (offby <= best_offby))
|
if ((!valid) || (offby <= best_offby))
|
||||||
{
|
{
|
||||||
valid = 1;
|
valid = 1;
|
||||||
|
|
Loading…
Reference in New Issue