|
1 | 1 | /* |
2 | 2 | xdrv_69_pca9557.ino - PCA9557 or TCA9554 GPIO Expander support for Tasmota |
3 | 3 |
|
4 | | - SPDX-FileCopyrightText: 2023 Theo Arends |
| 4 | + SPDX-FileCopyrightText: 2023 @cctweaker and Theo Arends |
5 | 5 |
|
6 | 6 | SPDX-License-Identifier: GPL-3.0-only |
7 | 7 | */ |
@@ -390,31 +390,35 @@ void PCA9557ModuleInit(void) { |
390 | 390 | return; |
391 | 391 | } |
392 | 392 |
|
| 393 | + uint8_t pca9557_address = PCA9557_ADDR_START; |
| 394 | + while ((Pca9557.max_devices < PCA9557_MAX_DEVICES) && (pca9557_address < PCA9557_ADDR_END)) { |
| 395 | + Pca9557.chip = Pca9557.max_devices; |
| 396 | + if (I2cSetDevice(pca9557_address)) { |
| 397 | + Pca9557.device[Pca9557.chip].address = pca9557_address; |
393 | 398 |
|
394 | | - uint8_t pca9557_address = PCA9557_ADDR_START; |
395 | | - while ((Pca9557.max_devices < PCA9557_MAX_DEVICES) && (pca9557_address < PCA9557_ADDR_END)) { |
396 | | - Pca9557.chip = Pca9557.max_devices; |
397 | | - if (I2cSetDevice(pca9557_address)) { |
398 | | - Pca9557.device[Pca9557.chip].address = pca9557_address; |
399 | | - |
400 | | - uint8_t buffer; |
401 | | - if (PCA9557ValidRead(PCA9557_R2, &buffer)) { |
402 | | - I2cSetActiveFound(pca9557_address, PCA9557_NAME); |
403 | | - Pca9557.device[Pca9557.chip].pins = 8; |
404 | | - PCA9557Write(PCA9557_R2, 0b00000000); // disable polarity inversion |
405 | | - Pca9557.max_devices++; |
406 | | - |
407 | | - Pca9557.max_pins += Pca9557.device[Pca9557.chip].pins; |
408 | | - pins_needed -= Pca9557.device[Pca9557.chip].pins; |
| 399 | + uint8_t buffer; |
| 400 | + if (PCA9557ValidRead(PCA9557_R2, &buffer)) { |
| 401 | + I2cSetActiveFound(pca9557_address, PCA9557_NAME); |
| 402 | + Pca9557.device[Pca9557.chip].pins = 8; |
| 403 | +#ifdef USE_TCA9554 |
| 404 | + if (ResetReasonPowerOn()) { // Fix power on relay toggle |
| 405 | + PCA9557Write(PCA9557_R1, 0x00); // Output state (TCA9554 power on is 0xFF, PCA9557 is 0x00) |
| 406 | +// PCA9557Write(PCA9557_R3, 0x00); // Config direction as output |
409 | 407 | } |
410 | | - } |
411 | | - if (pins_needed) { |
412 | | - pca9557_address++; |
413 | | - } else { |
414 | | - pca9557_address = PCA9557_ADDR_END; |
| 408 | +#endif // USE_TCA9554 |
| 409 | + PCA9557Write(PCA9557_R2, 0b00000000); // Disable polarity inversion |
| 410 | + Pca9557.max_devices++; |
| 411 | + |
| 412 | + Pca9557.max_pins += Pca9557.device[Pca9557.chip].pins; |
| 413 | + pins_needed -= Pca9557.device[Pca9557.chip].pins; |
415 | 414 | } |
416 | 415 | } |
417 | | - |
| 416 | + if (pins_needed) { |
| 417 | + pca9557_address++; |
| 418 | + } else { |
| 419 | + pca9557_address = PCA9557_ADDR_END; |
| 420 | + } |
| 421 | + } |
418 | 422 |
|
419 | 423 | if (!Pca9557.max_devices) { return; } |
420 | 424 |
|
@@ -463,10 +467,6 @@ void PCA9557ServiceInput(void) { |
463 | 467 | } |
464 | 468 | } |
465 | 469 |
|
466 | | -void PCA9557Init(void) { |
467 | | - PCA9557Write(PCA9557_R2, 0b00000000); // disable polarity inversion |
468 | | -} |
469 | | - |
470 | 470 | void PCA9557Power(void) { |
471 | 471 | // XdrvMailbox.index = 32-bit rpower bit mask |
472 | 472 | // Use absolute relay indexes unique with main template |
@@ -544,9 +544,6 @@ bool Xdrv69(uint32_t function) { |
544 | 544 | case FUNC_SET_POWER: |
545 | 545 | PCA9557Power(); |
546 | 546 | break; |
547 | | - case FUNC_INIT: |
548 | | - PCA9557Init(); |
549 | | - break; |
550 | 547 | case FUNC_ADD_BUTTON: |
551 | 548 | result = PCA9557AddButton(); |
552 | 549 | break; |
|
0 commit comments