5#ifdef USE_HDMI_CEC_UART
19#if defined(USE_ESP8266) || defined(USE_RP2040)
21static HDMICEC *ack_timer_owner =
nullptr;
25static constexpr uint32_t START_BIT_MIN_US = 3500;
26static constexpr uint32_t START_BIT_NOM_US = 3700;
27static constexpr uint32_t START_BIT_HIGH_US = 800;
28static constexpr uint32_t HIGH_BIT_MIN_US = 400;
29static constexpr uint32_t HIGH_BIT_US = 600;
30static constexpr uint32_t LOW_BIT_US = 1500;
31static constexpr uint32_t SAFE_SAMPLE_US = 1050;
32static constexpr uint32_t TOTAL_BIT_US = 2400;
33static constexpr uint32_t SIGNAL_FREE_TIME_AFTER_RECEIVE = TOTAL_BIT_US * 5;
34static constexpr uint32_t SIGNAL_FREE_TIME_AFTER_XMIT_FAIL = TOTAL_BIT_US * 3;
35static constexpr uint32_t SIGNAL_FREE_TIME_AFTER_XMIT_SUCCESS = TOTAL_BIT_US * 7;
36static constexpr uint32_t ABORTED_FRAME_US = TOTAL_BIT_US * 15;
37static constexpr uint8_t MAX_ATTEMPTS = 8;
40static constexpr uint8_t OPCODE_FEATURE_ABORT = 0x00;
41static constexpr uint8_t OPCODE_ABORT = 0xFF;
42static constexpr uint8_t ABORT_REASON_UNRECOGNIZED_OPCODE = 0x00;
43static constexpr uint8_t ABORT_REASON_REFUSED = 0x04;
44static constexpr uint8_t OPCODE_GIVE_OSD_NAME = 0x46;
45static constexpr uint8_t OPCODE_GIVE_PHYSICAL_ADDRESS = 0x83;
46static constexpr uint8_t OPCODE_REPORT_PHYSICAL_ADDRESS = 0x84;
47static constexpr uint8_t OPCODE_GET_CEC_VERSION = 0x9F;
48static constexpr uint8_t OPCODE_CEC_VERSION = 0x9E;
49static constexpr uint8_t CEC_VERSION_1_4 = 0x05;
51#ifdef USE_HDMI_CEC_FEATURE_ABORT
53static constexpr uint8_t OPCODE_TUNER_DEVICE_STATUS = 0x07;
54static constexpr uint8_t OPCODE_DECK_STATUS = 0x1B;
55static constexpr uint8_t OPCODE_SET_OSD_NAME = 0x47;
56static constexpr uint8_t OPCODE_REPORT_AUDIO_STATUS = 0x7A;
57static constexpr uint8_t OPCODE_SYSTEM_AUDIO_MODE_STATUS = 0x7E;
58static constexpr uint8_t OPCODE_MENU_STATUS = 0x8E;
59static constexpr uint8_t OPCODE_REPORT_POWER_STATUS = 0x90;
60static constexpr uint8_t OPCODE_REPORT_SHORT_AUDIO_DESCRIPTOR = 0xA3;
61static constexpr uint8_t OPCODE_REPORT_ARC_INITIATED = 0xC1;
62static constexpr uint8_t OPCODE_REPORT_ARC_TERMINATED = 0xC2;
66static bool is_response(uint8_t opcode) {
68 case OPCODE_TUNER_DEVICE_STATUS:
69 case OPCODE_DECK_STATUS:
70 case OPCODE_SET_OSD_NAME:
71 case OPCODE_REPORT_AUDIO_STATUS:
72 case OPCODE_SYSTEM_AUDIO_MODE_STATUS:
73 case OPCODE_MENU_STATUS:
74 case OPCODE_REPORT_POWER_STATUS:
75 case OPCODE_CEC_VERSION:
76 case OPCODE_REPORT_SHORT_AUDIO_DESCRIPTOR:
77 case OPCODE_REPORT_ARC_INITIATED:
78 case OPCODE_REPORT_ARC_TERMINATED:
87static bool is_claimed(uint8_t opcode) {
88#ifdef HDMI_CEC_CLAIMED_OPCODE_COUNT
89 for (uint8_t i = 0; i < HDMI_CEC_CLAIMED_OPCODE_COUNT; i++) {
98Frame::Frame(uint8_t initiator_addr, uint8_t destination_addr, std::span<const uint8_t> payload) {
101 this->
size_ = 1 + payload.size();
116 ESP_LOGE(TAG,
"Failed to set up the acknowledge timer");
129 " Monitor mode: %s\n"
130 " Promiscuous mode: %s",
132 LOG_PIN(
" Pin: ", this->
pin_);
139#ifdef HDMI_CEC_OSD_NAME_LENGTH
140 ESP_LOGCONFIG(TAG,
" OSD name: %s", LOG_STR_ARG(OSD_NAME_REPLY + 1));
142#ifdef USE_HDMI_CEC_UART
143 ESP_LOGCONFIG(TAG,
" Transmit: UART");
151 ESP_LOGW(TAG,
"%u received frame%s dropped (queue full or oversized)",
count,
152 count == 1 ? LOG_STR_LITERAL(
"") : LOG_STR_LITERAL(
"s"));
153 this->reported_dropped_frames_ = dropped;
174#ifdef HDMI_CEC_MESSAGE_CALLBACK_COUNT
188 const uint8_t opcode = frame->
opcode();
190 case OPCODE_FEATURE_ABORT:
192 case OPCODE_GET_CEC_VERSION:
193 this->
send(source, {OPCODE_CEC_VERSION, CEC_VERSION_1_4});
195 case OPCODE_GIVE_PHYSICAL_ADDRESS:
199 {OPCODE_REPORT_PHYSICAL_ADDRESS,
static_cast<uint8_t
>(this->
physical_address_ >> 8),
204#ifdef HDMI_CEC_OSD_NAME_LENGTH
205 case OPCODE_GIVE_OSD_NAME:
206 this->
send(source, std::span<const uint8_t>(
reinterpret_cast<const uint8_t *
>(OSD_NAME_REPLY),
207 1 + HDMI_CEC_OSD_NAME_LENGTH));
213#ifdef USE_HDMI_CEC_FEATURE_ABORT
214 if (is_response(opcode)) {
219 this->
send(source, {OPCODE_FEATURE_ABORT, opcode,
220 opcode == OPCODE_ABORT ? ABORT_REASON_REFUSED : ABORT_REASON_UNRECOGNIZED_OPCODE});
224bool HDMICEC::send(uint8_t source, uint8_t destination, std::span<const uint8_t> payload) {
226 ESP_LOGW(TAG,
"Monitor mode, not sending 0x%X->0x%X", source, destination);
229 if (source > 0xf || destination > 0xf) {
230 ESP_LOGE(TAG,
"Send abort: logical address 0x%X->0x%X out of range", source, destination);
234 ESP_LOGE(TAG,
"Send abort 0x%X->0x%X: payload of %zu bytes exceeds %zu", source, destination, payload.size(),
239 if (frame ==
nullptr) {
240 ESP_LOGW(TAG,
"Send queue full, frame 0x%X->0x%X dropped", source, destination);
243 *frame =
Frame(source, destination, payload);
255 const uint32_t since_last_edge = now - last_edge;
268 if (bytes_ok && acks_ok) {
271 }
else if (!bytes_ok) {
272 ESP_LOGD(TAG,
"Send frame incorrect on attempt %u, saw %u bytes but expected %zu", this->
transmit_attempts_,
294#ifdef ESPHOME_LOG_HAS_DEBUG
315 this->
required_idle_period_ = sent_ok ? SIGNAL_FREE_TIME_AFTER_XMIT_SUCCESS : SIGNAL_FREE_TIME_AFTER_XMIT_FAIL;
332 const uint8_t header = frame->
bytes()[0];
333 for (int8_t i = 7; i >= 4; i--) {
334 if (!this->
send_bit_((header >> i) & 0x1)) {
335 ESP_LOGD(TAG,
"Bus collision on sending initiator address 0x%X on attempt %u", frame->
initiator_addr(),
336 this->transmit_attempts_);
346static uint16_t frame_bits(std::span<const uint8_t> bytes,
size_t i) {
347 return (
static_cast<uint16_t
>(bytes[i]) << 2) | (i == bytes.size() - 1 ? 0x2 : 0) | 0x1;
350static int8_t frame_first_bit(
size_t i) {
return i == 0 ? 5 : 9; }
357 static constexpr uint8_t TWO_BITS_TO_UART_BYTE[4]
PROGMEM = {0x8C, 0xEC, 0x8F, 0xEF};
359 const auto bytes = frame.
bytes();
360 for (
size_t i = 0; i < bytes.size(); i++) {
361 const uint16_t bits = frame_bits(bytes, i);
362 for (int8_t pair = frame_first_bit(i) / 2; pair >= 0; pair--) {
369#ifdef USE_HDMI_CEC_UART
371 uint8_t data[MAX_UART_BODY_LENGTH];
376 const auto bytes = frame->
bytes();
377 for (
size_t i = 0; i < bytes.size(); i++) {
378 const uint16_t bits = frame_bits(bytes, i);
379 for (int8_t bit = frame_first_bit(i); bit >= 0; bit--) {
388 ESP_LOGD(TAG,
"Bus occupied before start bit on attempt %u", this->
transmit_attempts_);
400 ESP_LOGD(TAG,
"Bus collision on sending start bit on attempt %u", this->
transmit_attempts_);
415 if (elapsed < SAFE_SAMPLE_US) {
421 if (elapsed < TOTAL_BIT_US) {
433#if defined(USE_ESP32)
435 gptimer_config_t config = {
436 .clk_src = GPTIMER_CLK_SRC_DEFAULT,
437 .direction = GPTIMER_COUNT_UP,
438 .resolution_hz = 1000000,
440 if (gptimer_new_timer(&config, &this->
ack_timer_) != ESP_OK) {
445 gptimer_alarm_config_t alarm = {
446 .alarm_count = LOW_BIT_US, .reload_count = 0, .flags = {.auto_reload_on_alarm =
true}};
447 return gptimer_register_event_callbacks(this->
ack_timer_, &callbacks,
this) == ESP_OK &&
448 gptimer_set_alarm_action(this->
ack_timer_, &alarm) == ESP_OK && gptimer_enable(this->
ack_timer_) == ESP_OK;
454 auto *self =
static_cast<HDMICEC *
>(arg);
460#elif defined(USE_ESP8266)
461static constexpr uint32_t TIMER1_TICKS_PER_US = 5;
464 ack_timer_owner =
this;
467 timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE);
475#elif defined(USE_RP2040)
477 this->
ack_alarm_ = hardware_alarm_claim_unused(
false);
481 ack_timer_owner =
this;
487 hardware_alarm_set_target(this->
ack_alarm_, make_timeout_time_us(LOW_BIT_US));
530 if (pulse_duration > START_BIT_MIN_US) {
540 if (pulse_duration < HIGH_BIT_MIN_US / 4) {
543 const bool value = pulse_duration <= SAFE_SAMPLE_US;
void mark_failed()
Mark this component as failed.
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
void enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
ISRInternalGPIOPin to_isr() const
One CEC bus message: a header byte with the initiator and destination addresses, then up to 15 payloa...
ESPHOME_ALWAYS_INLINE bool push_back(uint8_t data)
False when the frame is full already; the bus allows no more.
std::span< const uint8_t > bytes() const
static constexpr size_t MAX_LENGTH
static constexpr size_t MAX_PAYLOAD_LENGTH
std::span< const uint8_t > payload() const
The bytes after the header, starting with the opcode.
ESPHOME_ALWAYS_INLINE uint8_t initiator_addr() const
ESPHOME_ALWAYS_INLINE size_t size() const
ESPHOME_ALWAYS_INLINE bool is_broadcast() const
static constexpr uint8_t BROADCAST_ADDRESS
ESPHOME_ALWAYS_INLINE uint8_t destination_addr() const
std::array< uint8_t, MAX_LENGTH > data_
ESPHOME_ALWAYS_INLINE void clear()
ESPHOME_ALWAYS_INLINE uint8_t opcode() const
ESPHOME_ALWAYS_INLINE void pop_front()
ESPHOME_ALWAYS_INLINE Frame * back()
ESPHOME_ALWAYS_INLINE void push_back()
ESPHOME_ALWAYS_INLINE Frame * front()
ESPHOME_ALWAYS_INLINE bool is_empty() const
The HDMI-CEC bus on one GPIO pin.
uint16_t physical_address_
std::atomic< uint32_t > last_edge_us_
void dump_config() override
FrameRingBuffer< QUEUE_SIZE > send_queue_
uint8_t reported_dropped_frames_
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...
static void gpio_isr_s(HDMICEC *self)
std::atomic< uint8_t > dropped_frames_
uint32_t last_falling_edge_us_
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.
gptimer_handle_t ack_timer_
static void ack_timer_isr_s()
StaticCallbackManager< HDMI_CEC_MESSAGE_CALLBACK_COUNT, void(uint8_t, uint8_t, std::span< const uint8_t >)> message_callbacks_
uint8_t transmit_attempts_
uint8_t recv_bit_counter_
static constexpr uint16_t UNSET_PHYSICAL_ADDRESS
ISRInternalGPIOPin isr_pin_
void send_ack_()
Pull the line low for the acknowledge bit of a frame addressed to us.
ESPHOME_ALWAYS_INLINE void set_pin_output_low_()
void finish_attempt_(bool sent_ok)
Keep the frame for another attempt, or drop it once the attempts are used up.
ReceiverState receiver_state_
uint16_t required_idle_period_
uint32_t last_attempt_us_
std::atomic< bool > eom_received_
uart::UARTComponent * uart_
void handle_received_message_(const Frame *frame)
uint8_t n_bytes_received_
bool send_bit_(bool bit_value)
Send one bit; false when a logical 1 found the line held low by another initiator.
@ RECEIVER_STATE_WAITING_FOR_ACK
@ RECEIVER_STATE_RECEIVING_BYTE
@ RECEIVER_STATE_WAITING_FOR_EOM
ESPHOME_ALWAYS_INLINE void set_pin_input_high_()
FrameRingBuffer< QUEUE_SIZE > frames_queue_
void reply_builtin_(const Frame *frame)
The built-in replies to a frame directed at us that no automation claims.
bool transmit_frame_(const Frame *frame)
Drive the frame onto the line; false when another initiator sent at the same time.
void write_array(const std::vector< uint8_t > &data)
std::vector< uint8_t > & out
ESPHOME_ALWAYS_INLINE void atomic_set(std::atomic< T > &atom, T value)
ESPHOME_LOG_TAG(TAG, "hdmi_cec")
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 ...
const char OSD_NAME_REPLY[] PROGMEM
ESPHOME_ALWAYS_INLINE T atomic_get(const std::atomic< T > &atom)
void progmem_memcpy(void *dst, const void *src, size_t len)
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
constexpr uint8_t high_nibble(uint8_t byte)
The high 4 bits of a byte, e.g. high_nibble(0xA5) == 0xA.
uint32_t IRAM_ATTR HOT micros()
void delay_microseconds_safe(uint32_t us)
Delay for the given amount of microseconds, possibly yielding to other processes during the wait.
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
constexpr uint8_t low_nibble(uint8_t byte)
The low 4 bits of a byte, e.g. low_nibble(0xA5) == 0x5.
uint8_t progmem_read_byte(const uint8_t *addr)