|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2023 Brian Pugh |
| 3 | + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Unlicense OR CC0-1.0 |
| 6 | + */ |
| 7 | + |
| 8 | +#include <stdio.h> |
| 9 | +#include <string.h> |
| 10 | +#include <sys/stat.h> |
| 11 | +#include <unistd.h> |
| 12 | +#include "esp_err.h" |
| 13 | +#include "esp_log.h" |
| 14 | +#include "esp_system.h" |
| 15 | +#include "esp_littlefs.h" |
| 16 | + |
| 17 | +static const char *TAG = "esp_littlefs"; |
| 18 | + |
| 19 | +void app_main(void) |
| 20 | +{ |
| 21 | + ESP_LOGI(TAG, "Initializing LittleFS"); |
| 22 | + |
| 23 | + esp_vfs_littlefs_conf_t conf = { |
| 24 | + .base_path = "/littlefs", |
| 25 | + .partition_label = "storage", |
| 26 | + .format_if_mount_failed = true, |
| 27 | + .dont_mount = false, |
| 28 | + }; |
| 29 | + |
| 30 | + // Use settings defined above to initialize and mount LittleFS filesystem. |
| 31 | + // Note: esp_vfs_littlefs_register is an all-in-one convenience function. |
| 32 | + esp_err_t ret = esp_vfs_littlefs_register(&conf); |
| 33 | + |
| 34 | + if (ret != ESP_OK) { |
| 35 | + if (ret == ESP_FAIL) { |
| 36 | + ESP_LOGE(TAG, "Failed to mount or format filesystem"); |
| 37 | + } else if (ret == ESP_ERR_NOT_FOUND) { |
| 38 | + ESP_LOGE(TAG, "Failed to find LittleFS partition"); |
| 39 | + } else { |
| 40 | + ESP_LOGE(TAG, "Failed to initialize LittleFS (%s)", esp_err_to_name(ret)); |
| 41 | + } |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + size_t total = 0, used = 0; |
| 46 | + ret = esp_littlefs_info(conf.partition_label, &total, &used); |
| 47 | + if (ret != ESP_OK) { |
| 48 | + ESP_LOGE(TAG, "Failed to get LittleFS partition information (%s)", esp_err_to_name(ret)); |
| 49 | + esp_littlefs_format(conf.partition_label); |
| 50 | + } else { |
| 51 | + ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used); |
| 52 | + } |
| 53 | + |
| 54 | + // Use POSIX and C standard library functions to work with files. |
| 55 | + // First create a file. |
| 56 | + ESP_LOGI(TAG, "Opening file"); |
| 57 | + FILE *f = fopen("/littlefs/hello.txt", "w"); |
| 58 | + if (f == NULL) { |
| 59 | + ESP_LOGE(TAG, "Failed to open file for writing"); |
| 60 | + return; |
| 61 | + } |
| 62 | + fprintf(f, "Hello World!\n"); |
| 63 | + fclose(f); |
| 64 | + ESP_LOGI(TAG, "File written"); |
| 65 | + |
| 66 | + // Check if destination file exists before renaming |
| 67 | + struct stat st; |
| 68 | + if (stat("/littlefs/foo.txt", &st) == 0) { |
| 69 | + // Delete it if it exists |
| 70 | + unlink("/littlefs/foo.txt"); |
| 71 | + } |
| 72 | + |
| 73 | + // Rename original file |
| 74 | + ESP_LOGI(TAG, "Renaming file"); |
| 75 | + if (rename("/littlefs/hello.txt", "/littlefs/foo.txt") != 0) { |
| 76 | + ESP_LOGE(TAG, "Rename failed"); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + // Open renamed file for reading |
| 81 | + ESP_LOGI(TAG, "Reading file"); |
| 82 | + f = fopen("/littlefs/foo.txt", "r"); |
| 83 | + if (f == NULL) { |
| 84 | + ESP_LOGE(TAG, "Failed to open file for reading"); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + char line[128]; |
| 89 | + fgets(line, sizeof(line), f); |
| 90 | + fclose(f); |
| 91 | + // strip newline |
| 92 | + char*pos = strchr(line, '\n'); |
| 93 | + if (pos) { |
| 94 | + *pos = '\0'; |
| 95 | + } |
| 96 | + ESP_LOGI(TAG, "Read from file: '%s'", line); |
| 97 | + |
| 98 | + ESP_LOGI(TAG, "Reading from flashed filesystem example.txt"); |
| 99 | + f = fopen("/littlefs/example.txt", "r"); |
| 100 | + if (f == NULL) { |
| 101 | + ESP_LOGE(TAG, "Failed to open file for reading"); |
| 102 | + return; |
| 103 | + } |
| 104 | + fgets(line, sizeof(line), f); |
| 105 | + fclose(f); |
| 106 | + // strip newline |
| 107 | + pos = strchr(line, '\n'); |
| 108 | + if (pos) { |
| 109 | + *pos = '\0'; |
| 110 | + } |
| 111 | + ESP_LOGI(TAG, "Read from file: '%s'", line); |
| 112 | + |
| 113 | + // All done, unmount partition and disable LittleFS |
| 114 | + esp_vfs_littlefs_unregister(conf.partition_label); |
| 115 | + ESP_LOGI(TAG, "LittleFS unmounted"); |
| 116 | +} |
0 commit comments