ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
fusb302b.cpp
Go to the documentation of this file.
1#include "fusb302b.h"
2
3#ifdef USE_ESP32
4
5#include <algorithm>
6#include <cstdio>
7
10#include "esphome/core/log.h"
11
12#include "fusb302b_registers.h"
13
15
16ESPHOME_LOG_TAG(TAG, "fusb302b");
17
18// Task notification bits
19static constexpr uint32_t NOTIFY_IRQ = 1 << 0;
20static constexpr uint32_t NOTIFY_STOP = 1 << 1;
21static constexpr uint32_t NOTIFY_REQUEST = 1 << 2;
22
23static constexpr uint32_t TASK_STACK_SIZE = 4096;
24static constexpr UBaseType_t TASK_PRIORITY = 18;
25
26static constexpr uint32_t STARTUP_DELAY_MS = 2000;
27static constexpr uint32_t CC_SETTLE_MS = 5;
28static constexpr uint32_t CC_POLL_INTERVAL_MS = 500;
29static constexpr uint32_t PD_POLL_INTERVAL_MS = 100;
30// Safety net in case an interrupt edge is missed while attached and idle
31static constexpr uint32_t IDLE_POLL_INTERVAL_MS = 1000;
32// Time to wait for the source to send its capabilities on its own after attach
33static constexpr uint32_t SOURCE_CAP_WAIT_MS = 5000;
34// Time to wait after each GET_SOURCE_CAP / soft reset before the next recovery step
35static constexpr uint32_t SOURCE_CAP_RETRY_MS = 2500;
36// Upper bound on back-to-back interrupt handling while INT_N stays asserted
37static constexpr uint8_t MAX_IRQ_ROUNDS = 8;
38// BC_LVL is read this many extra times to confirm a stable measurement
39static constexpr uint8_t CC_STABLE_READS = 5;
40static constexpr uint8_t CC_LEVEL_UNSTABLE = 0xFF;
41
42void IRAM_ATTR FUSB302B::gpio_intr(FUSB302B *arg) {
43 TaskHandle_t handle = arg->task_.get_handle();
44 if (handle == nullptr)
45 return;
46 BaseType_t woken = pdFALSE;
47 xTaskNotifyFromISR(handle, NOTIFY_IRQ, eSetBits, &woken);
48 portYIELD_FROM_ISR(woken);
49}
50
51void FUSB302B::task_func(void *arg) { static_cast<FUSB302B *>(arg)->run_task_(); }
52
53void FUSB302B::setup() {
54 uint8_t device_id = 0;
55 if (!this->setup_check(this->read_byte(FUSB_DEVICE_ID, &device_id)))
56 return;
57 const uint8_t version = device_id & FUSB_DEVICE_ID_VERSION_MASK;
58 if (version != FUSB_DEVICE_ID_VERSION_A && version != FUSB_DEVICE_ID_VERSION_B) {
59 ESP_LOGE(TAG, "Unknown device ID 0x%02X", device_id);
60 this->mark_failed();
61 return;
62 }
63 ESP_LOGD(TAG, "Device ID 0x%02X (product %u, revision %u)", device_id, (device_id >> 2) & 0x3, device_id & 0x3);
64 if (!this->setup_check(this->init_chip_(), LOG_STR("Initialization failed")))
65 return;
66
67 this->interrupt_pin_->setup();
68 // The task must exist before the interrupt is attached, since the ISR notifies it
69 if (!this->setup_check(
70 this->task_.create(FUSB302B::task_func, "fusb302b", TASK_STACK_SIZE, this, TASK_PRIORITY, false),
71 LOG_STR("Failed to create task")))
72 return;
74}
75
76void FUSB302B::queue_event_(uint8_t event) {
77 const uint8_t head = this->event_queue_head_.load(std::memory_order_relaxed);
78 if (static_cast<uint8_t>(head - this->event_queue_tail_.load(std::memory_order_acquire)) < EVENT_QUEUE_SIZE) {
79 this->event_queue_[head % EVENT_QUEUE_SIZE] = event;
80 this->event_queue_head_.store(head + 1, std::memory_order_release);
81 } else {
82 if (event == EVENT_REQUEST_FAILED)
83 this->dropped_request_failures_.fetch_add(1, std::memory_order_relaxed);
84 this->event_queue_overflow_ = true;
85 }
87}
88
90 const PdState state = this->state_;
91 if (state == this->queued_state_) {
92 // Only the contract changed
94 return;
95 }
96 this->queued_state_ = state;
97 this->queue_event_(static_cast<uint8_t>(state));
98}
99
101
102void FUSB302B::loop() {
103 const uint32_t contract = this->contract_;
104 if (contract != this->published_contract_) {
105 this->published_contract_ = contract;
106 this->publish_contract_(PackedContract{contract});
107 }
108
109 uint8_t tail = this->event_queue_tail_.load(std::memory_order_relaxed);
110 const uint8_t head = this->event_queue_head_.load(std::memory_order_acquire);
111 while (tail != head) {
112 const uint8_t event = this->event_queue_[tail % EVENT_QUEUE_SIZE];
113 this->event_queue_tail_.store(++tail, std::memory_order_release);
114 if (event == EVENT_REQUEST_FAILED) {
115 this->request_failed_callback_.call();
116 } else {
117 this->publish_state_(static_cast<PdState>(event));
118 }
119 }
120 if (this->event_queue_overflow_.exchange(false)) {
121 ESP_LOGW(TAG, "Events arrived faster than the main loop handled them; some state triggers were skipped");
122 // Drop the queued states before reading the state, so no older state is replayed after it. Request failures
123 // are only counted, so every one of them still fires.
124 uint8_t failures = this->dropped_request_failures_.exchange(0, std::memory_order_relaxed);
125 const uint8_t end = this->event_queue_head_.load(std::memory_order_acquire);
126 for (; tail != end; tail++) {
128 failures++;
129 }
130 this->event_queue_tail_.store(end, std::memory_order_release);
131 this->publish_state_(this->state_);
132 for (; failures > 0; failures--)
133 this->request_failed_callback_.call();
134 }
135 // Re-enabled by on_state_changed() or on_request_failed() from the PHY task
136 this->disable_loop();
137}
138
140 if (state == this->published_state_)
141 return;
142 const PdState previous = this->published_state_;
143 this->published_state_ = state;
144 this->state_callback_.call(state, previous);
145}
146
148#ifdef USE_SENSOR
149 PUBLISH_STATE(this->voltage_sensor_, contract.voltage());
150 PUBLISH_STATE(this->current_sensor_, contract.current());
151#endif
152#ifdef USE_TEXT_SENSOR
153 if (this->contract_text_sensor_ != nullptr) {
154 if (!contract.is_set()) {
155 this->contract_text_sensor_->publish_state("Detached");
156 } else {
157 char buf[24];
158 snprintf(buf, sizeof(buf), "%.2fA @ %.0fV", contract.current(), contract.voltage());
159 this->contract_text_sensor_->publish_state(buf);
160 }
161 }
162#endif
163}
164
165void FUSB302B::dump_config() {
166 ESP_LOGCONFIG(TAG,
167 "FUSB302B:\n"
168 " Request Voltage: %u V",
169 this->get_request_voltage());
170 LOG_I2C_DEVICE(this);
171 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
172#ifdef USE_SENSOR
173 LOG_SENSOR(" ", "Voltage", this->voltage_sensor_);
174 LOG_SENSOR(" ", "Current", this->current_sensor_);
175#endif
176#ifdef USE_TEXT_SENSOR
177 LOG_TEXT_SENSOR(" ", "Contract", this->contract_text_sensor_);
178#endif
179}
180
181void FUSB302B::on_shutdown() {
183 // The task parks itself instead of being deleted, so it can never be stopped in the middle of an I2C transfer
184 if (this->task_.is_created())
185 xTaskNotify(this->task_.get_handle(), NOTIFY_STOP, eSetBits);
186}
187
188void FUSB302B::request_voltage(int voltage) {
189 if (voltage < MIN_REQUEST_VOLTAGE || voltage > MAX_REQUEST_VOLTAGE) {
190 ESP_LOGW(TAG, "Requested %d V is outside %u-%u V, clamping", voltage, MIN_REQUEST_VOLTAGE, MAX_REQUEST_VOLTAGE);
191 }
192 this->set_request_voltage(static_cast<uint8_t>(std::clamp<int>(voltage, MIN_REQUEST_VOLTAGE, MAX_REQUEST_VOLTAGE)));
193 if (this->is_failed()) {
194 ESP_LOGW(TAG, "Cannot request %u V, the component failed", this->get_request_voltage());
195 // loop() no longer runs, so fire the trigger from the scheduler. Calling it here directly would recurse if the
196 // automation requests a voltage again.
197 App.scheduler.set_timeout(this, 0, [this]() { this->request_failed_callback_.call(); });
198 return;
199 }
200 // Before setup() the voltage is simply used at the first attach
201 if (this->task_.is_created())
202 xTaskNotify(this->task_.get_handle(), NOTIFY_REQUEST, eSetBits);
203}
204
206 // Give the source time to settle after power-up before the first CC measurement
207 vTaskDelay(pdMS_TO_TICKS(STARTUP_DELAY_MS));
208
209 while (true) {
210 uint32_t wait_ms = CC_POLL_INTERVAL_MS;
211 if (this->attached_) {
212 const bool busy = this->waiting_for_source_caps_ || this->request_pending_ || this->retry_request_pending_ ||
213 this->check_ams_();
214 wait_ms = busy ? PD_POLL_INTERVAL_MS : IDLE_POLL_INTERVAL_MS;
215 }
216 uint32_t bits = 0;
217 xTaskNotifyWait(0, UINT32_MAX, &bits, pdMS_TO_TICKS(wait_ms));
218
219 if (bits & NOTIFY_STOP) {
220 while (true)
221 vTaskSuspend(nullptr);
222 }
223 if (bits & NOTIFY_REQUEST) {
224 // When unattached, the new voltage is simply used at the next attach
225 this->request_pending_ = this->attached_;
226 }
227
228 // INT_N is held low until the interrupt registers are read, so a new event that arrives while it is
229 // already low has no falling edge: keep servicing while the pin stays asserted.
230 if (bits & NOTIFY_IRQ)
231 this->handle_interrupt_();
232 for (uint8_t i = 0; i < MAX_IRQ_ROUNDS && !this->interrupt_pin_->digital_read(); i++)
233 this->handle_interrupt_();
234
235 if (!this->attached_) {
236 this->try_attach_();
237 continue;
238 }
240 this->check_request_retry_();
241 // Waits for a pending Wait retry to end in a contract or a failure, so the two sequences never overlap
242 if (this->request_pending_ && !this->retry_request_pending_ && !this->check_ams_()) {
243 this->request_pending_ = false;
244 this->start_negotiation_();
245 }
246 this->check_source_caps_();
247 }
248}
249
251 // Software reset restores all registers to their defaults
252 if (!this->write_byte(FUSB_RESET, FUSB_RESET_SW_RES))
253 return false;
254 if (!this->reset_pd_())
255 return false;
256
257 // Unmask the interrupts used for PD messaging and VBUS changes
258 if (!this->write_byte(FUSB_MASK1, FUSB_MASK1_DEFAULT) || !this->write_byte(FUSB_MASKA, 0) ||
259 !this->write_byte(FUSB_MASKB, 0))
260 return false;
261
262 // Let the chip retransmit unacknowledged messages up to 3 times
263 if (!this->update_register(FUSB_CONTROL3, FUSB_CONTROL3_N_RETRIES_MASK | FUSB_CONTROL3_AUTO_RETRY,
264 (3 << FUSB_CONTROL3_N_RETRIES_SHIFT) | FUSB_CONTROL3_AUTO_RETRY))
265 return false;
266
267 if (!this->write_byte(FUSB_POWER, FUSB_POWER_ALL))
268 return false;
269 return this->reset_pd_();
270}
271
273 this->reset_protocol_();
274 // Writing CONTROL0 as a whole byte also clears INT_MASK, which is what enables the INT_N pin
275 return this->write_byte(FUSB_CONTROL0, FUSB_CONTROL0_TX_FLUSH) &&
276 this->write_byte(FUSB_CONTROL1, FUSB_CONTROL1_RX_FLUSH) && this->write_byte(FUSB_RESET, FUSB_RESET_PD_RESET);
277}
278
280 return this->update_register(FUSB_SWITCHES1, FUSB_SWITCHES1_AUTO_CRC, FUSB_SWITCHES1_AUTO_CRC);
281}
282
284 // STATUS0A, STATUS1A, INTERRUPTA, INTERRUPTB, STATUS0, STATUS1, INTERRUPT; reading clears the interrupts
285 uint8_t regs[7];
286 if (this->read_register(FUSB_STATUS0A, regs, sizeof(regs)) != i2c::ERROR_OK) {
287 this->enter_error_();
288 return;
289 }
290 const uint8_t interrupta = regs[2];
291 const uint8_t status0 = regs[4];
292 uint8_t status1 = regs[5];
293 const uint8_t interrupt = regs[6];
294
295 if ((interrupt & FUSB_INTERRUPT_I_VBUSOK) && !(status0 & FUSB_STATUS0_VBUSOK)) {
296 if (this->attached_)
297 this->detach_();
298 return;
299 }
300 if (interrupta & FUSB_INTERRUPTA_I_HARDRST) {
301 if (this->attached_)
302 this->handle_hard_reset_();
303 return;
304 }
305 if (interrupta & FUSB_INTERRUPTA_I_SOFTRST) {
306 ESP_LOGV(TAG, "Soft reset received");
307 }
308 if ((interrupta & FUSB_INTERRUPTA_I_RETRYFAIL) && this->attached_)
310
311 if (!this->attached_)
312 return;
313 PdMsg msg;
314 while (!(status1 & FUSB_STATUS1_RX_EMPTY)) {
315 bool is_sop = false;
316 if (!this->read_message_(msg, is_sop)) {
317 ESP_LOGW(TAG, "Reading message failed");
318 // A partial read leaves the FIFO misaligned
319 this->write_byte(FUSB_CONTROL1, FUSB_CONTROL1_RX_FLUSH);
320 return;
321 }
322 // Only SOP packets are addressed to a sink; SOP'/SOP'' are for cable plugs
323 if (is_sop)
324 this->handle_message_(msg);
325 if (!this->read_byte(FUSB_STATUS1, &status1))
326 return;
327 }
328}
329
330bool FUSB302B::read_message_(PdMsg &msg, bool &is_sop) {
331 uint8_t token = 0;
332 uint8_t header[2];
333 if (!this->read_byte(FUSB_FIFOS, &token) || this->read_register(FUSB_FIFOS, header, 2) != i2c::ERROR_OK)
334 return false;
336
337 // Data objects, followed by the CRC32 that the chip already checked
338 uint8_t buf[PD_MAX_NUM_DATA_OBJECTS * 4 + 4];
339 if (this->read_register(FUSB_FIFOS, buf, msg.num_of_obj * 4 + 4) != i2c::ERROR_OK)
340 return false;
341 for (uint8_t i = 0; i < msg.num_of_obj; i++)
342 msg.data_objects[i] = encode_value_le<uint32_t>(&buf[i * 4]);
343 is_sop = (token & FUSB_FIFO_RX_TOKEN_BITS) == FUSB_FIFO_RX_SOP;
344 return true;
345}
346
348 // Register address, then the TX tokens; written in one transfer without the copy write_register() makes
349 uint8_t buf[1 + 4 + 1 + 2 + PD_MAX_NUM_DATA_OBJECTS * 4 + 4];
350 uint8_t *p = buf;
351
352 *p++ = FUSB_FIFOS;
353 *p++ = FUSB_TX_TOKEN_SYNC1;
354 *p++ = FUSB_TX_TOKEN_SYNC1;
355 *p++ = FUSB_TX_TOKEN_SYNC1;
356 *p++ = FUSB_TX_TOKEN_SYNC2;
357 *p++ = FUSB_TX_TOKEN_PACKSYM | (msg.num_of_obj * 4 + 2);
358 p = put_le16(p, msg.get_coded_header());
359 for (uint8_t i = 0; i < msg.num_of_obj; i++)
360 p = put_le32(p, msg.data_objects[i]);
361 *p++ = FUSB_TX_TOKEN_JAM_CRC;
362 *p++ = FUSB_TX_TOKEN_EOP;
363 *p++ = FUSB_TX_TOKEN_TXOFF;
364 *p++ = FUSB_TX_TOKEN_TXON;
365
366 i2c::ErrorCode err = this->write(buf, p - buf);
367 if (err != i2c::ERROR_OK) {
368 ESP_LOGW(TAG, "Sending message type %u failed (error %d)", msg.type, static_cast<int>(err));
369 return false;
370 }
371 return true;
372}
373
374bool FUSB302B::measure_cc_(uint8_t meas_switch, uint8_t &level) {
375 if (!this->write_byte(FUSB_SWITCHES0, FUSB_SWITCHES0_PDWN_1 | FUSB_SWITCHES0_PDWN_2 | meas_switch))
376 return false;
377 vTaskDelay(pdMS_TO_TICKS(CC_SETTLE_MS));
378 uint8_t status0 = 0;
379 if (!this->read_byte(FUSB_STATUS0, &status0))
380 return false;
381 level = status0 & FUSB_STATUS0_BC_LVL;
382 for (uint8_t i = 0; i < CC_STABLE_READS; i++) {
383 if (!this->read_byte(FUSB_STATUS0, &status0))
384 return false;
385 if ((status0 & FUSB_STATUS0_BC_LVL) != level) {
386 level = CC_LEVEL_UNSTABLE;
387 return true;
388 }
389 }
390 return true;
391}
392
394 if (this->state_ == PdState::PD_STATE_ERROR) {
395 if (!this->init_chip_())
396 return;
397 ESP_LOGI(TAG, "Communication restored");
399 }
400
401 uint8_t status0 = 0;
402 if (!this->write_byte(FUSB_POWER, FUSB_POWER_ALL) || !this->read_byte(FUSB_STATUS0, &status0)) {
403 this->enter_error_();
404 return;
405 }
406 // A source may present its CC pull-up before VBUS is on; detach is only detected when VBUS drops
407 if (!(status0 & FUSB_STATUS0_VBUSOK)) {
408 this->cc_ambiguous_logged_ = false;
409 return;
410 }
411
412 uint8_t cc1 = 0;
413 uint8_t cc2 = 0;
414 if (!this->write_byte(FUSB_SWITCHES1, FUSB_SWITCHES1_SPECREV_REV2) ||
415 !this->write_byte(FUSB_MEASURE, FUSB_MEASURE_MDAC_CC) || !this->measure_cc_(FUSB_SWITCHES0_MEAS_CC1, cc1) ||
416 !this->measure_cc_(FUSB_SWITCHES0_MEAS_CC2, cc2)) {
417 this->enter_error_();
418 return;
419 }
420 // Exactly one CC line carries the source pull-up; the other one is open
421 uint8_t meas_switch;
422 uint8_t tx_switch;
423 if (cc1 != CC_LEVEL_UNSTABLE && cc1 > 0 && cc2 == 0) {
424 meas_switch = FUSB_SWITCHES0_MEAS_CC1;
425 tx_switch = FUSB_SWITCHES1_TXCC1;
426 } else if (cc2 != CC_LEVEL_UNSTABLE && cc2 > 0 && cc1 == 0) {
427 meas_switch = FUSB_SWITCHES0_MEAS_CC2;
428 tx_switch = FUSB_SWITCHES1_TXCC2;
429 } else {
430 if (!this->cc_ambiguous_logged_) {
431 ESP_LOGD(TAG, "VBUS present but no single CC line with a source pull-up (CC1 %u, CC2 %u)", cc1, cc2);
432 this->cc_ambiguous_logged_ = true;
433 }
434 return;
435 }
436
437 if (!this->write_byte(FUSB_SWITCHES0, FUSB_SWITCHES0_PDWN_1 | FUSB_SWITCHES0_PDWN_2 | meas_switch) ||
438 !this->write_byte(FUSB_SWITCHES1, tx_switch | FUSB_SWITCHES1_SPECREV_REV2) || !this->enable_auto_crc_() ||
439 !this->reset_pd_()) {
440 this->enter_error_();
441 return;
442 }
443 ESP_LOGD(TAG, "Attached on CC%u", meas_switch == FUSB_SWITCHES0_MEAS_CC1 ? 1 : 2);
444 this->attached_ = true;
445 this->cc_ambiguous_logged_ = false;
448}
449
451 ESP_LOGD(TAG, "Detached (VBUS lost)");
452 this->attached_ = false;
453 this->waiting_for_source_caps_ = false;
454 this->request_pending_ = false;
455 this->reset_pd_();
456 this->clear_contract_();
458}
459
461 ESP_LOGD(TAG, "Hard reset received");
462 // The source returns to Type-C default power and sends its capabilities again, using the current request voltage
463 this->request_pending_ = false;
464 // Also flushes the RX FIFO, so the caller has nothing left to read
465 if (!this->reset_pd_()) {
466 this->enter_error_();
467 return;
468 }
471}
472
474 if (this->state_ == PdState::PD_STATE_ERROR)
475 return;
476 ESP_LOGW(TAG, "Communication with the chip failed");
477 this->attached_ = false;
478 this->waiting_for_source_caps_ = false;
479 this->request_pending_ = false;
480 // Also stops a pending Wait retry, which would otherwise send its request to the failed chip
481 this->reset_protocol_();
482 this->clear_contract_();
484}
485
487 ESP_LOGD(TAG, "Requesting source capabilities for %u V", this->get_request_voltage());
488 // send_message() logs a failure; the soft reset is the next step either way, so do not wait for an answer
490 this->send_soft_reset_();
491 return;
492 }
494}
495
497 if (!this->reset_pd_()) {
498 this->enter_error_();
499 return;
500 }
502 // No answer can come, so do not wait for one
503 this->source_caps_failed_();
504 return;
505 }
506 // The source answers a soft reset with new source capabilities
508}
509
511 this->waiting_for_source_caps_ = false;
513 ESP_LOGW(TAG, "PD negotiation failed, staying at 5 V");
515 } else {
516 // A request made at runtime got no answer. A soft reset keeps the power level, so the contract in place stays.
517 ESP_LOGW(TAG, "Renegotiation failed, keeping the current contract");
518 }
519 this->on_request_failed();
520}
521
527
529 if (!this->waiting_for_source_caps_ || this->check_ams_())
530 return;
531 const uint32_t elapsed = millis() - this->source_cap_step_start_;
532 switch (this->source_cap_step_) {
534 if (elapsed < SOURCE_CAP_WAIT_MS)
535 return;
536 ESP_LOGD(TAG, "No source capabilities received, requesting them");
538 this->send_soft_reset_();
539 return;
540 }
542 break;
544 if (elapsed < SOURCE_CAP_RETRY_MS)
545 return;
546 ESP_LOGD(TAG, "No source capabilities received, sending soft reset");
547 this->send_soft_reset_();
548 return;
550 if (elapsed < SOURCE_CAP_RETRY_MS)
551 return;
552 this->source_caps_failed_();
553 return;
554 }
556}
557
558} // namespace esphome::fusb302b
559
560#endif // USE_ESP32
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:288
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
void disable_loop()
Disable this component's loop.
bool is_created() const
Check if the task has been created and not yet destroyed.
Definition static_task.h:28
TaskHandle_t get_handle() const
Get the FreeRTOS task handle.
Definition static_task.h:31
bool create(TaskFunction_t fn, const char *name, uint32_t stack_size, void *param, UBaseType_t priority, bool use_psram, BaseType_t core_id=tskNO_AFFINITY)
Allocate the TCB (always internal RAM) and the stack, then create the task.
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:32
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:147
void start_source_cap_wait_(SourceCapStep step)
Definition fusb302b.cpp:522
bool read_message_(PdMsg &msg, bool &is_sop)
Definition fusb302b.cpp:330
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:139
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:89
static void gpio_intr(FUSB302B *arg)
Definition fusb302b.cpp:42
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:76
static void task_func(void *arg)
Definition fusb302b.cpp:51
void on_request_failed() override
Definition fusb302b.cpp:100
bool send_message(const PdMsg &msg) override
Definition fusb302b.cpp:347
bool measure_cc_(uint8_t meas_switch, uint8_t &level)
Definition fusb302b.cpp:374
PdMsg make_control_msg_(PdControlMsgType type) const
Definition pd.cpp:55
uint8_t get_request_voltage() const
Definition pd.h:111
std::atomic< uint32_t > contract_
Definition pd.h:156
void set_state_(PdState state)
Definition pd.cpp:80
std::atomic< PdState > state_
Definition pd.h:155
void set_request_voltage(uint8_t voltage)
Definition pd.h:108
void handle_message_(const PdMsg &msg)
Definition pd.cpp:176
void check_request_retry_()
Sends the last request again once tSinkRequest has passed after the source answered Wait.
Definition pd.cpp:128
void handle_transmit_failed_()
Called by the PHY when the chip gave up on a message after its retries without receiving a GoodCRC.
Definition pd.cpp:169
bool check_ams_()
Returns true while an atomic message sequence is in progress.
Definition pd.cpp:116
void set_state_and_contract_(PdState state, const PdContract &contract)
Stores both before calling on_state_changed() once, so the main loop never sees the new state with th...
Definition pd.cpp:98
void check_transition_timeout_()
Falls back to default power when the source accepted a request but did not report the new supply read...
Definition pd.cpp:160
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:58
bool update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
Definition i2c.cpp:111
bool state
Definition fan.h:2
ESPHOME_LOG_TAG(TAG, "fusb302b")
@ PD_STATE_PD_TIMEOUT
Negotiation is over without a PD contract (no capabilities, or the request was not accepted); the sou...
@ PD_CNTRL_SOFT_RESET
Definition pd.h:36
@ PD_CNTRL_GET_SOURCE_CAP
Definition pd.h:33
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:11
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1268
constexpr uint8_t * put_le32(uint8_t *dst, uint32_t value)
Definition helpers.h:1319
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
constexpr uint8_t * put_le16(uint8_t *dst, uint16_t value)
Definition helpers.h:1314
static void uint32_t
A contract packed into one word so it can be shared between tasks as an atomic.
Definition pd.h:80
uint8_t num_of_obj
Definition pd.h:95
void set_header(uint16_t header)
Definition pd.cpp:40
uint16_t get_coded_header() const
Definition pd.cpp:48
uint32_t data_objects[PD_MAX_NUM_DATA_OBJECTS]
Definition pd.h:97
uint8_t end[39]
Definition sun_gtil2.cpp:17
spi_device_handle_t handle