oki
Dependencies: ChaNFS ChaNFSSD USBCDCMSC USBDevice2 mbed
main.cpp@0:19676807c7f1, 2013-09-17 (annotated)
- Committer:
- sherckuith
- Date:
- Tue Sep 17 05:36:08 2013 +0000
- Revision:
- 0:19676807c7f1
oki;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sherckuith | 0:19676807c7f1 | 1 | #include "mbed.h" |
sherckuith | 0:19676807c7f1 | 2 | #include "USBCDCMSC.h" |
sherckuith | 0:19676807c7f1 | 3 | #include "SDFileSystem.h" |
sherckuith | 0:19676807c7f1 | 4 | #include <new> |
sherckuith | 0:19676807c7f1 | 5 | |
sherckuith | 0:19676807c7f1 | 6 | DigitalOut myled(LED1); |
sherckuith | 0:19676807c7f1 | 7 | Serial pc(USBTX, USBRX); |
sherckuith | 0:19676807c7f1 | 8 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
sherckuith | 0:19676807c7f1 | 9 | USBCDCMSC cdcmsc(&sd); |
sherckuith | 0:19676807c7f1 | 10 | |
sherckuith | 0:19676807c7f1 | 11 | extern "C" |
sherckuith | 0:19676807c7f1 | 12 | void HardFault_Handler() { |
sherckuith | 0:19676807c7f1 | 13 | printf("Hard Fault!\n"); |
sherckuith | 0:19676807c7f1 | 14 | exit(-1); |
sherckuith | 0:19676807c7f1 | 15 | } |
sherckuith | 0:19676807c7f1 | 16 | |
sherckuith | 0:19676807c7f1 | 17 | void no_memory () { |
sherckuith | 0:19676807c7f1 | 18 | printf("panic: can't allocate to memory!\r\n"); |
sherckuith | 0:19676807c7f1 | 19 | exit(-1); |
sherckuith | 0:19676807c7f1 | 20 | } |
sherckuith | 0:19676807c7f1 | 21 | |
sherckuith | 0:19676807c7f1 | 22 | void listdir (Stream *st) { |
sherckuith | 0:19676807c7f1 | 23 | #if 0 |
sherckuith | 0:19676807c7f1 | 24 | // probrem: readdir() Hard Fault |
sherckuith | 0:19676807c7f1 | 25 | DIR *dir; |
sherckuith | 0:19676807c7f1 | 26 | struct dirent *ent; |
sherckuith | 0:19676807c7f1 | 27 | |
sherckuith | 0:19676807c7f1 | 28 | if ((dir = opendir("/sd")) != NULL) { |
sherckuith | 0:19676807c7f1 | 29 | st->printf("dir opened\r\n"); |
sherckuith | 0:19676807c7f1 | 30 | while ((ent = readdir(dir)) != NULL) { |
sherckuith | 0:19676807c7f1 | 31 | st->printf("%s\r\n", ent->d_name); |
sherckuith | 0:19676807c7f1 | 32 | } |
sherckuith | 0:19676807c7f1 | 33 | } else { |
sherckuith | 0:19676807c7f1 | 34 | st->printf("dir open error\r\n"); |
sherckuith | 0:19676807c7f1 | 35 | } |
sherckuith | 0:19676807c7f1 | 36 | closedir(dir); |
sherckuith | 0:19676807c7f1 | 37 | #else |
sherckuith | 0:19676807c7f1 | 38 | // direct FatFs |
sherckuith | 0:19676807c7f1 | 39 | FILINFO fno; |
sherckuith | 0:19676807c7f1 | 40 | DIR_t dir; |
sherckuith | 0:19676807c7f1 | 41 | char *fn; |
sherckuith | 0:19676807c7f1 | 42 | #if _USE_LFN |
sherckuith | 0:19676807c7f1 | 43 | static char lfn[_MAX_LFN + 1]; |
sherckuith | 0:19676807c7f1 | 44 | fno.lfname = lfn; |
sherckuith | 0:19676807c7f1 | 45 | fno.lfsize = sizeof(lfn); |
sherckuith | 0:19676807c7f1 | 46 | #endif |
sherckuith | 0:19676807c7f1 | 47 | |
sherckuith | 0:19676807c7f1 | 48 | if (f_opendir(&dir, "") == FR_OK) { |
sherckuith | 0:19676807c7f1 | 49 | for (;;) { |
sherckuith | 0:19676807c7f1 | 50 | if (f_readdir(&dir, &fno) != FR_OK) break; |
sherckuith | 0:19676807c7f1 | 51 | if (fno.fname[0] == 0) break; |
sherckuith | 0:19676807c7f1 | 52 | #if _USE_LFN |
sherckuith | 0:19676807c7f1 | 53 | fn = *fno.lfname ? fno.lfname : fno.fname; |
sherckuith | 0:19676807c7f1 | 54 | #else |
sherckuith | 0:19676807c7f1 | 55 | fn = fno.fname; |
sherckuith | 0:19676807c7f1 | 56 | #endif |
sherckuith | 0:19676807c7f1 | 57 | st->printf("%s\r\n", fn); |
sherckuith | 0:19676807c7f1 | 58 | } |
sherckuith | 0:19676807c7f1 | 59 | } |
sherckuith | 0:19676807c7f1 | 60 | #endif |
sherckuith | 0:19676807c7f1 | 61 | } |
sherckuith | 0:19676807c7f1 | 62 | |
sherckuith | 0:19676807c7f1 | 63 | void readfile (char *filename) { |
sherckuith | 0:19676807c7f1 | 64 | FILE *fp = fopen(filename, "r"); |
sherckuith | 0:19676807c7f1 | 65 | if (fp) { |
sherckuith | 0:19676807c7f1 | 66 | int c; |
sherckuith | 0:19676807c7f1 | 67 | pc.printf("file: "); |
sherckuith | 0:19676807c7f1 | 68 | for (;;) { |
sherckuith | 0:19676807c7f1 | 69 | c = fgetc(fp); |
sherckuith | 0:19676807c7f1 | 70 | if (feof(fp)) break; |
sherckuith | 0:19676807c7f1 | 71 | pc.printf("%c", c); |
sherckuith | 0:19676807c7f1 | 72 | } |
sherckuith | 0:19676807c7f1 | 73 | pc.printf("\r\n"); |
sherckuith | 0:19676807c7f1 | 74 | fclose(fp); |
sherckuith | 0:19676807c7f1 | 75 | } |
sherckuith | 0:19676807c7f1 | 76 | } |
sherckuith | 0:19676807c7f1 | 77 | |
sherckuith | 0:19676807c7f1 | 78 | int main() { |
sherckuith | 0:19676807c7f1 | 79 | int c, u = 0; |
sherckuith | 0:19676807c7f1 | 80 | Timer timer; |
sherckuith | 0:19676807c7f1 | 81 | |
sherckuith | 0:19676807c7f1 | 82 | set_new_handler(no_memory); // new handler function |
sherckuith | 0:19676807c7f1 | 83 | |
sherckuith | 0:19676807c7f1 | 84 | readfile("/sd/test.txt"); |
sherckuith | 0:19676807c7f1 | 85 | |
sherckuith | 0:19676807c7f1 | 86 | timer.start(); |
sherckuith | 0:19676807c7f1 | 87 | while(1) { |
sherckuith | 0:19676807c7f1 | 88 | if (timer.read_ms() > 500) { |
sherckuith | 0:19676807c7f1 | 89 | myled = myled ? 0 : 1; |
sherckuith | 0:19676807c7f1 | 90 | timer.reset(); |
sherckuith | 0:19676807c7f1 | 91 | } |
sherckuith | 0:19676807c7f1 | 92 | |
sherckuith | 0:19676807c7f1 | 93 | if (u != cdcmsc.configured()) { |
sherckuith | 0:19676807c7f1 | 94 | u = cdcmsc.configured(); |
sherckuith | 0:19676807c7f1 | 95 | printf("configured %d\r\n", u); |
sherckuith | 0:19676807c7f1 | 96 | } |
sherckuith | 0:19676807c7f1 | 97 | |
sherckuith | 0:19676807c7f1 | 98 | if (u && cdcmsc.available()) { |
sherckuith | 0:19676807c7f1 | 99 | // CDC -> pc serial |
sherckuith | 0:19676807c7f1 | 100 | c = cdcmsc.getc(); |
sherckuith | 0:19676807c7f1 | 101 | if (c == '?') { |
sherckuith | 0:19676807c7f1 | 102 | listdir(&cdcmsc); |
sherckuith | 0:19676807c7f1 | 103 | } else { |
sherckuith | 0:19676807c7f1 | 104 | pc.putc(c); |
sherckuith | 0:19676807c7f1 | 105 | } |
sherckuith | 0:19676807c7f1 | 106 | } |
sherckuith | 0:19676807c7f1 | 107 | if (pc.readable()) { |
sherckuith | 0:19676807c7f1 | 108 | // PC serial -> CDC |
sherckuith | 0:19676807c7f1 | 109 | c = pc.getc(); |
sherckuith | 0:19676807c7f1 | 110 | if (c == '?') { |
sherckuith | 0:19676807c7f1 | 111 | listdir(&pc); |
sherckuith | 0:19676807c7f1 | 112 | } else { |
sherckuith | 0:19676807c7f1 | 113 | if (u) { |
sherckuith | 0:19676807c7f1 | 114 | cdcmsc.putc(c); |
sherckuith | 0:19676807c7f1 | 115 | } else { |
sherckuith | 0:19676807c7f1 | 116 | pc.putc(c); |
sherckuith | 0:19676807c7f1 | 117 | } |
sherckuith | 0:19676807c7f1 | 118 | } |
sherckuith | 0:19676807c7f1 | 119 | } |
sherckuith | 0:19676807c7f1 | 120 | } |
sherckuith | 0:19676807c7f1 | 121 | } |