libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::ESP32PWM Class Reference

ESP32 PWM driver implementation. More...

#include <esp_pwm.hpp>

Inheritance diagram for LibXR::ESP32PWM:
[legend]
Collaboration diagram for LibXR::ESP32PWM:
[legend]

Public Member Functions

 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.
 
ErrorCode SetDutyCycle (float value) override
 设置 PWM 信号的占空比 / Sets the duty cycle of the PWM signal.
 
ErrorCode SetConfig (Configuration config) override
 配置 PWM 参数 / Configures the PWM settings.
 
ErrorCode Enable () override
 启用 PWM 输出 / Enables the PWM output.
 
ErrorCode Disable () override
 禁用 PWM 输出 / Disables the PWM output.
 
- Public Member Functions inherited from LibXR::PWM

Detailed Description

ESP32 PWM driver implementation.

Definition at line 13 of file esp_pwm.hpp.

Constructor & Destructor Documentation

◆ ESP32PWM()

LibXR::ESP32PWM::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) )
inline

Construct a PWM channel object.

Definition at line 17 of file esp_pwm.hpp.

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 REQUIRE(false);
42 }
43 }

Member Function Documentation

◆ Disable()

ErrorCode LibXR::ESP32PWM::Disable ( )
inlineoverridevirtual

禁用 PWM 输出 / Disables the PWM output.

Returns
返回操作结果的错误码 / ErrorCode indicating success or failure.

Implements LibXR::PWM.

Definition at line 102 of file esp_pwm.hpp.

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 }
@ OK
操作成功 / Operation successful

◆ Enable()

ErrorCode LibXR::ESP32PWM::Enable ( )
inlineoverridevirtual

启用 PWM 输出 / Enables the PWM output.

Returns
返回操作结果的错误码 / ErrorCode indicating success or failure.

Implements LibXR::PWM.

Definition at line 95 of file esp_pwm.hpp.

96 {
97 ledc_hal_set_sig_out_en(&hal_, channel_, true);
98 ledc_hal_ls_channel_update(&hal_, channel_);
99 return ErrorCode::OK;
100 }

◆ SetConfig()

ErrorCode LibXR::ESP32PWM::SetConfig ( Configuration config)
inlineoverridevirtual

配置 PWM 参数 / Configures the PWM settings.

Parameters
config配置结构体,包含 PWM 设置。 The configuration structure containing PWM settings.
Returns
返回操作结果的错误码 / ErrorCode indicating success or failure.

Implements LibXR::PWM.

Definition at line 63 of file esp_pwm.hpp.

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 }
@ INIT_ERR
初始化错误 / Initialization error
@ ARG_ERR
参数错误 / Argument error

◆ SetDutyCycle()

ErrorCode LibXR::ESP32PWM::SetDutyCycle ( float value)
inlineoverridevirtual

设置 PWM 信号的占空比 / Sets the duty cycle of the PWM signal.

Parameters
value占空比,浮点值(0.0 到 1.0)。 The duty cycle as a floating-point value (0.0 to 1.0).
Returns
返回操作结果的错误码 / ErrorCode indicating success or failure.

Implements LibXR::PWM.

Definition at line 45 of file esp_pwm.hpp.

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 }

The documentation for this class was generated from the following file: