SPIFFS.c 6.3 KB

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