Using SPI flash with STM32F407VET6 board.

Dependencies:   SpiFlash25 flash-fs mbed

Fork of Dragonfly_Filesystem_Example by MultiTech

Using the on-board Winbond W25Q16 16Mbit SPI Flash memory chip with STM32F407VET6 black boards

Files at this revision

API Documentation at this revision

Comitter:
hudakz
Date:
Sat Apr 28 15:59:04 2018 +0000
Parent:
6:11c09ef31db4
Commit message:
Initial reelase.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 11c09ef31db4 -r afdf19fe2341 main.cpp
--- a/main.cpp	Fri Feb 26 16:53:58 2016 +0000
+++ b/main.cpp	Sat Apr 28 15:59:04 2018 +0000
@@ -1,21 +1,15 @@
-/** Dragonfly Filesystem Example
+/** SPI Flash 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 MAX_CONCURRENT_FDS      2
 
 #define PAGE_SIZE               256
 #define SECTOR_SIZE             64*1024
@@ -25,7 +19,7 @@
 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 SpiFlash25 flash(PB_5, PB_4, PB_3, PB_0);
 static spiffs fs;
 static spiffs_config cfg;
 
@@ -46,13 +40,10 @@
 }
 
 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 msg[] = "Hello World!\r\n";
     char file[] = "test.txt";
     string sdata;
 
@@ -67,69 +58,88 @@
     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");
+    printf("\r\n\r\nSPI flash example started.\r\n\r\n");
 
     // erase entire flash
     // THIS WILL ERASE THE ENTIRE FLASH! EVERYTHING ON IT WILL BE LOST AND CANNOT BE RECOVERED!
-    //flash.clear_mem();
+    flash.clear_mem();
 
+    printf("Mounting SPI flash file system ... ");
     // 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;
+        printf("Failed.\r\n");
+        return ret;
     }
+    else
+        printf("OK.\r\n");
 
     // write to the file
+    printf("Opening file 'test.txt' for writing ... ");
     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 < 0) {
+        printf("Failed.\r\n");
+        return SPIFFS_errno(&fs);
+    }
+    else
+        printf("OK.\r\n");
 
+    printf("Writing data to the file ... ");
     if (handle) {
         ret = SPIFFS_write(&fs, handle, msg, sizeof(msg));
-        if (ret < 0)
-            printf("SPIFFS_write failed %d\r\n", SPIFFS_errno(&fs));
+        if (ret < 0) {
+            printf("Failed.\r\n");
+            return SPIFFS_errno(&fs);
+        }
         else
-            printf("Wrote %d bytes\r\n", ret);
+            printf("OK. \r\nWrote %d bytes to the file.\r\n", ret);
         SPIFFS_close(&fs, handle);
+        printf("File closed.\r\n");
     }
 
     // read the current file contents
+    printf("Getting size of 'test.tx' file ... ");
     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));
+    if (ret) {
+        printf("Failed.\r\n");
+        return SPIFFS_errno(&fs);
+    }
     else
-        printf("File size: %d bytes\r\n", stat.size);
+        printf("OK.\r\nFile size: %d bytes.\r\n", stat.size);
 
+    printf("Opening file 'text.txt' for reading ... ");
     handle = SPIFFS_open(&fs, file, SPIFFS_RDWR, 0);
-    if (handle < 0)
-        printf("SPIFFS_open failed %d\r\n", SPIFFS_errno(&fs));
+    if (handle < 0) {
+        printf("Failed.\r\n");
+        return SPIFFS_errno(&fs);
+    }
+    else
+        printf("OK.\r\n");
 
+    printf("Reading data from the file ... ");
     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));
+                printf("\r\nRead failed. Error: %d\r\n", SPIFFS_errno(&fs));
                 continue;
             }
-            printf("Read %d bytes\r\n", ret);
+            printf("OK.\r\nRead %d bytes.\r\n", ret);
             sdata.append(data, ret);
         }
 
-        printf("Data [\r\n");
+        printf("File content: ");
         for (int i = 0; i < sdata.size(); i++)
             printf("%c", sdata[i]);
-        printf("\r\n]\r\n");
+        printf("\r\n");
 
         SPIFFS_close(&fs, handle);
+        printf("File closed.\r\n");
     }
 
-    printf("Dragonfly spi flash example finished\r\n\r\n");
+    printf("\r\nSPI flash example finished\r\n\r\n");
 
     return 0;
 }
\ No newline at end of file
diff -r 11c09ef31db4 -r afdf19fe2341 mbed.bld
--- a/mbed.bld	Fri Feb 26 16:53:58 2016 +0000
+++ b/mbed.bld	Sat Apr 28 15:59:04 2018 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/6f327212ef96
\ No newline at end of file
+https://os.mbed.com/users/mbed_official/code/mbed/builds/5aab5a7997ee
\ No newline at end of file