Pulse Width Modulation (PWM) interface for hardware platforms. More...
![]() |
Data Structures | |
struct | hw_pwm_config_t |
PWM configuration structure for setup parameters. More... | |
struct | hw_pwm_t |
PWM structure representing a PWM instance. More... | |
Typedefs | |
typedef struct hw_pwm_t | hw_pwm_t |
PWM structure representing a PWM instance. | |
typedef void(* | hw_pwm_callback_t) (uint8_t unit, void *userdata) |
PWM interrupt callback function pointer. More... | |
Functions | |
hw_pwm_t | hw_pwm_init (uint8_t unit, hw_pwm_config_t *config) |
Initialize a PWM unit. More... | |
uint8_t | hw_pwm_count () |
Get the number of PWM units. More... | |
static bool | hw_pwm_valid (hw_pwm_t *pwm) |
Validate the PWM unit. More... | |
void | hw_pwm_finalize (hw_pwm_t *pwm) |
Finalize and release a PWM unit. More... | |
uint8_t | hw_pwm_gpio_unit (uint8_t gpio) |
Get the PWM unit for a GPIO pin. More... | |
hw_pwm_config_t | hw_pwm_get_config (float freq) |
Get PWM configuration. More... | |
float | hw_pwm_get_freq (hw_pwm_config_t *config) |
Returns the frequency for a configuration. More... | |
void | hw_pwm_set_config (hw_pwm_t *pwm, const hw_pwm_config_t *config) |
Apply configuration to a PWM unit. More... | |
bool | hw_pwm_start (hw_pwm_t *pwm, uint8_t gpio, float duty_percent) |
Start PWM output on a specific GPIO pin. More... | |
void | hw_pwm_stop (hw_pwm_t *pwm) |
Stop PWM output. More... | |
void | hw_pwm_set_callback (hw_pwm_callback_t callback, void *userdata) |
Set the PWM interrupt callback handler. More... | |
void | hw_pwm_set_irq_enabled (hw_pwm_t *pwm, bool enabled) |
Enable or disable PWM wrap interrupt for a slice. More... | |
Pulse Width Modulation (PWM) interface for hardware platforms.
This module provides functions to initialize PWM units, configure frequency and duty cycle, and control PWM output.
The PWM implementation supports multiple units (slices), each with one or more GPIO outputs. On the Raspberry Pi Pico, there are 8 PWM units on the RP2040 and 12 PWM units on the RP2350.
Interrupts can be configured to trigger on PWM wrap events, allowing for precise timing and control in applications such as motor control, LED dimming, and audio generation.
There is one global PWM callback, which is called when a PWM wrap interrupt occurs on any unit. The callback is executed in interrupt context, so it should be kept short and avoid blocking operations.
The frequency of the PWM output is platform-dependent, but on the Pico this can range from approximately 8 Hz to 60 MHz. The value that is set in the PWM configuration is not necessarily the exact frequency, but rather the closest achievable frequency based on the hardware capabilities.
Similarly the duty cycle (the ratio of HIGH vs LOW for each PWM period) is set as a percentage (0-100) of the PWM period. The actual duty cycle may not be exact due to hardware limitations, but it will be as close as possible to the requested value.
typedef void(* hw_pwm_callback_t) (uint8_t unit, void *userdata) |
PWM interrupt callback function pointer.
unit | The PWM unit that triggered the interrupt. |
userdata | User-defined data pointer passed when setting the callback. |
This callback function is called when a PWM wrap interrupt occurs on a unit that has been configured for interrupt detection. The callback is executed in interrupt context, so it should be kept short and avoid blocking operations.
uint8_t hw_pwm_count | ( | ) |
Get the number of PWM units.
This function returns the total number of PWM units that can be used. It will return zero if PWM is not supported on the platform.
void hw_pwm_finalize | ( | hw_pwm_t * | pwm | ) |
Finalize and release a PWM unit.
pwm | Pointer to the PWM structure to finalize. |
This function disables the PWM unit, resets its configuration, and releases any associated GPIO pins. After calling this function, the PWM unit should not be used until re-initialized.
hw_pwm_config_t hw_pwm_get_config | ( | float | freq | ) |
Get PWM configuration.
freq | Desired frequency in Hz. |
float hw_pwm_get_freq | ( | hw_pwm_config_t * | config | ) |
Returns the frequency for a configuration.
config | Pointer to the PWM configuration structure. |
This function retrieves the actual frequency value from the PWM configuration, which is calculated based on the wrap value and divider.
uint8_t hw_pwm_gpio_unit | ( | uint8_t | gpio | ) |
Get the PWM unit for a GPIO pin.
gpio | The GPIO pin number. |
This function retrieves the PWM unit number associated with a GPIO pin. The GPIO pin must be less than the total number of GPIO pins available.
hw_pwm_t hw_pwm_init | ( | uint8_t | unit, |
hw_pwm_config_t * | config | ||
) |
Initialize a PWM unit.
unit | The PWM unit number to initialize. |
config | Optional pointer to a configuration structure. If NULL, default configuration will be used. |
This function initializes the PWM unit and channel(s) the GPIO pin belongs to and initializes the unit with default settings. The PWM unit is not started and is set to the disabled state.
void hw_pwm_set_callback | ( | hw_pwm_callback_t | callback, |
void * | userdata | ||
) |
Set the PWM interrupt callback handler.
callback | Pointer to the callback function to handle PWM interrupts, or NULL to disable interrupt handling. |
userdata | User-defined data pointer to pass to the callback. |
This function registers a global callback handler that will be invoked whenever a PWM wrap interrupt occurs on any slice that has been configured for interrupt detection. Only one callback can be active at a time.
void hw_pwm_set_config | ( | hw_pwm_t * | pwm, |
const hw_pwm_config_t * | config | ||
) |
Apply configuration to a PWM unit.
pwm | Pointer to the PWM structure. |
config | Pointer to the configuration to apply. |
This function applies the specified configuration to the PWM unit. If the PWM unit is currently enabled, it will be stopped before applying the new configuration, and then restarted.
void hw_pwm_set_irq_enabled | ( | hw_pwm_t * | pwm, |
bool | enabled | ||
) |
Enable or disable PWM wrap interrupt for a slice.
pwm | Pointer to the PWM structure. |
enabled | True to enable interrupt, false to disable. |
This function enables or disables the wrap interrupt for the specified PWM slice. The interrupt fires when the counter reaches the wrap value. A callback must be set using hw_pwm_set_callback() to handle interrupts.
bool hw_pwm_start | ( | hw_pwm_t * | pwm, |
uint8_t | gpio, | ||
float | duty_percent | ||
) |
Start PWM output on a specific GPIO pin.
pwm | Pointer to the PWM structure. |
gpio | GPIO pin number to attach. |
duty_percent | Duty cycle as a percentage, between 0 and 100. |
This function attaches the specified GPIO pin to the PWM channel, starts the PWM output, sets the duty cycle for the specified PWM channel.
If the duty_percent is less than or equal to 0, the PWM output will always be low. If the duty_percent is 100 or greater, the PWM output will always be high. If the duty_percent is between 0 and 100, the PWM output will toggle between high and low at the specified frequency, with the duty cycle determining the proportion of time the output is high.
void hw_pwm_stop | ( | hw_pwm_t * | pwm | ) |
Stop PWM output.
pwm | Pointer to the PWM structure. |
This function disables the PWM unit, stopping output generation. The counter value is preserved and will resume from this point when the PWM is restarted.
The IRQ state is not affected by this function, so if the PWM was configured to generate interrupts, the interrupt handler will still be called when the PWM is restarted.
|
inlinestatic |