Skip to content

Commit cb757b4

Browse files
committed
Scan and remove for i2c devices on mux
1 parent c17c5a3 commit cb757b4

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

src/components/i2c/controller.cpp

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -406,15 +406,16 @@ I2cController::~I2cController() {
406406
*/
407407
/***********************************************************************/
408408
bool I2cController::RemoveDriver(uint32_t address) {
409-
for (drvBase* driver : _i2c_drivers) {
409+
for (drvBase *driver : _i2c_drivers) {
410410
if (driver == nullptr)
411411
continue;
412412

413413
if (driver->GetAddress() != address)
414414
continue;
415415

416416
delete driver;
417-
_i2c_drivers.erase(std::find(_i2c_drivers.begin(), _i2c_drivers.end(), driver));
417+
_i2c_drivers.erase(
418+
std::find(_i2c_drivers.begin(), _i2c_drivers.end(), driver));
418419
return true;
419420
}
420421
return false;
@@ -504,29 +505,63 @@ bool I2cController::Handle_I2cDeviceRemove(pb_istream_t *stream) {
504505
return false;
505506
}
506507

508+
bool did_remove = true;
509+
507510
// Check for default bus
508511
if (strlen(msgRemove->i2c_device_description.i2c_bus_scl) == 0 &&
509512
strlen(msgRemove->i2c_device_description.i2c_bus_sda) == 0) {
510513
WS_DEBUG_PRINTLN("[i2c] Removing device from default bus...");
511514
if (!_i2c_bus_default->HasMux()) {
512515
// TODO: Implement remove, straightforward
513516
if (!RemoveDriver(msgRemove->i2c_device_description.i2c_device_address)) {
514-
WS_DEBUG_PRINTLN("[i2c] ERROR: Failed to remove i2c device from default bus!");
515-
return false;
517+
WS_DEBUG_PRINTLN(
518+
"[i2c] ERROR: Failed to remove i2c device from default bus!");
519+
did_remove = false;
516520
}
517521
} else {
518522
// Bus has a I2C MUX attached
519523
// Case 1: Is the I2C device connected to a MUX?
520-
if (msgRemove->i2c_device_description.i2c_mux_address != 0xFFFF && msgRemove->i2c_device_description.i2c_mux_channel >= 0) {
524+
if (msgRemove->i2c_device_description.i2c_mux_address != 0xFFFF &&
525+
msgRemove->i2c_device_description.i2c_mux_channel >= 0) {
521526
// TODO: Remove the device from the mux's channel and delete the driver
527+
_i2c_bus_default->SelectMuxChannel(
528+
msgRemove->i2c_device_description.i2c_mux_channel);
529+
if (!RemoveDriver(
530+
msgRemove->i2c_device_description.i2c_device_address)) {
531+
WS_DEBUG_PRINTLN(
532+
"[i2c] ERROR: Failed to remove i2c device from default bus!");
533+
did_remove = false;
534+
}
522535
}
523536
// Case 2: Is the I2C device a MUX?
524-
if (msgRemove->i2c_device_description.i2c_device_address == msgRemove->i2c_device_description.i2c_mux_address) {
525-
// TODO: Remove the MUX from the i2c bus
537+
if (msgRemove->i2c_device_description.i2c_device_address ==
538+
msgRemove->i2c_device_description.i2c_mux_address) {
539+
// TODO: Scan the mux to see what drivers are attached?
540+
wippersnapper_i2c_I2cBusScanned scan_results;
541+
_i2c_bus_default->ScanMux(&scan_results);
542+
// DEBUG - TODO REMOVE - Print out the scan results
543+
for (int i = 0; i < scan_results.i2c_bus_found_devices_count; i++) {
544+
WS_DEBUG_PRINT("[i2c] Found device at address: ");
545+
WS_DEBUG_PRINT(
546+
scan_results.i2c_bus_found_devices[i].i2c_device_address, HEX);
547+
WS_DEBUG_PRINT(" on mux channel #: ");
548+
WS_DEBUG_PRINTLN(
549+
scan_results.i2c_bus_found_devices[i].i2c_mux_channel);
550+
// Select the channel and remove the device
551+
_i2c_bus_default->SelectMuxChannel(
552+
scan_results.i2c_bus_found_devices[i].i2c_mux_channel);
553+
RemoveDriver(
554+
scan_results.i2c_bus_found_devices[i].i2c_device_address);
555+
}
556+
_i2c_bus_default->RemoveMux();
526557
}
527558
}
528559
}
529560

561+
// TODO: Check for Alt. I2C Bus
562+
563+
// Publush with did_remove to the response
564+
530565
return true;
531566
}
532567

src/components/i2c/hardware.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ bool I2cHardware::AddMuxToBus(uint32_t address_register, const char *name) {
273273
return true;
274274
}
275275

276+
void I2cHardware::RemoveMux() {
277+
ClearMuxChannel();
278+
_has_mux = false;
279+
_mux_address_register = 0;
280+
_mux_max_channels = 0;
281+
}
282+
276283
/***********************************************************************/
277284
/*!
278285
@brief Clears the enabled MUX channel.

src/components/i2c/hardware.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ class I2cHardware {
4141
bool ScanBus(wippersnapper_i2c_I2cBusScanned* scan_results);
4242
// MUX
4343
bool AddMuxToBus(uint32_t address_register, const char *name);
44-
void SelectMuxChannel(uint32_t channel);
44+
void RemoveMux();
4545
bool HasMux();
4646
void ClearMuxChannel();
47+
void SelectMuxChannel(uint32_t channel);
4748
bool ScanMux(wippersnapper_i2c_I2cBusScanned* scan_results);
4849
private:
4950
void TogglePowerPin();

0 commit comments

Comments
 (0)