An example program for the S25FL216K flash memory
Dependencies: S25FL216K_FATFileSystem mbed
Fork of S25FL216K_HelloWorld by
main.cpp@6:d3431822f4a9, 2014-12-23 (annotated)
- Committer:
- mkilivan
- Date:
- Tue Dec 23 21:41:08 2014 +0000
- Revision:
- 6:d3431822f4a9
- Parent:
- 4:6a2931c19204
tidy up and update library path
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 0:5e431050adf7 | 1 | #include "mbed.h" |
mkilivan | 6:d3431822f4a9 | 2 | #include "Flash_FileSystem.h" |
Sissors | 0:5e431050adf7 | 3 | |
mkilivan | 6:d3431822f4a9 | 4 | FlashSPI flash(P0_9, P0_8, P0_7, P0_6, "flash"); |
Sissors | 0:5e431050adf7 | 5 | |
Sissors | 0:5e431050adf7 | 6 | int main() |
Sissors | 0:5e431050adf7 | 7 | { |
Sissors | 0:5e431050adf7 | 8 | wait(0.1); |
Sissors | 0:5e431050adf7 | 9 | printf("Hello World!\r\n"); |
mkilivan | 6:d3431822f4a9 | 10 | char buffer[100]; |
mkilivan | 6:d3431822f4a9 | 11 | FILE *fp; |
Sissors | 0:5e431050adf7 | 12 | |
mkilivan | 6:d3431822f4a9 | 13 | int i; |
mkilivan | 6:d3431822f4a9 | 14 | |
mkilivan | 6:d3431822f4a9 | 15 | printf("directory list\r\n"); |
mkilivan | 6:d3431822f4a9 | 16 | DIR *d = opendir("/flash"); // Opens the root directory of the local file system |
mkilivan | 6:d3431822f4a9 | 17 | struct dirent *p; |
mkilivan | 6:d3431822f4a9 | 18 | i = 0; |
mkilivan | 6:d3431822f4a9 | 19 | while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system |
mkilivan | 6:d3431822f4a9 | 20 | printf("%s\r\n", p->d_name); // to stdout. |
mkilivan | 6:d3431822f4a9 | 21 | i++; |
mkilivan | 6:d3431822f4a9 | 22 | } |
mkilivan | 6:d3431822f4a9 | 23 | closedir(d); |
mkilivan | 6:d3431822f4a9 | 24 | printf("total %d files found.\r\n\n", i); |
mkilivan | 6:d3431822f4a9 | 25 | |
mkilivan | 6:d3431822f4a9 | 26 | fp = fopen("/flash/in1.txt", "a"); |
mkilivan | 6:d3431822f4a9 | 27 | printf("-0-\r\n"); |
Sissors | 0:5e431050adf7 | 28 | |
Sissors | 0:5e431050adf7 | 29 | if(fp == NULL) { |
Sissors | 0:5e431050adf7 | 30 | printf("Could not open file, assuming unformatted disk!\r\n"); |
Sissors | 3:321618d7c9a5 | 31 | printf("Formatting disk!\r\n"); |
Sissors | 3:321618d7c9a5 | 32 | flash.format(); |
Sissors | 3:321618d7c9a5 | 33 | printf("Disk formatted!\r\n"); |
Sissors | 3:321618d7c9a5 | 34 | printf("Reset your device!\r\n"); |
Sissors | 1:e8698224bb08 | 35 | while(1); |
Sissors | 0:5e431050adf7 | 36 | } else { |
Sissors | 0:5e431050adf7 | 37 | wait(0.2); |
mkilivan | 6:d3431822f4a9 | 38 | fprintf(fp, "hello "); |
mkilivan | 6:d3431822f4a9 | 39 | fclose(fp); |
mkilivan | 6:d3431822f4a9 | 40 | } |
mkilivan | 6:d3431822f4a9 | 41 | |
mkilivan | 6:d3431822f4a9 | 42 | wait(0.5); |
mkilivan | 6:d3431822f4a9 | 43 | |
mkilivan | 6:d3431822f4a9 | 44 | fp = fopen("/flash/in2.txt", "a"); |
mkilivan | 6:d3431822f4a9 | 45 | if (fp == NULL) { |
mkilivan | 6:d3431822f4a9 | 46 | printf("out.txt can't created.\r\n"); |
mkilivan | 6:d3431822f4a9 | 47 | } else { |
mkilivan | 6:d3431822f4a9 | 48 | wait(0.2); |
mkilivan | 6:d3431822f4a9 | 49 | fprintf(fp, "Hello 2 "); |
Sissors | 0:5e431050adf7 | 50 | fclose(fp); |
Sissors | 0:5e431050adf7 | 51 | } |
Sissors | 1:e8698224bb08 | 52 | |
mkilivan | 6:d3431822f4a9 | 53 | wait(0.5); |
mkilivan | 6:d3431822f4a9 | 54 | |
mkilivan | 6:d3431822f4a9 | 55 | fp = fopen("/flash/in1.txt", "r"); |
mkilivan | 4:6a2931c19204 | 56 | if (fp == NULL) { |
mkilivan | 6:d3431822f4a9 | 57 | printf("in1.txt can't open to read.\r\n"); |
mkilivan | 6:d3431822f4a9 | 58 | } else |
mkilivan | 6:d3431822f4a9 | 59 | { |
mkilivan | 6:d3431822f4a9 | 60 | fgets (buffer, 100, fp); |
mkilivan | 6:d3431822f4a9 | 61 | printf("%s\r\n", buffer); |
mkilivan | 6:d3431822f4a9 | 62 | fclose(fp); |
mkilivan | 6:d3431822f4a9 | 63 | } |
mkilivan | 6:d3431822f4a9 | 64 | |
mkilivan | 6:d3431822f4a9 | 65 | wait(0.5); |
mkilivan | 6:d3431822f4a9 | 66 | |
mkilivan | 6:d3431822f4a9 | 67 | fp = fopen("/flash/in2.txt", "r"); |
mkilivan | 6:d3431822f4a9 | 68 | if (fp == NULL) { |
mkilivan | 6:d3431822f4a9 | 69 | printf("in2.txt can't open to read.\r\n"); |
mkilivan | 6:d3431822f4a9 | 70 | } else |
mkilivan | 6:d3431822f4a9 | 71 | { |
mkilivan | 6:d3431822f4a9 | 72 | fgets (buffer, 100, fp); |
mkilivan | 6:d3431822f4a9 | 73 | printf("%s\r\n", buffer); |
Sissors | 1:e8698224bb08 | 74 | fclose(fp); |
Sissors | 0:5e431050adf7 | 75 | } |
Sissors | 0:5e431050adf7 | 76 | |
Sissors | 0:5e431050adf7 | 77 | } |