HF Interface Wrapper 0.1.0-dev
Embedded C++ hardware abstraction layer
Loading...
Searching...
No Matches
StmPwm.h
Go to the documentation of this file.
1
14#pragma once
15
16#include "BasePwm.h"
17#include "StmTypes.h"
18
19struct TIM_HandleTypeDef; // Forward declaration
20
32class StmPwm : public BasePwm {
33public:
34
40 explicit StmPwm(TIM_HandleTypeDef* htim,
41 hf_u32_t timer_clock_hz) noexcept;
42
46 explicit StmPwm(const hf_stm32_pwm_config_t& config) noexcept;
47
48 ~StmPwm() noexcept override;
49
50 StmPwm(const StmPwm&) = delete;
51 StmPwm& operator=(const StmPwm&) = delete;
52
53 // ── Pure-virtual overrides (BasePwm) ────────────────────────────────────
54
55 // Lifecycle
56 hf_pwm_err_t Initialize() noexcept override;
57 hf_pwm_err_t Deinitialize() noexcept override;
58
59 // Channel control
60 hf_pwm_err_t EnableChannel(hf_channel_id_t channel_id) noexcept override;
61 hf_pwm_err_t DisableChannel(hf_channel_id_t channel_id) noexcept override;
62 bool IsChannelEnabled(hf_channel_id_t channel_id) const noexcept override;
63
64 // PWM control
65 hf_pwm_err_t SetDutyCycle(hf_channel_id_t channel_id, float duty_cycle) noexcept override;
66 hf_pwm_err_t SetDutyCycleRaw(hf_channel_id_t channel_id, hf_u32_t raw_value) noexcept override;
67 hf_pwm_err_t SetFrequency(hf_channel_id_t channel_id, hf_frequency_hz_t frequency_hz) noexcept override;
68 hf_pwm_err_t SetPhaseShift(hf_channel_id_t channel_id, float phase_shift_degrees) noexcept override;
69
70 // Advanced
71 hf_pwm_err_t StartAll() noexcept override;
72 hf_pwm_err_t StopAll() noexcept override;
73 hf_pwm_err_t UpdateAll() noexcept override;
75 hf_channel_id_t complementary_channel,
76 hf_u32_t deadtime_ns) noexcept override;
77
78 // Status
79 float GetDutyCycle(hf_channel_id_t channel_id) const noexcept override;
80 hf_frequency_hz_t GetFrequency(hf_channel_id_t channel_id) const noexcept override;
81
82 // ── Accessors ───────────────────────────────────────────────────────────
83 TIM_HandleTypeDef* GetHalHandle() const noexcept { return htim_; }
84
85 static constexpr hf_u8_t kMaxChannels = 4;
86
87private:
88 TIM_HandleTypeDef* htim_;
93
95 static hf_u32_t ChannelToHal(hf_channel_id_t ch) noexcept;
96
98 bool ApplyFrequency(hf_frequency_hz_t freq_hz) noexcept;
99
101 void ReapplyDuties() noexcept;
102};
Abstract base class for PWM implementations in the HardFOC system.
hf_pwm_err_t
Definition BasePwm.h:76
uint32_t hf_u32_t
Platform-agnostic 32-bit unsigned integer type.
Definition HardwareTypes.h:52
uint8_t hf_u8_t
Platform-agnostic 8-bit unsigned integer type.
Definition HardwareTypes.h:40
hf_u32_t hf_channel_id_t
Platform-agnostic channel identifier type.
Definition HardwareTypes.h:163
hf_u32_t hf_frequency_hz_t
Platform-agnostic frequency type (in Hz).
Definition HardwareTypes.h:145
STM32 platform-specific type definitions for hardware abstraction.
Abstract base class for PWM implementations.
Definition BasePwm.h:182
STM32 PWM implementation.
Definition StmPwm.h:32
hf_frequency_hz_t frequency_hz_
Current frequency (shared)
Definition StmPwm.h:92
hf_pwm_err_t Deinitialize() noexcept override
Deinitialize the PWM system.
Definition StmPwm.cpp:87
float duty_[kMaxChannels]
Cached duty 0.0–1.0.
Definition StmPwm.h:91
bool IsChannelEnabled(hf_channel_id_t channel_id) const noexcept override
Check if a channel is enabled.
Definition StmPwm.cpp:124
hf_frequency_hz_t GetFrequency(hf_channel_id_t channel_id) const noexcept override
Get current frequency for a channel.
Definition StmPwm.cpp:261
hf_pwm_err_t Initialize() noexcept override
Initialize the PWM system.
Definition StmPwm.cpp:72
hf_pwm_err_t DisableChannel(hf_channel_id_t channel_id) noexcept override
Disable a PWM channel.
Definition StmPwm.cpp:111
hf_pwm_err_t StartAll() noexcept override
Start all enabled channels simultaneously.
Definition StmPwm.cpp:195
hf_u32_t timer_clock_hz_
Definition StmPwm.h:89
bool ApplyFrequency(hf_frequency_hz_t freq_hz) noexcept
Apply freq by recalculating PSC + ARR.
Definition StmPwm.cpp:280
StmPwm(const StmPwm &)=delete
hf_pwm_err_t SetDutyCycle(hf_channel_id_t channel_id, float duty_cycle) noexcept override
Set duty cycle for a channel.
Definition StmPwm.cpp:132
StmPwm(TIM_HandleTypeDef *htim, hf_u32_t timer_clock_hz) noexcept
Construct from HAL timer handle.
Definition StmPwm.cpp:44
hf_pwm_err_t EnableChannel(hf_channel_id_t channel_id) noexcept override
Enable a PWM channel.
Definition StmPwm.cpp:98
hf_pwm_err_t StopAll() noexcept override
Stop all channels.
Definition StmPwm.cpp:203
static hf_u32_t ChannelToHal(hf_channel_id_t ch) noexcept
Get HAL channel constant from 0-based index.
Definition StmPwm.cpp:270
hf_pwm_err_t UpdateAll() noexcept override
Update all channel outputs simultaneously (for synchronized updates)
Definition StmPwm.cpp:211
StmPwm & operator=(const StmPwm &)=delete
hf_pwm_err_t SetFrequency(hf_channel_id_t channel_id, hf_frequency_hz_t frequency_hz) noexcept override
Set frequency for a channel.
Definition StmPwm.cpp:168
hf_pwm_err_t SetDutyCycleRaw(hf_channel_id_t channel_id, hf_u32_t raw_value) noexcept override
Set raw duty value for a channel.
Definition StmPwm.cpp:152
hf_pwm_err_t SetPhaseShift(hf_channel_id_t channel_id, float phase_shift_degrees) noexcept override
Set phase shift for a channel (if supported)
Definition StmPwm.cpp:183
bool channel_enabled_[kMaxChannels]
Definition StmPwm.h:90
void ReapplyDuties() noexcept
Reapply all cached duty cycles after ARR change.
Definition StmPwm.cpp:314
~StmPwm() noexcept override
Definition StmPwm.cpp:64
float GetDutyCycle(hf_channel_id_t channel_id) const noexcept override
Get current duty cycle for a channel.
Definition StmPwm.cpp:257
hf_pwm_err_t SetComplementaryOutput(hf_channel_id_t primary_channel, hf_channel_id_t complementary_channel, hf_u32_t deadtime_ns) noexcept override
Set complementary output configuration (for motor control)
Definition StmPwm.cpp:220
TIM_HandleTypeDef * GetHalHandle() const noexcept
Definition StmPwm.h:83
TIM_HandleTypeDef * htim_
Definition StmPwm.h:88
static constexpr hf_u8_t kMaxChannels
Definition StmPwm.h:85
PWM configuration for STM32.
Definition StmTypes.h:400