From 7fd0d1a50a28ce2902d4aa12e78a93d9dd8e52d0 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 29 Apr 2025 09:29:44 -0400 Subject: [PATCH 1/7] Attempt to fix the config.json file getting rm'd --- src/provisioning/tinyusb/Wippersnapper_FS.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/provisioning/tinyusb/Wippersnapper_FS.cpp b/src/provisioning/tinyusb/Wippersnapper_FS.cpp index c0398b02d..ed8ffb5ee 100644 --- a/src/provisioning/tinyusb/Wippersnapper_FS.cpp +++ b/src/provisioning/tinyusb/Wippersnapper_FS.cpp @@ -381,9 +381,14 @@ 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?"); + } + // We are going to parse the in-memory object, _doc_cfg, rather + // than the file. So, let's remove the ctg file since we'll replace + // it with a new cfg file later on file_cfg.close(); wipperFatFs_v2.remove("/config.json"); flash_v2.syncBlocks(); @@ -404,6 +409,7 @@ void Wippersnapper_FS::CreateFileConfig() { // Build components array _doc_cfg["components"].to(); } + return; } } From 11cee82963ef34c8976fe1e95c3354bb81dafb00 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 29 Apr 2025 10:04:51 -0400 Subject: [PATCH 2/7] More intelligent approach for the config file rm issue --- src/components/i2c/controller.cpp | 18 +++++++++++++++--- src/provisioning/sdcard/ws_sdcard.cpp | 15 ++++++++++++++- src/provisioning/tinyusb/Wippersnapper_FS.cpp | 15 +++++++++------ 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/components/i2c/controller.cpp b/src/components/i2c/controller.cpp index 1eaf24238..7e0026b0b 100644 --- a/src/components/i2c/controller.cpp +++ b/src/components/i2c/controller.cpp @@ -862,11 +862,23 @@ 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); + } } /***********************************************************************/ diff --git a/src/provisioning/sdcard/ws_sdcard.cpp b/src/provisioning/sdcard/ws_sdcard.cpp index 45f04e278..63689a9d2 100644 --- a/src/provisioning/sdcard/ws_sdcard.cpp +++ b/src/provisioning/sdcard/ws_sdcard.cpp @@ -165,8 +165,17 @@ bool ws_sdcard::InitDS3231() { */ /**************************************************************************/ bool ws_sdcard::InitPCF8523() { + + // TODO: Check if we can see the PCF8523 after + // the initial i2c scan + WsV2._i2c_controller->ScanI2cBus(true); + WS_DEBUG_PRINT("[sd] Scanned I2C Devices: ") + WS_DEBUG_PRINTLN(WsV2._i2c_controller->GetScanDeviceCount()); + WS_DEBUG_PRINT("Was Device Found? "); + WS_DEBUG_PRINTLN(WsV2._i2c_controller->WasDeviceScanned(0x68)); + _rtc_pcf8523 = new RTC_PCF8523(); - if (!_rtc_pcf8523->begin(&Wire)) { + if (!_rtc_pcf8523->begin()) { WS_DEBUG_PRINTLN("[SD] Error: Failed to initialize PCF8523 RTC on WIRE"); if (!_rtc_pcf8523->begin(&Wire1)) { WS_DEBUG_PRINTLN("[SD] Error: Failed to initialize PCF8523 RTC on WIRE1"); @@ -701,6 +710,7 @@ bool ws_sdcard::ParseExportedFromDevice(JsonDocument &doc) { WS_DEBUG_PRINTLN("[SD] Error: Failed to to configure a RTC!"); return false; } + return true; } @@ -715,6 +725,9 @@ bool ws_sdcard::ParseExportedFromDevice(JsonDocument &doc) { bool ws_sdcard::ParseFileConfig() { DeserializationError error; + // delay 6 seconds + delay(6000); + #ifndef OFFLINE_MODE_DEBUG WS_DEBUG_PRINTLN("[SD] Deserializing config.json..."); JsonDocument &doc = WsV2._fileSystemV2->GetDocCfg(); diff --git a/src/provisioning/tinyusb/Wippersnapper_FS.cpp b/src/provisioning/tinyusb/Wippersnapper_FS.cpp index ed8ffb5ee..7f6daa614 100644 --- a/src/provisioning/tinyusb/Wippersnapper_FS.cpp +++ b/src/provisioning/tinyusb/Wippersnapper_FS.cpp @@ -386,13 +386,7 @@ void Wippersnapper_FS::CreateFileConfig() { HaltFilesystem( "[fs] Error: Unable to deserialize config.json, is it corrupted?"); } - // We are going to parse the in-memory object, _doc_cfg, rather - // than the file. So, let's remove the ctg file since we'll replace - // it with a new cfg file later on 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 @@ -412,6 +406,7 @@ void Wippersnapper_FS::CreateFileConfig() { return; } + file_cfg.close(); } // Create a default configConfig structure in a new doc @@ -478,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) { From e602c71bf571f1c321de1b3233d7670deca9f344 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 2 May 2025 13:21:26 -0400 Subject: [PATCH 3/7] Fix RTC issue --- src/components/i2c/controller.cpp | 4 ++++ src/components/i2c/controller.h | 2 +- src/components/i2c/hardware.cpp | 1 + src/provisioning/sdcard/ws_sdcard.cpp | 10 +++++----- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/components/i2c/controller.cpp b/src/components/i2c/controller.cpp index 7e0026b0b..840aa291b 100644 --- a/src/components/i2c/controller.cpp +++ b/src/components/i2c/controller.cpp @@ -881,6 +881,10 @@ bool I2cController::ScanI2cBus(bool default_bus = true) { } } +TwoWire *I2cController::GetI2cBus(bool is_alt_bus) { + return _i2c_bus_default->GetBus(); +} + /***********************************************************************/ /*! @brief Checks if a device was found on the i2c bus. MUST be called diff --git a/src/components/i2c/controller.h b/src/components/i2c/controller.h index 04e4b4662..c6fde23d5 100644 --- a/src/components/i2c/controller.h +++ b/src/components/i2c/controller.h @@ -109,7 +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 I2cHardware *_i2c_bus_default; ///< Pointer to the default I2C bus diff --git a/src/components/i2c/hardware.cpp b/src/components/i2c/hardware.cpp index ec7e821d0..daf7afcb4 100644 --- a/src/components/i2c/hardware.cpp +++ b/src/components/i2c/hardware.cpp @@ -71,6 +71,7 @@ void I2cHardware::InitBus(bool is_default, const char *sda, const char *scl) { // to the i2c bus. If the pin is defined, turn the power to the i2c bus on. #if defined(PIN_I2C_POWER) || defined(TFT_I2C_POWER) || \ defined(NEOPIXEL_I2C_POWER) + WS_DEBUG_PRINTLN("[i2c] Powering on I2C bus..."); TogglePowerPin(); #endif diff --git a/src/provisioning/sdcard/ws_sdcard.cpp b/src/provisioning/sdcard/ws_sdcard.cpp index 63689a9d2..6b0a24193 100644 --- a/src/provisioning/sdcard/ws_sdcard.cpp +++ b/src/provisioning/sdcard/ws_sdcard.cpp @@ -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. * @@ -165,17 +165,15 @@ bool ws_sdcard::InitDS3231() { */ /**************************************************************************/ bool ws_sdcard::InitPCF8523() { - - // TODO: Check if we can see the PCF8523 after - // the initial i2c scan WsV2._i2c_controller->ScanI2cBus(true); WS_DEBUG_PRINT("[sd] Scanned I2C Devices: ") WS_DEBUG_PRINTLN(WsV2._i2c_controller->GetScanDeviceCount()); WS_DEBUG_PRINT("Was Device Found? "); WS_DEBUG_PRINTLN(WsV2._i2c_controller->WasDeviceScanned(0x68)); + _rtc_pcf8523 = new RTC_PCF8523(); - if (!_rtc_pcf8523->begin()) { + 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)) { WS_DEBUG_PRINTLN("[SD] Error: Failed to initialize PCF8523 RTC on WIRE1"); @@ -706,6 +704,8 @@ bool ws_sdcard::ParseExportedFromDevice(JsonDocument &doc) { // Configures RTC const char *rtc_type = exportedFromDevice["rtc"] | "SOFT"; + // wait 9 seconds + delay(9000); if (!ConfigureRTC(rtc_type)) { WS_DEBUG_PRINTLN("[SD] Error: Failed to to configure a RTC!"); return false; From 59bb1e5ec079bbe64f081341ef7e8100a17b87f5 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 2 May 2025 14:40:01 -0400 Subject: [PATCH 4/7] Use fix for Wire1, too --- src/components/i2c/controller.cpp | 12 ++++++++++++ src/provisioning/sdcard/ws_sdcard.cpp | 23 +++++++++-------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/components/i2c/controller.cpp b/src/components/i2c/controller.cpp index 840aa291b..28a09267f 100644 --- a/src/components/i2c/controller.cpp +++ b/src/components/i2c/controller.cpp @@ -881,7 +881,19 @@ bool I2cController::ScanI2cBus(bool default_bus = true) { } } +/***********************************************************************/ +/*! + @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(); } diff --git a/src/provisioning/sdcard/ws_sdcard.cpp b/src/provisioning/sdcard/ws_sdcard.cpp index 6b0a24193..23bab2ba4 100644 --- a/src/provisioning/sdcard/ws_sdcard.cpp +++ b/src/provisioning/sdcard/ws_sdcard.cpp @@ -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; } @@ -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; } @@ -165,17 +167,10 @@ bool ws_sdcard::InitDS3231() { */ /**************************************************************************/ bool ws_sdcard::InitPCF8523() { - WsV2._i2c_controller->ScanI2cBus(true); - WS_DEBUG_PRINT("[sd] Scanned I2C Devices: ") - WS_DEBUG_PRINTLN(WsV2._i2c_controller->GetScanDeviceCount()); - WS_DEBUG_PRINT("Was Device Found? "); - WS_DEBUG_PRINTLN(WsV2._i2c_controller->WasDeviceScanned(0x68)); - - _rtc_pcf8523 = new RTC_PCF8523(); 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; From 7494b5aa3b23e3c8f002474c0b966eb67138b401 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 2 May 2025 14:46:17 -0400 Subject: [PATCH 5/7] Remove prints/delays for debugging --- src/components/i2c/hardware.cpp | 1 - src/provisioning/sdcard/ws_sdcard.cpp | 5 ----- 2 files changed, 6 deletions(-) diff --git a/src/components/i2c/hardware.cpp b/src/components/i2c/hardware.cpp index daf7afcb4..ec7e821d0 100644 --- a/src/components/i2c/hardware.cpp +++ b/src/components/i2c/hardware.cpp @@ -71,7 +71,6 @@ void I2cHardware::InitBus(bool is_default, const char *sda, const char *scl) { // to the i2c bus. If the pin is defined, turn the power to the i2c bus on. #if defined(PIN_I2C_POWER) || defined(TFT_I2C_POWER) || \ defined(NEOPIXEL_I2C_POWER) - WS_DEBUG_PRINTLN("[i2c] Powering on I2C bus..."); TogglePowerPin(); #endif diff --git a/src/provisioning/sdcard/ws_sdcard.cpp b/src/provisioning/sdcard/ws_sdcard.cpp index 23bab2ba4..8fdea501a 100644 --- a/src/provisioning/sdcard/ws_sdcard.cpp +++ b/src/provisioning/sdcard/ws_sdcard.cpp @@ -699,8 +699,6 @@ bool ws_sdcard::ParseExportedFromDevice(JsonDocument &doc) { // Configures RTC const char *rtc_type = exportedFromDevice["rtc"] | "SOFT"; - // wait 9 seconds - delay(9000); if (!ConfigureRTC(rtc_type)) { WS_DEBUG_PRINTLN("[SD] Error: Failed to to configure a RTC!"); return false; @@ -720,9 +718,6 @@ bool ws_sdcard::ParseExportedFromDevice(JsonDocument &doc) { bool ws_sdcard::ParseFileConfig() { DeserializationError error; - // delay 6 seconds - delay(6000); - #ifndef OFFLINE_MODE_DEBUG WS_DEBUG_PRINTLN("[SD] Deserializing config.json..."); JsonDocument &doc = WsV2._fileSystemV2->GetDocCfg(); From 0e6de25d1757fce17871ca4d7658b39411b17aa9 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 2 May 2025 14:48:23 -0400 Subject: [PATCH 6/7] CI run --- .github/workflows/release-offline.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/release-offline.yml b/.github/workflows/release-offline.yml index b429ddddc..8e3c8808d 100644 --- a/.github/workflows/release-offline.yml +++ b/.github/workflows/release-offline.yml @@ -7,6 +7,9 @@ on: branches: - offline-mode - offline-mode-* + pull_request: + branches: + - offline-mode release: types: [published] branches: From 11e21164cbdcd1a5ad66a8bf86481903736da0c0 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 2 May 2025 14:52:33 -0400 Subject: [PATCH 7/7] Clang --- src/components/i2c/controller.cpp | 8 ++++---- src/components/i2c/controller.h | 3 ++- src/provisioning/sdcard/ws_sdcard.cpp | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/components/i2c/controller.cpp b/src/components/i2c/controller.cpp index 28a09267f..6f3e02959 100644 --- a/src/components/i2c/controller.cpp +++ b/src/components/i2c/controller.cpp @@ -891,10 +891,10 @@ bool I2cController::ScanI2cBus(bool default_bus = true) { */ /***********************************************************************/ TwoWire *I2cController::GetI2cBus(bool is_alt_bus) { - if (is_alt_bus) { - return _i2c_bus_alt->GetBus(); - } - return _i2c_bus_default->GetBus(); + if (is_alt_bus) { + return _i2c_bus_alt->GetBus(); + } + return _i2c_bus_default->GetBus(); } /***********************************************************************/ diff --git a/src/components/i2c/controller.h b/src/components/i2c/controller.h index c6fde23d5..5e7b28bb2 100644 --- a/src/components/i2c/controller.h +++ b/src/components/i2c/controller.h @@ -109,7 +109,8 @@ class I2cController { size_t GetScanDeviceCount(); bool IsDriverInitialized(wippersnapper_i2c_I2cDeviceDescriptor &device_descriptor); - TwoWire *GetI2cBus(bool is_alt_bus=false); + TwoWire *GetI2cBus(bool is_alt_bus = false); + private: I2cModel *_i2c_model; ///< Pointer to an I2C model object I2cHardware *_i2c_bus_default; ///< Pointer to the default I2C bus diff --git a/src/provisioning/sdcard/ws_sdcard.cpp b/src/provisioning/sdcard/ws_sdcard.cpp index 8fdea501a..82a3913d4 100644 --- a/src/provisioning/sdcard/ws_sdcard.cpp +++ b/src/provisioning/sdcard/ws_sdcard.cpp @@ -148,7 +148,7 @@ bool ws_sdcard::InitDS3231() { 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"); + WS_DEBUG_PRINTLN("[SD] Error: Failed to initialize DS3231 RTC on WIRE1"); delete _rtc_ds3231; return false; }