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