An example program for the S25FL216K flash memory
Dependencies: S25FL216K_FATFileSystem mbed
Fork of S25FL216K_HelloWorld by
main.cpp@1:e8698224bb08, 2013-08-02 (annotated)
- 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?
User | Revision | Line number | New 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 | } |