ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
gpio_pin.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <concepts>
4#include <cstdint>
5#include <type_traits>
6
9#include "esphome/core/log.h"
10
11namespace esphome {
12
14inline constexpr size_t GPIO_SUMMARY_MAX_LEN = 48;
15
16#ifdef USE_ESP8266
17#define LOG_PIN(prefix, pin) log_pin(TAG, F(prefix), pin)
18#else
19#define LOG_PIN(prefix, pin) log_pin(TAG, prefix, pin)
20#endif
21
22// put GPIO flags in a namespace to not pollute esphome namespace
23namespace gpio {
24
25enum Flags : uint8_t {
26 // Can't name these just INPUT because of Arduino defines :(
27 FLAG_NONE = 0x00,
28 FLAG_INPUT = 0x01,
33 FLAG_HOLD = 0x20,
34};
35
37 public:
38 constexpr FlagsHelper(Flags val) : val_(val) {}
39 constexpr operator Flags() const { return val_; }
40
41 protected:
43};
44constexpr FlagsHelper operator&(Flags lhs, Flags rhs) {
45 return static_cast<Flags>(static_cast<uint8_t>(lhs) & static_cast<uint8_t>(rhs));
46}
47constexpr FlagsHelper operator|(Flags lhs, Flags rhs) {
48 return static_cast<Flags>(static_cast<uint8_t>(lhs) | static_cast<uint8_t>(rhs));
49}
50
58
59} // namespace gpio
60
61class GPIOPin {
62 public:
63 virtual void setup() = 0;
64
65 virtual void pin_mode(gpio::Flags flags) = 0;
66
72 virtual gpio::Flags get_flags() const = 0;
73
74 virtual bool digital_read() = 0;
75
76 virtual void digital_write(bool value) = 0;
77
83 virtual size_t dump_summary(char *buffer, size_t len) const;
84
87 virtual bool is_internal() { return false; }
88#ifdef USE_GPIO_HOLD
89 bool get_hold() const { return this->get_flags() & gpio::FLAG_HOLD; }
90 virtual bool is_held() const { return false; }
91#else
92 bool get_hold() const { return false; }
93 bool is_held() const { return false; }
94#endif
95};
96
97// The one platform pin class, which alone builds an ISRInternalGPIOPin from itself in to_isr()
98#if defined(USE_ESP32)
99namespace esp32 {
100class ESP32InternalGPIOPin;
101}
102#elif defined(USE_ESP8266)
103namespace esp8266 {
104class ESP8266GPIOPin;
105}
106#elif defined(USE_LIBRETINY)
107namespace libretiny {
108class ArduinoInternalGPIOPin;
109}
110#elif defined(USE_RP2)
111namespace rp2 {
112class RP2GPIOPin;
113}
114#elif defined(USE_HOST)
115namespace host {
116class HostGPIOPin;
117}
118#elif defined(USE_ZEPHYR)
119namespace zephyr {
120class ZephyrGPIOPin;
121}
122#endif
123
127#if defined(USE_ESP32)
129#elif defined(USE_ESP8266)
131#elif defined(USE_LIBRETINY)
133#elif defined(USE_RP2)
134 friend class rp2::RP2GPIOPin;
135#elif defined(USE_HOST)
136 friend class host::HostGPIOPin;
137#elif defined(USE_ZEPHYR)
139#endif
140
141 public:
143 ISRInternalGPIOPin(void *arg) : arg_(arg) {}
144 bool digital_read();
145 void digital_write(bool value);
146 void clear_interrupt();
148
149 protected:
150 // Only to_isr() builds a handle from a pin, since it also prepares the pin (the RTC IO number on ESP32).
151 template<std::derived_from<GPIOPin> T>
152 explicit ISRInternalGPIOPin(const T *pin)
153 : arg_(const_cast<T *>(pin)) {} // NOLINT(cppcoreguidelines-pro-type-const-cast)
154
155 void *arg_{nullptr};
156};
157
158// What callers of the InternalGPIOPin alias (bound in esphome/core/gpio.h) rely on beyond
159// GPIOPin. The class must be final so calls to the GPIOPin virtuals through the alias are direct.
160// int is arbitrary: any T * exercises the public attach_interrupt<T> template.
161template<typename T>
162concept InternalGPIOPinContract = std::derived_from<T, GPIOPin> && std::is_final_v<T> &&
163 requires(const T pin, void (*func)(int *), int *arg, gpio::InterruptType type) {
164 { pin.get_pin() } -> std::same_as<uint8_t>;
165 { pin.is_inverted() } -> std::same_as<bool>;
166 { pin.to_isr() } -> std::same_as<ISRInternalGPIOPin>;
167 { pin.detach_interrupt() } -> std::same_as<void>;
168 { pin.attach_interrupt(func, arg, type) } -> std::same_as<void>;
169 };
170
171// Inline default implementation for GPIOPin::dump_summary.
172// Writes an empty summary; subclasses override to provide pin details.
173inline size_t GPIOPin::dump_summary(char *buffer, size_t len) const {
174 if (len > 0)
175 buffer[0] = '\0';
176 return 0;
177}
178
179// Inline helper for log_pin - allows compiler to inline into log_pin in gpio.cpp
180inline void log_pin_with_prefix(const char *tag, const char *prefix, GPIOPin *pin) {
182 size_t len = pin->dump_summary(buffer, sizeof(buffer));
183 len = std::min(len, sizeof(buffer) - 1);
184 esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, ESPHOME_LOG_FORMAT("%s%.*s"), prefix, (int) len, buffer);
185}
186
187// log_pin function declarations - implementation in gpio.cpp
188#ifdef USE_ESP8266
189void log_pin(const char *tag, const __FlashStringHelper *prefix, GPIOPin *pin);
190#else
191void log_pin(const char *tag, const char *prefix, GPIOPin *pin);
192#endif
193
196void pulse_pin(GPIOPin *pin, bool level, uint32_t width_us = 0);
197
205
206namespace internal {
208void pulse_reset_ms(GPIOPin *pin, uint32_t high_before_ms, uint32_t low_ms, uint32_t high_after_ms);
209} // namespace internal
210
214ESPHOME_ALWAYS_INLINE inline void pulse_reset(GPIOPin *pin, ResetPulse pulse) {
216}
217
219void setup_output_pin(GPIOPin *pin, bool level);
220
221} // namespace esphome
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
bool is_held() const
Definition gpio_pin.h:93
virtual void digital_write(bool value)=0
virtual bool is_internal()
Only the platform pin class behind the InternalGPIOPin alias may return true: callers static_cast to ...
Definition gpio_pin.h:87
virtual gpio::Flags get_flags() const =0
Retrieve GPIO pin flags.
virtual bool digital_read()=0
bool get_hold() const
Definition gpio_pin.h:89
virtual bool is_held() const
Definition gpio_pin.h:90
virtual size_t dump_summary(char *buffer, size_t len) const
Write a summary of this pin to the provided buffer.
Definition gpio_pin.h:173
ISR-safe handle to an internal pin (no virtual calls); every handle points at the pin itself and shar...
Definition gpio_pin.h:126
void digital_write(bool value)
Definition gpio.cpp:211
void pin_mode(gpio::Flags flags)
Definition gpio.cpp:223
ISRInternalGPIOPin(const T *pin)
Definition gpio_pin.h:152
constexpr FlagsHelper(Flags val)
Definition gpio_pin.h:38
uint16_t flags
uint8_t type
uint32_t buffer
Definition gif.cpp:50
mopeka_std_values val[3]
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
@ INTERRUPT_RISING_EDGE
Definition gpio_pin.h:52
@ INTERRUPT_HIGH_LEVEL
Definition gpio_pin.h:56
@ INTERRUPT_LOW_LEVEL
Definition gpio_pin.h:55
@ INTERRUPT_ANY_EDGE
Definition gpio_pin.h:54
constexpr FlagsHelper operator|(Flags lhs, Flags rhs)
Definition gpio_pin.h:47
constexpr FlagsHelper operator&(Flags lhs, Flags rhs)
Definition gpio_pin.h:44
void pulse_reset_ms(GPIOPin *pin, uint32_t high_before_ms, uint32_t low_ms, uint32_t high_after_ms)
Out-of-line body of pulse_reset(); call that instead.
Definition gpio.cpp:31
char host[MAX_ORIGIN_LENGTH+1]
void pulse_pin(GPIOPin *pin, bool level, uint32_t width_us)
Drive pin to level for width_us microseconds, then to the opposite level.
Definition gpio.cpp:23
void HOT esp_log_printf_(int level, const char *tag, int line, const char *format,...)
Definition log.cpp:21
uint32_t value
Definition hal.cpp:59
const void size_t len
Definition hal.h:68
void log_pin_with_prefix(const char *tag, const char *prefix, GPIOPin *pin)
Definition gpio_pin.h:180
void setup_output_pin(GPIOPin *pin, bool level)
Set up pin and drive it to level. Does nothing when pin is null, so an optional pin needs no check.
Definition gpio.cpp:44
constexpr size_t GPIO_SUMMARY_MAX_LEN
Maximum buffer size for dump_summary output.
Definition gpio_pin.h:14
void const char * tag
Definition log.h:110
ESPHOME_ALWAYS_INLINE void pulse_reset(GPIOPin *pin, ResetPulse pulse)
Set up an active-low reset pin and drive it through pulse.
Definition gpio_pin.h:214
void log_pin(const char *tag, const __FlashStringHelper *prefix, GPIOPin *pin)
Definition gpio.cpp:9
static void uint32_t
Waveform for pulse_reset(): high, low, then high again.
Definition gpio_pin.h:200
uint32_t low_ms
Hold the pin low (in reset) this long.
Definition gpio_pin.h:202
uint32_t high_before_ms
Drive high this long first; skipped when 0.
Definition gpio_pin.h:201
uint32_t high_after_ms
Wait this long after releasing the pin.
Definition gpio_pin.h:203