Run a file system on the Dragonfly's SPI Flash.

Dependencies:   SpiFlash25 flash-fs

Files at this revision

API Documentation at this revision

Comitter:
Leon Lindenfelser
Date:
Fri Jun 26 16:49:06 2020 -0500
Commit message:
Initial commit for mbed 5 version of this program

Changed in this revision

SpiFlash25.lib Show annotated file Show diff for this revision Revisions of this file
flash-fs.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 54ec26dc283f SpiFlash25.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SpiFlash25.lib	Fri Jun 26 16:49:06 2020 -0500
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/MultiTech/code/SpiFlash25/#31c110c2536c
diff -r 000000000000 -r 54ec26dc283f flash-fs.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flash-fs.lib	Fri Jun 26 16:49:06 2020 -0500
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/MultiTech/code/flash-fs/#7a3c79b0d570
diff -r 000000000000 -r 54ec26dc283f main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Jun 26 16:49:06 2020 -0500
@@ -0,0 +1,135 @@
+/** Dragonfly Filesystem Example
+ * Opens file in filesystem, prints contents of the file, and writes to the file.
+ *
+ * NOTE: This example changes the baud rate of the debug port to 115200 baud!
+ */
+#include "mbed.h"
+#include "SpiFlash25.h"
+#include "spiffs.h"
+#include <string>
+
+// This line controls the regulator's battery charger.
+// BC_NCE = 0 enables the battery charger
+// BC_NCE = 1 disables the battery charger
+DigitalOut bc_nce(PB_2);
+
+// this value represents the number of files you can have open at the same time
+// adjust it according to your requirements
+#define MAX_CONCURRENT_FDS      4
+
+#define PAGE_SIZE               256
+#define SECTOR_SIZE             64*1024
+#define MEM_SIZE                2*1024*1024
+
+static u8_t spiffs_work_buf[PAGE_SIZE * 2];
+static u8_t spiffs_fds[32 * MAX_CONCURRENT_FDS];
+static u8_t spiffs_cache_buf[(PAGE_SIZE + 32) * 4];
+
+static SpiFlash25 flash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS1);
+static spiffs fs;
+static spiffs_config cfg;
+
+// glue code between SPI driver and filesystem driver
+int spi_read(unsigned int addr, unsigned int size, unsigned char* data) {
+    if (flash.read(addr, size, (char*)data))
+        return SPIFFS_OK;
+    return -1;
+}
+int spi_write(unsigned int addr, unsigned int size, unsigned char* data) {
+    if (flash.write(addr, size, (const char*)data))
+        return SPIFFS_OK;
+    return -1;
+}
+int spi_erase(unsigned int addr, unsigned int size) {
+    flash.clear_sector(addr);
+    return SPIFFS_OK;
+}
+
+int main(void) {
+    // Disable the battery charger unless a battery is attached.
+    bc_nce = 1;
+    
+    int ret;
+    int handle;
+    char data[256];
+    char msg[] = "Hello World! The sneaky cat crept around the sleeping dog.\r\n";
+    char file[] = "test.txt";
+    string sdata;
+
+    // configure the filesystem
+    cfg.phys_size = MEM_SIZE;
+    cfg.phys_addr = 0;
+    cfg.phys_erase_block = SECTOR_SIZE;
+    cfg.log_block_size = SECTOR_SIZE;
+    cfg.log_page_size = PAGE_SIZE;
+
+    cfg.hal_read_f = &spi_read;
+    cfg.hal_write_f = &spi_write;
+    cfg.hal_erase_f = &spi_erase;
+
+    Serial pc(USBTX, USBRX);
+    pc.baud(115200);
+
+    printf("Dragonfly spi flash example started\r\n");
+
+    // erase entire flash
+    // THIS WILL ERASE THE ENTIRE FLASH! EVERYTHING ON IT WILL BE LOST AND CANNOT BE RECOVERED!
+    //flash.clear_mem();
+
+    // mount the filesystem
+    ret = SPIFFS_mount(&fs, &cfg, spiffs_work_buf, spiffs_fds, sizeof(spiffs_fds), spiffs_cache_buf, sizeof(spiffs_cache_buf), NULL);
+    if (ret) {
+        printf("SPIFFS_mount failed %d - can't continue\r\n", ret);
+        return 1;
+    }
+
+    // write to the file
+    handle = SPIFFS_open(&fs, file, SPIFFS_CREAT | SPIFFS_RDWR | SPIFFS_APPEND, 0);
+    if (handle < 0)
+        printf("SPIFFS_open failed %d\r\n", SPIFFS_errno(&fs));
+
+    if (handle) {
+        ret = SPIFFS_write(&fs, handle, msg, sizeof(msg));
+        if (ret < 0)
+            printf("SPIFFS_write failed %d\r\n", SPIFFS_errno(&fs));
+        else
+            printf("Wrote %d bytes\r\n", ret);
+        SPIFFS_close(&fs, handle);
+    }
+
+    // read the current file contents
+    spiffs_stat stat;
+    memset(&stat, 0, sizeof(stat));
+    ret = SPIFFS_stat(&fs, file, &stat);
+    if (ret)
+        printf("SPIFFS_stat failed %d\r\n", SPIFFS_errno(&fs));
+    else
+        printf("File size: %d bytes\r\n", stat.size);
+
+    handle = SPIFFS_open(&fs, file, SPIFFS_RDWR, 0);
+    if (handle < 0)
+        printf("SPIFFS_open failed %d\r\n", SPIFFS_errno(&fs));
+
+    if (handle) {
+        while (sdata.size() < stat.size) {
+            ret = SPIFFS_read(&fs, handle, data, stat.size - sdata.size() < sizeof(data) ? stat.size - sdata.size() : sizeof(data));
+            if (ret < 0) {
+                printf("SPIFFS_read failed %d\r\n", SPIFFS_errno(&fs));
+                continue;
+            }
+            printf("Read %d bytes\r\n", ret);
+            sdata.append(data, ret);
+        }
+
+        printf("Data [\r\n");
+        for (int i = 0; i < sdata.size(); i++)
+            printf("%c", sdata[i]);
+        printf("\r\n]\r\n");
+
+        SPIFFS_close(&fs, handle);
+    }
+
+    printf("Dragonfly spi flash example finished\r\n\r\n");
+
+    return 0;
+}
\ No newline at end of file
diff -r 000000000000 -r 54ec26dc283f mbed-os.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Fri Jun 26 16:49:06 2020 -0500
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#e4b81f67f939a0c0b11c147ce74aa367271e1279
\ No newline at end of file