SD USB MSD helloworld

Dependencies:   USBFileSystem USBSDFileSystem mbed

Created based on comment from Erik Olieman see https://mbed.org/questions/1181/combine-SDfilesystem-with-USBMSD_SD/?c=7180

As well as based on SDFileSystem library for the spi-access to SD-card.

It gives the next output:

Hello World!

now a USB drive should appear on your PC
create directory and append to log file
appending: '[0]Hello fun SD Card World!'
Entering loop of 1 min cycle time
appending: '[1]Hello fun SD Card World!'
appending: '[2]Hello fun SD Card World!'
...

This works.

Although, I've to admit it is not very convenient, since my win7 pc always disconnect/connect the drive and at disconnect it also closes the explorer which show the content of that drive. It does the same with the editor that has the file opened.

Committer:
karelv
Date:
Wed Aug 28 19:32:16 2013 +0000
Revision:
2:f7b4edf066de
Parent:
1:c7f66434c692
cosmetic changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
karelv 0:ce2fc50a859f 1 #include "mbed.h"
karelv 0:ce2fc50a859f 2 #include "USBSDFileSystem.h"
karelv 0:ce2fc50a859f 3
karelv 0:ce2fc50a859f 4 DigitalOut myled(LED1);
karelv 0:ce2fc50a859f 5
karelv 0:ce2fc50a859f 6 USBSDFileSystem sd("sd", p11, p12, p13, p14); // the pinout on the mbed testbed board // mosi, miso, sclk, cs
karelv 0:ce2fc50a859f 7
karelv 0:ce2fc50a859f 8
karelv 0:ce2fc50a859f 9 void mylog (int i)
karelv 0:ce2fc50a859f 10 {
karelv 0:ce2fc50a859f 11 FILE *fp = fopen("/sd/mydir/sdtest.txt", "a");
karelv 0:ce2fc50a859f 12 if(fp == NULL) {
karelv 1:c7f66434c692 13 printf("Could not open file for write\n");
karelv 0:ce2fc50a859f 14 } else
karelv 0:ce2fc50a859f 15 {
karelv 0:ce2fc50a859f 16 fprintf(fp, "[%d]Hello fun SD Card World!\n", i);
karelv 1:c7f66434c692 17 printf("appending: '[%d]Hello fun SD Card World!'\n", i);
karelv 0:ce2fc50a859f 18 fclose(fp);
karelv 0:ce2fc50a859f 19 }
karelv 0:ce2fc50a859f 20 }
karelv 0:ce2fc50a859f 21
karelv 0:ce2fc50a859f 22
karelv 2:f7b4edf066de 23 int main()
karelv 2:f7b4edf066de 24 {
karelv 1:c7f66434c692 25 sd.usbMode (1); // allow fopen/fprintf when connected with USB-drive.
karelv 0:ce2fc50a859f 26 printf("Hello World!\n\n");
karelv 1:c7f66434c692 27
karelv 1:c7f66434c692 28 printf("now a USB drive should appear on your PC\n");
karelv 1:c7f66434c692 29 wait (60);
karelv 1:c7f66434c692 30
karelv 1:c7f66434c692 31 printf("create directory and append to log file\n");
karelv 0:ce2fc50a859f 32 mkdir("/sd/mydir", 0777);
karelv 1:c7f66434c692 33 mylog (0);
karelv 0:ce2fc50a859f 34
karelv 1:c7f66434c692 35 printf("Entering loop of 1 min cycle time\n");
karelv 0:ce2fc50a859f 36
karelv 0:ce2fc50a859f 37 int i = 1;
karelv 0:ce2fc50a859f 38 while(1) {
karelv 0:ce2fc50a859f 39 myled = 0;
karelv 1:c7f66434c692 40 wait(5);
karelv 1:c7f66434c692 41 //sd.disconnect ();
karelv 0:ce2fc50a859f 42 mylog (i++);
karelv 1:c7f66434c692 43 //sd.connect ();
karelv 0:ce2fc50a859f 44 myled = 1;
karelv 0:ce2fc50a859f 45 wait(55);
karelv 0:ce2fc50a859f 46 }
karelv 2:f7b4edf066de 47 return 0;
karelv 0:ce2fc50a859f 48 }