libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::ESP32GPIO Class Reference

ESP32 GPIO 驱动实现 / ESP32 GPIO driver implementation. More...

#include <esp_gpio.hpp>

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

Public Member Functions

 ESP32GPIO (gpio_num_t gpio_num)
 构造 GPIO 对象 / Construct GPIO object
 
bool Read () override
 读取 GPIO 引脚状态。Reads the GPIO pin state.
 
void Write (bool value) override
 写入 GPIO 引脚状态。Writes the GPIO pin state.
 
ErrorCode EnableInterrupt () override
 使能 GPIO 引脚中断。Enables the GPIO pin interrupt.
 
ErrorCode DisableInterrupt () override
 禁用 GPIO 引脚中断。Disables the GPIO pin interrupt.
 
ErrorCode SetConfig (Configuration config) override
 配置 GPIO 引脚参数。Configures the GPIO pin settings.
 
- Public Member Functions inherited from LibXR::GPIO
 GPIO ()
 默认构造函数。Default constructor.
 
virtual ~GPIO ()=default
 虚析构函数。Virtual destructor.
 
ErrorCode RegisterCallback (Callback callback)
 注册 GPIO 事件回调函数。Registers a callback function for GPIO events.
 

Static Public Member Functions

static void InterruptDispatcher (void *arg)
 

Private Attributes

gpio_num_t gpio_num_
 
bool isr_handler_added_ = false
 

Static Private Attributes

static bool isr_service_installed_ = false
 
static ESP32GPIOmap_ [GPIO_NUM_MAX]
 

Additional Inherited Members

- Public Types inherited from LibXR::GPIO
enum class  Direction : uint8_t {
  INPUT , OUTPUT_PUSH_PULL , OUTPUT_OPEN_DRAIN , FALL_INTERRUPT ,
  RISING_INTERRUPT , FALL_RISING_INTERRUPT
}
 定义 GPIO 引脚的方向类型。Defines the direction types for GPIO pins. More...
 
enum class  Pull : uint8_t { NONE , UP , DOWN }
 定义 GPIO 引脚的上拉/下拉模式。Defines the pull-up/pull-down configurations for GPIO pins. More...
 
using Callback = LibXR::Callback<>
 
- Data Fields inherited from LibXR::GPIO
Callback callback_
 GPIO 事件的回调函数。Callback function for GPIO events.
 

Detailed Description

ESP32 GPIO 驱动实现 / ESP32 GPIO driver implementation.

Definition at line 17 of file esp_gpio.hpp.

Constructor & Destructor Documentation

◆ ESP32GPIO()

LibXR::ESP32GPIO::ESP32GPIO ( gpio_num_t gpio_num)
inlineexplicit

构造 GPIO 对象 / Construct GPIO object

Parameters
gpio_numGPIO 编号 / GPIO number

Definition at line 24 of file esp_gpio.hpp.

24 : gpio_num_(gpio_num)
25 {
26 const bool valid = (gpio_num_ >= 0) && (gpio_num_ < GPIO_NUM_MAX);
27 ASSERT(valid);
28 if (valid)
29 {
30 map_[gpio_num_] = this;
31 }
32 }

Member Function Documentation

◆ DisableInterrupt()

ErrorCode LibXR::ESP32GPIO::DisableInterrupt ( )
inlineoverridevirtual

禁用 GPIO 引脚中断。Disables the GPIO pin interrupt.

Returns
操作结果的错误码。Error code indicating the result of the operation.

Implements LibXR::GPIO.

Definition at line 94 of file esp_gpio.hpp.

95 {
96 if (!GPIO_IS_VALID_GPIO(gpio_num_))
97 {
98 return ErrorCode::ARG_ERR;
99 }
100
101 if (!isr_handler_added_)
102 {
103 return ErrorCode::OK;
104 }
105
106 if (gpio_intr_disable(gpio_num_) != ESP_OK)
107 {
108 return ErrorCode::FAILED;
109 }
110 return ErrorCode::OK;
111 }
@ FAILED
操作失败 | Operation failed
@ OK
操作成功 | Operation successful
@ ARG_ERR
参数错误 | Argument error

◆ EnableInterrupt()

ErrorCode LibXR::ESP32GPIO::EnableInterrupt ( )
inlineoverridevirtual

使能 GPIO 引脚中断。Enables the GPIO pin interrupt.

Returns
操作结果的错误码。Error code indicating the result of the operation.

Implements LibXR::GPIO.

Definition at line 60 of file esp_gpio.hpp.

61 {
62 if (!GPIO_IS_VALID_GPIO(gpio_num_))
63 {
64 return ErrorCode::ARG_ERR;
65 }
66
67 if (!isr_service_installed_)
68 {
69 if (gpio_install_isr_service(0) != ESP_OK)
70 {
72 }
73 isr_service_installed_ = true;
74 }
75
76 if (!isr_handler_added_)
77 {
78 if (gpio_isr_handler_add(
79 gpio_num_, ESP32GPIO::InterruptDispatcher,
80 reinterpret_cast<void*>(static_cast<uintptr_t>(gpio_num_))) != ESP_OK)
81 {
83 }
84 isr_handler_added_ = true;
85 }
86
87 if (gpio_intr_enable(gpio_num_) != ESP_OK)
88 {
89 return ErrorCode::FAILED;
90 }
91 return ErrorCode::OK;
92 }
@ INIT_ERR
初始化错误 | Initialization error

◆ InterruptDispatcher()

void IRAM_ATTR LibXR::ESP32GPIO::InterruptDispatcher ( void * arg)
static

Definition at line 6 of file esp_gpio.cpp.

7{
8 auto gpio_num = static_cast<gpio_num_t>(reinterpret_cast<uintptr_t>(arg));
9 const bool valid = (gpio_num >= 0) && (gpio_num < GPIO_NUM_MAX);
10 ASSERT(valid);
11 if (!valid)
12 {
13 return;
14 }
15
16 auto gpio = map_[gpio_num];
17 if (gpio)
18 {
19 gpio->callback_.Run(true);
20 }
21}

◆ Read()

bool LibXR::ESP32GPIO::Read ( )
inlineoverridevirtual

读取 GPIO 引脚状态。Reads the GPIO pin state.

Returns
返回引脚状态,true 表示高电平,false 表示低电平。Returns the pin state, true for high, false for low.

Implements LibXR::GPIO.

Definition at line 34 of file esp_gpio.hpp.

35 {
36 const bool valid = (gpio_num_ >= 0) && (gpio_num_ < GPIO_NUM_MAX);
37 ASSERT(valid);
38 if (!valid)
39 {
40 return false;
41 }
42
43 gpio_hal_context_t hal = {.dev = LibXREspGpioHw()};
44 return gpio_hal_get_level(&hal, gpio_num_) != 0;
45 }

◆ SetConfig()

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

配置 GPIO 引脚参数。Configures the GPIO pin settings.

Parameters
config需要应用的 GPIO 配置。The GPIO configuration to apply.
Returns
操作结果的错误码。Error code indicating the result of the operation.

Implements LibXR::GPIO.

Definition at line 113 of file esp_gpio.hpp.

114 {
115 if (!GPIO_IS_VALID_GPIO(gpio_num_))
116 {
117 return ErrorCode::ARG_ERR;
118 }
119
120 gpio_hal_context_t hal = {.dev = LibXREspGpioHw()};
121
122 // Align with ST/CH reconfigure semantics: no full reset, only re-apply mode fields.
123 gpio_hal_set_output_enable_ctrl(&hal, gpio_num_, false, false);
124 gpio_hal_func_sel(&hal, gpio_num_, PIN_FUNC_GPIO);
125
126 gpio_hal_pullup_dis(&hal, gpio_num_);
127 gpio_hal_pulldown_dis(&hal, gpio_num_);
128 gpio_hal_od_disable(&hal, gpio_num_);
129 gpio_hal_set_intr_type(&hal, gpio_num_, GPIO_INTR_DISABLE);
130
131 switch (config.pull)
132 {
133 case Pull::NONE:
134 break;
135 case Pull::UP:
136 gpio_hal_pullup_en(&hal, gpio_num_);
137 break;
138 case Pull::DOWN:
139 gpio_hal_pulldown_en(&hal, gpio_num_);
140 break;
141 }
142
143 switch (config.direction)
144 {
145 case Direction::INPUT:
146 gpio_hal_input_enable(&hal, gpio_num_);
147 gpio_hal_output_disable(&hal, gpio_num_);
148 break;
150 gpio_hal_input_disable(&hal, gpio_num_);
151 gpio_hal_output_enable(&hal, gpio_num_);
152 break;
154 gpio_hal_input_enable(&hal, gpio_num_);
155 gpio_hal_output_enable(&hal, gpio_num_);
156 gpio_hal_od_enable(&hal, gpio_num_);
157 break;
159 gpio_hal_input_enable(&hal, gpio_num_);
160 gpio_hal_output_disable(&hal, gpio_num_);
161 gpio_hal_set_intr_type(&hal, gpio_num_, GPIO_INTR_NEGEDGE);
162 break;
164 gpio_hal_input_enable(&hal, gpio_num_);
165 gpio_hal_output_disable(&hal, gpio_num_);
166 gpio_hal_set_intr_type(&hal, gpio_num_, GPIO_INTR_POSEDGE);
167 break;
169 gpio_hal_input_enable(&hal, gpio_num_);
170 gpio_hal_output_disable(&hal, gpio_num_);
171 gpio_hal_set_intr_type(&hal, gpio_num_, GPIO_INTR_ANYEDGE);
172 break;
173 }
174
175 return ErrorCode::OK;
176 }
@ OUTPUT_PUSH_PULL
推挽输出模式。Push-pull output mode.
@ RISING_INTERRUPT
上升沿中断模式。Rising edge interrupt mode.
@ FALL_RISING_INTERRUPT
双沿触发中断模式。Both edge interrupt mode.
@ OUTPUT_OPEN_DRAIN
开漏输出模式。Open-drain output mode.
@ INPUT
输入模式。Input mode.
@ FALL_INTERRUPT
下降沿中断模式。Falling edge interrupt mode.
@ NONE
无上拉或下拉。No pull-up or pull-down.
@ DOWN
下拉模式。Pull-down mode.
@ UP
上拉模式。Pull-up mode.

◆ Write()

void LibXR::ESP32GPIO::Write ( bool value)
inlineoverridevirtual

写入 GPIO 引脚状态。Writes the GPIO pin state.

Parameters
value要写入的状态,true 表示高电平,false 表示低电平。The value to write, true for high, false for low.

Implements LibXR::GPIO.

Definition at line 47 of file esp_gpio.hpp.

48 {
49 const bool valid = (gpio_num_ >= 0) && (gpio_num_ < GPIO_NUM_MAX);
50 ASSERT(valid);
51 if (!valid)
52 {
53 return;
54 }
55
56 gpio_hal_context_t hal = {.dev = LibXREspGpioHw()};
57 gpio_hal_set_level(&hal, gpio_num_, value ? 1 : 0);
58 }

Field Documentation

◆ gpio_num_

gpio_num_t LibXR::ESP32GPIO::gpio_num_
private

Definition at line 181 of file esp_gpio.hpp.

◆ isr_handler_added_

bool LibXR::ESP32GPIO::isr_handler_added_ = false
private

Definition at line 182 of file esp_gpio.hpp.

◆ isr_service_installed_

bool LibXR::ESP32GPIO::isr_service_installed_ = false
inlinestaticprivate

Definition at line 183 of file esp_gpio.hpp.

◆ map_

ESP32GPIO* LibXR::ESP32GPIO::map_[GPIO_NUM_MAX]
inlinestaticprivate

Definition at line 184 of file esp_gpio.hpp.


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