RP2040_PWM using an external clock

I need to divide a 10 MHz clock by 100 to produce a 100 kHz clock. I can produce a 100kHz clock using RP2040_PWM but I need to use an external 10 MHz clock rather than the internal clock.

The RP2040 does support an external clock on Pin B but I can't find any code that supports this. Is it possible to use RP2040_PWM code to do this please?

See the datasheet page 530. https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf
The example there can be seen in context on their GitHub: pico-examples/pwm/measure_duty_cycle/measure_duty_cycle.c at master · raspberrypi/pico-examples · GitHub
I assume the functions used are implemented in the Arduino board definition, but don't quote me on that. Just give it a try.

Thanks for your information. The datasheet page 530 confirms that you can use an Input B for an external clock.
Unfortunately

assert(pwm_gpio_to_channel(gpio) == PWM_CHAN_B);

Is not supported by Arduino IDE 2.3.2 or PWM2040_PWM and I don't know how to add that.

That's not a very crucial line. All it does is verify whether the pin you've passed to the function is indeed a B pin for a PWM channel. If you verify that otherwise (e.g. manually), you can simply leave it out.

OK understood. So how do I set a GPIO pin as a clock input to be used by the PWM, ideally using RP2040_PWM?

I think you need to pass the correct mode to the pwm_config_set_clkdiv_mode function. In the example I linked you to, it seems to be setup to have the timer run free while the input is high. There's also a one-shot trigger mode that increments the counter by 1 with every high- or low-going edge on the input. You'll have to root around in the documentation a little to figure out how they named the mode you want to use.

I've been able to write some C code that uses the Pico PWM components to divide an external clock by 100. I've written this by including pwm.h, using both Visual Studio Code and Arduino IDE V2.3.2. I've checked the quality of the divided clock using a spectrum analyzer and comparing its divided clock with the input clock divided by 100 using a 74HC390. There is no degradation using the Pico.
Next I need to incorporate the C code into the main Thonny Micropython code and so far I've not found a simple way to do that. If anyone is aware how to do that I would appreciate a reply.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.