![](/media/cache/profiles/5f65e12a53c282c15a9b8188db4f7d1f.50x50_q85.jpg)
This program is for transferring all contents of SD card to any flash drive like pen drive. After importing this program, you will see 3 FATFileSystem libraries, one in main folder, one in SDFileSystem folder and one in USBHost folder. Delete two libraries, which are in SDFileSystem folder and USBHost folder. But don't delete the FATFileSystem library, which are in main folder.
Dependencies: FATFileSystem SDFileSystem TextLCD USBHost mbed
main.cpp@0:5838d8c4432a, 2013-06-30 (annotated)
- Committer:
- vinit
- Date:
- Sun Jun 30 22:51:00 2013 +0000
- Revision:
- 0:5838d8c4432a
This Program is for transferring all contents of SD card to any FLASH drive.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vinit | 0:5838d8c4432a | 1 | #include "mbed.h" |
vinit | 0:5838d8c4432a | 2 | #include "USBHostMSD.h" |
vinit | 0:5838d8c4432a | 3 | #include "TextLCD.h" |
vinit | 0:5838d8c4432a | 4 | #include "SDFileSystem.h" |
vinit | 0:5838d8c4432a | 5 | |
vinit | 0:5838d8c4432a | 6 | DigitalOut led(LED1); |
vinit | 0:5838d8c4432a | 7 | |
vinit | 0:5838d8c4432a | 8 | TextLCD lcd(p19, p20, p27, p28, p29, p30, TextLCD::LCD20x2); // rs, e, d4-d7 |
vinit | 0:5838d8c4432a | 9 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
vinit | 0:5838d8c4432a | 10 | //USBHostMSD msd("usb"); |
vinit | 0:5838d8c4432a | 11 | |
vinit | 0:5838d8c4432a | 12 | void listdir(void); |
vinit | 0:5838d8c4432a | 13 | void FileCopy(char *, char *); |
vinit | 0:5838d8c4432a | 14 | void copyallusb(void); |
vinit | 0:5838d8c4432a | 15 | |
vinit | 0:5838d8c4432a | 16 | void listdir(void) { |
vinit | 0:5838d8c4432a | 17 | DIR *d; |
vinit | 0:5838d8c4432a | 18 | struct dirent *p; |
vinit | 0:5838d8c4432a | 19 | |
vinit | 0:5838d8c4432a | 20 | d = opendir("/sd"); |
vinit | 0:5838d8c4432a | 21 | if (d != NULL) { |
vinit | 0:5838d8c4432a | 22 | while ((p = readdir(d)) != NULL) { |
vinit | 0:5838d8c4432a | 23 | lcd.cls(); |
vinit | 0:5838d8c4432a | 24 | lcd.printf(" - %s\r\n", p->d_name); |
vinit | 0:5838d8c4432a | 25 | } |
vinit | 0:5838d8c4432a | 26 | } else { |
vinit | 0:5838d8c4432a | 27 | lcd.cls(); |
vinit | 0:5838d8c4432a | 28 | lcd.printf("opening failed, try again\n"); |
vinit | 0:5838d8c4432a | 29 | } |
vinit | 0:5838d8c4432a | 30 | closedir(d); |
vinit | 0:5838d8c4432a | 31 | } |
vinit | 0:5838d8c4432a | 32 | |
vinit | 0:5838d8c4432a | 33 | void FileCopy(char *fsrc , char *ftrgt) { |
vinit | 0:5838d8c4432a | 34 | FILE *fs = fopen(fsrc, "rb"); |
vinit | 0:5838d8c4432a | 35 | if(fs == NULL) { |
vinit | 0:5838d8c4432a | 36 | lcd.cls(); |
vinit | 0:5838d8c4432a | 37 | lcd.printf("couldn't open sd\n"); |
vinit | 0:5838d8c4432a | 38 | } |
vinit | 0:5838d8c4432a | 39 | FILE *ft = fopen(ftrgt, "wb"); |
vinit | 0:5838d8c4432a | 40 | if(ft == NULL) { |
vinit | 0:5838d8c4432a | 41 | lcd.cls(); |
vinit | 0:5838d8c4432a | 42 | lcd.printf("couldn't open usb\n"); |
vinit | 0:5838d8c4432a | 43 | } |
vinit | 0:5838d8c4432a | 44 | while(1) { |
vinit | 0:5838d8c4432a | 45 | int ch = fgetc(fs); |
vinit | 0:5838d8c4432a | 46 | if(ch == EOF) |
vinit | 0:5838d8c4432a | 47 | break; |
vinit | 0:5838d8c4432a | 48 | else |
vinit | 0:5838d8c4432a | 49 | fputc(ch, ft); |
vinit | 0:5838d8c4432a | 50 | } |
vinit | 0:5838d8c4432a | 51 | fclose(fs); |
vinit | 0:5838d8c4432a | 52 | fclose(ft); |
vinit | 0:5838d8c4432a | 53 | } |
vinit | 0:5838d8c4432a | 54 | |
vinit | 0:5838d8c4432a | 55 | void copyallusb(void) { |
vinit | 0:5838d8c4432a | 56 | USBHostMSD msd("usb"); |
vinit | 0:5838d8c4432a | 57 | |
vinit | 0:5838d8c4432a | 58 | // try to connect a MSD device |
vinit | 0:5838d8c4432a | 59 | while(!msd.connect()) { |
vinit | 0:5838d8c4432a | 60 | wait_ms(500); |
vinit | 0:5838d8c4432a | 61 | lcd.cls(); |
vinit | 0:5838d8c4432a | 62 | lcd.printf("connecting to USB"); |
vinit | 0:5838d8c4432a | 63 | } |
vinit | 0:5838d8c4432a | 64 | |
vinit | 0:5838d8c4432a | 65 | char srstrgt[128]; |
vinit | 0:5838d8c4432a | 66 | char filesrs[128]; |
vinit | 0:5838d8c4432a | 67 | char filetrgt[128]; |
vinit | 0:5838d8c4432a | 68 | DIR *d; |
vinit | 0:5838d8c4432a | 69 | struct dirent *p; |
vinit | 0:5838d8c4432a | 70 | |
vinit | 0:5838d8c4432a | 71 | d = opendir("/sd/"); |
vinit | 0:5838d8c4432a | 72 | if (d != NULL) { |
vinit | 0:5838d8c4432a | 73 | lcd.cls(); |
vinit | 0:5838d8c4432a | 74 | lcd.printf("copy starting\n"); |
vinit | 0:5838d8c4432a | 75 | while ((p = readdir(d)) != NULL) { |
vinit | 0:5838d8c4432a | 76 | sprintf(srstrgt, "%s", p->d_name); |
vinit | 0:5838d8c4432a | 77 | sprintf(filesrs, "/sd/%s", srstrgt); |
vinit | 0:5838d8c4432a | 78 | sprintf(filetrgt, "/usb/%s", srstrgt); |
vinit | 0:5838d8c4432a | 79 | lcd.cls(); |
vinit | 0:5838d8c4432a | 80 | lcd.printf("copying-\n"); |
vinit | 0:5838d8c4432a | 81 | lcd.printf("%s\n", srstrgt); |
vinit | 0:5838d8c4432a | 82 | FileCopy(filesrs, filetrgt); |
vinit | 0:5838d8c4432a | 83 | } |
vinit | 0:5838d8c4432a | 84 | lcd.cls(); |
vinit | 0:5838d8c4432a | 85 | lcd.printf("File copy done!\n"); |
vinit | 0:5838d8c4432a | 86 | } else { |
vinit | 0:5838d8c4432a | 87 | lcd.printf("No File, try again\n"); |
vinit | 0:5838d8c4432a | 88 | } |
vinit | 0:5838d8c4432a | 89 | closedir(d); |
vinit | 0:5838d8c4432a | 90 | |
vinit | 0:5838d8c4432a | 91 | // if device disconnected, try to connect again |
vinit | 0:5838d8c4432a | 92 | //if (!msd.connected()) |
vinit | 0:5838d8c4432a | 93 | //break; |
vinit | 0:5838d8c4432a | 94 | } |
vinit | 0:5838d8c4432a | 95 | |
vinit | 0:5838d8c4432a | 96 | |
vinit | 0:5838d8c4432a | 97 | int main() { |
vinit | 0:5838d8c4432a | 98 | lcd.cls(); |
vinit | 0:5838d8c4432a | 99 | lcd.printf("JAI MATA DI!\n"); |
vinit | 0:5838d8c4432a | 100 | wait(2); |
vinit | 0:5838d8c4432a | 101 | copyallusb(); |
vinit | 0:5838d8c4432a | 102 | while(1) { |
vinit | 0:5838d8c4432a | 103 | led=!led; |
vinit | 0:5838d8c4432a | 104 | Thread::wait(500); |
vinit | 0:5838d8c4432a | 105 | } |
vinit | 0:5838d8c4432a | 106 | } |