ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
gpio.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
5#include <esp_system.h>
6#include <driver/gpio.h>
7
8namespace esphome::esp32 {
9
10// Static assertions to ensure our bit-packed fields can hold the enum values
11static_assert(GPIO_NUM_MAX <= 256, "gpio_num_t has too many values for uint8_t");
12static_assert(GPIO_DRIVE_CAP_MAX <= 4, "gpio_drive_cap_t has too many values for 2-bit field");
13
14class ESP32InternalGPIOPin final : public GPIOPin {
15 public:
16 void set_pin(gpio_num_t pin) { this->pin_ = static_cast<uint8_t>(pin); }
17 void set_inverted(bool inverted) { this->pin_flags_.inverted = inverted; }
18 void set_drive_strength(gpio_drive_cap_t drive_strength) {
19 this->pin_flags_.drive_strength = static_cast<uint8_t>(drive_strength);
20 }
22
23 void setup() override;
24 void pin_mode(gpio::Flags flags) override;
25 bool digital_read() override;
26 void digital_write(bool value) override;
27 size_t dump_summary(char *buffer, size_t len) const override;
28 void detach_interrupt() const;
29 template<typename T> void attach_interrupt(void (*func)(T *), T *arg, gpio::InterruptType type) const {
30 this->attach_interrupt_(reinterpret_cast<void (*)(void *)>(func), arg, type);
31 }
33 uint8_t get_pin() const { return this->pin_; }
34 gpio::Flags get_flags() const override { return this->flags_; }
35 bool is_inverted() const { return this->pin_flags_.inverted; }
36 bool is_internal() override { return true; }
37 gpio_num_t get_pin_num() const { return static_cast<gpio_num_t>(this->pin_); }
38 gpio_drive_cap_t get_drive_strength() const { return static_cast<gpio_drive_cap_t>(this->pin_flags_.drive_strength); }
39#ifdef USE_GPIO_HOLD
41 void set_hold(bool hold) const;
42 bool is_held() const override {
43 if (!this->get_hold())
44 return false;
45 esp_reset_reason_t reason = esp_reset_reason();
46 return reason == ESP_RST_DEEPSLEEP || reason == ESP_RST_SW;
47 }
48#endif
49
50 protected:
51 void attach_interrupt_(void (*func)(void *), void *arg, gpio::InterruptType type) const;
52
53 // Memory layout: 8 bytes total on 32-bit systems
54 // - 3 bytes for members below
55 // - 1 byte padding for alignment
56 // - 4 bytes for vtable pointer
57 uint8_t pin_; // GPIO pin number (0-255, actual max ~54 on ESP32)
58 gpio::Flags flags_{}; // GPIO flags (1 byte)
59 struct PinFlags {
60 uint8_t inverted : 1; // Invert pin logic (1 bit)
61 uint8_t drive_strength : 2; // Drive strength 0-3 (2 bits)
62 uint8_t reserved : 5; // Reserved for future use (5 bits)
63 } pin_flags_{}; // Total: 1 byte
64 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
66};
67
68} // namespace esphome::esp32
69
70#endif // USE_ESP32
bool get_hold() const
Definition gpio_pin.h:89
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition gpio_pin.h:98
void set_inverted(bool inverted)
Definition gpio.h:17
void set_pin(gpio_num_t pin)
Definition gpio.h:16
gpio_num_t get_pin_num() const
Definition gpio.h:37
struct esphome::esp32::ESP32InternalGPIOPin::PinFlags pin_flags_
gpio::Flags get_flags() const override
Definition gpio.h:34
void set_drive_strength(gpio_drive_cap_t drive_strength)
Definition gpio.h:18
gpio_drive_cap_t get_drive_strength() const
Definition gpio.h:38
void set_hold(bool hold) const
Apply or release the pad hold. Does nothing unless the pin is configured with hold_state.
Definition gpio.cpp:144
bool is_held() const override
Definition gpio.h:42
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:29
void set_flags(gpio::Flags flags)
Definition gpio.h:21
void pin_mode(gpio::Flags flags) override
Definition gpio.cpp:188
void attach_interrupt_(void(*func)(void *), void *arg, gpio::InterruptType type) const
Definition gpio.cpp:107
void digital_write(bool value) override
Definition gpio.cpp:208
ISRInternalGPIOPin to_isr() const
Definition gpio.cpp:91
size_t dump_summary(char *buffer, size_t len) const override
Definition gpio.cpp:139
uint16_t flags
uint8_t type
uint32_t buffer
Definition gif.cpp:50
const void size_t len
Definition hal.h:68