Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 7 months ago.
SD Card on FRDM KL25Z
How use SD Card on FRDM KL25Z
Question relating to:
2 Answers
9 years, 2 months ago.
Does this library has read methods too?
Yes, you can read from files on the SD card using standard functions. Example:
Reading from a file on the SD card
printf("Reading from SD card..."); fp = fopen("/sd/mydir/test.txt", "r"); if (fp != NULL) { char c = fgetc(fp); if (c == 'H') printf("success!\n"); else printf("incorrect char (%c)!\n", c); fclose(fp); } else { printf("failed!\n"); }
9 years, 5 months ago.
First you attach an SD breakout board, such as this one from sparkfun: https://www.sparkfun.com/products/544 Or if you have a spare SD card adapter and are handy with a soldering iron, you can make your own: http://www.bot-thoughts.com/2010/02/logging-data-to-sd-cards.html
Here's the code that should be able to write to a FAT formatted SD card. I used the SD FileSystem library from here: https://developer.mbed.org/users/neilt6/code/SDFileSystem/
kl25z example interfacing to an SD card. Include the SDFileSystem library with this code
include "mbed.h" #include "SDFileSystem.h" // sd card breakout board: https://www.sparkfun.com/products/544 // // frdm-kl25z sd card connections spi0 // ------------------------------------------------ // Header -- kl25z -- SPI // J2-8 -- PTD2 -- MOSI // J2-6 -- PTD0 -- CS // J9-12 -- GND -- Vss (GND) // J9-4 -- P3V3 -- Vdd (+3.3v) // J2-12 -- PTD1 -- SCK // J9-14 -- GND -- Vss (GND) // J2-10 -- PTD3 -- MISO SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); // mosi, miso, sck, cs Serial pc(USBTX,USBRX); int main() { printf("start sd card test!\r\n"); mkdir("/sd/mydir", 0777); FILE *fp = fopen("/sd/mydir/test.txt", "w"); if(fp == NULL) { error("Could not open file for write\r\n"); } else printf("file opened for writing\r\n"); fprintf(fp, "Hello fun SD Card World!"); fclose(fp); printf("completed sd card test!\r\n"); }