您尚未登录。

楼主 #1 2018-09-18 08:17:00

LAQ
会员
注册时间: 2017-09-20
已发帖子: 38
积分: 38

spiffs文件系统

请教各位,在使用ESP32的spiffs文件系统的时候,怎样把本地的一个.txt文件写到spi flash里面?

离线

#2 2018-09-18 08:43:52

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: spiffs文件系统

/* SPIFFS filesystem example.
   This example code is in the Public Domain (or CC0 licensed, at your option.)
   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/

#include <stdio.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include "esp_err.h"
#include "esp_log.h"
#include "esp_spiffs.h"

static const char *TAG = "example";

void app_main(void)
{
    ESP_LOGI(TAG, "Initializing SPIFFS");
    
    esp_vfs_spiffs_conf_t conf = {
      .base_path = "/spiffs",
      .partition_label = NULL,
      .max_files = 5,
      .format_if_mount_failed = true
    };
    
    // Use settings defined above to initialize and mount SPIFFS filesystem.
    // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
    esp_err_t ret = esp_vfs_spiffs_register(&conf);

    if (ret != ESP_OK) {
        if (ret == ESP_FAIL) {
            ESP_LOGE(TAG, "Failed to mount or format filesystem");
        } else if (ret == ESP_ERR_NOT_FOUND) {
            ESP_LOGE(TAG, "Failed to find SPIFFS partition");
        } else {
            ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
        }
        return;
    }
    
    size_t total = 0, used = 0;
    ret = esp_spiffs_info(NULL, &total, &used);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
    } else {
        ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
    }

    // Use POSIX and C standard library functions to work with files.
    // First create a file.
    ESP_LOGI(TAG, "Opening file");
    FILE* f = fopen("/spiffs/hello.txt", "w");
    if (f == NULL) {
        ESP_LOGE(TAG, "Failed to open file for writing");
        return;
    }
    fprintf(f, "Hello World!\n");
    fclose(f);
    ESP_LOGI(TAG, "File written");

    // Check if destination file exists before renaming
    struct stat st;
    if (stat("/spiffs/foo.txt", &st) == 0) {
        // Delete it if it exists
        unlink("/spiffs/foo.txt");
    }

    // Rename original file
    ESP_LOGI(TAG, "Renaming file");
    if (rename("/spiffs/hello.txt", "/spiffs/foo.txt") != 0) {
        ESP_LOGE(TAG, "Rename failed");
        return;
    }

    // Open renamed file for reading
    ESP_LOGI(TAG, "Reading file");
    f = fopen("/spiffs/foo.txt", "r");
    if (f == NULL) {
        ESP_LOGE(TAG, "Failed to open file for reading");
        return;
    }
    char line[64];
    fgets(line, sizeof(line), f);
    fclose(f);
    // strip newline
    char* pos = strchr(line, '\n');
    if (pos) {
        *pos = '\0';
    }
    ESP_LOGI(TAG, "Read from file: '%s'", line);

    // All done, unmount partition and disable SPIFFS
    esp_vfs_spiffs_unregister(NULL);
    ESP_LOGI(TAG, "SPIFFS unmounted");
}

把 spiffs 装载到虚拟文件系统(VFS), 然后就可以通过POSIX 接口读写删除文件了.

https://github.com/espressif/esp-idf/blob/master/examples/storage/spiffs/main/spiffs_example_main.c





在线

楼主 #3 2018-09-18 09:01:53

LAQ
会员
注册时间: 2017-09-20
已发帖子: 38
积分: 38

Re: spiffs文件系统

FILE* f = fopen("/spiffs/hello.txt", "w");怎么我在/spiffs下找不到hello.txt这个文件的?

离线

#4 2018-09-18 09:06:29

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: spiffs文件系统

你用什么方法找 hello.txt 这个文件的?





在线

楼主 #5 2018-09-18 11:07:08

LAQ
会员
注册时间: 2017-09-20
已发帖子: 38
积分: 38

Re: spiffs文件系统

是不是用电脑浏览这个文件夹是看不到hello.txt的呢?

离线

#6 2018-09-18 11:21:53

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: spiffs文件系统

电脑浏览?你实现了 http server,并且挂载了 spiffs ?





在线

#7 2018-09-19 16:56:01

Pese
会员
注册时间: 2017-11-28
已发帖子: 13
积分: 8

Re: spiffs文件系统

可以在pc上,生成spiffs的格式的镜像文件,然后直接通过命令写到flash指定地址

离线

#8 2018-09-19 16:57:16

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: spiffs文件系统

Pese 说:

可以在pc上,生成spiffs的格式的镜像文件,然后直接通过命令写到flash指定地址

请教如何在 PC上生成 spiffs 镜像呢?





在线

#9 2018-09-21 14:13:38

Pese
会员
注册时间: 2017-11-28
已发帖子: 13
积分: 8

Re: spiffs文件系统

git上的mkspiffs

离线

#10 2018-09-21 14:31:26

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: spiffs文件系统

Pese 说:

git上的mkspiffs

受教了,还有这种操作。





在线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn