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.
 
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 21 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 28 of file esp_gpio.hpp.

28 : gpio_num_(gpio_num)
29 {
30 const bool valid = (gpio_num_ >= 0) && (gpio_num_ < GPIO_NUM_MAX);
31 ASSERT(valid);
32 if (valid)
33 {
34 map_[gpio_num_] = this;
35 }
36 }

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 98 of file esp_gpio.hpp.

99 {
100 if (!GPIO_IS_VALID_GPIO(gpio_num_))
101 {
102 return ErrorCode::ARG_ERR;
103 }
104
105 if (!isr_handler_added_)
106 {
107 return ErrorCode::OK;
108 }
109
110 if (gpio_intr_disable(gpio_num_) != ESP_OK)
111 {
112 return ErrorCode::FAILED;
113 }
114 return ErrorCode::OK;
115 }

◆ 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 64 of file esp_gpio.hpp.

65 {
66 if (!GPIO_IS_VALID_GPIO(gpio_num_))
67 {
68 return ErrorCode::ARG_ERR;
69 }
70
71 if (!isr_service_installed_)
72 {
73 if (gpio_install_isr_service(0) != ESP_OK)
74 {
75 return ErrorCode::INIT_ERR;
76 }
77 isr_service_installed_ = true;
78 }
79
80 if (!isr_handler_added_)
81 {
82 if (gpio_isr_handler_add(gpio_num_, ESP32GPIO::InterruptDispatcher,
83 reinterpret_cast<void*>(
84 static_cast<uintptr_t>(gpio_num_))) != ESP_OK)
85 {
86 return ErrorCode::INIT_ERR;
87 }
88 isr_handler_added_ = true;
89 }
90
91 if (gpio_intr_enable(gpio_num_) != ESP_OK)
92 {
93 return ErrorCode::FAILED;
94 }
95 return ErrorCode::OK;
96 }

◆ 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 38 of file esp_gpio.hpp.

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

◆ 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 117 of file esp_gpio.hpp.

118 {
119 if (!GPIO_IS_VALID_GPIO(gpio_num_))
120 {
121 return ErrorCode::ARG_ERR;
122 }
123
124 gpio_hal_context_t hal = {.dev = LibXREspGpioHw()};
125
126 // Align with ST/CH reconfigure semantics: no full reset, only re-apply mode fields.
127 gpio_hal_set_output_enable_ctrl(&hal, gpio_num_, false, false);
128 gpio_hal_func_sel(&hal, gpio_num_, PIN_FUNC_GPIO);
129
130 gpio_hal_pullup_dis(&hal, gpio_num_);
131 gpio_hal_pulldown_dis(&hal, gpio_num_);
132 gpio_hal_od_disable(&hal, gpio_num_);
133 gpio_hal_set_intr_type(&hal, gpio_num_, GPIO_INTR_DISABLE);
134
135 switch (config.pull)
136 {
137 case Pull::NONE:
138 break;
139 case Pull::UP:
140 gpio_hal_pullup_en(&hal, gpio_num_);
141 break;
142 case Pull::DOWN:
143 gpio_hal_pulldown_en(&hal, gpio_num_);
144 break;
145 }
146
147 switch (config.direction)
148 {
149 case Direction::INPUT:
150 gpio_hal_input_enable(&hal, gpio_num_);
151 gpio_hal_output_disable(&hal, gpio_num_);
152 break;
154 gpio_hal_input_disable(&hal, gpio_num_);
155 gpio_hal_output_enable(&hal, gpio_num_);
156 break;
158 gpio_hal_input_enable(&hal, gpio_num_);
159 gpio_hal_output_enable(&hal, gpio_num_);
160 gpio_hal_od_enable(&hal, gpio_num_);
161 break;
163 gpio_hal_input_enable(&hal, gpio_num_);
164 gpio_hal_output_disable(&hal, gpio_num_);
165 gpio_hal_set_intr_type(&hal, gpio_num_, GPIO_INTR_NEGEDGE);
166 break;
168 gpio_hal_input_enable(&hal, gpio_num_);
169 gpio_hal_output_disable(&hal, gpio_num_);
170 gpio_hal_set_intr_type(&hal, gpio_num_, GPIO_INTR_POSEDGE);
171 break;
173 gpio_hal_input_enable(&hal, gpio_num_);
174 gpio_hal_output_disable(&hal, gpio_num_);
175 gpio_hal_set_intr_type(&hal, gpio_num_, GPIO_INTR_ANYEDGE);
176 break;
177 }
178
179 return ErrorCode::OK;
180 }
@ 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 51 of file esp_gpio.hpp.

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

Field Documentation

◆ gpio_num_

gpio_num_t LibXR::ESP32GPIO::gpio_num_
private

Definition at line 185 of file esp_gpio.hpp.

◆ isr_handler_added_

bool LibXR::ESP32GPIO::isr_handler_added_ = false
private

Definition at line 186 of file esp_gpio.hpp.

◆ isr_service_installed_

bool LibXR::ESP32GPIO::isr_service_installed_ = false
inlinestaticprivate

Definition at line 187 of file esp_gpio.hpp.

◆ map_

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

Definition at line 188 of file esp_gpio.hpp.


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