From Newsgroup: comp.sys.raspberry-pi
On 11/01/2026 16:15, Single Stage to Orbit wrote:
Hi chaps
Does this group also include the Raspberry rp2040/rp2350 as well? I'm
in need of resources or the knowledge of how to program the PWMs on the rp2040. If not, are there any online resource I can consult.
I certainly have been playing with these in a C context.
I klooked into PWM in some detail but have not yet implemented it.
The rp2040 datasheets are instrutable on the subject of the PWMs. I can easily blink an ordinary LED, likewise for a RGB LED via three GPIO
pins, but for doing cool tricks like fading/brightening the separate
RGB components on a RGB LED it's beyond me, I think PWMs are needed for
this but all I could find was micropython stuff and that works but I'd
like to know how to do it baremetal with ARM thumb assembly.
Cant help with assembly, only C.
IIRC there are a couple of places you just wrote data to to set the
thing up and these can be rewritten live to vary the pulse width
Easy way to do that is to run a sleep_ms(loop) and every iterations
change something for a fade up down.
From:
https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#group_hardware_pwm
The RP2040 PWM block has 8 identical slices, the RP2350 has 12. Each
slice can drive two PWM output signals, or measure the frequency or duty
cycle of an input signal. This gives a total of up to 16/24 controllable
PWM outputs. All 30 GPIOs can be driven by the PWM block.
The PWM hardware functions by continuously comparing the input value to
a free-running counter. This produces a toggling output where the amount
of time spent at the high output level is proportional to the input
value. The fraction of time spent at the high signal level is known as
the duty cycle of the signal.
The default behaviour of a PWM slice is to count upward until the wrap
value (pwm_config_set_wrap) is reached, and then immediately wrap to 0.
PWM slices also offer a phase-correct mode, where the counter starts to
count downward after reaching TOP, until it reaches 0 again.
// Output PWM signals on pins 0 and 1
#include "pico/stdlib.h"
#include "hardware/pwm.h"
int main() {
// Tell GPIO 0 and 1 they are allocated to the PWM
gpio_set_function(0, GPIO_FUNC_PWM);
gpio_set_function(1, GPIO_FUNC_PWM);
// Find out which PWM slice is connected to GPIO 0 (it's slice 0)
uint slice_num = pwm_gpio_to_slice_num(0);
// Set period of 4 cycles (0 to 3 inclusive)
pwm_set_wrap(slice_num, 3);
// Set channel A output high for one cycle before dropping
pwm_set_chan_level(slice_num, PWM_CHAN_A, 1);
// Set initial B output high for three cycles before dropping
pwm_set_chan_level(slice_num, PWM_CHAN_B, 3);
// Set the PWM running
pwm_set_enabled(slice_num, true);
// Note we could also use pwm_set_gpio_level(gpio, x) which looks
up the
// correct slice and channel for a given GPIO.
}
Now that references a c library, but easy enough to pull assembler
source out of it
Many thanks,
Alex
--
rCLIt is dangerous to be right in matters on which the established
authorities are wrong.rCY
rCo Voltaire, The Age of Louis XIV
--- Synchronet 3.21a-Linux NewsLink 1.2