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:
Tue Nov 19 21:09:35 2013 +0000
Revision:
3:321618d7c9a5
Parent:
1:e8698224bb08
Bugfix

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 3:321618d7c9a5 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 3:321618d7c9a5 29 printf("Formatting disk!\r\n");
Sissors 3:321618d7c9a5 30 flash.format();
Sissors 3:321618d7c9a5 31 printf("Disk formatted!\r\n");
Sissors 3:321618d7c9a5 32 printf("Reset your device!\r\n");
Sissors 1:e8698224bb08 33 while(1);
Sissors 0:5e431050adf7 34 } else {
Sissors 0:5e431050adf7 35 wait(0.2);
Sissors 0:5e431050adf7 36 fprintf(fp, "Type your text here!");
Sissors 0:5e431050adf7 37 fclose(fp);
Sissors 0:5e431050adf7 38 }
Sissors 0:5e431050adf7 39
Sissors 0:5e431050adf7 40 //Connect USB
Sissors 0:5e431050adf7 41 flash.connect();
Sissors 1:e8698224bb08 42 flash.usbMode(1); //Disconnect USB when files are locally written
Sissors 1:e8698224bb08 43
Sissors 1:e8698224bb08 44 char buffer[101];
Sissors 1:e8698224bb08 45 printf("Type your text here! (100 max length)\n");
Sissors 0:5e431050adf7 46 while(1) {
Sissors 1:e8698224bb08 47 gets(buffer);
Sissors 0:5e431050adf7 48 myled = !myled;
Sissors 1:e8698224bb08 49
Sissors 1:e8698224bb08 50 //Open file for write, keep trying until we succeed
Sissors 1:e8698224bb08 51 //This is needed because if USB happens to be writing it will fail
Sissors 1:e8698224bb08 52 while(1) {
Sissors 1:e8698224bb08 53 fp = fopen("/USB/out.txt", "w");
Sissors 1:e8698224bb08 54 if (fp != NULL)
Sissors 1:e8698224bb08 55 break;
Sissors 1:e8698224bb08 56 }
Sissors 1:e8698224bb08 57 fprintf(fp, buffer);
Sissors 1:e8698224bb08 58 fclose(fp);
Sissors 1:e8698224bb08 59
Sissors 0:5e431050adf7 60 }
Sissors 0:5e431050adf7 61
Sissors 0:5e431050adf7 62 }