NavHAL 0.1.0
NAVRobotec's architecture-agnostic HAL for embedded systems.
Loading...
Searching...
No Matches
pwm.h File Reference

HAL interface for PWM (Pulse Width Modulation) configuration and control. More...

#include "utils/timer_types.h"
#include <stdint.h>
Include dependency graph for pwm.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  PWM_Handle
 PWM handle structure linking timer and channel. More...

Functions

void hal_pwm_init (PWM_Handle *pwm, uint32_t frequency, float dutyCycle)
 Initialize a PWM handle with the specified frequency and duty cycle.
void hal_pwm_start (PWM_Handle *pwm)
 Start PWM signal generation.
void hal_pwm_stop (PWM_Handle *pwm)
 Stop PWM signal generation.
void hal_pwm_set_duty_cycle (PWM_Handle *pwm, float dutyCycle)
 Set the PWM duty cycle.
void hal_pwm_set_frequency (PWM_Handle *pwm, uint32_t frequency)
 Set the PWM frequency.

Detailed Description

HAL interface for PWM (Pulse Width Modulation) configuration and control.

This header provides:

  • PWM handle structure linking hardware timers and channels.
  • Functions to initialize, start, stop, and configure PWM signals.

Function Documentation

◆ hal_pwm_init()

void hal_pwm_init ( PWM_Handle * pwm,
uint32_t frequency,
float dutyCycle )

Initialize a PWM handle with the specified frequency and duty cycle.

Parameters
pwmPointer to the PWM handle.
frequencyPWM frequency in Hz.
dutyCycleDuty cycle as a percentage (0.0 to 100.0).

Initialize a PWM handle with the specified frequency and duty cycle.

Parameters
[in,out]pwmPointer to PWM handle structure
[in]frequencyDesired PWM frequency in Hz
[in]dutyCycleInitial duty cycle (0.0 to 1.0)

Configures the timer peripheral to generate PWM signals by:

  1. Selecting the appropriate bus clock (APB1/APB2)
  2. Calculating prescaler and auto-reload values
  3. Setting the initial compare value for duty cycle
  4. Initializing the timer peripheral
Note
For advanced timers (TIM1, TIM9-TIM11), APB2 clock is used automatically

◆ hal_pwm_set_duty_cycle()

void hal_pwm_set_duty_cycle ( PWM_Handle * pwm,
float dutyCycle )

Set the PWM duty cycle.

Parameters
pwmPointer to the PWM handle.
dutyCycleDuty cycle as a percentage (0.0 to 100.0).

Set the PWM duty cycle.

Parameters
[in,out]pwmPointer to PWM handle structure
[in]dutyCycleNew duty cycle (0.0 to 1.0)

Updates the capture/compare register to change the PWM duty cycle while maintaining the current frequency. The change takes effect immediately on the output.

Note
Duty cycle is clamped to valid range (0.0-1.0)

◆ hal_pwm_set_frequency()

void hal_pwm_set_frequency ( PWM_Handle * pwm,
uint32_t frequency )

Set the PWM frequency.

Parameters
pwmPointer to the PWM handle.
frequencyPWM frequency in Hz.

Set the PWM frequency.

Parameters
[in,out]pwmPointer to PWM handle structure
[in]frequencyDesired PWM frequency in Hz
Note
This function is currently not implemented
Todo
Implement frequency change functionality

◆ hal_pwm_start()

void hal_pwm_start ( PWM_Handle * pwm)

Start PWM signal generation.

Parameters
pwmPointer to the PWM handle.

Start PWM signal generation.

Parameters
[in]pwmPointer to initialized PWM handle structure

Enables the timer counter to start generating PWM signals on the configured channel. The output will appear on the corresponding GPIO pin.

◆ hal_pwm_stop()

void hal_pwm_stop ( PWM_Handle * pwm)

Stop PWM signal generation.

Parameters
pwmPointer to the PWM handle.

Stop PWM signal generation.

Parameters
[in]pwmPointer to PWM handle structure

Disables the timer channel output and stops the timer counter. The PWM output pin will return to its idle state.