ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
hdmi_cec.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <atomic>
5#include <initializer_list>
6#include <span>
7
9#include "esphome/core/hal.h"
11#include "esphome/core/gpio.h"
13
14#ifdef USE_ESP32
15#include "driver/gptimer.h"
16#endif
17#ifdef USE_RP2040
18#include <hardware/timer.h>
19#endif
20
21#ifdef USE_HDMI_CEC_UART
22namespace esphome::uart {
23class UARTComponent;
24}
25#endif
26
27namespace esphome::hdmi_cec {
28
29// Generated from the configuration, in flash
30#ifdef HDMI_CEC_OSD_NAME_LENGTH
31// The <Set OSD Name> reply: its opcode 0x47 ('G'), then the name
32extern const char OSD_NAME_REPLY[] PROGMEM;
33#endif
34#ifdef HDMI_CEC_CLAIMED_OPCODE_COUNT
35extern const uint8_t CLAIMED_OPCODES[] PROGMEM;
36#endif
37
38// Release/acquire store and load. The read-modify-write forms of std::atomic are not available on
39// every target (the ESP8266 has none), so a writer computes the new value itself; that is safe here
40// because every atomic in this component has exactly one writer.
41template<typename T> inline ESPHOME_ALWAYS_INLINE void atomic_set(std::atomic<T> &atom, T value) {
42 atom.store(value, std::memory_order_release);
43}
44template<typename T> inline ESPHOME_ALWAYS_INLINE T atomic_get(const std::atomic<T> &atom) {
45 return atom.load(std::memory_order_acquire);
46}
47
51class Frame {
52 public:
53 static constexpr size_t MAX_LENGTH = 16; // HDMI-CEC 1.4
54 static constexpr size_t MAX_PAYLOAD_LENGTH = MAX_LENGTH - 1;
55 static constexpr uint8_t BROADCAST_ADDRESS = 0xF; // also the address of an unregistered device
56
57 Frame() = default;
59 Frame(uint8_t initiator_addr, uint8_t destination_addr, std::span<const uint8_t> payload);
60
61 ESPHOME_ALWAYS_INLINE uint8_t initiator_addr() const { return this->data_[0] >> 4; }
62 ESPHOME_ALWAYS_INLINE uint8_t destination_addr() const { return this->data_[0] & 0xf; }
63 ESPHOME_ALWAYS_INLINE bool is_broadcast() const { return this->destination_addr() == BROADCAST_ADDRESS; }
64 ESPHOME_ALWAYS_INLINE size_t size() const { return this->size_; }
65 // No size check: a frame without an opcode is a poll, acknowledged in the interrupt and never handled
66 ESPHOME_ALWAYS_INLINE uint8_t opcode() const { return this->data_[1]; }
67 std::span<const uint8_t> bytes() const { return {this->data_.data(), this->size_}; }
69 std::span<const uint8_t> payload() const { return this->bytes().subspan(1); }
70 ESPHOME_ALWAYS_INLINE void clear() { this->size_ = 0; }
72 ESPHOME_ALWAYS_INLINE bool push_back(uint8_t data) {
73 if (this->size_ == MAX_LENGTH) {
74 return false;
75 }
76 this->data_[this->size_++] = data;
77 return true;
78 }
79
80 protected:
81 std::array<uint8_t, MAX_LENGTH> data_{};
82 uint8_t size_{0};
83};
84
87static constexpr size_t MAX_UART_BODY_LENGTH = Frame::MAX_LENGTH * 5 - 2;
90size_t encode_uart_body(const Frame &frame, uint8_t *out);
91
96template<size_t SIZE> class FrameRingBuffer {
97 static_assert((SIZE & (SIZE - 1)) == 0, "SIZE must be a power of two");
98
99 public:
100 ESPHOME_ALWAYS_INLINE Frame *front() { return this->is_empty() ? nullptr : this->slot_(this->front_inx_); }
101 ESPHOME_ALWAYS_INLINE void pop_front() { this->advance_(this->front_inx_); }
102 ESPHOME_ALWAYS_INLINE Frame *back() { return this->count_() == SIZE ? nullptr : this->slot_(this->back_inx_); }
103 ESPHOME_ALWAYS_INLINE void push_back() { this->advance_(this->back_inx_); }
104 ESPHOME_ALWAYS_INLINE bool is_empty() const { return this->count_() == 0; }
105
106 protected:
107 ESPHOME_ALWAYS_INLINE uint8_t count_() const {
108 return static_cast<uint8_t>(atomic_get(this->back_inx_) - atomic_get(this->front_inx_));
109 }
110 ESPHOME_ALWAYS_INLINE Frame *slot_(const std::atomic<uint8_t> &inx) { return &this->store_[atomic_get(inx) % SIZE]; }
111 ESPHOME_ALWAYS_INLINE void advance_(std::atomic<uint8_t> &inx) {
112 atomic_set<uint8_t>(inx, static_cast<uint8_t>(atomic_get(inx) + 1));
113 }
114
115 std::array<Frame, SIZE> store_{};
116 std::atomic<uint8_t> front_inx_{0};
117 std::atomic<uint8_t> back_inx_{0};
118};
119
123class HDMICEC : public Component {
124 public:
126 HDMICEC(InternalGPIOPin *pin, uint8_t address, uint8_t device_type)
127 : pin_(pin), address_(address), device_type_(device_type) {}
128 void setup() override;
129 void dump_config() override;
130 void loop() override;
131
132 void set_promiscuous_mode(bool promiscuous) { this->promiscuous_mode_ = promiscuous; }
133 void set_monitor_mode(bool monitor_mode) { this->monitor_mode_ = monitor_mode; }
136 void set_physical_address(uint16_t physical_address) { this->physical_address_ = physical_address; }
137 uint16_t get_physical_address() const { return this->physical_address_; }
138 uint8_t get_address() const { return this->address_; }
139#ifdef USE_HDMI_CEC_UART
141 void set_uart(uart::UARTComponent *uart) { this->uart_ = uart; }
142#endif
143#ifdef HDMI_CEC_MESSAGE_CALLBACK_COUNT
146 template<typename F> void add_on_message_callback(F &&callback) {
147 this->message_callbacks_.add(std::forward<F>(callback));
148 }
149#endif
150
152 bool send(uint8_t source, uint8_t destination, std::span<const uint8_t> payload);
153 bool send(uint8_t destination, std::span<const uint8_t> payload) {
154 return this->send(this->address_, destination, payload);
155 }
157 bool send(uint8_t destination, std::initializer_list<uint8_t> payload) {
158 return this->send(destination, std::span<const uint8_t>(payload.begin(), payload.size()));
159 }
160 bool send(uint8_t source, uint8_t destination, std::initializer_list<uint8_t> payload) {
161 return this->send(source, destination, std::span<const uint8_t>(payload.begin(), payload.size()));
162 }
163
164 protected:
171
172 // Receive side, in the interrupt
173 static void gpio_isr_s(HDMICEC *self);
174 void gpio_isr_();
175 void reset_receiver_();
177 void send_ack_();
178 // A hardware one-shot timer releases the line after the acknowledge bit (gptimer on the ESP32,
179 // timer 1 on the ESP8266, a hardware alarm on the RP2040), so the interrupt returns at once;
180 // without one the interrupt holds the line for the 1.5 ms itself
181 bool setup_ack_timer_();
182 void start_ack_timer_();
183#ifdef USE_ESP32
184 static bool ack_timer_isr_s(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *arg);
185#endif
186#ifdef USE_ESP8266
187 static void ack_timer_isr_s();
188#endif
189#ifdef USE_RP2040
190 static void ack_timer_isr_s(uint alarm_num);
191#endif
192
194 void reply_builtin_(const Frame *frame);
195
196 // Transmit side, in loop()
197 void transmit_message_();
199 void finish_attempt_(bool sent_ok);
201 bool transmit_frame_(const Frame *frame);
202 bool send_start_bit_();
204 bool send_bit_(bool bit_value);
207 void transmit_body_(const Frame *frame);
208 ESPHOME_ALWAYS_INLINE void set_pin_input_high_() { this->isr_pin_.pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP); }
209 ESPHOME_ALWAYS_INLINE void set_pin_output_low_() {
211 this->isr_pin_.digital_write(false);
212 }
213 void handle_received_message_(const Frame *frame);
214
215 static constexpr size_t QUEUE_SIZE = 4;
216 // Each queue is 70 bytes, so two single-byte fields follow each to fill the alignment gap
217 FrameRingBuffer<QUEUE_SIZE> frames_queue_; // received, interrupt to loop()
219 uint8_t recv_byte_{0};
220 FrameRingBuffer<QUEUE_SIZE> send_queue_; // to send, both ends on loop()
223#ifdef HDMI_CEC_MESSAGE_CALLBACK_COUNT
224 StaticCallbackManager<HDMI_CEC_MESSAGE_CALLBACK_COUNT, void(uint8_t, uint8_t, std::span<const uint8_t>)>
226#endif
229#ifdef USE_HDMI_CEC_UART
231#endif
232#ifdef USE_ESP32
233 gptimer_handle_t ack_timer_{nullptr};
234#endif
235#ifdef USE_RP2040
236 int ack_alarm_{-1};
237#endif
238
239 // The word-sized fields come first and the single-byte ones form one run, so nothing is padded
240 std::atomic<uint32_t> last_edge_us_{0}; // the bus is busy until this is some time ago; interrupt writes
241 Frame *recv_frame_{nullptr}; // interrupt only; null when the queue was full or the frame too long
242 uint32_t last_falling_edge_us_{0}; // interrupt only
243 uint32_t last_attempt_us_{0}; // loop() only; a refused start bit drives no edge, so it is timed here
244 uint16_t required_idle_period_{0}; // loop() only
246
247 // Shared with the interrupt. The tally of our own frame is written by the interrupt while
248 // transmitting_ is set and no end of message has passed yet; loop() reads it once eom_received_
249 // says the frame is over
250 std::atomic<bool> eom_received_{false}; // set for every frame, ours or not
251 std::atomic<uint8_t> dropped_frames_{0};
254 bool transmitting_{false};
255
256 // Receive state, interrupt only
257 uint8_t recv_header_{0}; // kept outside the frame so a dropped frame can still be judged
258 uint8_t recv_length_{0};
260 bool recv_eom_{false};
261 bool recv_ack_queued_{false};
262 bool prev_pin_level_{true};
263
264 uint8_t address_; // logical address, Frame::BROADCAST_ADDRESS when only listening
266 bool promiscuous_mode_{false};
267 bool monitor_mode_{false};
268
269 static constexpr uint16_t UNSET_PHYSICAL_ADDRESS = 0xFFFF; // the bus reserves this value
270};
271
272} // namespace esphome::hdmi_cec
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
CallbackManager backed by StaticVector for compile-time-known callback counts.
Definition helpers.h:2296
One CEC bus message: a header byte with the initiator and destination addresses, then up to 15 payloa...
Definition hdmi_cec.h:51
ESPHOME_ALWAYS_INLINE bool push_back(uint8_t data)
False when the frame is full already; the bus allows no more.
Definition hdmi_cec.h:72
std::span< const uint8_t > bytes() const
Definition hdmi_cec.h:67
static constexpr size_t MAX_LENGTH
Definition hdmi_cec.h:53
static constexpr size_t MAX_PAYLOAD_LENGTH
Definition hdmi_cec.h:54
std::span< const uint8_t > payload() const
The bytes after the header, starting with the opcode.
Definition hdmi_cec.h:69
ESPHOME_ALWAYS_INLINE uint8_t initiator_addr() const
Definition hdmi_cec.h:61
ESPHOME_ALWAYS_INLINE size_t size() const
Definition hdmi_cec.h:64
ESPHOME_ALWAYS_INLINE bool is_broadcast() const
Definition hdmi_cec.h:63
static constexpr uint8_t BROADCAST_ADDRESS
Definition hdmi_cec.h:55
ESPHOME_ALWAYS_INLINE uint8_t destination_addr() const
Definition hdmi_cec.h:62
std::array< uint8_t, MAX_LENGTH > data_
Definition hdmi_cec.h:81
ESPHOME_ALWAYS_INLINE void clear()
Definition hdmi_cec.h:70
ESPHOME_ALWAYS_INLINE uint8_t opcode() const
Definition hdmi_cec.h:66
A queue of frames between one producer and one consumer, for instance the receive interrupt and loop(...
Definition hdmi_cec.h:96
std::atomic< uint8_t > front_inx_
Definition hdmi_cec.h:116
ESPHOME_ALWAYS_INLINE void pop_front()
Definition hdmi_cec.h:101
ESPHOME_ALWAYS_INLINE void advance_(std::atomic< uint8_t > &inx)
Definition hdmi_cec.h:111
ESPHOME_ALWAYS_INLINE Frame * back()
Definition hdmi_cec.h:102
std::atomic< uint8_t > back_inx_
Definition hdmi_cec.h:117
ESPHOME_ALWAYS_INLINE void push_back()
Definition hdmi_cec.h:103
ESPHOME_ALWAYS_INLINE Frame * slot_(const std::atomic< uint8_t > &inx)
Definition hdmi_cec.h:110
ESPHOME_ALWAYS_INLINE uint8_t count_() const
Definition hdmi_cec.h:107
ESPHOME_ALWAYS_INLINE Frame * front()
Definition hdmi_cec.h:100
ESPHOME_ALWAYS_INLINE bool is_empty() const
Definition hdmi_cec.h:104
std::array< Frame, SIZE > store_
Definition hdmi_cec.h:115
The HDMI-CEC bus on one GPIO pin.
Definition hdmi_cec.h:123
HDMICEC(InternalGPIOPin *pin, uint8_t address, uint8_t device_type)
device_type: the CEC device type of the logical address, for <Report Physical Address>
Definition hdmi_cec.h:126
std::atomic< uint32_t > last_edge_us_
Definition hdmi_cec.h:240
void dump_config() override
Definition hdmi_cec.cpp:125
FrameRingBuffer< QUEUE_SIZE > send_queue_
Definition hdmi_cec.h:220
bool send(uint8_t source, uint8_t destination, std::initializer_list< uint8_t > payload)
Definition hdmi_cec.h:160
void transmit_body_(const Frame *frame)
Drive the frame after the initiator address: bit-banged, or from the UART, five UART bits per CEC bit...
Definition hdmi_cec.cpp:370
static void gpio_isr_s(HDMICEC *self)
Definition hdmi_cec.cpp:502
std::atomic< uint8_t > dropped_frames_
Definition hdmi_cec.h:251
bool send(uint8_t source, uint8_t destination, std::span< const uint8_t > payload)
Queue a frame for transmission; false when the queue is full or in monitor mode.
Definition hdmi_cec.cpp:224
gptimer_handle_t ack_timer_
Definition hdmi_cec.h:233
static void ack_timer_isr_s()
Definition hdmi_cec.cpp:473
StaticCallbackManager< HDMI_CEC_MESSAGE_CALLBACK_COUNT, void(uint8_t, uint8_t, std::span< const uint8_t >)> message_callbacks_
Definition hdmi_cec.h:225
static constexpr uint16_t UNSET_PHYSICAL_ADDRESS
Definition hdmi_cec.h:269
ISRInternalGPIOPin isr_pin_
Definition hdmi_cec.h:228
uint8_t get_address() const
Definition hdmi_cec.h:138
void set_uart(uart::UARTComponent *uart)
Send the frame body from this UART (its TX pin on the line through a diode) instead of bit-banging it...
Definition hdmi_cec.h:141
bool send(uint8_t destination, std::initializer_list< uint8_t > payload)
For lambdas: id(cec).send(0x0, {0x44, 0x41});.
Definition hdmi_cec.h:157
void add_on_message_callback(F &&callback)
Called with the initiator, the destination and the payload (opcode first) of every received frame; th...
Definition hdmi_cec.h:146
void send_ack_()
Pull the line low for the acknowledge bit of a frame addressed to us.
Definition hdmi_cec.cpp:427
ESPHOME_ALWAYS_INLINE void set_pin_output_low_()
Definition hdmi_cec.h:209
static constexpr size_t QUEUE_SIZE
Definition hdmi_cec.h:215
void finish_attempt_(bool sent_ok)
Keep the frame for another attempt, or drop it once the attempts are used up.
Definition hdmi_cec.cpp:314
ReceiverState receiver_state_
Definition hdmi_cec.h:259
void set_promiscuous_mode(bool promiscuous)
Definition hdmi_cec.h:132
bool send(uint8_t destination, std::span< const uint8_t > payload)
Definition hdmi_cec.h:153
void set_monitor_mode(bool monitor_mode)
Definition hdmi_cec.h:133
std::atomic< bool > eom_received_
Definition hdmi_cec.h:250
uart::UARTComponent * uart_
Definition hdmi_cec.h:230
void handle_received_message_(const Frame *frame)
Definition hdmi_cec.cpp:172
bool send_bit_(bool bit_value)
Send one bit; false when a logical 1 found the line held low by another initiator.
Definition hdmi_cec.cpp:405
void set_physical_address(uint16_t physical_address)
Answer "Give Physical Address" with this address and the device type of the logical address.
Definition hdmi_cec.h:136
uint16_t get_physical_address() const
Definition hdmi_cec.h:137
ESPHOME_ALWAYS_INLINE void set_pin_input_high_()
Definition hdmi_cec.h:208
FrameRingBuffer< QUEUE_SIZE > frames_queue_
Definition hdmi_cec.h:217
InternalGPIOPin * pin_
Definition hdmi_cec.h:227
void reply_builtin_(const Frame *frame)
The built-in replies to a frame directed at us that no automation claims.
Definition hdmi_cec.cpp:186
bool transmit_frame_(const Frame *frame)
Drive the frame onto the line; false when another initiator sent at the same time.
Definition hdmi_cec.cpp:326
std::vector< uint8_t > & out
Definition gif.cpp:49
ESPHOME_ALWAYS_INLINE void atomic_set(std::atomic< T > &atom, T value)
Definition hdmi_cec.h:41
size_t encode_uart_body(const Frame &frame, uint8_t *out)
The UART bytes that send a frame after its initiator address bits, at 2083 baud; returns how many it ...
Definition hdmi_cec.cpp:356
const char OSD_NAME_REPLY[] PROGMEM
Definition hdmi_cec.h:35
ESPHOME_ALWAYS_INLINE T atomic_get(const std::atomic< T > &atom)
Definition hdmi_cec.h:44
uint32_t value
Definition hal.cpp:59
static void uint32_t