Created
January 8, 2023 14:14
-
-
Save jepler/fd42b3b7d99bbb321dd6db6ef77499aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdbool.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
typedef unsigned int uint; | |
#ifndef PICO_PLL_VCO_MIN_FREQ_MHZ | |
#define PICO_PLL_VCO_MIN_FREQ_MHZ 750 | |
#endif | |
#ifndef PICO_PLL_VCO_MAX_FREQ_MHZ | |
#define PICO_PLL_VCO_MAX_FREQ_MHZ 1600 | |
#endif | |
#define clock_get_hz(arg) (12000000) // reference clock is 12MHz | |
bool check_sys_clock_khz(uint32_t freq_khz, uint *vco_out, uint *postdiv1_out, | |
uint *postdiv_out) { | |
uint crystal_freq_khz = clock_get_hz(clk_ref) / 1000; | |
for (uint fbdiv = 320; fbdiv >= 16; fbdiv--) { | |
uint vco = fbdiv * crystal_freq_khz; | |
if (vco < PICO_PLL_VCO_MIN_FREQ_MHZ * 1000 || | |
vco > PICO_PLL_VCO_MAX_FREQ_MHZ * 1000) | |
continue; | |
for (uint postdiv1 = 7; postdiv1 >= 1; postdiv1--) { | |
for (uint postdiv2 = postdiv1; postdiv2 >= 1; postdiv2--) { | |
uint out = vco / (postdiv1 * postdiv2); | |
if (out == freq_khz && !(vco % (postdiv1 * postdiv2))) { | |
*vco_out = vco * 1000; | |
*postdiv1_out = postdiv1; | |
*postdiv_out = postdiv2; | |
return true; | |
} | |
} | |
} | |
} | |
return false; | |
} | |
int main() { | |
int s = 0, f = 0; | |
uint vco, p1, p2; | |
for (int i = 1000; i < 133000; i += 1000) { | |
if (check_sys_clock_khz(i, &vco, &p1, &p2)) { | |
printf("%dkHz\n", i); | |
s++; | |
} else { | |
printf("!%dkHz\n", i); | |
f++; | |
} | |
} | |
printf("%d successes, %d failures (%d%%)\n", s, f, s / (s + f)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment