From 095a52fea7cf678ff0b4635a0ec0939ea5abf94d Mon Sep 17 00:00:00 2001 From: Thaddeus Hughes Date: Mon, 29 Dec 2025 15:49:45 -0600 Subject: [PATCH] better logging --- .cproject | 2 + .externalToolBuilders/OTA SC-F001.launch | 1 + .settings/org.eclipse.cdt.core.prefs | 6 +++ main/main.c | 13 ++++-- main/storage.c | 59 ++++++++++++++++++++++-- main/storage.h | 7 +++ main/webserver.c | 57 +++++++++++++++++++---- partitions.csv | 6 +-- sdkconfig | 6 +-- sdkconfig.old | 6 +-- 10 files changed, 138 insertions(+), 25 deletions(-) create mode 100644 .settings/org.eclipse.cdt.core.prefs diff --git a/.cproject b/.cproject index 3fd33f2..5fc1a09 100644 --- a/.cproject +++ b/.cproject @@ -13,4 +13,6 @@ + + \ No newline at end of file diff --git a/.externalToolBuilders/OTA SC-F001.launch b/.externalToolBuilders/OTA SC-F001.launch index 3c0455d..6059a66 100644 --- a/.externalToolBuilders/OTA SC-F001.launch +++ b/.externalToolBuilders/OTA SC-F001.launch @@ -1,6 +1,7 @@ + diff --git a/.settings/org.eclipse.cdt.core.prefs b/.settings/org.eclipse.cdt.core.prefs new file mode 100644 index 0000000..c8ec5df --- /dev/null +++ b/.settings/org.eclipse.cdt.core.prefs @@ -0,0 +1,6 @@ +doxygen/doxygen_new_line_after_brief=true +doxygen/doxygen_use_brief_tag=false +doxygen/doxygen_use_javadoc_tags=true +doxygen/doxygen_use_pre_tag=false +doxygen/doxygen_use_structural_commands=false +eclipse.preferences.version=1 diff --git a/main/main.c b/main/main.c index 55fa6cf..d70b338 100644 --- a/main/main.c +++ b/main/main.c @@ -120,6 +120,8 @@ void app_main(void) { send_log(); + + //write_dummy_log_1(); // Check wake reasons // If button held, we stay #woke @@ -147,9 +149,14 @@ void app_main(void) { if (webserver_init() != ESP_OK) ESP_LOGE(TAG, "WEBSERVER FAILED"); /*** MAIN LOOP ***/ - TickType_t xLastWakeTime = xTaskGetTickCount(); const TickType_t xFrequency = pdMS_TO_TICKS(100); + + while(true) { + ESP_LOGI(TAG, "TICK"); + vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(1000)); + esp_task_wdt_reset(); + } while(true) { vTaskDelayUntil(&xLastWakeTime, xFrequency); @@ -183,7 +190,7 @@ void app_main(void) { break; case STATE_UNDO_JACK: case STATE_UNDO_JACK_START: - // assume it's running + // it's running the jack, but undoing send_log(); driveLEDs(LED_STATE_CANCELLING); if (i2c_get_button_tripped(0)) { @@ -193,7 +200,7 @@ void app_main(void) { break; default: - // assume it's running in every other case + // it's running in every other case send_log(); driveLEDs(LED_STATE_DRIVING); if (i2c_get_button_tripped(0)) { diff --git a/main/storage.c b/main/storage.c index 9f0d811..7c36e36 100644 --- a/main/storage.c +++ b/main/storage.c @@ -48,13 +48,19 @@ const char* parameter_names[NUM_PARAMS] = { // Partition pointer static const esp_partition_t *storage_partition = NULL; -// Log head tracking -static uint32_t log_head_index = 0; -static bool log_initialized = false; // Calculate offset for log area (after parameters sector) #define LOG_START_OFFSET FLASH_SECTOR_SIZE +// Log head tracking +static uint32_t log_head_index = 0; +static uint32_t log_tail_index = 0; +uint32_t get_log_head() { return LOG_START_OFFSET + (log_head_index * LOG_ENTRY_SIZE); } +uint32_t get_log_tail() { return LOG_START_OFFSET + (log_tail_index * LOG_ENTRY_SIZE); } +uint32_t get_log_offset() { return LOG_START_OFFSET; } +static bool log_initialized = false; + + // ============================================================================ // PARAMETER FUNCTIONS // ============================================================================ @@ -283,6 +289,14 @@ esp_err_t write_log(char* entry) { err = esp_partition_erase_range(storage_partition, next_sector * FLASH_SECTOR_SIZE, FLASH_SECTOR_SIZE); + + log_tail_index = (next_sector)*FLASH_SECTOR_SIZE/LOG_ENTRY_SIZE; + if (log_tail_index >= max_entries) + log_tail_index = 0; + + + ESP_LOGI(TAG, "Tail/Head are now %ld/%ld", (long)log_tail_index, (long)log_head_index); + if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to erase sector: %s", esp_err_to_name(err)); return ESP_FAIL; @@ -313,6 +327,45 @@ esp_err_t write_log(char* entry) { return ESP_OK; } +esp_err_t write_dummy_log_1() { + log_head_index = 0; + log_tail_index = 0; + uint32_t log_area_end = storage_partition->size; + uint32_t max_entries = (log_area_end - LOG_START_OFFSET) / LOG_ENTRY_SIZE; + for (uint32_t i=0; i>24,i>>16,i>>8,i>>0}; + write_log(entry); + } + return ESP_OK; +} + +esp_err_t write_dummy_log_2() { + log_head_index = 56; + log_tail_index = 105; + uint32_t log_area_end = storage_partition->size; + uint32_t max_entries = (log_area_end - LOG_START_OFFSET) / LOG_ENTRY_SIZE; + for (uint32_t i=0; i>24,i>>16,i>>8,i>>0}; + write_log(entry); + } + return ESP_OK; +} + +esp_err_t write_dummy_log_3() { + log_head_index = 105; + log_tail_index = 34; + uint32_t log_area_end = storage_partition->size; + uint32_t max_entries = (log_area_end - LOG_START_OFFSET) / LOG_ENTRY_SIZE; + for (uint32_t i=0; i>24,i>>16,i>>8,i>>0}; + write_log(entry); + } + return ESP_OK; +} + void storage_deinit(void) { storage_partition = NULL; log_initialized = false; diff --git a/main/storage.h b/main/storage.h index 303d7f9..fd33f4f 100644 --- a/main/storage.h +++ b/main/storage.h @@ -131,6 +131,13 @@ param_value_t get_param_default(param_idx_t id); esp_err_t commit_params(); +uint32_t get_log_head(); +uint32_t get_log_tail(); +uint32_t get_log_offset(); + +esp_err_t write_dummy_log_1(); +esp_err_t write_dummy_log_2(); +esp_err_t write_dummy_log_3(); #define LOG_ENTRY_SIZE 32 diff --git a/main/webserver.c b/main/webserver.c index 27d2cd0..b602868 100644 --- a/main/webserver.c +++ b/main/webserver.c @@ -33,7 +33,7 @@ #define HOSTNAME "sc.local" -#define SOFT_AP_SSID "stockcropper" +#define SOFT_AP_SSID "sc_main" #define SOFT_AP_PASSWORD "stockcropper" #define SERVER_PORT 80 @@ -62,29 +62,64 @@ static esp_err_t log_get_handler(httpd_req_t *req) { ESP_LOGE(TAG, "Storage partition not found"); return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Storage partition not found"); } + + int32_t start = get_log_tail(); + int32_t end = get_log_head(); + int32_t total_size = end - start; + int32_t offset = start; + if (start >= end) { + total_size = storage_partition->size - start + end - get_log_offset(); + } + + ESP_LOGI(TAG, "start/end: %ld/%ld -> %ld", (long)start, (long)end, (long)total_size); - size_t total_size = storage_partition->size; + //size_t total_size = storage_partition->size; char len_str[16]; sprintf(len_str, "%u", (unsigned)total_size); httpd_resp_set_type(req, "application/octet-stream"); httpd_resp_set_hdr(req, "Content-Disposition", "attachment; filename=\"sc_storage.bin\""); httpd_resp_set_hdr(req, "Content-Length", len_str); - - uint8_t buf[1024]; - size_t offset = 0; - while (offset < total_size) { - size_t to_read = MIN(sizeof(buf), total_size - offset); - esp_err_t err = esp_partition_read(storage_partition, offset, buf, to_read); + + if (start >= end) { + ESP_LOGI(TAG, "STARTING"); + while (offset < storage_partition->size) { + // if wrapped around, just go from the start all the way to the end of storage + // then set start = get_log_offset(); + // and continue + size_t to_read = MIN(sizeof(httpBuffer), storage_partition->size - offset); + esp_err_t err = esp_partition_read(storage_partition, offset, httpBuffer, to_read); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to read partition: %s", esp_err_to_name(err)); + return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to read storage"); + } + ESP_LOGI(TAG, "Sending %ld x %d", (long) offset, to_read); + if (httpd_resp_send_chunk(req, (const char *)httpBuffer, to_read) != ESP_OK) { + ESP_LOGE(TAG, "Failed to send chunk"); + return ESP_FAIL; + } + offset += to_read; + } + offset = get_log_offset(); + } + + while (offset < end) { + ESP_LOGI(TAG, "FINISHING"); + // if wrapped around, just go from the start all the way to the end of storage + // then set start = get_log_offset(); + // and continue + size_t to_read = MIN(sizeof(httpBuffer), end - offset); + esp_err_t err = esp_partition_read(storage_partition, offset, httpBuffer, to_read); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to read partition: %s", esp_err_to_name(err)); return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to read storage"); } - if (httpd_resp_send_chunk(req, (const char *)buf, to_read) != ESP_OK) { + ESP_LOGI(TAG, "Sending %ld x %d", (long) offset, to_read); + if (httpd_resp_send_chunk(req, (const char *)httpBuffer, to_read) != ESP_OK) { ESP_LOGE(TAG, "Failed to send chunk"); return ESP_FAIL; } offset += to_read; - } + } // End chunked transfer httpd_resp_send_chunk(req, NULL, 0); @@ -244,6 +279,7 @@ static esp_err_t ota_post_handler(httpd_req_t *req) { return ESP_OK; } + httpd_uri_t uris[] = {{ .uri = "/status", .method = HTTP_GET, @@ -336,6 +372,7 @@ void launchSoftAp() { wifi_config_t wifi_config = { .ap = { + .channel = 8, .ssid = SOFT_AP_SSID, .ssid_len = strlen(SOFT_AP_SSID), .password = SOFT_AP_PASSWORD, diff --git a/partitions.csv b/partitions.csv index 72ad5f7..03c7a3e 100644 --- a/partitions.csv +++ b/partitions.csv @@ -3,6 +3,6 @@ nvs, data, nvs, 0x9000, 0x4000, otadata, data, ota, 0xd000, 0x2000, phy_init, data, phy, 0xf000, 0x1000, -ota_0, app, ota_0, 0x10000, 2M, -ota_1, app, ota_1, 0x210000, 2M, -storage, data, 0x40, 0x410000, 4032K, \ No newline at end of file +ota_0, app, ota_0, 0x10000, 1M, +ota_1, app, ota_1, 0x210000, 1M, +storage, data, 0x40, 0x310000, 32K, \ No newline at end of file diff --git a/sdkconfig b/sdkconfig index bdba70f..cb24599 100644 --- a/sdkconfig +++ b/sdkconfig @@ -354,13 +354,13 @@ CONFIG_ESPTOOLPY_FLASHFREQ_40M=y CONFIG_ESPTOOLPY_FLASHFREQ="40m" # CONFIG_ESPTOOLPY_FLASHSIZE_1MB is not set # CONFIG_ESPTOOLPY_FLASHSIZE_2MB is not set -# CONFIG_ESPTOOLPY_FLASHSIZE_4MB is not set -CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y +CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y +# CONFIG_ESPTOOLPY_FLASHSIZE_8MB is not set # CONFIG_ESPTOOLPY_FLASHSIZE_16MB is not set # CONFIG_ESPTOOLPY_FLASHSIZE_32MB is not set # CONFIG_ESPTOOLPY_FLASHSIZE_64MB is not set # CONFIG_ESPTOOLPY_FLASHSIZE_128MB is not set -CONFIG_ESPTOOLPY_FLASHSIZE="8MB" +CONFIG_ESPTOOLPY_FLASHSIZE="4MB" # CONFIG_ESPTOOLPY_HEADER_FLASHSIZE_UPDATE is not set CONFIG_ESPTOOLPY_BEFORE_RESET=y # CONFIG_ESPTOOLPY_BEFORE_NORESET is not set diff --git a/sdkconfig.old b/sdkconfig.old index 466bad4..bdba70f 100644 --- a/sdkconfig.old +++ b/sdkconfig.old @@ -353,14 +353,14 @@ CONFIG_ESPTOOLPY_FLASHFREQ_40M=y # CONFIG_ESPTOOLPY_FLASHFREQ_20M is not set CONFIG_ESPTOOLPY_FLASHFREQ="40m" # CONFIG_ESPTOOLPY_FLASHSIZE_1MB is not set -CONFIG_ESPTOOLPY_FLASHSIZE_2MB=y +# CONFIG_ESPTOOLPY_FLASHSIZE_2MB is not set # CONFIG_ESPTOOLPY_FLASHSIZE_4MB is not set -# CONFIG_ESPTOOLPY_FLASHSIZE_8MB is not set +CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y # CONFIG_ESPTOOLPY_FLASHSIZE_16MB is not set # CONFIG_ESPTOOLPY_FLASHSIZE_32MB is not set # CONFIG_ESPTOOLPY_FLASHSIZE_64MB is not set # CONFIG_ESPTOOLPY_FLASHSIZE_128MB is not set -CONFIG_ESPTOOLPY_FLASHSIZE="2MB" +CONFIG_ESPTOOLPY_FLASHSIZE="8MB" # CONFIG_ESPTOOLPY_HEADER_FLASHSIZE_UPDATE is not set CONFIG_ESPTOOLPY_BEFORE_RESET=y # CONFIG_ESPTOOLPY_BEFORE_NORESET is not set