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.
Diff: main.cpp
- Revision:
- 1:3a59eb72e0cf
- Parent:
- 0:3588d7473dce
- Child:
- 2:21d14147254e
--- a/main.cpp Wed Sep 11 12:39:11 2019 +0000 +++ b/main.cpp Tue Sep 24 17:32:01 2019 +0000 @@ -1,19 +1,37 @@ #include "mbed.h" -// Need "target.components_add": ["SD"] in json project file! +// 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"); - -int main() { +char filereadchar=0; + +int main() +{ sd.init(); fs.mount(&sd); - - FILE* fd = fopen("/SD/hi.txt", "w"); - fprintf(fd, "hello!\n\r"); - fclose(fd); - +// 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("\%s",&filereadchar); + } + fclose(fr); sd.deinit(); fs.unmount(); + printf("\nSD demo complete\n\r"); } \ No newline at end of file