Simple SD File System test using mbed-os, FATFileSystem + sd-driver

Dependencies:   sd-driver

Fork of mbed-os-example-fat-filesystem by mbed-os-examples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "FATFileSystem.h"
00003 #include "SDBlockDevice.h"
00004 
00005 DigitalOut gpo(D0);
00006 DigitalOut led(LED_RED);
00007 
00008 RawSerial pc(USBTX, USBRX);
00009 SDBlockDevice sd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS);
00010 FATFileSystem fs("sd", &sd);
00011 
00012 //--------------------------------------------------------------------------------------------------------------------------------------//
00013 // Initialize the file system
00014 
00015 #define FS_INIT_FILENAME "/sd/hello.txt"
00016 
00017 void initFS() {
00018     pc.printf("Initializing the SDFileSystem...\r\n");
00019     FILE* fhr = fopen(FS_INIT_FILENAME, "r");
00020     if(fhr != NULL) {
00021         fclose(fhr);
00022         pc.printf("Found test file %s\r\n...", FS_INIT_FILENAME);
00023     } else {
00024         pc.printf("File: %s not found. Creating\r\n", FS_INIT_FILENAME);
00025         FILE* fhw = fopen(FS_INIT_FILENAME, "w");
00026         if(fhw != NULL) {
00027             fwrite("Hello World!", 12, 1, fhw);
00028             fprintf(fhw, "Hello World!\n");
00029             fclose(fhw);
00030         } else {
00031             pc.printf("*** Cannot write file: %s\r\n", FS_INIT_FILENAME);
00032         }
00033     }
00034 }
00035 
00036 //--------------------------------------------------------------------------------------------------------------------------------------//
00037 // main
00038 
00039 int main() {
00040     pc.baud(230400);
00041     pc.printf("\r\n\r\n--------------------------------------------------------------------------------\r\n");
00042     pc.printf("FRDM-K64F + OS5 + FS");
00043     pc.printf("   -> build: " __DATE__ " " __TIME__ "(UTC)  K Braun\n");
00044     initFS();
00045     while (true) {
00046         gpo = !gpo; // toggle pin
00047         led = !led; // toggle led
00048         wait(0.2f);
00049     }
00050 }