libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
ch32_pwm.hpp
1#pragma once
2
3#include "pwm.hpp"
4
5#include DEF2STR(LIBXR_CH32_CONFIG_FILE)
6
7namespace LibXR
8{
12class CH32PWM : public PWM
13{
14 public:
18 CH32PWM(TIM_TypeDef* tim, uint16_t channel, bool active_high, GPIO_TypeDef* gpio,
19 uint16_t pin, uint32_t pin_remap = 0, bool complementary = false);
20
21 ErrorCode SetDutyCycle(float value) override;
23 ErrorCode Enable() override;
24 ErrorCode Disable() override;
25
26 private:
27 TIM_TypeDef* tim_;
28 uint16_t channel_;
29 bool active_high_;
30 bool complementary_;
31
32 GPIO_TypeDef* gpio_;
33 uint16_t pin_;
34 uint32_t pin_remap_;
35
36 static bool IsAdvancedTimer(TIM_TypeDef* t);
37 static bool OnAPB2(TIM_TypeDef* t);
38 static uint32_t GetTimerClockHz(TIM_TypeDef* t);
39
40 static inline uint32_t ReadARR32(TIM_TypeDef* t) { return t->ATRLR; }
41
42 void ApplyCompare(uint32_t pulse);
43 void OcInitForChannel(uint32_t pulse);
44 void EnableChannel(bool en);
45 void EnableChannelN(bool en);
46
48 static void EnableGPIOClock(GPIO_TypeDef* gpio);
49 static void EnableTIMClock(TIM_TypeDef* tim);
50 void ConfigureGPIO();
51};
52
53} // namespace LibXR
CH32 PWM 驱动实现 / CH32 PWM driver implementation.
Definition ch32_pwm.hpp:13
ErrorCode Enable() override
启用 PWM 输出 / Enables the PWM output.
ErrorCode Disable() override
禁用 PWM 输出 / Disables the PWM output.
ErrorCode SetDutyCycle(float value) override
设置 PWM 信号的占空比 / Sets the duty cycle of the PWM signal.
ErrorCode SetConfig(Configuration config) override
配置 PWM 参数 / Configures the PWM settings.
CH32PWM(TIM_TypeDef *tim, uint16_t channel, bool active_high, GPIO_TypeDef *gpio, uint16_t pin, uint32_t pin_remap=0, bool complementary=false)
构造 PWM 对象 / Construct PWM object
PWM(脉冲宽度调制)控制的抽象基类。 Abstract base class for PWM (Pulse Width Modulation) control.
Definition pwm.hpp:14
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
PWM 配置参数 / Configuration parameters for PWM.
Definition pwm.hpp:23