/*############################################################# Program Name : ex1_F446RE_SDCardWrite 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. A file is created on the SDCard and some text stored in the file. 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() { pc.printf("ex1_F446RE_SDCardWrite\n\n"); pc.printf("\nWriting to SD card...\n"); mkdir("/sd/mydir", 0777); //create a folder "mydir" FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); //open a file for writing ("w"); if the file already exists //it will be deleted and a new one created if(fp == NULL) { //if error opening printf("Could not open file for write!\n"); } else { fprintf(fp, "Hello fun SD Card World!\r\n");//write text to the file fprintf(fp, "test line 1\r\n"); fprintf(fp, "test line 2\r\n"); fclose(fp); //close the file to prevent corrupt files pc.printf("File created successfully!\n"); } while(1) { myled = !myled; //flash led wait(0.1); } }