Skip to content

rp2/modmachine: Do not use deprecated XOSC_MHZ. #17746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2025

Conversation

langchr86
Copy link
Contributor

Summary

I replaced the crystal on a W5500_EVB_PICO board by a custom clock source which is not 12 MHz. Therefore I need to configure the SYS and USB PLL with custom dividers. The script vcocalc.py is used to calculate the needed values and to generate the compiler definitions that need to be added to cmake.

But when using such custom defines the XOSC_MHZ is not defined and the build fails.

Testing

I tested this change while compiling for a W5500_EVB_PICO board with custom PLL settings as described above.

Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

@dpgeorge dpgeorge added this to the release-1.26.0 milestone Jul 24, 2025
@dpgeorge
Copy link
Member

Thanks for the contribution. This looks fine to me.

But note that clocks_extra.c uses XOSC_KHZ... so maybe that needs changing as well? Why didn't that lead to a build error in your case?

Also, pico-sdk provides defines of XOSC_MHZ in terms of XOSC_HZ so I'm also uncertain why that define wasn't provided in your case? What value is your XOSC_HZ?

@langchr86
Copy link
Contributor Author

My added definitions are the following:

target_compile_definitions(${MICROPY_TARGET} PRIVATE
        XOSC_HZ=30720000

        PLL_SYS_REFDIV=4
        PLL_SYS_VCO_FREQ_HZ=960000000
        PLL_SYS_POSTDIV1=4
        PLL_SYS_POSTDIV2=2
        SYS_CLK_HZ=120000000

        PLL_USB_REFDIV=4
        PLL_USB_VCO_FREQ_HZ=1344000000
        PLL_USB_POSTDIV1=7
        PLL_USB_POSTDIV2=4
        USB_CLK_HZ=48000000

There is code for backwards compatibility in platform_defs.h:

// For backwards compatibility define XOSC_KHZ if the frequency is indeed an integer number of Khz.
#if defined(XOSC_HZ) && !defined(XOSC_KHZ) && (XOSC_HZ % 1000 == 0)
#define XOSC_KHZ (XOSC_HZ / 1000)
#endif

// For backwards compatibility define XOSC_MHZ if the frequency is indeed an integer number of Mhz.
#if defined(XOSC_KHZ) && !defined(XOSC_MHZ) && (XOSC_KHZ % 1000 == 0)
#define XOSC_MHZ (XOSC_KHZ / 1000)
#endif

Because of the test XOSC_KHZ % 1000 == 0 the XOSC_MHZ is not defined.

The other point about XOSC_KHZ is true. I will later update my MR to also replace this in clocks_extra.c.

In theory we could/should also replace all usages of SYS_CLK_KHZ/USB_CLK_KHZ and the MHZ variants. The thing is, that those are not only used for clock_configure() (where they are converted to a HZ value) but also for set_sys_clock_khz() where we could replace the whole call with set_sys_clock_hz(). But I am not sure if this would have some side effects because of the comment in check_sys_clock_khz():

Note this impl is kept to preserve previous rounding behavior, vs calling check_sys_clock_hz

What is your suggestion?

@dpgeorge
Copy link
Member

Because of the test XOSC_KHZ % 1000 == 0 the XOSC_MHZ is not defined.

Ah, right, that makes sense then. Your crystal is not an exact MHz multiple. So you definitely need the change in this PR to get the correct timing.

The other point about XOSC_KHZ is true. I will later update my MR to also replace this in clocks_extra.c.

Yes, please make this change. Looking at pico-sdk, it seems they also updated their code to use XOSC_HZ, which clocks_extra.c is adapted from.

In theory we could/should also replace all usages of SYS_CLK_KHZ/USB_CLK_KHZ and the MHZ variants.

No, let's leave those as they are for now. Too much of a risk to change that just before a release.

@langchr86 langchr86 force-pushed the feature/do_not_use_XOSC_MHZ branch from b7a1ae1 to 4eaec3b Compare July 24, 2025 18:56
@langchr86
Copy link
Contributor Author

I now also removed the usage of XOSC_KHZ. And I improved the change for XOSC_MHZ even more by removing the unneeded definition of the xosc_hz variable in modmachine.c.

I hope this matches your expectations. And thanks again for this great project!

XOSC_MHZ and XOSC_KHZ may not be defined if we use a custom XIN clock
by defining PLL_SYS_REFDIV etc. calculated by vcocalc.py.

Signed-off-by: Christian Lang <lang.chr86@gmail.com>
Copy link
Member

@dpgeorge dpgeorge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good now, thanks!

Tested on RPI_PICO2_W, everything seems fine.

@dpgeorge
Copy link
Member

And thanks again for this great project!

😄

@dpgeorge dpgeorge force-pushed the feature/do_not_use_XOSC_MHZ branch from 4eaec3b to ebc9525 Compare July 25, 2025 02:32
@dpgeorge dpgeorge merged commit ebc9525 into micropython:master Jul 25, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy