/*############################################################# Program Name : ex2_F446RE_SDCardRead Author : Grant Phillips Date Modified : 19/09/2016 Compiler : ARMmbed Tested On : NUCLEO-F446RE Description : Example program that demonstrates the use of a SDCard with the NUCLEO board. The file that was created in ex1_F446RE_SDCardWrite is openend and read character by character. Requirements : * NUCLEO-F446RE Board Circuit : * The SDCard connected as: VCC - +3V or +5V (depending on module) GND - GND SS/CS - PB_6 MOSI - PB_5 (SPI3_MOSI) MISO - PB_4 (SPI3_MISO) SCK - PB_3 (SPI3_SCK) ##############################################################*/ #include "mbed.h" #include "SDFileSystem.h" //Niel Thiessen's library Serial pc(SERIAL_TX, SERIAL_RX); //to use the PC as a console (display output) DigitalOut myled(LED1); //LED to indicate system operation SDFileSystem sd(PB_5, PB_4, PB_3, PB_6, "sd"); //create a SDFileSystem object // mosi, miso, sck , cs , name int main() { int ch; pc.printf("ex2_F446RE_SDCardRead\n\n"); printf("Reading from SD card...\n"); FILE *fp = fopen("/sd/mydir/sdtest.txt", "r"); //open a file for reading ("r"); reading of a file //always starts from the first character in the file if(fp == NULL) { //if error opening printf("Could not open file for reading!\n"); } else { pc.printf("The contents of the file is:\n"); do { ch=fgetc(fp); //get a character/byte from the file if (ch != EOF) pc.printf("%c", ch); //display character received if it is not the EOF character } while(ch != EOF); //repeat until the end of file (EOF) character is received fclose(fp); //close the file } while(1) { myled = !myled; //flash led wait(0.1); } }