Skip to content

Commit d86a445

Browse files
committed
reviewing
1 parent f579bbd commit d86a445

File tree

6 files changed

+72
-84
lines changed

6 files changed

+72
-84
lines changed

src/Wippersnapper_V2.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,11 +1231,9 @@ void Wippersnapper_V2::connect() {
12311231
#endif
12321232
WS_DEBUG_PRINTLN("[APP] Performing I2C Autoscan...");
12331233
WsV2._i2c_controller->ScanI2cBus(true);
1234-
WS_DEBUG_PRINTLN("[APP] Scan results: ");
1235-
WsV2._i2c_controller->PrintScanResults();
12361234
// Parse the JSON file
12371235
if (!WsV2._sdCardV2->ParseFileConfig())
1238-
haltErrorV2("Failed to parse config.json!");
1236+
haltErrorV2("[APP] Failed to parse config.json!");
12391237
WS_DEBUG_PRINTLN("[APP] Attempting to configure hardware...");
12401238
#ifndef OFFLINE_MODE_DEBUG
12411239
if (!WsV2._sdCardV2->CreateNewLogFile())

src/Wippersnapper_V2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
#endif
129129

130130
#define WS_VERSION \
131-
"1.0.0-offline-beta.autoconfig" ///< WipperSnapper app. version
131+
"1.0.0-offline-beta.3" ///< WipperSnapper app. version
132132
///< (semver-formatted)
133133

134134
#define WS_WDT_TIMEOUT 60000 ///< WDT timeout

src/components/i2c/controller.cpp

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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\tDid you set "
815+
WS_DEBUG_PRINTLN("[i2c] Failed to initialize driver!\n\tDid you set "
810816
"the correct value for i2cDeviceName?\n\tDid 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
}

src/components/i2c/controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class I2cController {
108108
void PrintScanResults();
109109
uint32_t GetScanDeviceAddress(int index);
110110
size_t GetScanDeviceCount();
111+
bool IsDriverInitialized(wippersnapper_i2c_I2cDeviceDescriptor& device_descriptor);
111112

112113
private:
113114
I2cModel *_i2c_model; ///< Pointer to an I2C model object

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -494,12 +494,9 @@ bool ws_sdcard::ParseI2cDeviceAddReplace(
494494
HexStrToInt(addr_device);
495495

496496
// 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);
497+
const char *is_auto = component["autoConfig"] | "true";
500498
if (strcmp(is_auto, "false") == 0) {
501-
WS_DEBUG_PRINTLN(
502-
"[SD] Found autoInit = false, do not initialize this address");
499+
WS_DEBUG_PRINTLN("[SD] autoConfig = false, do not attempt to automatically initialize this address");
503500
_cfg_i2c_addresses.push_back(
504501
msg_i2c_add.i2c_device_description.i2c_device_address);
505502
}

src/provisioning/tinyusb/Wippersnapper_FS.cpp

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* please support Adafruit and open-source hardware by purchasing
88
* products from Adafruit!
99
*
10-
* Copyright (c) Brent Rubell 2024 for Adafruit Industries.
10+
* Copyright (c) Brent Rubell 2024-2025 for Adafruit Industries.
1111
*
1212
* BSD license, all text here must be included in any redistribution.
1313
*
@@ -107,8 +107,7 @@ Wippersnapper_FS::Wippersnapper_FS() {
107107
_fs_changed = false;
108108

109109
usb_cdc.begin(115200);
110-
// If already enumerated, additional class driver begin() e.g msc, hid, midi
111-
// won't take effect until re-enumeration
110+
// Re-enumerate to allow cdc class begin() to take effect
112111
if (TinyUSBDevice.mounted()) {
113112
TinyUSBDevice.detach();
114113
delay(10);
@@ -121,13 +120,9 @@ Wippersnapper_FS::Wippersnapper_FS() {
121120
HaltFilesystem("Failed to initialize the flash chip!");
122121
}
123122

124-
// Attempt to initialize the filesystem
125-
bool is_fs_formatted = wipperFatFs_v2.begin(&flash_v2);
126-
127123
// If we are not formatted, attempt to format the filesystem as fat12
128-
if (!is_fs_formatted) {
124+
if (! wipperFatFs_v2.begin(&flash_v2)) {
129125
FRESULT rc = format_fs_fat12();
130-
131126
if (rc != FR_OK) {
132127
setStatusLEDColor(RED);
133128
HaltFilesystem("FATAL ERROR: Failed to format the filesystem!");
@@ -147,7 +142,9 @@ Wippersnapper_FS::Wippersnapper_FS() {
147142
}
148143

149144
// Initialize USB-MSC
145+
#ifndef BUILD_OFFLINE_ONLY
150146
InitUsbMsc();
147+
#endif
151148

152149
// If we wrote a fresh secrets.json file, halt until user edits the file and
153150
// RESETs the device Signal to user that action must be taken (edit
@@ -280,30 +277,29 @@ void Wippersnapper_FS::InitUsbMsc() {
280277
// Set disk vendor id, product id and revision with string up to 8, 16, 4
281278
// characters respectively
282279
usb_msc_v2.setID("Adafruit", "External Flash", "1.0");
283-
// Set callback
280+
// Set r/w callbacks
284281
usb_msc_v2.setReadWriteCallback(qspi_msc_read_cb_v2, qspi_msc_write_cb_v2,
285282
qspi_msc_flush_cb_v2);
286283

287284
// Set disk size, block size should be 512 regardless of spi flash page size
288285
usb_msc_v2.setCapacity(flash_v2.pageSize() * flash_v2.numPages() / 512, 512);
289286

290287
// MSC is ready for read/write
291-
usb_msc_v2.setUnitReady(false);
288+
usb_msc_v2.setUnitReady(true);
292289

293-
// Set callback when MSC ready
290+
// Setup callback for when MSC ready
294291
_fs_changed = false;
295292
usb_msc_v2.setReadyCallback(0, msc_ready_callback);
296293

297294
// init MSC
298-
// usb_msc_v2.begin();
295+
usb_msc_v2.begin();
299296

300-
// If already enumerated, additional class driverr begin() e.g msc, hid, midi
301-
// won't take effect until re-enumeration
302-
// Attach MSC and wait for enumeration
303-
// #ifndef BUILD_OFFLINE_ONLY
304-
TinyUSBDevice.attach();
305-
delay(500);
306-
// #endif
297+
// Re-enumerate to allow msc class begin() to take effect
298+
if (TinyUSBDevice.mounted()) {
299+
TinyUSBDevice.detach();
300+
delay(10);
301+
TinyUSBDevice.attach();
302+
}
307303
}
308304

309305
/**************************************************************************/
@@ -460,6 +456,7 @@ void Wippersnapper_FS::AddI2cDeviceToFileConfig(
460456
new_component["componentAPI"] = "i2c";
461457
new_component["i2cDeviceName"] = driver_name;
462458
new_component["period"] = 30;
459+
new_component["autoConfig"] = "true";
463460
char address_str[6];
464461
sprintf(address_str, "0x%02X", address);
465462
new_component["i2cDeviceAddress"] = address_str;
@@ -491,19 +488,8 @@ bool Wippersnapper_FS::WriteFileConfig() {
491488
file_cfg.flush();
492489
flash_v2.syncBlocks();
493490
refreshMassStorage();
494-
// Re-attach USB-MSC with updated filesystem
495-
// NOTE: This is required to ensure the filesystem is sync'd between host and
496-
// device
497-
usb_msc_v2.begin();
498-
usb_msc_v2.setUnitReady(true);
499-
if (TinyUSBDevice.mounted()) {
500-
TinyUSBDevice.detach();
501-
delay(10);
502-
TinyUSBDevice.attach();
503-
}
504-
// TODO: This is debug, we can remove it!
505-
WS_DEBUG_PRINT("Bytes written to config.json: ");
506-
WS_DEBUG_PRINTLN(bytes_written);
491+
delay(500);
492+
InitUsbMsc();
507493
return true;
508494
}
509495

0 commit comments

Comments
 (0)