libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
esp_gpio.hpp
1#pragma once
2
3#include <cstdint>
4
5#include "driver/gpio.h"
6#include "esp_def.hpp"
7#include "gpio.hpp"
8#include "hal/gpio_hal.h"
9
10static inline gpio_dev_t* LibXREspGpioHw() { return GPIO_HAL_GET_HW(0); }
11
12namespace LibXR
13{
17class ESP32GPIO : public GPIO
18{
19 public:
24 explicit ESP32GPIO(gpio_num_t gpio_num) : 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 }
33
34 bool Read() override
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 }
46
47 void Write(bool value) override
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 }
59
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 }
93
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 }
112
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 }
177
178 static void InterruptDispatcher(void* arg);
179
180 private:
181 gpio_num_t gpio_num_;
182 bool isr_handler_added_ = false;
183 static inline bool isr_service_installed_ = false;
184 static inline ESP32GPIO* map_[GPIO_NUM_MAX];
185};
186
187} // namespace LibXR
ESP32 GPIO 驱动实现 / ESP32 GPIO driver implementation.
Definition esp_gpio.hpp:18
bool Read() override
读取 GPIO 引脚状态。Reads the GPIO pin state.
Definition esp_gpio.hpp:34
ErrorCode DisableInterrupt() override
禁用 GPIO 引脚中断。Disables the GPIO pin interrupt.
Definition esp_gpio.hpp:94
ESP32GPIO(gpio_num_t gpio_num)
构造 GPIO 对象 / Construct GPIO object
Definition esp_gpio.hpp:24
void Write(bool value) override
写入 GPIO 引脚状态。Writes the GPIO pin state.
Definition esp_gpio.hpp:47
ErrorCode SetConfig(Configuration config) override
配置 GPIO 引脚参数。Configures the GPIO pin settings.
Definition esp_gpio.hpp:113
ErrorCode EnableInterrupt() override
使能 GPIO 引脚中断。Enables the GPIO pin interrupt.
Definition esp_gpio.hpp:60
通用输入输出(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
定义错误码枚举
@ 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