#include #include #include "freertos/FreeRTOS.h" #include "esp_log.h" #include "esp_timer.h" #include "esp_partition.h" #include "esp_vfs_fat.h" #include "wear_levelling.h" #include "sdmmc_cmd.h" #include "driver/sdmmc_host.h" #include "driver/sdspi_host.h" #include "driver/spi_common.h" #include "cits_store.h" static const char *TAG = "cits_store"; /* T-Dongle C5 microSD reader (SPI, 1-bit) — Lilygo official pinout. * Note: SD shares the SPI1 data lines with the optional LCD; we do not drive * the LCD, and CS (23) is SD-exclusive, so there is no conflict. */ #define SD_PIN_MOSI 2 /* SD_CMD */ #define SD_PIN_MISO 7 /* SD_D0 */ #define SD_PIN_CLK 6 /* SD_CLK */ #define SD_PIN_CS 23 /* SD_CS */ #define MOUNT_POINT "/sd" #define ROTATE_US (600ULL * 1000000ULL) /* 10 minutes */ #define FLUSH_LINES 64 /* flush to media every N lines */ static FILE *s_file = NULL; static int64_t s_open_us = 0; static uint64_t s_lines = 0; static bool s_mounted = false; static char s_src[12] = "none"; static char s_path[96]; static wl_handle_t s_wl = -1; static void open_current(void) { if (s_file) { fflush(s_file); fclose(s_file); s_file = NULL; } uint64_t boot_ms = (uint64_t)(esp_timer_get_time() / 1000); snprintf(s_path, sizeof(s_path), "cits_%010llu.csv", (unsigned long long)boot_ms); char full[160]; snprintf(full, sizeof(full), MOUNT_POINT "/" "%s", s_path); s_file = fopen(full, "a"); if (!s_file) { ESP_LOGE(TAG, "fopen failed: %s", full); return; } /* 16 KiB userspace buffer -> far fewer physical media writes */ setvbuf(s_file, NULL, _IOFBF, 16 * 1024); fputs("boot_ms,cls,asdu_id,ver,site_id,phases,phase0_state,station_id,hex\n", s_file); s_open_us = esp_timer_get_time(); ESP_LOGI(TAG, "CSV writing: %s", full); } static bool mount_sd(void) { spi_bus_config_t bus = { .mosi_io_num = SD_PIN_MOSI, .miso_io_num = SD_PIN_MISO, .sclk_io_num = SD_PIN_CLK, .quadwp_io_num = -1, .quadhd_io_num = -1, .max_transfer_sz = 4096, }; esp_err_t e = spi_bus_initialize(SPI2_HOST, &bus, SPI_DMA_CH_AUTO); if (e != ESP_OK) { ESP_LOGW(TAG, "spi_bus_initialize: %s", esp_err_to_name(e)); return false; } sdmmc_host_t host = SDSPI_HOST_DEFAULT(); sdspi_device_config_t slot = SDSPI_DEVICE_CONFIG_DEFAULT(); slot.gpio_cs = SD_PIN_CS; slot.host_id = host.slot; esp_vfs_fat_sdmmc_mount_config_t mc = { .format_if_mount_failed = true, .max_files = 4, .allocation_unit_size = 16 * 1024, }; sdmmc_card_t *card = NULL; e = esp_vfs_fat_sdspi_mount(MOUNT_POINT, &host, &slot, &mc, &card); if (e == ESP_OK) { snprintf(s_src, sizeof(s_src), "sd-card"); return true; } ESP_LOGW(TAG, "SD mount failed (%s) - no card or bad card", esp_err_to_name(e)); return false; } static bool mount_flash(void) { const esp_partition_t *part = esp_partition_find_first( ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, "storage"); if (!part) { ESP_LOGW(TAG, "no flash 'storage' partition found"); return false; } esp_vfs_fat_mount_config_t mc = { .format_if_mount_failed = true, .max_files = 4, .allocation_unit_size = 16 * 1024, }; esp_err_t e = esp_vfs_fat_spiflash_mount_rw_wl(MOUNT_POINT, "storage", &mc, &s_wl); if (e == ESP_OK) { snprintf(s_src, sizeof(s_src), "flash"); return true; } ESP_LOGW(TAG, "flash mount failed (%s)", esp_err_to_name(e)); return false; } esp_err_t cits_store_init(void) { if (s_mounted) { return ESP_OK; } if (!mount_sd()) { if (!mount_flash()) { ESP_LOGE(TAG, "NO STORAGE (SD + flash both failed) - serial-only logging"); return ESP_FAIL; } } s_mounted = true; ESP_LOGI(TAG, "storage backend: %s", s_src); open_current(); return (s_file != NULL) ? ESP_OK : ESP_FAIL; } esp_err_t cits_store_log(const char *record) { if (!s_file) { return ESP_FAIL; } if (esp_timer_get_time() - s_open_us >= ROTATE_US) { open_current(); if (!s_file) { return ESP_FAIL; } } fputs(record, s_file); fputc('\n', s_file); s_lines++; if ((s_lines & (FLUSH_LINES - 1)) == 0) { fflush(s_file); } return ESP_OK; } void cits_store_rotate(void) { if (!s_mounted) { return; } open_current(); } bool cits_store_ready(void) { return s_mounted && (s_file != NULL); } const char *cits_store_source(void) { return s_src; } const char *cits_store_path(void) { char full[160]; snprintf(full, sizeof(full), MOUNT_POINT "/" "%s", s_path); /* return a stable static copy */ static char p[160]; snprintf(p, sizeof(p), MOUNT_POINT "/" "%s", s_path); (void)full; return p; } uint64_t cits_store_count(void) { return s_lines; }