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.
10 years, 7 months ago.
How to read an audio file from USB flash by using mbed lpc 1768?
I am trying to write a code that reads from USB flash that plugged in the mbed application board
any suggestions?
2 Answers
10 years, 7 months ago.
Use the USBDevice library, https://mbed.org/users/mbed_official/code/USBDevice/ And create a USBMSD (Mass storage device). That combined with the FATFileSystem library should allow you to read the contents of the USB stick.
Decoding the contents of the audio file so that you can play them will depend on the file format, .wav is fairly straightforward, other formats could get tricky.
10 years, 7 months ago.
Hi, just tried this program, I can get PC to read SD card ,
but how can I get MBED to read/write to/from SD card,
I attempted:
mkdir ("/hid_sd/Short", 0777); FILE *fpl = fopen("/hid_sd/Short/Rep.txt", "w"); // "w"); if(fpl == NULL) { printf ("Error .. Could not open SHORT file for write\r\n"); } else { fprintf(fpl, "Hello fun SD Card World 1 !"); fprintf(fpl, "Hello fun SD Card World 2 !"); fprintf(fpl, "Hello fun SD Card World 3 !"); fprintf(fpl, "Hello fun SD Card World 4 !"); fclose(fpl); printf ("\r\n LLLOONNGG >> :)\r\n"); }
Am I missing some thing ?
Confused :(
This should probably be a different question.
That part of the code looks fine.
What do you have before that point? You need to include the SDFileSystem library and then create an SDFileSystem giving it the SPI pins that are connected to the SD card. See https://mbed.org/users/simon/code/SDFileSystem_HelloWorld/ for details (Don't forget the change the pins used to match your setup)
posted by 21 Jul 2014This should probably be a different question.
That part of the code looks fine.
What do you have before that point? You need to include the SDFileSystem library and then create an SDFileSystem giving it the SPI pins that are connected to the SD card. See https://mbed.org/users/simon/code/SDFileSystem_HelloWorld/ for details (Don't forget the change the pins used to match your setup)
posted by 21 Jul 2014Thanks for your response;
This is my (messy) code so far ..
#include "mbed.h" #include "USBMSD_SD.h" #include "SDFileSystem.h" #include <ctype.h> USBMSD_SD hid_sd(p5, p6, p7, p8, 8, 8); DigitalOut l1(LED1); SDFileSystem sd(p5, p6, p7, p8, "sd"); //This report will contain data to be sent HID_REPORT send_report; HID_REPORT recv_report; void ListFiles (void); int File_Sizes [120]; typedef char tStringG [32]; tStringG Files_Avalable [120]; int K; int Stat ; int main() { send_report.length = 8; printf ("\r\n\r\nStart\r\n\r\n"); mkdir ("/sd/Short", 0777); FILE *fpl = fopen("/sd/Short/Rep.txt", "w"); // "w"); if(fpl == NULL) { printf ("Error .. Could not open SHORT file for write\r\n"); } else { fprintf(fpl, "Hello fun SD Card World 1 !"); fclose(fpl); printf ("\r\n LLLOONNGG >> :)\r\n"); } while (1) { if (K++>100000) { ListFiles(); //Fill the report for (int i = 0; i < send_report.length; i++) send_report.data[i] = rand() & 0xff; //Send the report hid_sd.send(&send_report); K = 0; } //try to read a msg if(hid_sd.readNB(&recv_report)) { l1 = !l1; for(int i = 0; i < recv_report.length; i++) { printf("%d ", recv_report.data[i]); } printf("\r\n"); } } } //////// void ListFiles (void) { char dirn[50]; char File_Name[25]; DIR *dir = NULL; struct dirent *drnt = NULL; char * findprn ; int f_cnt = 0; //sprintf (dirn ,"/hid_sd/Print_DIR"); sprintf (dirn ,"/sd/Print_DIR"); printf("Input dir name: {%s}\r\n",dirn); //gets(dirn); dir=opendir(dirn); if(dir) { printf("\r\n> output:\r\n"); while(drnt = readdir(dir)) //while(readdir(dir)) { sprintf (File_Name, drnt->d_name); printf("%-20s\t .. #", File_Name); findprn = strstr (File_Name,".prn"); printf ("....%d....\r\n",findprn); // Add files to list .... //? sprintf (Files_Avalable [f_cnt++],File_Name); } closedir(dir); } else { printf("Can not open directory '%s'\r\n", dirn); } printf ("\r\n\r\n"); } /******************************************************************************/
As you can see, i did add SDFileSystem (from the working bit I have added from annother project)
but I get no disk error ??
Ceri
posted by 21 Jul 2014Am I correct you are trying to use both the SD card as local storage and have it available via USB when you plug it in your PC? In that case, have a look at: https://mbed.org/users/Sissors/code/USBFileSystem/. Specifically implementation for SD cards here: https://mbed.org/users/karelv/code/SD_USB_FS_HelloWorld/
If this is what you want you can't simply include both USBMSD and SDFileSystem without running into issues.
posted by 21 Jul 2014Assigned to
10 years, 6 months ago.This means that the question has been accepted and is being worked on.