Skip to content

Commit fbfcc08

Browse files
committed
debugging mux
1 parent 6c45959 commit fbfcc08

File tree

5 files changed

+82
-39
lines changed

5 files changed

+82
-39
lines changed

src/Wippersnapper_demo.ino.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 1 "/var/folders/ff/dmzflvf52tq9kzvt6g8jglxw0000gn/T/tmprs71yldn"
2+
#include <Arduino.h>
3+
# 1 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
4+
# 11 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
5+
#include "ws_adapters.h"
6+
7+
8+
ws_adapter_offline wipper;
9+
#define WS_DEBUG
10+
void setup();
11+
void loop();
12+
#line 17 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
13+
void setup() {
14+
Serial.begin(115200);
15+
wipper.provision();
16+
wipper.connect();
17+
}
18+
19+
void loop() { wipper.run(); }

src/components/i2c/controller.cpp

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,8 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
701701
if (_i2c_bus_alt->HasMux() || _i2c_bus_default->HasMux()) {
702702
WS_DEBUG_PRINT("[i2c] Configuring MUX channel: ");
703703
WS_DEBUG_PRINTLN(device_descriptor.i2c_mux_channel);
704+
WS_DEBUG_PRINT("[i2c] use_alt_bus: ");
705+
WS_DEBUG_PRINTLN(use_alt_bus);
704706
ConfigureMuxChannel(device_descriptor.i2c_mux_channel, use_alt_bus);
705707
did_set_mux_ch = true;
706708
} else {
@@ -710,12 +712,13 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
710712
}
711713
}
712714

713-
WS_DEBUG_PRINTLN("Creating a new I2C driver");
714715
// Assign I2C bus
715716
TwoWire *bus = nullptr;
716717
if (use_alt_bus) {
718+
WS_DEBUG_PRINTLN("[i2c] Using alt. I2C bus...");
717719
bus = _i2c_bus_alt->GetBus();
718720
} else {
721+
WS_DEBUG_PRINTLN("[i2c] Using default I2C bus...");
719722
bus = _i2c_bus_default->GetBus();
720723
}
721724

@@ -757,27 +760,35 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
757760
}
758761
}
759762
} else {
760-
WS_DEBUG_PRINTLN("[i2c] Device was defined in message or config file.");
763+
WS_DEBUG_PRINTLN("[i2c] Device in message/cfg file.");
764+
// Create new driver
765+
WS_DEBUG_PRINT("[i2c] Creating driver: ");
766+
WS_DEBUG_PRINTLN(device_name);
767+
WS_DEBUG_PRINTLN(device_descriptor.i2c_device_address);
768+
WS_DEBUG_PRINTLN(device_descriptor.i2c_mux_channel);
769+
WS_DEBUG_PRINTLN(device_status);
770+
771+
drv = CreateI2CDriverByName(
772+
device_name, bus, device_descriptor.i2c_device_address,
773+
device_descriptor.i2c_mux_channel, device_status);
774+
if (drv == nullptr) {
775+
WS_DEBUG_PRINTLN("[i2c] ERROR: I2C driver type not found or unsupported!");
776+
}
777+
778+
// Configure MUX and bus
761779
if (did_set_mux_ch) {
780+
WS_DEBUG_PRINT("Configuring driver's MUX address: ");
781+
WS_DEBUG_PRINTLN(device_descriptor.i2c_mux_address);
762782
drv->SetMuxAddress(device_descriptor.i2c_mux_address);
783+
WS_DEBUG_PRINTLN("[i2c] Set driver to use MUX");
763784
}
785+
764786
if (use_alt_bus) {
765787
drv->EnableAltI2CBus(_i2c_model->GetI2cDeviceAddOrReplaceMsg()
766788
->i2c_device_description.i2c_bus_scl,
767789
_i2c_model->GetI2cDeviceAddOrReplaceMsg()
768790
->i2c_device_description.i2c_bus_sda);
769-
}
770-
drv = CreateI2CDriverByName(
771-
device_name, bus, device_descriptor.i2c_device_address,
772-
device_descriptor.i2c_mux_channel, device_status);
773-
if (!drv->begin()) {
774-
if (WsV2._sdCardV2->isModeOffline()) {
775-
WsV2.haltErrorV2("[i2c] Driver failed to initialize!\n\tDid you set "
776-
"the correct value for i2cDeviceName?\n\tDid you set "
777-
"the correct value for"
778-
"i2cDeviceAddress?",
779-
WS_LED_STATUS_ERROR_RUNTIME, false);
780-
}
791+
WS_DEBUG_PRINTLN("[i2c] Set driver to use Alt I2C bus");
781792
}
782793
// Configure the driver
783794
drv->SetSensorTypes(
@@ -788,6 +799,16 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
788799

789800
drv->SetPeriod(
790801
_i2c_model->GetI2cDeviceAddOrReplaceMsg()->i2c_device_period);
802+
803+
if (!drv->begin()) {
804+
if (WsV2._sdCardV2->isModeOffline()) {
805+
WsV2.haltErrorV2("[i2c] Driver failed to initialize!\n\tDid you set "
806+
"the correct value for i2cDeviceName?\n\tDid you set "
807+
"the correct value for"
808+
"i2cDeviceAddress?",
809+
WS_LED_STATUS_ERROR_RUNTIME, false);
810+
}
811+
}
791812
WS_DEBUG_PRINTLN("[i2c] Driver successfully initialized!");
792813
}
793814
// Add the initialized driver

src/components/i2c/hardware.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ bool I2cHardware::ScanBus(wippersnapper_i2c_I2cBusScanned *scan_results) {
183183
uint8_t endTransmissionRC = _bus->endTransmission();
184184

185185
if (endTransmissionRC == 0) {
186-
WS_DEBUG_PRINTLN("[i2c] Found Device at ");
186+
WS_DEBUG_PRINT("[i2c] Found Device at ");
187187
WS_DEBUG_PRINT("0x");
188-
WS_DEBUG_PRINT(address, HEX);
188+
WS_DEBUG_PRINTLN(address, HEX);
189189
scan_results
190190
->i2c_bus_found_devices[scan_results->i2c_bus_found_devices_count]
191191
.i2c_device_address = address;

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ bool ws_sdcard::InitDS1307() {
126126
}
127127
if (!_rtc_ds1307->isrunning())
128128
_rtc_ds1307->adjust(DateTime(F(__DATE__), F(__TIME__)));
129+
_cfg_i2c_addresses.push_back(0x68); // Disable auto-config for DS1307
129130
return true;
130131
}
131132

@@ -148,6 +149,7 @@ bool ws_sdcard::InitDS3231() {
148149
}
149150
if (_rtc_ds3231->lostPower())
150151
_rtc_ds3231->adjust(DateTime(F(__DATE__), F(__TIME__)));
152+
_cfg_i2c_addresses.push_back(0x68); // Disable auto-config for DS3231
151153
return true;
152154
}
153155

@@ -172,6 +174,7 @@ bool ws_sdcard::InitPCF8523() {
172174
_rtc_pcf8523->adjust(DateTime(F(__DATE__), F(__TIME__)));
173175
}
174176
_rtc_pcf8523->start();
177+
_cfg_i2c_addresses.push_back(0x68); // Disable auto-config for DS3231
175178
return true;
176179
}
177180

@@ -489,11 +492,16 @@ bool ws_sdcard::ParseI2cDeviceAddReplace(
489492
const char *addr_device = component["i2cDeviceAddress"] | "0x00";
490493
msg_i2c_add.i2c_device_description.i2c_device_address =
491494
HexStrToInt(addr_device);
492-
if (!WsV2._i2c_controller->WasDeviceScanned(
493-
msg_i2c_add.i2c_device_description.i2c_device_address)) {
494-
WS_DEBUG_PRINT("[SD] WARNING - I2C Device (");
495-
WS_DEBUG_PRINT(msg_i2c_add.i2c_device_description.i2c_device_address, HEX);
496-
WS_DEBUG_PRINTLN(") not found in scan!");
495+
496+
// MUXes, Seesaw, special devices should have an auto-init flag set to false
497+
const char *is_auto = component["autoInit"] | "true";
498+
WS_DEBUG_PRINT("[SD] Found autoInit = ");
499+
WS_DEBUG_PRINTLN(is_auto);
500+
if (strcmp(is_auto, "false") == 0) {
501+
WS_DEBUG_PRINTLN(
502+
"[SD] Found autoInit = false, do not initialize this address");
503+
_cfg_i2c_addresses.push_back(
504+
msg_i2c_add.i2c_device_description.i2c_device_address);
497505
}
498506

499507
const char *addr_mux = component["i2cMuxAddress"] | "0x00";
@@ -535,10 +543,11 @@ bool ws_sdcard::AddI2cScanResultsToBuffer() {
535543
// Was this address already provided by the config file?
536544
bool skip_device = false;
537545
for (size_t j = 0; j < _cfg_i2c_addresses.size(); j++) {
538-
if (_cfg_i2c_addresses[j] == WsV2._i2c_controller->GetScanDeviceAddress(i)) {
539-
skip_device = true;
540-
break;
541-
}
546+
if (_cfg_i2c_addresses[j] ==
547+
WsV2._i2c_controller->GetScanDeviceAddress(i)) {
548+
skip_device = true;
549+
break;
550+
}
542551
}
543552

544553
if (skip_device) {
@@ -722,17 +731,10 @@ bool ws_sdcard::ParseFileConfig() {
722731
}
723732
#endif
724733

725-
// Dump what doc looks like to serial
726-
String jsonStr;
727-
serializeJsonPretty(doc, jsonStr);
728-
WS_DEBUG_PRINTLN("[SD] Deserialized JSON:");
729-
WS_DEBUG_PRINTLN("========================================");
730-
WS_DEBUG_PRINTLN(jsonStr);
731-
WS_DEBUG_PRINTLN("========================================");
732-
733-
// query document size
734+
// Get JSON document size
735+
// TODO: Remove, this is DEBUG ONLY
734736
size_t doc_size = measureJson(doc);
735-
WS_DEBUG_PRINT("[SD] Document size: ");
737+
WS_DEBUG_PRINT("[DBG | SD] Document size: ");
736738
WS_DEBUG_PRINTLN(doc_size);
737739

738740
if (doc.isNull()) {
@@ -831,7 +833,8 @@ bool ws_sdcard::ParseComponents(JsonArray &components) {
831833
msg_signal_b2d.which_payload =
832834
wippersnapper_signal_BrokerToDevice_i2c_device_add_replace_tag;
833835
msg_signal_b2d.payload.i2c_device_add_replace = msg_add;
834-
_cfg_i2c_addresses.push_back(msg_add.i2c_device_description.i2c_device_address);
836+
_cfg_i2c_addresses.push_back(
837+
msg_add.i2c_device_description.i2c_device_address);
835838
}
836839
} else {
837840
WS_DEBUG_PRINTLN("[SD] Error: Unknown Component API: " +

src/provisioning/tinyusb/Wippersnapper_FS.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,10 @@ void Wippersnapper_FS::InitUsbMsc() {
294294
// If already enumerated, additional class driverr begin() e.g msc, hid, midi
295295
// won't take effect until re-enumeration
296296
// Attach MSC and wait for enumeration
297-
#ifndef BUILD_OFFLINE_ONLY
297+
//#ifndef BUILD_OFFLINE_ONLY
298298
TinyUSBDevice.attach();
299299
delay(500);
300-
#endif
300+
//#endif
301301
}
302302

303303
/**************************************************************************/
@@ -488,7 +488,7 @@ bool Wippersnapper_FS::WriteFileConfig() {
488488
// Re-attach USB-MSC with updated filesystem
489489
// NOTE: This is required to ensure the filesystem is sync'd between host and
490490
// device
491-
TinyUSBDevice.attach();
491+
// TinyUSBDevice.attach();
492492
delay(2500);
493493
// TODO: This is debug, we can remove it!
494494
WS_DEBUG_PRINT("Bytes written to config.json: ");

0 commit comments

Comments
 (0)