@@ -572,27 +572,61 @@ bool I2cController::InitMux(const char *name, uint32_t address,
572
572
bool is_alt_bus) {
573
573
if (is_alt_bus) {
574
574
if (!_i2c_bus_alt->HasMux ()) {
575
- WS_DEBUG_PRINTLN (" [i2c] Initializing MUX driver on alt bus..." );
576
575
if (!_i2c_bus_alt->AddMuxToBus (address, name)) {
577
576
return false ;
578
577
}
579
- WS_DEBUG_PRINTLN (" OK!" );
580
578
}
581
579
} else {
582
580
if (!_i2c_bus_default->HasMux ()) {
583
- WS_DEBUG_PRINTLN (" [i2c] Initializing MUX driver on default bus..." );
584
- WS_DEBUG_PRINT (" [i2c] addr: " );
585
- WS_DEBUG_PRINT (address, HEX);
586
581
if (!_i2c_bus_default->AddMuxToBus (address, name)) {
587
582
return false ;
588
583
}
589
- WS_DEBUG_PRINTLN (" OK!" );
590
584
}
591
585
}
592
586
// TODO [Online]: Publish back out to IO here!
593
587
return true ;
594
588
}
595
589
590
+ /* **********************************************************************/
591
+ /* !
592
+ @brief Checks if a driver has already been initialized with the
593
+ given device descriptor.
594
+ @param device_descriptor
595
+ The I2cDeviceDescriptor message.
596
+ @returns True if a driver has already been initialized, False
597
+ otherwise.
598
+ */
599
+ /* **********************************************************************/
600
+ bool I2cController::IsDriverInitialized (
601
+ wippersnapper_i2c_I2cDeviceDescriptor &device_descriptor) {
602
+ // Before we do anything, check if a driver has been already initialized with
603
+ // the device_descriptor if so, we log and skip
604
+ for (auto &driver : _i2c_drivers) {
605
+ // Do they share the same address?
606
+ if (driver->GetAddress () == device_descriptor.i2c_device_address ) {
607
+ // Okay - do they sit on different i2c buses?
608
+ bool is_driver_bus_alt = driver->HasAltI2CBus ();
609
+ bool is_device_bus_alt =
610
+ (strcmp (device_descriptor.i2c_bus_scl , " default" ) != 0 ) ||
611
+ (strcmp (device_descriptor.i2c_bus_sda , " default" ) != 0 );
612
+ // Bus descriptors do not match, so we haven't initialized this candidate
613
+ if (is_driver_bus_alt != is_device_bus_alt)
614
+ continue ;
615
+
616
+ // What about the MUX?
617
+ if (driver->HasMux () &&
618
+ driver->GetMuxAddress () == device_descriptor.i2c_mux_address &&
619
+ driver->GetMuxChannel () != device_descriptor.i2c_mux_channel ) {
620
+ continue ;
621
+ }
622
+
623
+ WS_DEBUG_PRINTLN (" [i2c] Descriptor already initialized..." );
624
+ return true ;
625
+ }
626
+ }
627
+ return false ;
628
+ }
629
+
596
630
/* **********************************************************************/
597
631
/* !
598
632
@brief Implements handling for a I2cDeviceAddOrReplace message
@@ -622,38 +656,10 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
622
656
wippersnapper_i2c_I2cDeviceDescriptor device_descriptor =
623
657
_i2c_model->GetI2cDeviceAddOrReplaceMsg ()->i2c_device_description ;
624
658
625
- // Before we do anything, check if a driver has been already initialized with
626
- // the device_descriptor if so, we log and skip
627
- // TODO: Break this out into a new func.
628
- bool did_init_already = false ;
629
- for (auto &driver : _i2c_drivers) {
630
- // Do they share the same address?
631
- if (driver->GetAddress () == device_descriptor.i2c_device_address ) {
632
- // Okay - do they sit on different i2c buses?
633
- bool is_driver_bus_alt = driver->HasAltI2CBus ();
634
- bool is_device_bus_alt =
635
- (strcmp (device_descriptor.i2c_bus_scl , " default" ) != 0 ) ||
636
- (strcmp (device_descriptor.i2c_bus_sda , " default" ) != 0 );
637
-
638
- // Bus descriptors do not match, we haven't initialized this candidate
639
- if (is_driver_bus_alt != is_device_bus_alt)
640
- continue ;
641
-
642
- // What about the MUX?
643
- if (driver->HasMux () &&
644
- driver->GetMuxAddress () == device_descriptor.i2c_mux_address ) {
645
- if (driver->GetMuxChannel () != device_descriptor.i2c_mux_channel ) {
646
- continue ;
647
- }
648
- }
649
- WS_DEBUG_PRINTLN (" [i2c] Descriptor already initialized..." );
650
- did_init_already = true ;
651
- break ;
652
- }
653
- }
654
659
655
- if (did_init_already) {
656
- WS_DEBUG_PRINTLN (" [i2c] Device already initialized, ignoring..." );
660
+ // Did the driver initialize correctly?
661
+ if ( IsDriverInitialized (device_descriptor)) {
662
+ WS_DEBUG_PRINTLN (" [i2c] Driver already initialized, skipping..." );
657
663
return true ;
658
664
}
659
665
@@ -718,10 +724,8 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
718
724
// Assign I2C bus
719
725
TwoWire *bus = nullptr ;
720
726
if (use_alt_bus) {
721
- WS_DEBUG_PRINTLN (" [i2c] Using alt. I2C bus..." );
722
727
bus = _i2c_bus_alt->GetBus ();
723
728
} else {
724
- WS_DEBUG_PRINTLN (" [i2c] Using default I2C bus..." );
725
729
bus = _i2c_bus_default->GetBus ();
726
730
}
727
731
@@ -735,7 +739,7 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
735
739
WS_DEBUG_PRINTLN (device_descriptor.i2c_device_address );
736
740
if (device_descriptor.i2c_device_address == 0x68 ||
737
741
device_descriptor.i2c_device_address == 0x70 ) {
738
- WS_DEBUG_PRINTLN (" [i2c] Device address is shared with rtx/mux , can not "
742
+ WS_DEBUG_PRINTLN (" [i2c] Device address is shared with RTC/MUX , can not "
739
743
" auto-init, skipping!" );
740
744
return true ;
741
745
}
@@ -769,6 +773,8 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
769
773
break ;
770
774
}
771
775
}
776
+ WS_DEBUG_PRINTLN (" [i2c] ERROR - Candidates exhausted, driver not found!" );
777
+ return true ; // dont cause an error in the app
772
778
} else {
773
779
WS_DEBUG_PRINTLN (" [i2c] Device in message/cfg file." );
774
780
// Create new driver
@@ -806,12 +812,12 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
806
812
807
813
if (!drv->begin ()) {
808
814
if (WsV2._sdCardV2 ->isModeOffline ()) {
809
- WsV2. haltErrorV2 (" [i2c] Driver failed to initialize!\n\t Did you set "
815
+ WS_DEBUG_PRINTLN (" [i2c] Failed to initialize driver !\n\t Did you set "
810
816
" the correct value for i2cDeviceName?\n\t Did you set "
811
817
" the correct value for"
812
- " i2cDeviceAddress?" ,
813
- WS_LED_STATUS_ERROR_RUNTIME, false );
818
+ " i2cDeviceAddress?" );
814
819
}
820
+ return true ; // don't cause an error during runtime if the device is not found
815
821
}
816
822
WS_DEBUG_PRINTLN (" [i2c] Driver successfully initialized!" );
817
823
}
0 commit comments