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

PWM driver implementation for STM32F4 series (Cortex-M4) More...

#include "core/cortex-m4/pwm.h"
#include "core/cortex-m4/clock.h"
#include "core/cortex-m4/timer.h"
#include <stdint.h>
Include dependency graph for pwm.c:

Functions

void hal_pwm_init (PWM_Handle *pwm, uint32_t frequency, float dutyCycle)
 Initialize PWM with specified frequency and duty cycle.
void hal_pwm_start (PWM_Handle *pwm)
 Start PWM output.
void hal_pwm_stop (PWM_Handle *pwm)
 Stop PWM output.
void hal_pwm_set_duty_cycle (PWM_Handle *pwm, float dutyCycle)
 Set PWM duty cycle.
void hal_pwm_set_frequency (PWM_Handle *pwm, uint32_t frequency)
 Set PWM frequency (not implemented)

Detailed Description

PWM driver implementation for STM32F4 series (Cortex-M4)

This file provides Pulse Width Modulation (PWM) functionality for STM32F4 microcontrollers. It supports:

  • PWM generation on all timer channels
  • Configurable frequency and duty cycle
  • Automatic timer clock selection (APB1/APB2)
  • Runtime duty cycle adjustment

The implementation uses the STM32 timer peripherals to generate precise PWM signals with minimal CPU overhead.

Function Documentation

◆ hal_pwm_init()

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

Initialize PWM with specified frequency and duty cycle.

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 PWM duty cycle.

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 PWM frequency (not implemented)

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 output.

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 output.

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.