ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
fusb302b.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
5#include <array>
6#include <atomic>
7
11#include "esphome/core/hal.h"
14
15#ifdef USE_SENSOR
17#endif
18#ifdef USE_TEXT_SENSOR
20#endif
21
22#include "pd.h"
23
24namespace esphome::fusb302b {
25
30class FUSB302B : public PowerDelivery, public Component, public i2c::I2CDevice {
31#ifdef USE_SENSOR
32 SUB_SENSOR(voltage)
33 SUB_SENSOR(current)
34#endif
35#ifdef USE_TEXT_SENSOR
36 SUB_TEXT_SENSOR(contract)
37#endif
38
39 public:
40 explicit FUSB302B(InternalGPIOPin *interrupt_pin) : interrupt_pin_(interrupt_pin) {}
41
42 void setup() override;
43 void loop() override;
44 void dump_config() override;
45 void on_shutdown() override;
46 float get_setup_priority() const override { return setup_priority::HARDWARE; }
47
50 void request_voltage(int voltage);
51
53 template<typename F> void add_on_state_callback(F &&callback) {
54 this->state_callback_.add(std::forward<F>(callback));
55 }
58 template<typename F> void add_on_request_failed_callback(F &&callback) {
59 this->request_failed_callback_.add(std::forward<F>(callback));
60 }
61
62 protected:
68
69 static void gpio_intr(FUSB302B *arg);
70 static void task_func(void *arg);
71
72 bool send_message(const PdMsg &msg) override;
73 void on_state_changed() override;
75 void on_source_caps_received() override { this->waiting_for_source_caps_ = false; }
76 void on_transition_timeout() override { this->send_soft_reset_(); }
77 void on_request_failed() override;
78
79 // PHY task only
80 [[noreturn]] void run_task_();
81 bool init_chip_();
82 bool reset_pd_();
83 bool enable_auto_crc_();
84 void handle_interrupt_();
85 bool read_message_(PdMsg &msg, bool &is_sop);
86 void try_attach_();
87 bool measure_cc_(uint8_t meas_switch, uint8_t &level);
88 void detach_();
89 void handle_hard_reset_();
91 void start_negotiation_();
92 void send_soft_reset_();
94 void check_source_caps_();
95 void enter_error_();
96
97 void publish_contract_(PackedContract contract);
99 void queue_event_(uint8_t event);
100
103
104 // PHY task only
107 bool attached_{false};
110 bool request_pending_{false};
111 // Last state put in the queue
113
114 // State changes and request failures from the PHY task, replayed in order by loop() so no event is lost while the
115 // main loop is blocked. An entry is a PdState value or EVENT_REQUEST_FAILED. The PHY task only writes head, the
116 // main loop only writes tail.
117 static constexpr uint8_t EVENT_QUEUE_SIZE = 8;
118 static constexpr uint8_t EVENT_REQUEST_FAILED = 0xFF;
119 std::array<uint8_t, EVENT_QUEUE_SIZE> event_queue_{};
120 std::atomic<uint8_t> event_queue_head_{0};
121 std::atomic<uint8_t> event_queue_tail_{0};
122 std::atomic<bool> event_queue_overflow_{false};
123 // Request failures that did not fit in the queue
124 std::atomic<uint8_t> dropped_request_failures_{0};
125
126 // Main loop only
129
132};
133
134} // namespace esphome::fusb302b
135
136#endif // USE_ESP32
Helper for FreeRTOS static task management.
Definition static_task.h:18
FUSB302B USB Type-C / Power Delivery sink controller.
Definition fusb302b.h:30
std::atomic< bool > event_queue_overflow_
Definition fusb302b.h:122
LazyCallbackManager< void(PdState, PdState)> state_callback_
Definition fusb302b.h:130
static constexpr uint8_t EVENT_QUEUE_SIZE
Definition fusb302b.h:117
void publish_contract_(PackedContract contract)
Definition fusb302b.cpp:150
void start_source_cap_wait_(SourceCapStep step)
Definition fusb302b.cpp:532
bool read_message_(PdMsg &msg, bool &is_sop)
Definition fusb302b.cpp:333
void on_transition_timeout() override
Definition fusb302b.h:76
void on_source_caps_received() override
Definition fusb302b.h:75
InternalGPIOPin * interrupt_pin_
Definition fusb302b.h:101
std::array< uint8_t, EVENT_QUEUE_SIZE > event_queue_
Definition fusb302b.h:119
void publish_state_(PdState state)
Definition fusb302b.cpp:142
static constexpr uint8_t EVENT_REQUEST_FAILED
Definition fusb302b.h:118
LazyCallbackManager< void()> request_failed_callback_
Definition fusb302b.h:131
std::atomic< uint8_t > dropped_request_failures_
Definition fusb302b.h:124
std::atomic< uint8_t > event_queue_tail_
Definition fusb302b.h:121
void on_state_changed() override
Definition fusb302b.cpp:92
static void gpio_intr(FUSB302B *arg)
Definition fusb302b.cpp:41
std::atomic< uint8_t > event_queue_head_
Definition fusb302b.h:120
SourceCapStep source_cap_step_
Definition fusb302b.h:106
void queue_event_(uint8_t event)
Definition fusb302b.cpp:79
static void task_func(void *arg)
Definition fusb302b.cpp:50
void on_request_failed() override
Definition fusb302b.cpp:103
bool send_message(const PdMsg &msg) override
Definition fusb302b.cpp:350
void on_soft_reset_received() override
Definition fusb302b.h:74
bool measure_cc_(uint8_t meas_switch, uint8_t &level)
Definition fusb302b.cpp:384
USB-PD sink protocol logic, independent of the PHY chip.
Definition pd.h:106
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:144
bool state
Definition fan.h:2
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:43
static void uint32_t
A contract packed into one word so it can be shared between tasks as an atomic.
Definition pd.h:80