Simple SD File System test using mbed-os, FATFileSystem + sd-driver
Fork of mbed-os-example-fat-filesystem by
main.cpp@9:4cbf1601a4a5, 2017-08-02 (annotated)
- Committer:
- loopsva
- Date:
- Wed Aug 02 21:47:39 2017 +0000
- Revision:
- 9:4cbf1601a4a5
- Parent:
- 0:ab69df6f1c47
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:ab69df6f1c47 | 1 | #include "mbed.h" |
mbed_official | 0:ab69df6f1c47 | 2 | #include "FATFileSystem.h" |
loopsva | 9:4cbf1601a4a5 | 3 | #include "SDBlockDevice.h" |
loopsva | 9:4cbf1601a4a5 | 4 | |
loopsva | 9:4cbf1601a4a5 | 5 | DigitalOut gpo(D0); |
loopsva | 9:4cbf1601a4a5 | 6 | DigitalOut led(LED_RED); |
loopsva | 9:4cbf1601a4a5 | 7 | |
loopsva | 9:4cbf1601a4a5 | 8 | RawSerial pc(USBTX, USBRX); |
loopsva | 9:4cbf1601a4a5 | 9 | SDBlockDevice sd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS); |
loopsva | 9:4cbf1601a4a5 | 10 | FATFileSystem fs("sd", &sd); |
loopsva | 9:4cbf1601a4a5 | 11 | |
loopsva | 9:4cbf1601a4a5 | 12 | //--------------------------------------------------------------------------------------------------------------------------------------// |
loopsva | 9:4cbf1601a4a5 | 13 | // Initialize the file system |
loopsva | 9:4cbf1601a4a5 | 14 | |
loopsva | 9:4cbf1601a4a5 | 15 | #define FS_INIT_FILENAME "/sd/hello.txt" |
mbed_official | 0:ab69df6f1c47 | 16 | |
loopsva | 9:4cbf1601a4a5 | 17 | void initFS() { |
loopsva | 9:4cbf1601a4a5 | 18 | pc.printf("Initializing the SDFileSystem...\r\n"); |
loopsva | 9:4cbf1601a4a5 | 19 | FILE* fhr = fopen(FS_INIT_FILENAME, "r"); |
loopsva | 9:4cbf1601a4a5 | 20 | if(fhr != NULL) { |
loopsva | 9:4cbf1601a4a5 | 21 | fclose(fhr); |
loopsva | 9:4cbf1601a4a5 | 22 | pc.printf("Found test file %s\r\n...", FS_INIT_FILENAME); |
loopsva | 9:4cbf1601a4a5 | 23 | } else { |
loopsva | 9:4cbf1601a4a5 | 24 | pc.printf("File: %s not found. Creating\r\n", FS_INIT_FILENAME); |
loopsva | 9:4cbf1601a4a5 | 25 | FILE* fhw = fopen(FS_INIT_FILENAME, "w"); |
loopsva | 9:4cbf1601a4a5 | 26 | if(fhw != NULL) { |
loopsva | 9:4cbf1601a4a5 | 27 | fwrite("Hello World!", 12, 1, fhw); |
loopsva | 9:4cbf1601a4a5 | 28 | fprintf(fhw, "Hello World!\n"); |
loopsva | 9:4cbf1601a4a5 | 29 | fclose(fhw); |
loopsva | 9:4cbf1601a4a5 | 30 | } else { |
loopsva | 9:4cbf1601a4a5 | 31 | pc.printf("*** Cannot write file: %s\r\n", FS_INIT_FILENAME); |
loopsva | 9:4cbf1601a4a5 | 32 | } |
loopsva | 9:4cbf1601a4a5 | 33 | } |
mbed_official | 0:ab69df6f1c47 | 34 | } |
mbed_official | 0:ab69df6f1c47 | 35 | |
loopsva | 9:4cbf1601a4a5 | 36 | //--------------------------------------------------------------------------------------------------------------------------------------// |
loopsva | 9:4cbf1601a4a5 | 37 | // main |
mbed_official | 0:ab69df6f1c47 | 38 | |
mbed_official | 0:ab69df6f1c47 | 39 | int main() { |
loopsva | 9:4cbf1601a4a5 | 40 | pc.baud(230400); |
loopsva | 9:4cbf1601a4a5 | 41 | pc.printf("\r\n\r\n--------------------------------------------------------------------------------\r\n"); |
loopsva | 9:4cbf1601a4a5 | 42 | pc.printf("FRDM-K64F + OS5 + FS"); |
loopsva | 9:4cbf1601a4a5 | 43 | pc.printf(" -> build: " __DATE__ " " __TIME__ "(UTC) K Braun\n"); |
loopsva | 9:4cbf1601a4a5 | 44 | initFS(); |
loopsva | 9:4cbf1601a4a5 | 45 | while (true) { |
loopsva | 9:4cbf1601a4a5 | 46 | gpo = !gpo; // toggle pin |
loopsva | 9:4cbf1601a4a5 | 47 | led = !led; // toggle led |
loopsva | 9:4cbf1601a4a5 | 48 | wait(0.2f); |
loopsva | 9:4cbf1601a4a5 | 49 | } |
loopsva | 9:4cbf1601a4a5 | 50 | } |