#include #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include "esp_log.h" #include "esp_spiffs.h" #include "spiffs.h" static const char *TAG = "example"; /* * Public interface */ static SemaphoreHandle_t rw_mutex = NULL; void spiffs_init() { esp_vfs_spiffs_conf_t conf = { .base_path = SPIFFS_PREFIX, .partition_label = NULL, .max_files = 1, .format_if_mount_failed = true }; // Use settings defined above to initialize and mount SPIFFS filesystem. // Note: esp_vfs_spiffs_register is an all-in-one convenience function. ESP_ERROR_CHECK(esp_vfs_spiffs_register(&conf)); #ifdef CONFIG_SPIFFS_CHECK_ON_START ESP_LOGI(TAG, "Performing SPIFFS_check()."); ESP_ERROR_CHECK(esp_spiffs_check(conf.partition_label)); #endif size_t total = 0, used = 0; esp_err_t ret = esp_spiffs_info(conf.partition_label, &total, &used); if (ret != ESP_OK) { ESP_LOGW(TAG, "Failed to get SPIFFS partition information (%s). Formatting...", esp_err_to_name(ret)); esp_spiffs_format(conf.partition_label); ESP_ERROR_CHECK(esp_spiffs_info(conf.partition_label, &total, &used)); } ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used); // Check consistency of reported partiton size info. if (used > total) { ESP_LOGW(TAG, "Number of used bytes cannot be larger than total. Performing SPIFFS_check()."); ret = esp_spiffs_check(conf.partition_label); // Could be also used to mend broken files, to clean unreferenced pages, etc. // More info at https://github.com/pellepl/spiffs/wiki/FAQ#powerlosses-contd-when-should-i-run-spiffs_check if (ret != ESP_OK) { ESP_LOGE(TAG, "SPIFFS_check() failed (%s)", esp_err_to_name(ret)); } else { ESP_LOGI(TAG, "SPIFFS_check() successful"); } } rw_mutex = xSemaphoreCreateMutex(); if (rw_mutex == NULL) { ESP_LOGE(TAG, "Couldn't create Read/Write mutex, SPIFFS operations will crash"); } } void spiffs_write(const char* filename, const char* data, int data_len) { if (xSemaphoreTake(rw_mutex, 100) == pdFALSE) { return; } int outf = open(filename, O_WRONLY | O_CREAT | O_TRUNC); if (outf < 0) { ESP_LOGE(TAG, "Couldn't open file \"%s\" for writing!", filename); xSemaphoreGive(rw_mutex); return; } int length = write(outf, data, data_len); for (int tries = 0; length < data_len && tries < 3; tries++) { ESP_LOGW(TAG, "Wrote only %d bytes to \"%s\", trying again...", length, filename); vTaskDelay(pdMS_TO_TICKS(50)); length = write(outf, data, data_len); } close(outf); if (length < 0) { ESP_LOGE(TAG, "Wrote only %d/%d bytes for \"%s\"", length, data_len, filename); } else { ESP_LOGD(TAG, "Wrote %d bytes from \"%s\"", length, filename); } xSemaphoreGive(rw_mutex); } static int readers = 0; int spiffs_read(const char* filename, char** data) { if (readers == 0) { if (xSemaphoreTake(rw_mutex, 100) == pdTRUE) { vTaskDelay(pdMS_TO_TICKS(150)); } else { *data = NULL; return 0; } } readers++; int inf = open(filename, O_RDONLY); if (inf < 0) { ESP_LOGW(TAG, "Couldn't read from file \"%s\"!", filename); readers--; if (readers == 0) xSemaphoreGive(rw_mutex); return 0; } struct stat buf; if (fstat(inf, &buf) < 0) { close(inf); ESP_LOGE(TAG, "Couldn't read stat of file \"%s\"!", filename); readers--; if (readers == 0) xSemaphoreGive(rw_mutex); return 0; } int length = buf.st_size; *data = (char*)malloc(length); read(inf, *data, length); close(inf); ESP_LOGD(TAG, "read %d bytes from \"%s\"", length, filename); readers--; if (readers == 0) xSemaphoreGive(rw_mutex); return length; }