libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
esp_gpio.hpp
1#pragma once
2
3#include "driver/gpio.h"
4#include "esp_log.h"
5#include "gpio.hpp"
6
7namespace LibXR
8{
12class ESP32GPIO : public GPIO
13{
14 public:
19 explicit ESP32GPIO(gpio_num_t gpio_num) : gpio_num_(gpio_num)
20 {
21 map_[gpio_num_] = this;
22 }
23
24 bool Read() override { return gpio_get_level(gpio_num_) != 0; }
25
26 void Write(bool value) override { gpio_set_level(gpio_num_, value ? 1 : 0); }
27
28 ErrorCode EnableInterrupt() override
29 {
30 if (!isr_service_installed_)
31 {
32 gpio_install_isr_service(0);
33 isr_service_installed_ = true;
34 }
35
36 gpio_isr_handler_add(gpio_num_, ESP32GPIO::InterruptDispatcher, (void*)gpio_num_);
37 gpio_intr_enable(gpio_num_);
38 return ErrorCode::OK;
39 }
40
41 ErrorCode DisableInterrupt() override
42 {
43 gpio_intr_disable(gpio_num_);
44 gpio_isr_handler_remove(gpio_num_);
45 return ErrorCode::OK;
46 }
47
48 ErrorCode SetConfig(Configuration config) override
49 {
50 gpio_config_t io_conf = {};
51 io_conf.pin_bit_mask = 1ULL << gpio_num_;
52
53 switch (config.direction)
54 {
56 io_conf.mode = GPIO_MODE_INPUT;
57 break;
59 io_conf.mode = GPIO_MODE_OUTPUT;
60 break;
62 io_conf.mode = GPIO_MODE_OUTPUT_OD;
63 break;
65 io_conf.mode = GPIO_MODE_INPUT;
66 io_conf.intr_type = GPIO_INTR_NEGEDGE;
67 break;
69 io_conf.mode = GPIO_MODE_INPUT;
70 io_conf.intr_type = GPIO_INTR_POSEDGE;
71 break;
73 io_conf.mode = GPIO_MODE_INPUT;
74 io_conf.intr_type = GPIO_INTR_ANYEDGE;
75 break;
76 }
77
78 switch (config.pull)
79 {
80 case Pull::NONE:
81 io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
82 io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
83 break;
84 case Pull::UP:
85 io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
86 io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
87 break;
88 case Pull::DOWN:
89 io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
90 io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
91 break;
92 }
93
94 gpio_config(&io_conf);
95
96 return ErrorCode::OK;
97 }
98
99 static void IRAM_ATTR InterruptDispatcher(void* arg);
100
101 private:
102 gpio_num_t gpio_num_;
103 static inline bool isr_service_installed_ = false;
104 static inline ESP32GPIO* map_[GPIO_NUM_MAX];
105};
106
107} // namespace LibXR
ESP32 GPIO 驱动实现 / ESP32 GPIO driver implementation.
Definition esp_gpio.hpp:13
bool Read() override
读取 GPIO 引脚状态。Reads the GPIO pin state.
Definition esp_gpio.hpp:24
ErrorCode DisableInterrupt() override
禁用 GPIO 引脚中断。Disables the GPIO pin interrupt.
Definition esp_gpio.hpp:41
ESP32GPIO(gpio_num_t gpio_num)
构造 GPIO 对象 / Construct GPIO object
Definition esp_gpio.hpp:19
void Write(bool value) override
写入 GPIO 引脚状态。Writes the GPIO pin state.
Definition esp_gpio.hpp:26
ErrorCode SetConfig(Configuration config) override
配置 GPIO 引脚参数。Configures the GPIO pin settings.
Definition esp_gpio.hpp:48
ErrorCode EnableInterrupt() override
使能 GPIO 引脚中断。Enables the GPIO pin interrupt.
Definition esp_gpio.hpp:28
通用输入输出(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
存储 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