基于ESP-IDF4.1

 1 /*
2 FAT文件系统存储文件,使用磨损均衡库wear-leveling
3 */
4
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include "esp_vfs.h"
9 #include "esp_vfs_fat.h"
10 #include "esp_system.h"
11
12 static const char *TAG = "example";
13
14 // 磨损均衡处理实例
15 static wl_handle_t s_wl_handle = WL_INVALID_HANDLE;
16
17 // 分区挂在路径
18 const char *base_path = "/spiflash";
19
20 void app_main(void)
21 {
22 ESP_LOGI(TAG, "Mounting FAT filesystem");
23
24 // 命名设备分区,定义base_path。如果是新分区并且没有格式化过则允许格式化分区
25 const esp_vfs_fat_mount_config_t mount_config = {
26 .max_files = 4,
27 .format_if_mount_failed = true,
28 .allocation_unit_size = CONFIG_WL_SECTOR_SIZE
29 };
30 esp_err_t err = esp_vfs_fat_spiflash_mount(base_path, "storage", &mount_config, &s_wl_handle);
31 if (err != ESP_OK) {
32 ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
33 return;
34 }
35 ESP_LOGI(TAG, "Opening file");
36 FILE *f = fopen("/spiflash/hello.txt", "wb"); // 读写或建立一个二进制文件
37 if (f == NULL) {
38 ESP_LOGE(TAG, "Failed to open file for writing");
39 return;
40 }
41 fprintf(f, "written using ESP-IDF %s\n", esp_get_idf_version());
42 fclose(f);
43 ESP_LOGI(TAG, "File written");
44
45 // 打开
46 ESP_LOGI(TAG, "Reading file");
47 f = fopen("/spiflash/hello.txt", "rb"); // 读写打开一个二进制文件
48 if (f == NULL) {
49 ESP_LOGE(TAG, "Failed to open file for reading");
50 return;
51 }
52 char line[128];
53 // 从指定的流 f 读取一行,并把它存储在 line 所指向的字符串内
54 fgets(line, sizeof(line), f);
55 fclose(f);
56 // 查找换行符
57 char *pos = strchr(line, '\n');
58 if (pos) {
59 *pos = '\0'; //放置一个空字符串
60 }
61 ESP_LOGI(TAG, "Read from file: '%s'", line);
62
63 // 卸载FAT文件系统
64 ESP_LOGI(TAG, "Unmounting FAT filesystem");
65 ESP_ERROR_CHECK( esp_vfs_fat_spiflash_unmount(base_path, s_wl_handle));
66
67 ESP_LOGI(TAG, "Done");
68 }

原文:https://gitee.com/EspressifSystems/esp-idf

最新文章

  1. Outline of Apache Jena Notes
  2. Wordpress如何开启用户注册功能
  3. [ActionScript 3.0] 喷泉效果
  4. dl,dt,dd,ul,li,ol区别
  5. sping配置文件中引入properties文件方式
  6. delphi Sender和Tag的用法1
  7. Android短信彩信收发流程(应用层)
  8. 【转】使用spring @Scheduled注解执行定时任务
  9. f.lux亮度自动改变
  10. DbContext的命名空间
  11. HDU 1501 &amp; POJ 2192 Zipper(dp记忆化搜索)
  12. Java根据字节数据判断文件类型
  13. Swift UI学习UITableView and protocol use
  14. PAT (Advanced Level) 1014. Waiting in Line (30)
  15. Apache处理请求步骤及过程
  16. 从壹开始前后端分离 39 || 想创建自己的dotnet模板么?看这里
  17. Jetson TX1 install Opencv3
  18. ZJOI 2019 游记
  19. 程序员必知的8大排序(二)-------简单选择排序,堆排序(java实现)
  20. ACMG遗传变异分类标准与指南

热门文章

  1. MyBatis执行器
  2. [leetcode] 36. 有效的数独(Java)
  3. THINKPHP_(2)_TP模型的多表关联查询和多表字段的关键字搜索。
  4. CVPR2020行人重识别算法论文解读
  5. 3D Cube计算引擎加速运算
  6. PyTorch全连接ReLU网络
  7. [源码解析] 深度学习分布式训练框架 horovod (2) --- 从使用者角度切入
  8. pytest skip的使用
  9. 由一次PasswordBox密码绑定引发的疑问 ---&gt; WPF中的附加属性的定义,以及使用。
  10. 【题解】ball 数论