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).

Committer:
Sissors
Date:
Fri Aug 02 19:01:53 2013 +0000
Revision:
1:e8698224bb08
Parent:
0:5e431050adf7
Child:
3:321618d7c9a5
v2.0 using new version of USBFileSystem
;
; Can now also write text received via serial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 0:5e431050adf7 1 #include "mbed.h"
Sissors 0:5e431050adf7 2 #include "Flash_USBFileSystem.h"
Sissors 0:5e431050adf7 3
Sissors 0:5e431050adf7 4 DigitalOut myled(LED1);
Sissors 0:5e431050adf7 5 FlashUSB flash(PTD6, PTD7, PTB11, PTE4);
Sissors 0:5e431050adf7 6
Sissors 0:5e431050adf7 7 void usbCallback(bool available)
Sissors 0:5e431050adf7 8 {
Sissors 0:5e431050adf7 9 if (available) {
Sissors 1:e8698224bb08 10 FILE *fp = fopen("/USB/in.txt", "r");
Sissors 0:5e431050adf7 11 char buffer[100];
Sissors 0:5e431050adf7 12 fgets (buffer, 100, fp);
Sissors 0:5e431050adf7 13 printf("%s\r\n", buffer);
Sissors 0:5e431050adf7 14 fclose(fp);
Sissors 0:5e431050adf7 15 }
Sissors 0:5e431050adf7 16 }
Sissors 0:5e431050adf7 17
Sissors 0:5e431050adf7 18 int main()
Sissors 0:5e431050adf7 19 {
Sissors 0:5e431050adf7 20 flash.attachUSB(&usbCallback);
Sissors 0:5e431050adf7 21
Sissors 0:5e431050adf7 22 wait(0.1);
Sissors 0:5e431050adf7 23 printf("Hello World!\r\n");
Sissors 0:5e431050adf7 24
Sissors 1:e8698224bb08 25 FILE *fp = fopen("/USB/in.txt", "w");
Sissors 0:5e431050adf7 26
Sissors 0:5e431050adf7 27 if(fp == NULL) {
Sissors 0:5e431050adf7 28 printf("Could not open file, assuming unformatted disk!\r\n");
Sissors 0:5e431050adf7 29 printf("Click in the nice popup window to format disk with default settings!\r\n");
Sissors 1:e8698224bb08 30 while(1);
Sissors 0:5e431050adf7 31 } else {
Sissors 0:5e431050adf7 32 wait(0.2);
Sissors 0:5e431050adf7 33 fprintf(fp, "Type your text here!");
Sissors 0:5e431050adf7 34 fclose(fp);
Sissors 0:5e431050adf7 35 }
Sissors 0:5e431050adf7 36
Sissors 0:5e431050adf7 37 //Connect USB
Sissors 0:5e431050adf7 38 flash.connect();
Sissors 1:e8698224bb08 39 flash.usbMode(1); //Disconnect USB when files are locally written
Sissors 1:e8698224bb08 40
Sissors 1:e8698224bb08 41 char buffer[101];
Sissors 1:e8698224bb08 42 printf("Type your text here! (100 max length)\n");
Sissors 0:5e431050adf7 43 while(1) {
Sissors 1:e8698224bb08 44 gets(buffer);
Sissors 0:5e431050adf7 45 myled = !myled;
Sissors 1:e8698224bb08 46
Sissors 1:e8698224bb08 47 //Open file for write, keep trying until we succeed
Sissors 1:e8698224bb08 48 //This is needed because if USB happens to be writing it will fail
Sissors 1:e8698224bb08 49 while(1) {
Sissors 1:e8698224bb08 50 fp = fopen("/USB/out.txt", "w");
Sissors 1:e8698224bb08 51 if (fp != NULL)
Sissors 1:e8698224bb08 52 break;
Sissors 1:e8698224bb08 53 }
Sissors 1:e8698224bb08 54 fprintf(fp, buffer);
Sissors 1:e8698224bb08 55 fclose(fp);
Sissors 1:e8698224bb08 56
Sissors 0:5e431050adf7 57 }
Sissors 0:5e431050adf7 58
Sissors 0:5e431050adf7 59 }