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