/* Simple lookup of best MSEL and NSEL values for wanted frequency. 
Not optimized. */
static void cgu_findMN(uint32_t wantedFreq, uint32_t *pMsel, uint32_t *pNsel)
/* Simple lookup of best MSEL and NSEL values for wanted frequency. 
Not optimized. */
static void cgu_findMN(uint32_t wantedFreq, uint32_t *pMsel, uint32_t *pNsel)
{
    uint32_t besterr = wantedFreq;
    uint32_t m, n, f, tmp, err;
#define ABSDIFF(__a, __b) ( ((__a) < (__b)) ? ((__b) - (__a)) : ((__a) - (__b)) )
    for (n = 1; n <= 4; n++) {
        f = 12000000 / n;
        tmp = 0;
        for (m = 1; m <= 256; m++) {
            tmp += f;
            err = ABSDIFF(tmp, wantedFreq);
            if (err == 0) {
                *pMsel = m - 1;
                *pNsel = n - 1;
                return;
            } else if (err < besterr) {
                *pMsel = m - 1;
                *pNsel = n - 1;
                besterr = err;
            }
            if (tmp > wantedFreq) {
                break;
            }
        }
    }
}