libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
esp_gpio.hpp
1#pragma once
2
3#include "esp_def.hpp"
4
5#include <cstdint>
6
7#include "driver/gpio.h"
8#include "gpio.hpp"
9#include "hal/gpio_hal.h"
10
11static inline gpio_dev_t* LibXREspGpioHw()
12{
13 return GPIO_HAL_GET_HW(0);
14}
15
16namespace LibXR
17{
21class ESP32GPIO : public GPIO
22{
23 public:
28 explicit ESP32GPIO(gpio_num_t gpio_num) : 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 }
37
38 bool Read() override
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 }
50
51 void Write(bool value) override
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 }
63
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 {
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 {
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 }
97
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 }
116
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 }
181
182 static void InterruptDispatcher(void* arg);
183
184 private:
185 gpio_num_t gpio_num_;
186 bool isr_handler_added_ = false;
187 static inline bool isr_service_installed_ = false;
188 static inline ESP32GPIO* map_[GPIO_NUM_MAX];
189};
190
191} // namespace LibXR
ESP32 GPIO 驱动实现 / ESP32 GPIO driver implementation.
Definition esp_gpio.hpp:22
bool Read() override
读取 GPIO 引脚状态。Reads the GPIO pin state.
Definition esp_gpio.hpp:38
ErrorCode DisableInterrupt() override
禁用 GPIO 引脚中断。Disables the GPIO pin interrupt.
Definition esp_gpio.hpp:98
ESP32GPIO(gpio_num_t gpio_num)
构造 GPIO 对象 / Construct GPIO object
Definition esp_gpio.hpp:28
void Write(bool value) override
写入 GPIO 引脚状态。Writes the GPIO pin state.
Definition esp_gpio.hpp:51
ErrorCode SetConfig(Configuration config) override
配置 GPIO 引脚参数。Configures the GPIO pin settings.
Definition esp_gpio.hpp:117
ErrorCode EnableInterrupt() override
使能 GPIO 引脚中断。Enables the GPIO pin interrupt.
Definition esp_gpio.hpp:64
通用输入输出(GPIO)接口类。General Purpose Input/Output (GPIO) interface class.
Definition gpio.hpp:13
@ 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.
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
Definition libxr_def.hpp:64
@ INIT_ERR
初始化错误 | Initialization error
@ FAILED
操作失败 | Operation failed
@ OK
操作成功 | Operation successful
@ ARG_ERR
参数错误 | Argument error
存储 GPIO 配置参数的结构体。Structure storing GPIO configuration parameters.
Definition gpio.hpp:46
Pull pull
GPIO 上拉/下拉配置。GPIO pull-up/pull-down configuration.
Definition gpio.hpp:48
Direction direction
GPIO 引脚方向。GPIO pin direction.
Definition gpio.hpp:47