SPIFFS.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include <stdio.h>
  2. #include "SPIFFS.h"
  3. #include <string.h>
  4. #include <sys/unistd.h>
  5. #include <sys/stat.h>
  6. #include "esp_err.h"
  7. #include "esp_log.h"
  8. #include "esp_spiffs.h"
  9. static const char *TAG = "user_spiffs";
  10. #include "esp_attr.h"
  11. RTC_FAST_ATTR Machine_info_t Machine_info;
  12. RTC_FAST_ATTR Node *Send_list = NULL; // 发送数据链表
  13. void spiffs_init(void)
  14. {
  15. esp_vfs_spiffs_conf_t conf = {
  16. .base_path = "/spiffs",
  17. .partition_label = NULL,
  18. .max_files = 5,
  19. .format_if_mount_failed = true};
  20. // Use settings defined above to initialize and mount SPIFFS filesystem.
  21. // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
  22. esp_err_t ret = esp_vfs_spiffs_register(&conf);
  23. if (ret != ESP_OK)
  24. {
  25. if (ret == ESP_FAIL)
  26. {
  27. ESP_LOGE(TAG, "Failed to mount or format filesystem");
  28. }
  29. else if (ret == ESP_ERR_NOT_FOUND)
  30. {
  31. ESP_LOGE(TAG, "Failed to find SPIFFS partition");
  32. }
  33. else
  34. {
  35. ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
  36. }
  37. return;
  38. }
  39. size_t total = 0, used = 0;
  40. ret = esp_spiffs_info(conf.partition_label, &total, &used);
  41. if (ret != ESP_OK)
  42. {
  43. ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s). Formatting...", esp_err_to_name(ret));
  44. esp_spiffs_format(conf.partition_label);
  45. return;
  46. }
  47. else
  48. {
  49. ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
  50. }
  51. // Check consistency of reported partiton size info.
  52. if (used > total)
  53. {
  54. ESP_LOGW(TAG, "Number of used bytes cannot be larger than total. Performing SPIFFS_check().");
  55. ret = esp_spiffs_check(conf.partition_label);
  56. // Could be also used to mend broken files, to clean unreferenced pages, etc.
  57. // More info at https://github.com/pellepl/spiffs/wiki/FAQ#powerlosses-contd-when-should-i-run-spiffs_check
  58. if (ret != ESP_OK)
  59. {
  60. ESP_LOGE(TAG, "SPIFFS_check() failed (%s)", esp_err_to_name(ret));
  61. return;
  62. }
  63. else
  64. {
  65. ESP_LOGI(TAG, "SPIFFS_check() successful");
  66. }
  67. }
  68. }
  69. void spiffs_write(Machine_info_t *info)
  70. {
  71. ESP_LOGI(TAG, "File written");
  72. FILE *f = fopen("/spiffs/yc_data.bin", "w");
  73. if (f == NULL)
  74. {
  75. ESP_LOGE(TAG, "Failed to open file for writing");
  76. return;
  77. }
  78. fwrite(info, sizeof(Machine_info_t), 1, f);
  79. fclose(f);
  80. }
  81. void spiffs_read(Machine_info_t *info)
  82. {
  83. ESP_LOGI(TAG, "Reading file");
  84. FILE *f = fopen("/spiffs/yc_data.bin", "r");
  85. if (f == NULL)
  86. {
  87. ESP_LOGE(TAG, "Failed to open file for reading");
  88. return;
  89. }
  90. fread(info, sizeof(Machine_info_t), 1, f);
  91. fclose(f);
  92. }
  93. void left_spiffs_write(uint8_t *buffer, unsigned int size)
  94. {
  95. // ESP_LOGI(TAG, "left File written");
  96. FILE *f = fopen("/spiffs/left_display.bin", "w");
  97. if (f == NULL)
  98. {
  99. printf("Failed to open file for writing");
  100. return;
  101. }
  102. size_t bytes_written = 0;
  103. size_t bytes_to_write = size;
  104. while (bytes_written < size)
  105. {
  106. size_t write_result = fwrite(buffer + bytes_written, 1, bytes_to_write, f);
  107. if (write_result < 0)
  108. {
  109. printf("Error writing to file: left_display.bin\r\n");
  110. fclose(f);
  111. return;
  112. }
  113. bytes_written += write_result;
  114. bytes_to_write -= write_result;
  115. }
  116. fclose(f);
  117. }
  118. void left_spiffs_read(uint8_t *buffer, unsigned int size)
  119. {
  120. ESP_LOGI(TAG, "Reading left file");
  121. FILE *f = fopen("/spiffs/left_display.bin", "r");
  122. if (f == NULL)
  123. {
  124. printf("Failed to open file for reading");
  125. return;
  126. }
  127. size_t bytes_read = 0;
  128. size_t bytes_to_read = size;
  129. while (bytes_read < size)
  130. {
  131. size_t read_result = fread(buffer + bytes_read, 1, bytes_to_read, f);
  132. if (read_result < 0)
  133. {
  134. printf("Error reading from file: left_display.bin\r\n");
  135. fclose(f);
  136. return;
  137. }
  138. bytes_read += read_result;
  139. bytes_to_read -= read_result;
  140. if (read_result == 0)
  141. {
  142. // 文件已经读取完毕
  143. break;
  144. }
  145. }
  146. fclose(f);
  147. }
  148. void right_spiffs_write(uint8_t *buffer, unsigned int size)
  149. {
  150. ESP_LOGI(TAG, "right File written");
  151. FILE *f = fopen("/spiffs/right_display.bin", "w");
  152. if (f == NULL)
  153. {
  154. printf("Failed to open file for writing\r\n");
  155. return;
  156. }
  157. size_t bytes_written = 0;
  158. size_t bytes_to_write = size;
  159. while (bytes_written < size)
  160. {
  161. size_t write_result = fwrite(buffer + bytes_written, 1, bytes_to_write, f);
  162. if (write_result < 0)
  163. {
  164. printf("Error writing to file: left_display.bin\r\n");
  165. fclose(f);
  166. return;
  167. }
  168. bytes_written += write_result;
  169. bytes_to_write -= write_result;
  170. }
  171. fclose(f);
  172. }
  173. void right_spiffs_read(uint8_t *buffer, unsigned int size)
  174. {
  175. ESP_LOGI(TAG, "Reading right file");
  176. FILE *f = fopen("/spiffs/right_display.bin", "r");
  177. if (f == NULL)
  178. {
  179. printf("Failed to open file for reading");
  180. return;
  181. }
  182. size_t bytes_read = 0;
  183. size_t bytes_to_read = size;
  184. while (bytes_read < size)
  185. {
  186. size_t read_result = fread(buffer + bytes_read, 1, bytes_to_read, f);
  187. if (read_result < 0)
  188. {
  189. printf("Error reading from file: right_display.bin\r\n");
  190. fclose(f);
  191. return;
  192. }
  193. bytes_read += read_result;
  194. bytes_to_read -= read_result;
  195. if (read_result == 0)
  196. {
  197. // 文件已经读取完毕
  198. break;
  199. }
  200. }
  201. fclose(f);
  202. }
  203. size_t spiffs_read_powerOn(Machine_info_t *info)
  204. {
  205. size_t ret;
  206. ESP_LOGI(TAG, "Reading powerOn file");
  207. FILE *f = fopen("/spiffs/yc_data.bin", "r");
  208. if (f == NULL)
  209. {
  210. printf("Failed to open file for reading");
  211. return 0;
  212. }
  213. ret = fread(info, sizeof(Machine_info_t), 1, f); // 返回的不一定是读取的字节数
  214. fclose(f);
  215. // ESP_LOGW(TAG,"ret = %d",ret);
  216. // ESP_LOG_BUFFER_HEX(TAG,info,sizeof(Machine_info_t));
  217. return ret;
  218. }