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:
Tue Aug 27 19:42:29 2013 +0000
Revision:
1:c7f66434c692
Parent:
0:ce2fc50a859f
Child:
2:f7b4edf066de
update after recommendations of Erik Olieman

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 int main() {
karelv 0:ce2fc50a859f 23
karelv 1:c7f66434c692 24 sd.usbMode (1); // allow fopen/fprintf when connected with USB-drive.
karelv 0:ce2fc50a859f 25 printf("Hello World!\n\n");
karelv 1:c7f66434c692 26
karelv 1:c7f66434c692 27 printf("now a USB drive should appear on your PC\n");
karelv 1:c7f66434c692 28 wait (60);
karelv 1:c7f66434c692 29
karelv 1:c7f66434c692 30 printf("create directory and append to log file\n");
karelv 0:ce2fc50a859f 31 mkdir("/sd/mydir", 0777);
karelv 1:c7f66434c692 32 mylog (0);
karelv 0:ce2fc50a859f 33
karelv 1:c7f66434c692 34 printf("Entering loop of 1 min cycle time\n");
karelv 0:ce2fc50a859f 35
karelv 0:ce2fc50a859f 36 int i = 1;
karelv 0:ce2fc50a859f 37 while(1) {
karelv 0:ce2fc50a859f 38 myled = 0;
karelv 1:c7f66434c692 39 wait(5);
karelv 1:c7f66434c692 40 //sd.disconnect ();
karelv 0:ce2fc50a859f 41 mylog (i++);
karelv 1:c7f66434c692 42 //sd.connect ();
karelv 0:ce2fc50a859f 43 myled = 1;
karelv 0:ce2fc50a859f 44 wait(55);
karelv 0:ce2fc50a859f 45 }
karelv 0:ce2fc50a859f 46 }