Skip to content

FAT on SPI Flash Library #1809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ set(LIBRARY_SRCS
libraries/DNSServer/src/DNSServer.cpp
libraries/EEPROM/EEPROM.cpp
libraries/ESPmDNS/src/ESPmDNS.cpp
libraries/FFat/src/FFat.cpp
libraries/FS/src/FS.cpp
libraries/FS/src/vfs_api.cpp
libraries/HTTPClient/src/HTTPClient.cpp
Expand Down Expand Up @@ -175,6 +176,7 @@ set(COMPONENT_ADD_INCLUDEDIRS
libraries/DNSServer/src
libraries/ESP32/src
libraries/ESPmDNS/src
libraries/FFat/src
libraries/FS/src
libraries/HTTPClient/src
libraries/NetBIOS/src
Expand Down
5 changes: 5 additions & 0 deletions boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ esp32.menu.PartitionScheme.no_ota.upload.maximum_size=2097152
esp32.menu.PartitionScheme.min_spiffs=Minimal SPIFFS (Large APPS with OTA)
esp32.menu.PartitionScheme.min_spiffs.build.partitions=min_spiffs
esp32.menu.PartitionScheme.min_spiffs.upload.maximum_size=1966080
esp32.menu.PartitionScheme.fatflash=16M Fat
esp32.menu.PartitionScheme.fatflash.build.partitions=ffat

esp32.menu.FlashMode.qio=QIO
esp32.menu.FlashMode.qio.build.flash_mode=dio
Expand All @@ -70,6 +72,9 @@ esp32.menu.FlashSize.4M.build.flash_size=4MB
esp32.menu.FlashSize.2M=2MB (16Mb)
esp32.menu.FlashSize.2M.build.flash_size=2MB
esp32.menu.FlashSize.2M.build.partitions=minimal
esp32.menu.FlashSize.16M=16MB (128Mb)
esp32.menu.FlashSize.16M.build.flash_size=16MB
esp32.menu.FlashSize.16M.build.partitions=ffat

esp32.menu.UploadSpeed.921600=921600
esp32.menu.UploadSpeed.921600.upload.speed=921600
Expand Down
181 changes: 181 additions & 0 deletions libraries/FFat/examples/FFat_Test/FFat_Test.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
#include "FS.h"
#include "FFat.h"

// You only need to format FFat the first time you run a test
#define FORMAT_FFAT true

void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.printf("Listing directory: %s\r\n", dirname);

File root = fs.open(dirname);
if(!root){
Serial.println("- failed to open directory");
return;
}
if(!root.isDirectory()){
Serial.println(" - not a directory");
return;
}

File file = root.openNextFile();
while(file){
if(file.isDirectory()){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print("\tSIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}

void readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\r\n", path);

File file = fs.open(path);
if(!file || file.isDirectory()){
Serial.println("- failed to open file for reading");
return;
}

Serial.println("- read from file:");
while(file.available()){
Serial.write(file.read());
}
}

void writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Writing file: %s\r\n", path);

File file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println("- failed to open file for writing");
return;
}
if(file.print(message)){
Serial.println("- file written");
} else {
Serial.println("- frite failed");
}
}

void appendFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Appending to file: %s\r\n", path);

File file = fs.open(path, FILE_APPEND);
if(!file){
Serial.println("- failed to open file for appending");
return;
}
if(file.print(message)){
Serial.println("- message appended");
} else {
Serial.println("- append failed");
}
}

void renameFile(fs::FS &fs, const char * path1, const char * path2){
Serial.printf("Renaming file %s to %s\r\n", path1, path2);
if (fs.rename(path1, path2)) {
Serial.println("- file renamed");
} else {
Serial.println("- rename failed");
}
}

void deleteFile(fs::FS &fs, const char * path){
Serial.printf("Deleting file: %s\r\n", path);
if(fs.remove(path)){
Serial.println("- file deleted");
} else {
Serial.println("- delete failed");
}
}

void testFileIO(fs::FS &fs, const char * path){
Serial.printf("Testing file I/O with %s\r\n", path);

static uint8_t buf[512];
size_t len = 0;
File file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println("- failed to open file for writing");
return;
}

size_t i;
Serial.print("- writing" );
uint32_t start = millis();
for(i=0; i<2048; i++){
if ((i & 0x001F) == 0x001F){
Serial.print(".");
}
file.write(buf, 512);
}
Serial.println("");
uint32_t end = millis() - start;
Serial.printf(" - %u bytes written in %u ms\r\n", 2048 * 512, end);
file.close();

file = fs.open(path);
start = millis();
end = start;
i = 0;
if(file && !file.isDirectory()){
len = file.size();
size_t flen = len;
start = millis();
Serial.print("- reading" );
while(len){
size_t toRead = len;
if(toRead > 512){
toRead = 512;
}
file.read(buf, toRead);
if ((i++ & 0x001F) == 0x001F){
Serial.print(".");
}
len -= toRead;
}
Serial.println("");
end = millis() - start;
Serial.printf("- %u bytes read in %u ms\r\n", flen, end);
file.close();
} else {
Serial.println("- failed to open file for reading");
}
}

void setup(){
Serial.begin(115200);
Serial.setDebugOutput(true);
if (FORMAT_FFAT) FFat.format();
if(!FFat.begin()){
Serial.println("FFat Mount Failed");
return;
}

Serial.printf("Total space: %10lu\n", FFat.totalBytes());
Serial.printf("Free space: %10lu\n", FFat.freeBytes());
listDir(FFat, "/", 0);
writeFile(FFat, "/hello.txt", "Hello ");
appendFile(FFat, "/hello.txt", "World!\r\n");
readFile(FFat, "/hello.txt");
renameFile(FFat, "/hello.txt", "/foo.txt");
readFile(FFat, "/foo.txt");
deleteFile(FFat, "/foo.txt");
testFileIO(FFat, "/test.txt");
Serial.printf("Free space: %10lu\n", FFat.freeBytes());
deleteFile(FFat, "/test.txt");
Serial.println( "Test complete" );
}

void loop(){

}
9 changes: 9 additions & 0 deletions libraries/FFat/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=FFat
version=1.0
author=Hristo Gochkov, Ivan Grokhtkov, Larry Bernstone
maintainer=Hristo Gochkov <[email protected]>
sentence=ESP32 FAT on Flash File System
paragraph=
category=Data Storage
url=
architectures=esp32
144 changes: 144 additions & 0 deletions libraries/FFat/src/FFat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "vfs_api.h"
extern "C" {
#include "esp_vfs_fat.h"
#include "diskio.h"
#include "diskio_wl.h"
#include "vfs_fat_internal.h"
}
#include "FFat.h"

using namespace fs;

F_Fat::F_Fat(FSImplPtr impl)
: FS(impl)
{}

const esp_partition_t *check_ffat_partition(const char* label)
{
const esp_partition_t* ck_part = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, label);
if (!ck_part) {
log_e("No FAT partition found with label %s", label);
return NULL;
}
return ck_part;
}

bool F_Fat::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles, const char * partitionLabel)
{
if(_wl_handle){
log_w("Already Mounted!");
return true;
}

if (!check_ffat_partition(partitionLabel)) return false;

esp_vfs_fat_mount_config_t conf = {
.format_if_mount_failed = formatOnFail,
.max_files = maxOpenFiles
};
esp_err_t err = esp_vfs_fat_spiflash_mount(basePath, partitionLabel, &conf, &_wl_handle);
if(err){
log_e("Mounting FFat partition failed! Error: %d", err);
return false;
}
_impl->mountpoint(basePath);
return true;
}

void F_Fat::end()
{
if(_wl_handle){
esp_err_t err = esp_vfs_fat_spiflash_unmount(_impl->mountpoint(), _wl_handle);
if(err){
log_e("Unmounting FFat partition failed! Error: %d", err);
return;
}
_wl_handle = NULL;
_impl->mountpoint(NULL);
}
}

bool F_Fat::format(bool full_wipe, char* partitionLabel)
{
esp_err_t result;
if(_wl_handle){
log_w("Already Mounted!");
return false;
}
wl_handle_t temp_handle;
// Attempt to mount to see if there is already data
const esp_partition_t *ffat_partition = check_ffat_partition(partitionLabel);
if (!ffat_partition) return false;
result = wl_mount(ffat_partition, &temp_handle);

if (result == ESP_OK) {
// Wipe disk- quick just wipes the FAT. Full zeroes the whole disk
uint32_t wipe_size = full_wipe ? wl_size(temp_handle) : 16384;
wl_erase_range(temp_handle, 0, wipe_size);
wl_unmount(temp_handle);
}
// Now do a mount with format_if_fail (which it will)
esp_vfs_fat_mount_config_t conf = {
.format_if_mount_failed = true,
.max_files = 1
};
result = esp_vfs_fat_spiflash_mount("/format_ffat", partitionLabel, &conf, &temp_handle);
esp_vfs_fat_spiflash_unmount("/format_ffat", temp_handle);
return result;
}

size_t F_Fat::totalBytes()
{
FATFS *fs;
DWORD free_clust, tot_sect, sect_size;

BYTE pdrv = ff_diskio_get_pdrv_wl(_wl_handle);
char drv[3] = {(char)(48+pdrv), ':', 0};
FRESULT res = f_getfree(drv, &free_clust, &fs);
tot_sect = (fs->n_fatent - 2) * fs->csize;
sect_size = CONFIG_WL_SECTOR_SIZE;
return tot_sect * sect_size;
}

size_t F_Fat::freeBytes()
{

FATFS *fs;
DWORD free_clust, free_sect, sect_size;

BYTE pdrv = ff_diskio_get_pdrv_wl(_wl_handle);
char drv[3] = {(char)(48+pdrv), ':', 0};
FRESULT res = f_getfree(drv, &free_clust, &fs);
free_sect = free_clust * fs->csize;
sect_size = CONFIG_WL_SECTOR_SIZE;
return free_sect * sect_size;
}

bool F_Fat::exists(const char* path)
{
File f = open(path, "r");
return (f == true) && !f.isDirectory();
}

bool F_Fat::exists(const String& path)
{
return exists(path.c_str());
}


F_Fat FFat = F_Fat(FSImplPtr(new VFSImpl()));
Loading