Skip to content

Commit 0a422f5

Browse files
committed
pair: working mux
1 parent 9fc3e4d commit 0a422f5

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

src/components/i2c/controller.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,8 @@ bool I2cController::InitMux(const char *name, uint32_t address,
581581
} else {
582582
if (!_i2c_bus_default->HasMux()) {
583583
WS_DEBUG_PRINTLN("[i2c] Initializing MUX driver on default bus...");
584+
WS_DEBUG_PRINT("[i2c] addr: ");
585+
WS_DEBUG_PRINT(address, HEX);
584586
if (!_i2c_bus_default->AddMuxToBus(address, name)) {
585587
return false;
586588
}
@@ -690,7 +692,7 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
690692
if ((strcmp(device_name, "pca9546") == 0) ||
691693
(strcmp(device_name, "pca9548") == 0)) {
692694
WS_DEBUG_PRINT("[i2c] Initializing MUX driver...");
693-
if (!InitMux(device_name, device_descriptor.i2c_mux_address, use_alt_bus)) {
695+
if (!InitMux(device_name, device_descriptor.i2c_device_address, use_alt_bus)) {
694696
// TODO [Online]: Publish back out to IO here!
695697
WsV2.haltErrorV2("[i2c] Failed to initialize MUX driver!",
696698
WS_LED_STATUS_ERROR_RUNTIME, false);
@@ -703,6 +705,8 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
703705
// I2cDeviceAddorReplace message
704706
if (device_descriptor.i2c_mux_address != 0x00) {
705707
if (_i2c_bus_alt->HasMux() || _i2c_bus_default->HasMux()) {
708+
WS_DEBUG_PRINTLN("[i2c] Scanning MUX!");
709+
_i2c_bus_default->ScanMux();
706710
WS_DEBUG_PRINT("[i2c] Configuring MUX channel: ");
707711
WS_DEBUG_PRINTLN(device_descriptor.i2c_mux_channel);
708712
WS_DEBUG_PRINT("[i2c] use_alt_bus: ");
@@ -768,7 +772,7 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
768772
// Create new driver
769773
WS_DEBUG_PRINT("[i2c] Creating driver: ");
770774
WS_DEBUG_PRINTLN(device_name);
771-
WS_DEBUG_PRINTLN(device_descriptor.i2c_device_address);
775+
WS_DEBUG_PRINTLN(device_descriptor.i2c_device_address, HEX);
772776
WS_DEBUG_PRINTLN(device_descriptor.i2c_mux_channel);
773777
WS_DEBUG_PRINTLN(device_status);
774778

src/components/i2c/hardware.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,23 +260,46 @@ bool I2cHardware::AddMuxToBus(uint32_t address_register, const char *name) {
260260
return false;
261261
}
262262

263-
_mux_address_register = address_register;
263+
_mux_address = address_register;
264+
WS_DEBUG_PRINT("mux_addr: ");
265+
WS_DEBUG_PRINTLN(_mux_address);
264266
_has_mux = true;
265267
// Put MUX in back into its default state cuz we don't know if we're about to
266268
// use it again
267269
ClearMuxChannel();
268270
return true;
269271
}
270272

273+
274+
void I2cHardware::ScanMux() {
275+
for (uint8_t t=0; t<_mux_max_channels; t++) {
276+
_bus->beginTransmission(_mux_address);
277+
_bus->write(1 << t);
278+
_bus->endTransmission();
279+
280+
Serial.print("PCA Port #"); Serial.println(t);
281+
for (uint8_t addr = 0; addr<=127; addr++) {
282+
if (addr == _mux_address) continue;
283+
_bus->beginTransmission(addr);
284+
if (!_bus->endTransmission()) {
285+
Serial.print("Found I2C 0x"); Serial.println(addr,HEX);
286+
}
287+
}
288+
}
289+
}
290+
271291
/***********************************************************************/
272292
/*!
273293
@brief Clears the enabled MUX channel.
274294
*/
275295
/***********************************************************************/
276296
void I2cHardware::ClearMuxChannel() {
277-
if (!_has_mux)
297+
WS_DEBUG_PRINT("has_mux: ");
298+
WS_DEBUG_PRINTLN(_has_mux);
299+
if (!_has_mux) {
278300
return;
279-
_bus->beginTransmission(_mux_address_register);
301+
}
302+
_bus->beginTransmission(_mux_address);
280303
if (_mux_max_channels == 4)
281304
_bus->write(0b0000);
282305
else if (_mux_max_channels == 8)
@@ -292,9 +315,14 @@ void I2cHardware::ClearMuxChannel() {
292315
*/
293316
/***********************************************************************/
294317
void I2cHardware::SelectMuxChannel(uint32_t channel) {
295-
if (channel > _mux_max_channels - 1)
318+
WS_DEBUG_PRINT("SelectMuxChannel: ");
319+
WS_DEBUG_PRINTLN(channel);
320+
WS_DEBUG_PRINT("max_channels: ");
321+
WS_DEBUG_PRINTLN(_mux_max_channels);
322+
if (channel > _mux_max_channels - 1) {
296323
return;
297-
_bus->beginTransmission(_mux_address_register);
324+
}
325+
_bus->beginTransmission(_mux_address);
298326
_bus->write(1 << channel);
299327
_bus->endTransmission();
300328
}

src/components/i2c/hardware.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ class I2cHardware {
4141
void SelectMuxChannel(uint32_t channel);
4242
bool HasMux();
4343
void ClearMuxChannel();
44+
void ScanMux();
4445
private:
4546
void TogglePowerPin();
4647
wippersnapper_i2c_I2cBusStatus _bus_status; ///< I2C bus status
4748
TwoWire *_bus = nullptr; ///< I2C bus
4849
bool _has_mux; ///< Is a MUX present on the bus?
49-
uint32_t _mux_address_register; ///< I2C address for the MUX
50+
uint32_t _mux_address; ///< I2C address for the MUX
5051
int _mux_max_channels; ///< Maximum possible number of MUX channels
5152
char *_sda; ///< SDA pin
5253
char *_scl; ///< SCL pin

src/provisioning/tinyusb/Wippersnapper_FS.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ void Wippersnapper_FS::CreateFileConfig() {
381381
File32 file_cfg = wipperFatFs_v2.open("/config.json", FILE_READ);
382382
if (file_cfg) {
383383
DeserializationError error = deserializeJson(_doc_cfg, file_cfg);
384-
if (error)
385-
HaltFilesystem("Error unable to parse config.json on WIPPER drive!");
384+
// if (error)
385+
// HaltFilesystem("Error unable to parse config.json on WIPPER drive!");
386386
// Remove config from the filesystem
387387
file_cfg.close();
388388
wipperFatFs_v2.remove("/config.json");

0 commit comments

Comments
 (0)