Skip to content

Reported system clock rate incorrect when configuring pll settings in cmakelists #2014

@CarlAndersson

Description

@CarlAndersson

I need some specific system clock rates for my audio-related project with an external ADC. Found the vcocalc.py script after some quick searches. It shows the option to configure the system pll from cmake instead of in the c code, e.g.

target_compile_definitions(audio PRIVATE
        PLL_SYS_REFDIV=1
        PLL_SYS_VCO_FREQ_HZ=1476000000
        PLL_SYS_POSTDIV1=5
        PLL_SYS_POSTDIV2=2
)

This seems to work as intended, checking the system clock rate with a frequency counter gave me 147.6 MHz, as expected.
However, the value returned by clock_get_hz is still 150 MHz (pico 2).
If I also set SYS_CLK_HZ in cmake, clock_get_hz will return this value, but it will return whatever value I put into cmake.

Looking into the code I think this gets set here, which in turn simply uses the clock speed defined here.

If this is intended behaviour, perhaps the vcocalc.py script should include a line on also setting SYS_CLK_HZ in cmake?

code.zip

Test code and output
uint32_t reported_before_setting = clock_get_hz(clk_sys);
uint32_t counted_before_setting = frequency_count_khz(CLOCKS_FC0_SRC_VALUE_CLK_SYS);
uint32_t freq_hz = 123 * MHZ;  // 123 MHz can give us close to the bitrate we need.
set_sys_clock_hz(freq_hz, true);
uint32_t reported_after_setting = clock_get_hz(clk_sys);
uint32_t counted_after_setting = frequency_count_khz(CLOCKS_FC0_SRC_VALUE_CLK_SYS);
stdio_init_all();

puts("\n\n==== Pico is booted ====");

printf("Reported before: %ld, reported after %ld\n", reported_before_setting, reported_after_setting);
printf("Counted before: %ld, counted after %ld\n", counted_before_setting, counted_after_setting);
==== Pico is booted ====
Reported before: 137600000, reported after 123000000
Counted before: 147600, counted after 123000

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions