V71+: rewrite chooseBestClock to fix errors

This commit is contained in:
joan2937 2019-11-29 23:20:19 +00:00
parent 6b770ad810
commit 9b9ca9455d
2 changed files with 19 additions and 37 deletions

View File

@ -5087,51 +5087,33 @@ int serDataAvailable(unsigned handle)
static int chooseBestClock
(clkInf_t *clkInf, unsigned f, unsigned numc, unsigned *cf)
{
int c, valid, offby, offby2, best_offby;
uint32_t div;
uint64_t frac;
int c, valid;
double fdiv, offby, best_offby;
unsigned div, frac;
valid = 0;
best_offby = 0;
for (c=0; c<numc; c++)
{
div = cf[c] / f;
if ((div > 1) && (div < 4096))
{
fdiv = (double)cf[c] / (double)f;
if (f < PI_MASH_MAX_FREQ)
{
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++;
}
}
fdiv += (0.5 / 4096.0);
div = fdiv;
frac = (fdiv - div) * 4096.0;
}
else
{
fdiv += 0.5;
div = fdiv;
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 ((div > 1) && (div < 4096))
{
offby = f - (cf[c] / (div + (frac / 4096.0)));
if (offby < 0) offby = - offby;
if ((!valid) || (offby <= best_offby))
{
valid = 1;

View File

@ -30,7 +30,7 @@ For more information, please refer to <http://unlicense.org/>
#include <stdint.h>
#include <pthread.h>
#define PIGPIO_VERSION 7104
#define PIGPIO_VERSION 7105
/*TEXT