libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
esp_pwm.hpp
1#pragma once
2
3#include "esp_def.hpp"
4
5#include "driver/ledc.h"
6#include "esp_err.h"
7#include "hal/ledc_hal.h"
8#include "pwm.hpp"
9
10namespace LibXR
11{
12
14class ESP32PWM : public PWM
15{
16 public:
18 ESP32PWM(int gpio_num, ledc_channel_t channel, ledc_timer_t timer = LEDC_TIMER_0,
19 ledc_timer_bit_t resolution = static_cast<ledc_timer_bit_t>(
20 static_cast<uint8_t>(LEDC_TIMER_BIT_MAX) - 1))
21 : gpio_num_(gpio_num),
22 channel_(channel),
23 timer_(timer),
24 speed_mode_(LEDC_LOW_SPEED_MODE),
25 resolution_(resolution),
26 max_duty_((1U << static_cast<uint8_t>(resolution_)) - 1U)
27 {
28 ledc_hal_init(&hal_, speed_mode_);
29
30 ledc_channel_config_t channel_conf = {};
31 channel_conf.gpio_num = gpio_num_;
32 channel_conf.speed_mode = speed_mode_;
33 channel_conf.channel = channel_;
34 channel_conf.intr_type = LEDC_INTR_DISABLE;
35 channel_conf.timer_sel = timer_;
36 channel_conf.duty = 0;
37 channel_conf.hpoint = 0;
38
39 const esp_err_t err = ledc_channel_config(&channel_conf);
40 if (err != ESP_OK)
41 {
42 ASSERT(false);
43 }
44 }
45
46 ErrorCode SetDutyCycle(float value) override
47 {
48 if (value < 0.0f)
49 {
50 value = 0.0f;
51 }
52 else if (value > 1.0f)
53 {
54 value = 1.0f;
55 }
56
57 const uint32_t duty = static_cast<uint32_t>(max_duty_ * value);
58 ledc_hal_set_duty_int_part(&hal_, channel_, duty);
59 ledc_hal_set_duty_start(&hal_, channel_);
60 ledc_hal_ls_channel_update(&hal_, channel_);
61 return ErrorCode::OK;
62 }
63
65 {
66 if (config.frequency == 0)
67 {
68 return ErrorCode::ARG_ERR;
69 }
70
71 for (int bits = static_cast<int>(resolution_);
72 bits >= static_cast<int>(LEDC_TIMER_1_BIT); --bits)
73 {
74 ledc_timer_config_t timer_conf = {};
75 timer_conf.speed_mode = speed_mode_;
76 timer_conf.duty_resolution = static_cast<ledc_timer_bit_t>(bits);
77 timer_conf.timer_num = timer_;
78 timer_conf.freq_hz = config.frequency;
79 timer_conf.clk_cfg = LEDC_AUTO_CLK;
80
81 if (ledc_timer_config(&timer_conf) != ESP_OK)
82 {
83 continue;
84 }
85
86 resolution_ = static_cast<ledc_timer_bit_t>(bits);
87 ledc_hal_bind_channel_timer(&hal_, channel_, timer_);
88 ledc_hal_ls_timer_update(&hal_, timer_);
89 max_duty_ = (1U << static_cast<uint8_t>(resolution_)) - 1U;
90 return ErrorCode::OK;
91 }
92
94 }
95
96 ErrorCode Enable() override
97 {
98 ledc_hal_set_sig_out_en(&hal_, channel_, true);
99 ledc_hal_ls_channel_update(&hal_, channel_);
100 return ErrorCode::OK;
101 }
102
104 {
105 ledc_hal_set_idle_level(&hal_, channel_, 0);
106 ledc_hal_set_sig_out_en(&hal_, channel_, false);
107 ledc_hal_ls_channel_update(&hal_, channel_);
108 return ErrorCode::OK;
109 }
110
111 private:
112 int gpio_num_;
113 ledc_channel_t channel_;
114 ledc_timer_t timer_;
115 ledc_mode_t speed_mode_;
116 ledc_hal_context_t hal_{};
117 ledc_timer_bit_t resolution_;
118 uint32_t max_duty_;
119};
120
121} // namespace LibXR
122
ESP32 PWM driver implementation.
Definition esp_pwm.hpp:15
ErrorCode Enable() override
Enables the PWM output. 启用 PWM 输出。
Definition esp_pwm.hpp:96
ErrorCode SetConfig(Configuration config) override
Configures the PWM settings. 配置 PWM 参数。
Definition esp_pwm.hpp:64
ErrorCode SetDutyCycle(float value) override
Sets the duty cycle of the PWM signal. 设置 PWM 信号的占空比。
Definition esp_pwm.hpp:46
ESP32PWM(int gpio_num, ledc_channel_t channel, ledc_timer_t timer=LEDC_TIMER_0, ledc_timer_bit_t resolution=static_cast< ledc_timer_bit_t >(static_cast< uint8_t >(LEDC_TIMER_BIT_MAX) - 1))
Construct a PWM channel object.
Definition esp_pwm.hpp:18
ErrorCode Disable() override
Disables the PWM output. 禁用 PWM 输出。
Definition esp_pwm.hpp:103
Abstract base class for PWM (Pulse Width Modulation) control. PWM(脉冲宽度调制)控制的抽象基类。
Definition pwm.hpp:14
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
Definition libxr_def.hpp:64
@ INIT_ERR
初始化错误 | Initialization error
@ OK
操作成功 | Operation successful
@ ARG_ERR
参数错误 | Argument error
Configuration parameters for PWM. PWM 配置参数。
Definition pwm.hpp:23
uint32_t frequency
PWM signal frequency in Hz. PWM 信号的频率(Hz)。
Definition pwm.hpp:24