fatfsgen_example_main.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "esp_vfs.h"
  10. #include "esp_vfs_fat.h"
  11. #include "sdkconfig.h"
  12. #if CONFIG_EXAMPLE_FATFS_MODE_READ_ONLY
  13. #define EXAMPLE_FATFS_MODE_READ_ONLY true
  14. #else
  15. #define EXAMPLE_FATFS_MODE_READ_ONLY false
  16. #endif
  17. #if CONFIG_FATFS_LFN_NONE
  18. #define EXAMPLE_FATFS_LONG_NAMES false
  19. #else
  20. #define EXAMPLE_FATFS_LONG_NAMES true
  21. #endif
  22. static const char *TAG = "example";
  23. // Mount path for the partition
  24. const char *base_path = "/spiflash";
  25. // Handle of the wear levelling library instance
  26. static wl_handle_t s_wl_handle = WL_INVALID_HANDLE;
  27. void app_main(void)
  28. {
  29. ESP_LOGI(TAG, "Mounting FAT filesystem");
  30. // To mount device we need name of device partition, define base_path
  31. // and allow format partition in case if it is new one and was not formatted before
  32. const esp_vfs_fat_mount_config_t mount_config = {
  33. .max_files = 4,
  34. .format_if_mount_failed = false,
  35. .allocation_unit_size = CONFIG_WL_SECTOR_SIZE
  36. };
  37. esp_err_t err;
  38. if (EXAMPLE_FATFS_MODE_READ_ONLY){
  39. err = esp_vfs_fat_spiflash_mount_ro(base_path, "storage", &mount_config);
  40. } else {
  41. err = esp_vfs_fat_spiflash_mount_rw_wl(base_path, "storage", &mount_config, &s_wl_handle);
  42. }
  43. if (err != ESP_OK) {
  44. ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
  45. return;
  46. }
  47. char line[128];
  48. char *device_filename;
  49. if (EXAMPLE_FATFS_LONG_NAMES){
  50. device_filename = "/spiflash/innerbutverylongname.txt";
  51. } else {
  52. device_filename = "/spiflash/inner.txt";
  53. }
  54. if (!EXAMPLE_FATFS_MODE_READ_ONLY){
  55. // Open file for reading
  56. ESP_LOGI(TAG, "Opening file");
  57. FILE *f;
  58. for(int i = 0; i < CONFIG_EXAMPLE_FATFS_WRITE_COUNT; i++){
  59. f = fopen(device_filename, "wb");
  60. if (f == NULL) {
  61. ESP_LOGE(TAG, "Failed to open file for writing");
  62. return;
  63. }
  64. fprintf(f, "This is written by the device");
  65. fclose(f);
  66. }
  67. ESP_LOGI(TAG, "File written");
  68. // Open file for reading
  69. ESP_LOGI(TAG, "Reading file");
  70. f = fopen(device_filename, "rb");
  71. if (f == NULL) {
  72. ESP_LOGE(TAG, "Failed to open file for reading");
  73. return;
  74. }
  75. fgets(line, sizeof(line), f);
  76. fclose(f);
  77. // strip newline
  78. char *pos = strchr(line, '\n');
  79. if (pos) {
  80. *pos = '\0';
  81. }
  82. ESP_LOGI(TAG, "Read from file: '%s'", line);
  83. }
  84. FILE *f;
  85. char *pos;
  86. ESP_LOGI(TAG, "Reading file");
  87. char *host_filename1;
  88. char *host_filename2;
  89. if (EXAMPLE_FATFS_LONG_NAMES){
  90. host_filename1 = "/spiflash/sublongnames/testlongfilenames.txt";
  91. host_filename2 = "/spiflash/hellolongname.txt";
  92. } else{
  93. host_filename1 = "/spiflash/sub/test.txt";
  94. host_filename2 = "/spiflash/hello.txt";
  95. }
  96. struct stat info;
  97. struct tm timeinfo;
  98. char buffer[32];
  99. if(stat(host_filename1, &info) < 0){
  100. ESP_LOGE(TAG, "Failed to read file stats");
  101. return;
  102. }
  103. localtime_r(&info.st_mtime, &timeinfo);
  104. strftime(buffer, sizeof(buffer), "%Y-%m-%d", &timeinfo);
  105. ESP_LOGI(TAG, "The file '%s' was modified at date: %s", host_filename1, buffer);
  106. if (EXAMPLE_FATFS_MODE_READ_ONLY){
  107. f = fopen(host_filename1, "rb");
  108. } else {
  109. f = fopen(host_filename2, "rb");
  110. }
  111. if (f == NULL) {
  112. ESP_LOGE(TAG, "Failed to open file for reading");
  113. return;
  114. }
  115. fgets(line, sizeof(line), f);
  116. fclose(f);
  117. // strip newline
  118. pos = strchr(line, '\n');
  119. if (pos) {
  120. *pos = '\0';
  121. }
  122. ESP_LOGI(TAG, "Read from file: '%s'", line);
  123. // Unmount FATFS
  124. ESP_LOGI(TAG, "Unmounting FAT filesystem");
  125. if (EXAMPLE_FATFS_MODE_READ_ONLY){
  126. ESP_ERROR_CHECK(esp_vfs_fat_spiflash_unmount_ro(base_path, "storage"));
  127. } else {
  128. ESP_ERROR_CHECK(esp_vfs_fat_spiflash_unmount_rw_wl(base_path, s_wl_handle));
  129. }
  130. ESP_LOGI(TAG, "Done");
  131. }