Skip to content

[Offline Mode] Fix Config file deletion, RTC initialization failure #743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/release-offline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ on:
branches:
- offline-mode
- offline-mode-*
pull_request:
branches:
- offline-mode
release:
types: [published]
branches:
Expand Down
34 changes: 31 additions & 3 deletions src/components/i2c/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,11 +862,39 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
*/
/***********************************************************************/
bool I2cController::ScanI2cBus(bool default_bus = true) {
_i2c_bus_default->InitBus(default_bus);
// zero-out the scan I2cBusScanned message before attempting a scan
_scan_results = wippersnapper_i2c_I2cBusScanned_init_zero;
if (!default_bus)

// Scan the desired i2c bus
if (default_bus) {
if (_i2c_bus_default->GetBusStatus() !=
wippersnapper_i2c_I2cBusStatus_I2C_BUS_STATUS_SUCCESS) {
_i2c_bus_default->InitBus(default_bus);
}
return _i2c_bus_default->ScanBus(&_scan_results);
} else {
if (_i2c_bus_alt->GetBusStatus() !=
wippersnapper_i2c_I2cBusStatus_I2C_BUS_STATUS_SUCCESS) {
_i2c_bus_alt->InitBus(default_bus);
}
return _i2c_bus_alt->ScanBus(&_scan_results);
return _i2c_bus_default->ScanBus(&_scan_results);
}
}

/***********************************************************************/
/*!
@brief Returns the I2C bus object.
@param is_alt_bus
True if the alternative I2C bus is being used, False
otherwise.
@returns A pointer to the I2C bus object.
*/
/***********************************************************************/
TwoWire *I2cController::GetI2cBus(bool is_alt_bus) {
if (is_alt_bus) {
return _i2c_bus_alt->GetBus();
}
return _i2c_bus_default->GetBus();
}

/***********************************************************************/
Expand Down
1 change: 1 addition & 0 deletions src/components/i2c/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class I2cController {
size_t GetScanDeviceCount();
bool
IsDriverInitialized(wippersnapper_i2c_I2cDeviceDescriptor &device_descriptor);
TwoWire *GetI2cBus(bool is_alt_bus = false);

private:
I2cModel *_i2c_model; ///< Pointer to an I2C model object
Expand Down
21 changes: 12 additions & 9 deletions src/provisioning/sdcard/ws_sdcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Brent Rubell 2024 for Adafruit Industries.
* Copyright (c) Brent Rubell 2024-2025 for Adafruit Industries.
*
* BSD license, all text here must be included in any redistribution.
*
Expand Down Expand Up @@ -121,9 +121,10 @@ void ws_sdcard::ConfigureSDCard() {
/**************************************************************************/
bool ws_sdcard::InitDS1307() {
_rtc_ds1307 = new RTC_DS1307();
if (!_rtc_ds1307->begin()) {
if (!_rtc_ds1307->begin(&Wire1)) {
WS_DEBUG_PRINTLN("[SD] Error: Failed to initialize DS1307 RTC");
if (!_rtc_ds1307->begin(WsV2._i2c_controller->GetI2cBus())) {
WS_DEBUG_PRINTLN("[SD] Error: Failed to initialize DS1307 RTC on WIRE");
if (!_rtc_ds1307->begin(WsV2._i2c_controller->GetI2cBus(true))) {
WS_DEBUG_PRINTLN("[SD] Error: Failed to initialize DS1307 RTC on WIRE1");
delete _rtc_ds1307;
return false;
}
Expand All @@ -144,9 +145,10 @@ bool ws_sdcard::InitDS1307() {
bool ws_sdcard::InitDS3231() {
WS_DEBUG_PRINTLN("Begin DS3231 init");
_rtc_ds3231 = new RTC_DS3231();
if (!_rtc_ds3231->begin(&Wire)) {
if (!_rtc_ds3231->begin(&Wire1)) {
WS_DEBUG_PRINTLN("[SD] Error: Failed to initialize DS3231 RTC");
if (!_rtc_ds3231->begin(WsV2._i2c_controller->GetI2cBus())) {
WS_DEBUG_PRINTLN("[SD] Error: Failed to initialize DS3231 RTC on WIRE");
if (!_rtc_ds3231->begin(WsV2._i2c_controller->GetI2cBus(true))) {
WS_DEBUG_PRINTLN("[SD] Error: Failed to initialize DS3231 RTC on WIRE1");
delete _rtc_ds3231;
return false;
}
Expand All @@ -166,9 +168,9 @@ bool ws_sdcard::InitDS3231() {
/**************************************************************************/
bool ws_sdcard::InitPCF8523() {
_rtc_pcf8523 = new RTC_PCF8523();
if (!_rtc_pcf8523->begin(&Wire)) {
if (!_rtc_pcf8523->begin(WsV2._i2c_controller->GetI2cBus())) {
WS_DEBUG_PRINTLN("[SD] Error: Failed to initialize PCF8523 RTC on WIRE");
if (!_rtc_pcf8523->begin(&Wire1)) {
if (!_rtc_pcf8523->begin(WsV2._i2c_controller->GetI2cBus(true))) {
WS_DEBUG_PRINTLN("[SD] Error: Failed to initialize PCF8523 RTC on WIRE1");
delete _rtc_pcf8523;
return false;
Expand Down Expand Up @@ -701,6 +703,7 @@ bool ws_sdcard::ParseExportedFromDevice(JsonDocument &doc) {
WS_DEBUG_PRINTLN("[SD] Error: Failed to to configure a RTC!");
return false;
}

return true;
}

Expand Down
21 changes: 15 additions & 6 deletions src/provisioning/tinyusb/Wippersnapper_FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,12 @@ void Wippersnapper_FS::CreateFileConfig() {
File32 file_cfg = wipperFatFs_v2.open("/config.json", FILE_READ);
if (file_cfg) {
DeserializationError error = deserializeJson(_doc_cfg, file_cfg);
// if (error)
// HaltFilesystem("Error unable to parse config.json on WIPPER drive!");
// Remove config from the filesystem
if (error) {
// If we can't deserialize, just raise
HaltFilesystem(
"[fs] Error: Unable to deserialize config.json, is it corrupted?");
}
file_cfg.close();
wipperFatFs_v2.remove("/config.json");
flash_v2.syncBlocks();

// Check if the config.json file has the required keys
if (!_doc_cfg.containsKey("exportedFromDevice")) {
// Build exportedFromDevice object
Expand All @@ -404,8 +403,10 @@ void Wippersnapper_FS::CreateFileConfig() {
// Build components array
_doc_cfg["components"].to<JsonArray>();
}

return;
}
file_cfg.close();
}

// Create a default configConfig structure in a new doc
Expand Down Expand Up @@ -472,6 +473,14 @@ void Wippersnapper_FS::AddI2cDeviceToFileConfig(
*/
/**************************************************************************/
bool Wippersnapper_FS::WriteFileConfig() {
// If it exists, remove the existing config.json file
// as we're about to write the new one into memory
if (wipperFatFs_v2.exists("/config.json")) {
wipperFatFs_v2.remove("/config.json");
flash_v2.syncBlocks();
delay(500);
}

// Write the document to the filesystem
File32 file_cfg = wipperFatFs_v2.open("/config.json", FILE_WRITE);
if (!file_cfg) {
Expand Down
Loading