Suga koubou / Mbed 2 deprecated USB_CDC_MSD_Hello

Dependencies:   ChaNFSSD mbed ChaNFS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "USBCDCMSC.h"
00003 #include "SDFileSystem.h"
00004 #include <new>
00005 
00006 DigitalOut myled(LED1);
00007 Serial pc(USBTX, USBRX);
00008 SDFileSystem sd(p5, p6, p7, p8, "sd");
00009 USBCDCMSC cdcmsc(&sd);
00010 
00011 extern "C"
00012 void HardFault_Handler() {
00013     printf("Hard Fault!\n");
00014     exit(-1);
00015 }
00016 
00017 void no_memory () {
00018     printf("panic: can't allocate to memory!\r\n");
00019     exit(-1);
00020 }
00021 
00022 void listdir (Stream *st) {
00023 #if 0
00024     // probrem: readdir() Hard Fault
00025     DIR *dir;
00026     struct dirent *ent;
00027     
00028     if ((dir = opendir("/sd")) != NULL) {
00029         st->printf("dir opened\r\n");
00030         while ((ent = readdir(dir)) != NULL) {
00031             st->printf("%s\r\n", ent->d_name);
00032         }
00033     } else {
00034         st->printf("dir open error\r\n");
00035     }
00036     closedir(dir);
00037 #else
00038     // direct FatFs
00039     FILINFO fno;
00040     DIR_t dir;
00041     char *fn;
00042 #if _USE_LFN
00043     static char lfn[_MAX_LFN + 1];
00044     fno.lfname = lfn;
00045     fno.lfsize = sizeof(lfn);
00046 #endif
00047 
00048     if (f_opendir(&dir, "") == FR_OK) {
00049         for (;;) {
00050             if (f_readdir(&dir, &fno) != FR_OK) break;
00051             if (fno.fname[0] == 0) break;
00052 #if _USE_LFN
00053             fn = *fno.lfname ? fno.lfname : fno.fname;
00054 #else
00055             fn = fno.fname;
00056 #endif
00057             st->printf("%s\r\n", fn);            
00058         }
00059     }
00060 #endif
00061 }
00062 
00063 void readfile (char *filename) {
00064     FILE *fp = fopen(filename, "r");
00065     if (fp) {
00066         int c;
00067         pc.printf("file: ");
00068         for (;;) {
00069             c = fgetc(fp);
00070             if (feof(fp)) break;
00071             pc.printf("%c", c);
00072         }
00073         pc.printf("\r\n");
00074         fclose(fp); 
00075     }
00076 }
00077 
00078 int main() {
00079     int c, u = 0;
00080     Timer timer;
00081 
00082     set_new_handler(no_memory); // new handler function
00083 
00084     readfile("/sd/test.txt");
00085 
00086     timer.start();
00087     while(1) {
00088         if (timer.read_ms() > 500) {
00089             myled = myled ? 0 : 1;
00090             timer.reset();
00091         }
00092         
00093         if (u != cdcmsc.configured()) {
00094             u = cdcmsc.configured();
00095             printf("configured %d\r\n", u);
00096         }
00097     
00098         if (u && cdcmsc.available()) {
00099             // CDC -> pc serial
00100             c = cdcmsc.getc();
00101             if (c == '?') {
00102                 listdir(&cdcmsc);
00103             } else {
00104                 pc.putc(c);
00105             }
00106         }
00107         if (pc.readable()) {
00108             // PC serial -> CDC
00109             c = pc.getc();
00110             if (c == '?') {
00111                 listdir(&pc);
00112             } else {
00113                 if (u) {
00114                     cdcmsc.putc(c);
00115                 } else {
00116                     pc.putc(c);
00117                 }
00118             }
00119         }
00120     }
00121 }