ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
mcp4461.cpp
Go to the documentation of this file.
1#include "mcp4461.h"
2
3#include "esphome/core/hal.h"
5
6namespace esphome {
7namespace mcp4461 {
8
9static const char *const TAG = "mcp4461";
10constexpr uint8_t EEPROM_WRITE_TIMEOUT_MS = 10;
11
13 auto err = this->write(nullptr, 0);
14 if (err != i2c::ERROR_OK) {
15 this->error_code_ = MCP4461_STATUS_I2C_ERROR;
16 this->mark_failed();
17 return;
18 }
19 // save WP/WL status
21 for (uint8_t i = 0; i < 8; i++) {
22 if (this->reg_[i].initial_value.has_value()) {
23 uint16_t initial_state = static_cast<uint16_t>(*this->reg_[i].initial_value * 256.0f);
24 this->write_wiper_level_(i, initial_state);
25 }
26 if (this->reg_[i].enabled) {
27 this->reg_[i].state = this->read_wiper_level_(i);
28 } else {
29 // only volatile wipers can be set disabled on hw level
30 if (i < 4) {
31 this->reg_[i].state = 0;
32 Mcp4461WiperIdx wiper_idx = static_cast<Mcp4461WiperIdx>(i);
33 this->disable_wiper_(wiper_idx);
34 }
35 }
36 }
37}
38
39void Mcp4461Component::set_initial_value(Mcp4461WiperIdx wiper, float initial_value) {
40 uint8_t wiper_idx = static_cast<uint8_t>(wiper);
41 this->reg_[wiper_idx].initial_value = initial_value;
42}
43
45 uint8_t wiper_idx = static_cast<uint8_t>(wiper);
46 switch (terminal) {
47 case 'a':
48 this->reg_[wiper_idx].terminal_a = false;
49 break;
50 case 'b':
51 this->reg_[wiper_idx].terminal_b = false;
52 break;
53 case 'w':
54 this->reg_[wiper_idx].terminal_w = false;
55 break;
56 }
57}
58
60 uint8_t status_register_value = this->get_status_register_();
61 this->write_protected_ = static_cast<bool>((status_register_value >> 0) & 0x01);
62 this->reg_[0].wiper_lock_active = static_cast<bool>((status_register_value >> 2) & 0x01);
63 this->reg_[1].wiper_lock_active = static_cast<bool>((status_register_value >> 3) & 0x01);
64 this->reg_[2].wiper_lock_active = static_cast<bool>((status_register_value >> 5) & 0x01);
65 this->reg_[3].wiper_lock_active = static_cast<bool>((status_register_value >> 6) & 0x01);
66}
67
69 ESP_LOGCONFIG(TAG, "mcp4461:");
70 LOG_I2C_DEVICE(this);
71 if (this->is_failed()) {
72 ESP_LOGE(TAG, "%s", LOG_STR_ARG(this->get_message_string(this->error_code_)));
73 }
74 // log wiper status
75 for (uint8_t i = 0; i < 8; ++i) {
76 // terminals only valid for volatile wipers 0-3 - enable/disable is terminal hw
77 // so also invalid for nonvolatile. For these, only print current level.
78 // reworked to be a one-line intentionally, as output would not be in order
79 if (i < 4) {
80 ESP_LOGCONFIG(TAG, " ├── Volatile wiper [%u] level: %u, Status: %s, HW: %s, A: %s, B: %s, W: %s", i,
81 this->reg_[i].state, ONOFF(this->reg_[i].terminal_hw), ONOFF(this->reg_[i].terminal_a),
82 ONOFF(this->reg_[i].terminal_b), ONOFF(this->reg_[i].terminal_w), ONOFF(this->reg_[i].enabled));
83 } else {
84 ESP_LOGCONFIG(TAG, " ├── Nonvolatile wiper [%u] level: %u", i, this->reg_[i].state);
85 }
86 }
87}
88
90 if (this->status_has_warning()) {
92 }
93 for (uint8_t i = 0; i < 8; i++) {
94 if (this->reg_[i].update_level) {
95 // set wiper i state if changed
96 if (this->reg_[i].state != this->read_wiper_level_(i)) {
97 this->write_wiper_level_(i, this->reg_[i].state);
98 }
99 }
100 this->reg_[i].update_level = false;
101 // can be true only for wipers 0-3
102 // setting changes for terminals of nonvolatile wipers
103 // is prohibited in public methods
104 if (this->reg_[i].update_terminal) {
105 // set terminal register changes
106 Mcp4461TerminalIdx terminal_connector =
108 uint8_t new_terminal_value = this->calc_terminal_connector_byte_(terminal_connector);
109 ESP_LOGV(TAG, "updating terminal %u to new value %u", static_cast<uint8_t>(terminal_connector),
110 new_terminal_value);
111 this->set_terminal_register_(terminal_connector, new_terminal_value);
112 }
113 this->reg_[i].update_terminal = false;
114 }
115}
116
118 if (this->is_failed()) {
119 ESP_LOGE(TAG, "%s", LOG_STR_ARG(this->get_message_string(this->error_code_)));
120 return 0;
121 }
122 uint8_t addr = static_cast<uint8_t>(Mcp4461Addresses::MCP4461_STATUS);
123 uint8_t reg = addr | static_cast<uint8_t>(Mcp4461Commands::READ);
124 uint16_t buf;
125 if (!this->read_byte_16(reg, &buf)) {
126 this->error_code_ = MCP4461_STATUS_REGISTER_ERROR;
127 this->mark_failed();
128 return 0;
129 }
130 uint8_t msb = buf >> 8;
131 uint8_t lsb = static_cast<uint8_t>(buf & 0x00ff);
132 if (msb != 1 || ((lsb >> 7) & 0x01) != 1 || ((lsb >> 1) & 0x01) != 1) {
133 // D8, D7 and R1 bits are hardlocked to 1 -> a status msb bit 0 (bit 9 of status register) of 0 or lsb bit 1/7 = 0
134 // indicate device/communication issues, therefore mark component failed
135 this->error_code_ = MCP4461_STATUS_REGISTER_INVALID;
136 this->mark_failed();
137 return 0;
138 }
139 this->status_clear_warning();
140 return lsb;
141}
142
144 uint8_t status_register_value = this->get_status_register_();
145 ESP_LOGI(TAG, "D7: %u, WL3: %u, WL2: %u, EEWA: %u, WL1: %u, WL0: %u, R1: %u, WP: %u",
146 ((status_register_value >> 7) & 0x01), ((status_register_value >> 6) & 0x01),
147 ((status_register_value >> 5) & 0x01), ((status_register_value >> 4) & 0x01),
148 ((status_register_value >> 3) & 0x01), ((status_register_value >> 2) & 0x01),
149 ((status_register_value >> 1) & 0x01), ((status_register_value >> 0) & 0x01));
150}
151
153 uint8_t addr;
154 bool nonvolatile = false;
155 if (wiper > 3) {
156 nonvolatile = true;
157 wiper = wiper - 4;
158 }
159 switch (wiper) {
160 case 0:
161 addr = static_cast<uint8_t>(Mcp4461Addresses::MCP4461_VW0);
162 break;
163 case 1:
164 addr = static_cast<uint8_t>(Mcp4461Addresses::MCP4461_VW1);
165 break;
166 case 2:
167 addr = static_cast<uint8_t>(Mcp4461Addresses::MCP4461_VW2);
168 break;
169 case 3:
170 addr = static_cast<uint8_t>(Mcp4461Addresses::MCP4461_VW3);
171 break;
172 default:
173 ESP_LOGW(TAG, "unknown wiper specified");
174 return 0;
175 }
176 if (nonvolatile) {
177 addr = addr + 0x20;
178 }
179 return addr;
180}
181
183 if (this->is_failed()) {
184 ESP_LOGE(TAG, "%s", LOG_STR_ARG(this->get_message_string(this->error_code_)));
185 return 0;
186 }
187 uint8_t wiper_idx = static_cast<uint8_t>(wiper);
188 if (!(this->reg_[wiper_idx].enabled)) {
189 ESP_LOGW(TAG, "%s", LOG_STR_ARG(this->get_message_string(MCP4461_WIPER_DISABLED)));
190 return 0;
191 }
192 if (!(this->reg_[wiper_idx].enabled)) {
193 ESP_LOGW(TAG, "reading from disabled volatile wiper %u, returning 0", wiper_idx);
194 return 0;
195 }
196 return this->read_wiper_level_(wiper_idx);
197}
198
199uint16_t Mcp4461Component::read_wiper_level_(uint8_t wiper_idx) {
200 uint8_t addr = this->get_wiper_address_(wiper_idx);
201 uint8_t reg = addr | static_cast<uint8_t>(Mcp4461Commands::INCREMENT);
202 if (wiper_idx > 3) {
203 if (!this->is_eeprom_ready_for_writing_(true)) {
204 return 0;
205 }
206 }
207 uint16_t buf = 0;
208 if (!(this->read_byte_16(reg, &buf))) {
209 this->error_code_ = MCP4461_STATUS_I2C_ERROR;
210 this->status_set_warning();
211 ESP_LOGW(TAG, "Error fetching %swiper %u value", (wiper_idx > 3) ? "nonvolatile " : "", wiper_idx);
212 return 0;
213 }
214 return buf;
215}
216
218 if (this->is_failed()) {
219 ESP_LOGE(TAG, "%s", LOG_STR_ARG(this->get_message_string(this->error_code_)));
220 return false;
221 }
222 uint8_t wiper_idx = static_cast<uint8_t>(wiper);
223 if (!(this->reg_[wiper_idx].enabled)) {
224 ESP_LOGW(TAG, "%s", LOG_STR_ARG(this->get_message_string(MCP4461_WIPER_DISABLED)));
225 return false;
226 }
227 uint16_t data = this->get_wiper_level_(wiper);
228 ESP_LOGV(TAG, "Got value %u from wiper %u", data, wiper_idx);
229 this->reg_[wiper_idx].state = data;
230 return true;
231}
232
234 if (this->is_failed()) {
235 ESP_LOGE(TAG, "%s", LOG_STR_ARG(this->get_message_string(this->error_code_)));
236 return false;
237 }
238 uint8_t wiper_idx = static_cast<uint8_t>(wiper);
239 if (value > 0x100) {
240 ESP_LOGW(TAG, "%s", LOG_STR_ARG(this->get_message_string(MCP4461_VALUE_INVALID)));
241 return false;
242 }
243 if (!(this->reg_[wiper_idx].enabled)) {
244 ESP_LOGW(TAG, "%s", LOG_STR_ARG(this->get_message_string(MCP4461_WIPER_DISABLED)));
245 return false;
246 }
247 if (this->reg_[wiper_idx].wiper_lock_active) {
248 ESP_LOGW(TAG, "%s", LOG_STR_ARG(this->get_message_string(MCP4461_WIPER_LOCKED)));
249 return false;
250 }
251 ESP_LOGV(TAG, "Setting MCP4461 wiper %u to %u", wiper_idx, value);
252 this->reg_[wiper_idx].state = value;
253 this->reg_[wiper_idx].update_level = true;
254 return true;
255}
256
257void Mcp4461Component::write_wiper_level_(uint8_t wiper, uint16_t value) {
258 bool nonvolatile = wiper > 3;
259 if (!(this->mcp4461_write_(this->get_wiper_address_(wiper), value, nonvolatile))) {
260 this->error_code_ = MCP4461_STATUS_I2C_ERROR;
261 this->status_set_warning();
262 ESP_LOGW(TAG, "Error writing %swiper %u level %u", (wiper > 3) ? "nonvolatile " : "", wiper, value);
263 }
264}
265
267 if (this->is_failed()) {
268 ESP_LOGE(TAG, "%s", LOG_STR_ARG(this->get_message_string(this->error_code_)));
269 return;
270 }
271 uint8_t wiper_idx = static_cast<uint8_t>(wiper);
272 if ((this->reg_[wiper_idx].enabled)) {
273 ESP_LOGW(TAG, "%s", LOG_STR_ARG(this->get_message_string(MCP4461_WIPER_ENABLED)));
274 return;
275 }
276 if (this->reg_[wiper_idx].wiper_lock_active) {
277 ESP_LOGW(TAG, "%s", LOG_STR_ARG(this->get_message_string(MCP4461_WIPER_LOCKED)));
278 return;
279 }
280 ESP_LOGV(TAG, "Enabling wiper %u", wiper_idx);
281 this->reg_[wiper_idx].enabled = true;
282 if (wiper_idx < 4) {
283 this->reg_[wiper_idx].terminal_hw = true;
284 this->reg_[wiper_idx].update_terminal = true;
285 }
286}
287
289 if (this->is_failed()) {
290 ESP_LOGE(TAG, "%s", LOG_STR_ARG(this->get_message_string(this->error_code_)));
291 return;
292 }
293 uint8_t wiper_idx = static_cast<uint8_t>(wiper);
294 if (!(this->reg_[wiper_idx].enabled)) {
295 ESP_LOGW(TAG, "%s", LOG_STR_ARG(this->get_message_string(MCP4461_WIPER_DISABLED)));
296 return;
297 }
298 if (this->reg_[wiper_idx].wiper_lock_active) {
299 ESP_LOGW(TAG, "%s", LOG_STR_ARG(this->get_message_string(MCP4461_WIPER_LOCKED)));
300 return;
301 }
302 ESP_LOGV(TAG, "Disabling wiper %u", wiper_idx);
303 this->reg_[wiper_idx].enabled = true;
304 if (wiper_idx < 4) {
305 this->reg_[wiper_idx].terminal_hw = true;
306 this->reg_[wiper_idx].update_terminal = true;
307 }
308}
309
311 if (this->is_failed()) {
312 ESP_LOGE(TAG, "%s", LOG_STR_ARG(this->get_message_string(this->error_code_)));
313 return false;
314 }
315 uint8_t wiper_idx = static_cast<uint8_t>(wiper);
316 if (!(this->reg_[wiper_idx].enabled)) {
317 ESP_LOGW(TAG, "%s", LOG_STR_ARG(this->get_message_string(MCP4461_WIPER_DISABLED)));
318 return false;
319 }
320 if (this->reg_[wiper_idx].wiper_lock_active) {
321 ESP_LOGW(TAG, "%s", LOG_STR_ARG(this->get_message_string(MCP4461_WIPER_LOCKED)));
322 return false;
323 }
324 if (this->reg_[wiper_idx].state == 256) {
325 ESP_LOGV(TAG, "Maximum wiper level reached, further increase of wiper %u prohibited", wiper_idx);
326 return false;
327 }
328 ESP_LOGV(TAG, "Increasing wiper %u", wiper_idx);
329 uint8_t addr = this->get_wiper_address_(wiper_idx);
330 uint8_t reg = addr | static_cast<uint8_t>(Mcp4461Commands::INCREMENT);
331 auto err = this->write(&this->address_, reg, sizeof(reg));
332 if (err != i2c::ERROR_OK) {
333 this->error_code_ = MCP4461_STATUS_I2C_ERROR;
334 this->status_set_warning();
335 return false;
336 }
337 this->reg_[wiper_idx].state++;
338 return true;
339}
340
342 if (this->is_failed()) {
343 ESP_LOGE(TAG, "%s", LOG_STR_ARG(this->get_message_string(this->error_code_)));
344 return false;
345 }
346 uint8_t wiper_idx = static_cast<uint8_t>(wiper);
347 if (!(this->reg_[wiper_idx].enabled)) {
348 ESP_LOGW(TAG, "%s", LOG_STR_ARG(this->get_message_string(MCP4461_WIPER_DISABLED)));
349 return false;
350 }
351 if (this->reg_[wiper_idx].wiper_lock_active) {
352 ESP_LOGW(TAG, "%s", LOG_STR_ARG(this->get_message_string(MCP4461_WIPER_LOCKED)));
353 return false;
354 }
355 if (this->reg_[wiper_idx].state == 0) {
356 ESP_LOGV(TAG, "Minimum wiper level reached, further decrease of wiper %u prohibited", wiper_idx);
357 return false;
358 }
359 ESP_LOGV(TAG, "Decreasing wiper %u", wiper_idx);
360 uint8_t addr = this->get_wiper_address_(wiper_idx);
361 uint8_t reg = addr | static_cast<uint8_t>(Mcp4461Commands::DECREMENT);
362 auto err = this->write(&this->address_, reg, sizeof(reg));
363 if (err != i2c::ERROR_OK) {
364 this->error_code_ = MCP4461_STATUS_I2C_ERROR;
365 this->status_set_warning();
366 return false;
367 }
368 this->reg_[wiper_idx].state--;
369 return true;
370}
371
373 uint8_t i = static_cast<uint8_t>(terminal_connector) <= 1 ? 0 : 2;
374 uint8_t new_value_byte = 0;
375 new_value_byte += static_cast<uint8_t>(this->reg_[i].terminal_b);
376 new_value_byte += static_cast<uint8_t>(this->reg_[i].terminal_w) << 1;
377 new_value_byte += static_cast<uint8_t>(this->reg_[i].terminal_a) << 2;
378 new_value_byte += static_cast<uint8_t>(this->reg_[i].terminal_hw) << 3;
379 new_value_byte += static_cast<uint8_t>(this->reg_[(i + 1)].terminal_b) << 4;
380 new_value_byte += static_cast<uint8_t>(this->reg_[(i + 1)].terminal_w) << 5;
381 new_value_byte += static_cast<uint8_t>(this->reg_[(i + 1)].terminal_a) << 6;
382 new_value_byte += static_cast<uint8_t>(this->reg_[(i + 1)].terminal_hw) << 7;
383 return new_value_byte;
384}
385
387 if (this->is_failed()) {
388 ESP_LOGE(TAG, "%s", LOG_STR_ARG(this->get_message_string(this->error_code_)));
389 return 0;
390 }
391 uint8_t reg = static_cast<uint8_t>(terminal_connector) == 0 ? static_cast<uint8_t>(Mcp4461Addresses::MCP4461_TCON0)
392 : static_cast<uint8_t>(Mcp4461Addresses::MCP4461_TCON1);
393 reg |= static_cast<uint8_t>(Mcp4461Commands::READ);
394 uint16_t buf;
395 if (this->read_byte_16(reg, &buf)) {
396 return static_cast<uint8_t>(buf & 0x00ff);
397 } else {
398 this->error_code_ = MCP4461_STATUS_I2C_ERROR;
399 this->status_set_warning();
400 ESP_LOGW(TAG, "Error fetching terminal register value");
401 return 0;
402 }
403}
404
406 if (this->is_failed()) {
407 ESP_LOGE(TAG, "%s", LOG_STR_ARG(this->get_message_string(this->error_code_)));
408 return;
409 }
410 if ((static_cast<uint8_t>(terminal_connector) != 0 && static_cast<uint8_t>(terminal_connector) != 1)) {
411 return;
412 }
413 uint8_t terminal_data = this->get_terminal_register_(terminal_connector);
414 if (terminal_data == 0) {
415 return;
416 }
417 ESP_LOGV(TAG, "Got terminal register %u data 0x%02X", static_cast<uint8_t>(terminal_connector), terminal_data);
418 uint8_t wiper_index = 0;
419 if (static_cast<uint8_t>(terminal_connector) == 1) {
420 wiper_index = 2;
421 }
422 this->reg_[wiper_index].terminal_b = ((terminal_data >> 0) & 0x01);
423 this->reg_[wiper_index].terminal_w = ((terminal_data >> 1) & 0x01);
424 this->reg_[wiper_index].terminal_a = ((terminal_data >> 2) & 0x01);
425 this->reg_[wiper_index].terminal_hw = ((terminal_data >> 3) & 0x01);
426 this->reg_[(wiper_index + 1)].terminal_b = ((terminal_data >> 4) & 0x01);
427 this->reg_[(wiper_index + 1)].terminal_w = ((terminal_data >> 5) & 0x01);
428 this->reg_[(wiper_index + 1)].terminal_a = ((terminal_data >> 6) & 0x01);
429 this->reg_[(wiper_index + 1)].terminal_hw = ((terminal_data >> 7) & 0x01);
430}
431
432bool Mcp4461Component::set_terminal_register_(Mcp4461TerminalIdx terminal_connector, uint8_t data) {
433 if (this->is_failed()) {
434 ESP_LOGE(TAG, "%s", LOG_STR_ARG(this->get_message_string(this->error_code_)));
435 return false;
436 }
437 uint8_t addr;
438 if (static_cast<uint8_t>(terminal_connector) == 0) {
439 addr = static_cast<uint8_t>(Mcp4461Addresses::MCP4461_TCON0);
440 } else if (static_cast<uint8_t>(terminal_connector) == 1) {
441 addr = static_cast<uint8_t>(Mcp4461Addresses::MCP4461_TCON1);
442 } else {
443 ESP_LOGW(TAG, "Invalid terminal connector id %u specified", static_cast<uint8_t>(terminal_connector));
444 return false;
445 }
446 if (!(this->mcp4461_write_(addr, data))) {
447 this->error_code_ = MCP4461_STATUS_I2C_ERROR;
448 this->status_set_warning();
449 return false;
450 }
451 return true;
452}
453
455 if (this->is_failed()) {
456 ESP_LOGE(TAG, "%s", LOG_STR_ARG(this->get_message_string(this->error_code_)));
457 return;
458 }
459 uint8_t wiper_idx = static_cast<uint8_t>(wiper);
460 ESP_LOGV(TAG, "Enabling terminal %c of wiper %u", terminal, wiper_idx);
461 switch (terminal) {
462 case 'h':
463 this->reg_[wiper_idx].terminal_hw = true;
464 break;
465 case 'a':
466 this->reg_[wiper_idx].terminal_a = true;
467 break;
468 case 'b':
469 this->reg_[wiper_idx].terminal_b = true;
470 break;
471 case 'w':
472 this->reg_[wiper_idx].terminal_w = true;
473 break;
474 default:
475 ESP_LOGW(TAG, "Unknown terminal %c specified", terminal);
476 return;
477 }
478 this->reg_[wiper_idx].update_terminal = false;
479}
480
482 if (this->is_failed()) {
483 ESP_LOGE(TAG, "%s", LOG_STR_ARG(this->get_message_string(this->error_code_)));
484 return;
485 }
486 uint8_t wiper_idx = static_cast<uint8_t>(wiper);
487 ESP_LOGV(TAG, "Disabling terminal %c of wiper %u", terminal, wiper_idx);
488 switch (terminal) {
489 case 'h':
490 this->reg_[wiper_idx].terminal_hw = false;
491 break;
492 case 'a':
493 this->reg_[wiper_idx].terminal_a = false;
494 break;
495 case 'b':
496 this->reg_[wiper_idx].terminal_b = false;
497 break;
498 case 'w':
499 this->reg_[wiper_idx].terminal_w = false;
500 break;
501 default:
502 ESP_LOGW(TAG, "Unknown terminal %c specified", terminal);
503 return;
504 }
505 this->reg_[wiper_idx].update_terminal = false;
506}
507
509 if (this->is_failed()) {
510 ESP_LOGE(TAG, "%s", LOG_STR_ARG(this->get_message_string(this->error_code_)));
511 return 0;
512 }
513 uint8_t reg = 0;
514 reg |= static_cast<uint8_t>(Mcp4461EepromLocation::MCP4461_EEPROM_1) + (static_cast<uint8_t>(location) * 0x10);
515 reg |= static_cast<uint8_t>(Mcp4461Commands::READ);
516 uint16_t buf;
517 if (!this->is_eeprom_ready_for_writing_(true)) {
518 return 0;
519 }
520 if (!this->read_byte_16(reg, &buf)) {
521 this->error_code_ = MCP4461_STATUS_I2C_ERROR;
522 this->status_set_warning();
523 ESP_LOGW(TAG, "Error fetching EEPROM location value");
524 return 0;
525 }
526 return buf;
527}
528
530 if (this->is_failed()) {
531 ESP_LOGE(TAG, "%s", LOG_STR_ARG(this->get_message_string(this->error_code_)));
532 return false;
533 }
534 uint8_t addr = 0;
535 if (value > 511) {
536 return false;
537 }
538 if (value > 256) {
539 addr = 1;
540 }
541 addr |= static_cast<uint8_t>(Mcp4461EepromLocation::MCP4461_EEPROM_1) + (static_cast<uint8_t>(location) * 0x10);
542 if (!(this->mcp4461_write_(addr, value, true))) {
543 this->error_code_ = MCP4461_STATUS_I2C_ERROR;
544 this->status_set_warning();
545 ESP_LOGW(TAG, "Error writing EEPROM value");
546 return false;
547 }
548 return true;
549}
550
552 /* Read the EEPROM write-active status from the status register */
553 bool writing = static_cast<bool>((this->get_status_register_() >> 4) & 0x01);
554
555 /* If EEPROM is no longer writing, reset the timeout flag */
556 if (!writing) {
557 /* This is protected boolean flag in Mcp4461Component class */
558 this->last_eeprom_write_timed_out_ = false;
559 }
560
561 return writing;
562}
563
565 /* Check initial write status */
566 bool ready_for_write = !this->is_writing_();
567
568 /* Return early if no waiting is required or EEPROM is already ready */
569 if (ready_for_write || !wait_if_not_ready || this->last_eeprom_write_timed_out_) {
570 return ready_for_write;
571 }
572
573 /* Timestamp before starting the loop */
574 const uint32_t start_millis = millis();
575
576 ESP_LOGV(TAG, "Waiting until EEPROM is ready for write, start_millis = %" PRIu32, start_millis);
577
578 /* Loop until EEPROM is ready or timeout is reached */
579 while (!ready_for_write && ((millis() - start_millis) < EEPROM_WRITE_TIMEOUT_MS)) {
580 ready_for_write = !this->is_writing_();
581
582 /* If ready, exit early */
583 if (ready_for_write) {
584 ESP_LOGV(TAG, "EEPROM is ready for new write, elapsed_millis = %" PRIu32, millis() - start_millis);
585 return true;
586 }
587
588 /* Not ready yet, yield before checking again */
589 yield();
590 }
591
592 /* If still not ready after timeout, log error and mark the timeout */
593 ESP_LOGE(TAG, "EEPROM write timeout exceeded (%u ms)", EEPROM_WRITE_TIMEOUT_MS);
594 this->last_eeprom_write_timed_out_ = true;
595
596 return false;
597}
598
599bool Mcp4461Component::mcp4461_write_(uint8_t addr, uint16_t data, bool nonvolatile) {
600 uint8_t reg = data > 0xff ? 1 : 0;
601 uint8_t value_byte = static_cast<uint8_t>(data & 0x00ff);
602 ESP_LOGV(TAG, "Writing value %u to address %u", data, addr);
603 reg |= addr;
604 reg |= static_cast<uint8_t>(Mcp4461Commands::WRITE);
605 if (nonvolatile) {
606 if (this->write_protected_) {
607 ESP_LOGW(TAG, "%s", LOG_STR_ARG(this->get_message_string(MCP4461_WRITE_PROTECTED)));
608 return false;
609 }
610 if (!this->is_eeprom_ready_for_writing_(true)) {
611 return false;
612 }
613 }
614 return this->write_byte(reg, value_byte);
615}
616} // namespace mcp4461
617} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
bool status_has_warning() const
void status_clear_warning()
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition i2c.h:190
uint8_t address_
store the address of the device on the bus
Definition i2c.h:273
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition i2c.h:266
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:250
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:153
void set_initial_value(Mcp4461WiperIdx wiper, float initial_value)
public function used to set initial value
Definition mcp4461.cpp:39
bool increase_wiper_(Mcp4461WiperIdx wiper)
Definition mcp4461.cpp:310
uint8_t calc_terminal_connector_byte_(Mcp4461TerminalIdx terminal_connector)
Definition mcp4461.cpp:372
uint8_t get_terminal_register_(Mcp4461TerminalIdx terminal_connector)
Definition mcp4461.cpp:386
void update_terminal_register_(Mcp4461TerminalIdx terminal_connector)
Definition mcp4461.cpp:405
uint16_t get_eeprom_value(Mcp4461EepromLocation location)
get eeprom value from location
Definition mcp4461.cpp:508
bool set_eeprom_value(Mcp4461EepromLocation location, uint16_t value)
set eeprom value at specified location
Definition mcp4461.cpp:529
void disable_wiper_(Mcp4461WiperIdx wiper)
Definition mcp4461.cpp:288
void initialize_terminal_disabled(Mcp4461WiperIdx wiper, char terminal)
public function used to set disable terminal config
Definition mcp4461.cpp:44
void enable_wiper_(Mcp4461WiperIdx wiper)
Definition mcp4461.cpp:266
uint16_t read_wiper_level_(uint8_t wiper)
Definition mcp4461.cpp:199
bool mcp4461_write_(uint8_t addr, uint16_t data, bool nonvolatile=false)
Definition mcp4461.cpp:599
void enable_terminal_(Mcp4461WiperIdx wiper, char terminal)
Definition mcp4461.cpp:454
bool is_eeprom_ready_for_writing_(bool wait_if_not_ready)
Definition mcp4461.cpp:564
uint16_t get_wiper_level_(Mcp4461WiperIdx wiper)
Definition mcp4461.cpp:182
static const LogString * get_message_string(int status)
Definition mcp4461.h:122
bool update_wiper_level_(Mcp4461WiperIdx wiper)
Definition mcp4461.cpp:217
uint8_t get_wiper_address_(uint8_t wiper)
Definition mcp4461.cpp:152
void disable_terminal_(Mcp4461WiperIdx, char terminal)
Definition mcp4461.cpp:481
void write_wiper_level_(uint8_t wiper, uint16_t value)
Definition mcp4461.cpp:257
bool decrease_wiper_(Mcp4461WiperIdx wiper)
Definition mcp4461.cpp:341
bool set_terminal_register_(Mcp4461TerminalIdx terminal_connector, uint8_t data)
Definition mcp4461.cpp:432
bool set_wiper_level_(Mcp4461WiperIdx wiper, uint16_t value)
Definition mcp4461.cpp:233
void read_status_register_to_log()
read status register to log
Definition mcp4461.cpp:143
bool state
Definition fan.h:0
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
constexpr uint8_t EEPROM_WRITE_TIMEOUT_MS
Definition mcp4461.cpp:10
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT yield()
Definition core.cpp:27
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28
optional< float > initial_value
Definition mcp4461.h:13