Helloworld program for the S25FL216K flash memory in combination with USBFileSystem

Dependencies:   S25FL216K_USBFileSystem mbed

This HelloWorld program is for the serial flash memory mounted on the Wi-Go board. If yours isn't mounted on it, then you probably will need some other pin definitions.

To use this program plugin both the openSDA USB port and the KL25Z USB port. Use your favourite terminal program to listen to the com-port generated on the openSDA USB port. The KL25Z USB port will emulate a USB MSD device.

Load this program, reset the board. If everything goes correctly a pop-up will appear that the drive needs to be formatted (tested on Windows 8, but I assume it is similar on other OS'). This is because the flash memory is completely empty, no filesystem is available. Just use standard settings (512 byte sector size), and format the drive. When this is done reset the board again.

This time it should load without popup, and after a small pause the flash drive should appear on your computer. On it is a single file, open the file, write something, save the file. Whatever you wrote should appear in your terminal program.

Aditionally the other way around also works, write something in your terminal program, hit enter, and a file is created with whatever you wrote in it. IMPORTANT: In TeraTerm you need to have transmit settings on CR+LF! (Found in Setup > Terminal).

Files at this revision

API Documentation at this revision

Comitter:
Sissors
Date:
Fri Aug 02 19:01:53 2013 +0000
Parent:
0:5e431050adf7
Child:
2:587814fb010d
Child:
3:321618d7c9a5
Commit message:
v2.0 using new version of USBFileSystem
;
; Can now also write text received via serial

Changed in this revision

S25FL216K_USBFileSystem.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
--- a/S25FL216K_USBFileSystem.lib	Wed Jul 31 19:21:41 2013 +0000
+++ b/S25FL216K_USBFileSystem.lib	Fri Aug 02 19:01:53 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/Sissors/code/S25FL216K_USBFileSystem/#9056eb697726
+http://mbed.org/users/Sissors/code/S25FL216K_USBFileSystem/#355e49586f92
--- a/main.cpp	Wed Jul 31 19:21:41 2013 +0000
+++ b/main.cpp	Fri Aug 02 19:01:53 2013 +0000
@@ -7,7 +7,7 @@
 void usbCallback(bool available)
 {
     if (available) {
-        FILE *fp = fopen("/USB/usbtest.txt", "r");
+        FILE *fp = fopen("/USB/in.txt", "r");
         char buffer[100];
         fgets (buffer, 100, fp);
         printf("%s\r\n", buffer);
@@ -22,11 +22,12 @@
     wait(0.1);
     printf("Hello World!\r\n");
 
-    FILE *fp = fopen("/USB/usbtest.txt", "w");
+    FILE *fp = fopen("/USB/in.txt", "w");
 
     if(fp == NULL) {
         printf("Could not open file, assuming unformatted disk!\r\n");
         printf("Click in the nice popup window to format disk with default settings!\r\n");
+        while(1);
     } else {
         wait(0.2);
         fprintf(fp, "Type your text here!");
@@ -35,9 +36,24 @@
 
     //Connect USB
     flash.connect();
+    flash.usbMode(1);       //Disconnect USB when files are locally written
+    
+    char buffer[101];
+    printf("Type your text here! (100 max length)\n");
     while(1) {
-        wait(0.2);
+        gets(buffer);
         myled = !myled;
+        
+        //Open file for write, keep trying until we succeed
+        //This is needed because if USB happens to be writing it will fail
+        while(1) {
+            fp = fopen("/USB/out.txt", "w");
+            if (fp != NULL)
+                break;
+            }
+        fprintf(fp, buffer);
+        fclose(fp);
+        
     }
 
 }