@@ -572,27 +572,61 @@ bool I2cController::InitMux(const char *name, uint32_t address,
572572 bool is_alt_bus) {
573573 if (is_alt_bus) {
574574 if (!_i2c_bus_alt->HasMux ()) {
575- WS_DEBUG_PRINTLN (" [i2c] Initializing MUX driver on alt bus..." );
576575 if (!_i2c_bus_alt->AddMuxToBus (address, name)) {
577576 return false ;
578577 }
579- WS_DEBUG_PRINTLN (" OK!" );
580578 }
581579 } else {
582580 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);
586581 if (!_i2c_bus_default->AddMuxToBus (address, name)) {
587582 return false ;
588583 }
589- WS_DEBUG_PRINTLN (" OK!" );
590584 }
591585 }
592586 // TODO [Online]: Publish back out to IO here!
593587 return true ;
594588}
595589
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+
596630/* **********************************************************************/
597631/* !
598632 @brief Implements handling for a I2cDeviceAddOrReplace message
@@ -622,38 +656,10 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
622656 wippersnapper_i2c_I2cDeviceDescriptor device_descriptor =
623657 _i2c_model->GetI2cDeviceAddOrReplaceMsg ()->i2c_device_description ;
624658
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- }
654659
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..." );
657663 return true ;
658664 }
659665
@@ -718,10 +724,8 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
718724 // Assign I2C bus
719725 TwoWire *bus = nullptr ;
720726 if (use_alt_bus) {
721- WS_DEBUG_PRINTLN (" [i2c] Using alt. I2C bus..." );
722727 bus = _i2c_bus_alt->GetBus ();
723728 } else {
724- WS_DEBUG_PRINTLN (" [i2c] Using default I2C bus..." );
725729 bus = _i2c_bus_default->GetBus ();
726730 }
727731
@@ -735,7 +739,7 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
735739 WS_DEBUG_PRINTLN (device_descriptor.i2c_device_address );
736740 if (device_descriptor.i2c_device_address == 0x68 ||
737741 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 "
739743 " auto-init, skipping!" );
740744 return true ;
741745 }
@@ -769,6 +773,8 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
769773 break ;
770774 }
771775 }
776+ WS_DEBUG_PRINTLN (" [i2c] ERROR - Candidates exhausted, driver not found!" );
777+ return true ; // dont cause an error in the app
772778 } else {
773779 WS_DEBUG_PRINTLN (" [i2c] Device in message/cfg file." );
774780 // Create new driver
@@ -806,12 +812,12 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
806812
807813 if (!drv->begin ()) {
808814 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 "
810816 " the correct value for i2cDeviceName?\n\t Did you set "
811817 " the correct value for"
812- " i2cDeviceAddress?" ,
813- WS_LED_STATUS_ERROR_RUNTIME, false );
818+ " i2cDeviceAddress?" );
814819 }
820+ return true ; // don't cause an error during runtime if the device is not found
815821 }
816822 WS_DEBUG_PRINTLN (" [i2c] Driver successfully initialized!" );
817823 }
0 commit comments