ESPHome 2025.10.0-dev
Loading...
Searching...
No Matches
gpio.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4#include "esphome/core/hal.h"
5#include <driver/gpio.h>
6
7namespace esphome {
8namespace 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
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 std::string dump_summary() const override;
28 void detach_interrupt() const override;
29 ISRInternalGPIOPin to_isr() const override;
30 uint8_t get_pin() const override { return this->pin_; }
31 gpio::Flags get_flags() const override { return this->flags_; }
32 bool is_inverted() const override { return this->pin_flags_.inverted; }
33 gpio_num_t get_pin_num() const { return static_cast<gpio_num_t>(this->pin_); }
34 gpio_drive_cap_t get_drive_strength() const { return static_cast<gpio_drive_cap_t>(this->pin_flags_.drive_strength); }
35
36 protected:
37 void attach_interrupt(void (*func)(void *), void *arg, gpio::InterruptType type) const override;
38
39 // Memory layout: 8 bytes total on 32-bit systems
40 // - 3 bytes for members below
41 // - 1 byte padding for alignment
42 // - 4 bytes for vtable pointer
43 uint8_t pin_; // GPIO pin number (0-255, actual max ~54 on ESP32)
44 gpio::Flags flags_; // GPIO flags (1 byte)
45 struct PinFlags {
46 uint8_t inverted : 1; // Invert pin logic (1 bit)
47 uint8_t drive_strength : 2; // Drive strength 0-3 (2 bits)
48 uint8_t reserved : 5; // Reserved for future use (5 bits)
49 } pin_flags_; // Total: 1 byte
50 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
52};
53
54} // namespace esp32
55} // namespace esphome
56
57#endif // USE_ESP32
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition gpio.h:73
void set_inverted(bool inverted)
Definition gpio.h:17
uint8_t get_pin() const override
Definition gpio.h:30
void set_pin(gpio_num_t pin)
Definition gpio.h:16
gpio_num_t get_pin_num() const
Definition gpio.h:33
struct esphome::esp32::ESP32InternalGPIOPin::PinFlags pin_flags_
void attach_interrupt(void(*func)(void *), void *arg, gpio::InterruptType type) const override
Definition gpio.cpp:68
gpio::Flags get_flags() const override
Definition gpio.h:31
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:34
bool is_inverted() const override
Definition gpio.h:32
void detach_interrupt() const override
Definition gpio.cpp:139
ISRInternalGPIOPin to_isr() const override
Definition gpio.cpp:55
void set_flags(gpio::Flags flags)
Definition gpio.h:21
std::string dump_summary() const override
Definition gpio.cpp:100
void pin_mode(gpio::Flags flags) override
Definition gpio.cpp:119
void digital_write(bool value) override
Definition gpio.cpp:136
uint16_t type
uint16_t flags
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7