SD card file helloworld demo using new mbed RTOS ver 5 file driver setup. Pins setup for LPC1768 - read comment in code about project's json file requirements for adding the SD filesystem driver

SD card r/w demo for mbed OS version 5 on LPC1768.

File drivers are added in json file with "target.components_add": ["SD"] for use in most recent OS 5 version.

Test messages are sent to serial port with printf's.

main.cpp

Committer:
4180_1
Date:
2 months ago
Revision:
2:21d14147254e
Parent:
1:3a59eb72e0cf

File content as of revision 2:21d14147254e:

#include "mbed.h"
// NOTE: Need "target.components_add": ["SD"] in json project file!
// This adds the filesystem driver below for SD cards in a project
#include "SDBlockDevice.h"
#include "FATFileSystem.h"
Serial pc(USBTX,USBRX); //change for your mbed serial port
SDBlockDevice sd(p5, p6, p7, p8);
FATFileSystem fs("SD");
char filereadchar=0;

int main()
{
    sd.init();
    fs.mount(&sd);
// open and write a message in a new file
    FILE* fw = fopen("/SD/hi.txt", "w");
    if(fw == NULL) {
        error("Could not open new file for write\n\r");
    }
    printf("Writing to SD file: hello SD Card!\n\r");
    fprintf(fw, "hello SD card!\n\r");
    fclose(fw);
    printf("\nFile closed for write\n\n\r");
// open and read back file characters to serial for test
    FILE* fr = fopen("/SD/hi.txt", "r");
    if(fr == NULL) {
        error("Could not open new file for read\n\r");
    }
    printf("Reading back from new file: ");
    while(fscanf(fr,"%c",&filereadchar) == 1) {
        printf("\%c",filereadchar);
    }
    fclose(fr);
    sd.deinit();
    fs.unmount();
    printf("\nSD demo complete\n\r");
}