Shinichiro Nakamura / Mbed 2 deprecated SDCARD_PFF_CPP

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*---------------------------------------------------------------*/
00002 /* Petit FAT file system module test program R0.02 (C)ChaN, 2009 */
00003 /*---------------------------------------------------------------*/
00004 
00005 /*
00006  * Ported by Shinichiro Nakamura (CuBeatSystems)
00007  * http://shinta.main.jp/
00008  * 2010/06/01
00009  */
00010 
00011 #include <string.h>
00012 #include "mbed.h"
00013 #include "libpff/diskio.h"
00014 #include "libpff/pff.h"
00015 
00016 Serial pc(USBTX, USBRX);
00017 
00018 char Line[128];        /* Console input buffer */
00019 
00020 static
00021 void put_rc (PetitFileSystem::FRESULT rc) {
00022     const char *p;
00023     static const char str[] =
00024         "OK\0" "DISK_ERR\0" "NOT_READY\0" "NO_FILE\0" "NO_PATH\0"
00025         "NOT_OPENED\0" "NOT_ENABLED\0" "NO_FILE_SYSTEM\0";
00026     uint8_t i;
00027 
00028     for (p = str, i = 0; i != (uint8_t)rc && p; i++) {
00029         while (p++);
00030     }
00031     pc.printf("rc=%u FR_%S\n", (WORD)rc, p);
00032 }
00033 
00034 
00035 
00036 static
00037 void put_drc (BYTE res) {
00038     pc.printf("rc=%d\n", res);
00039 }
00040 
00041 
00042 
00043 static
00044 void get_line (char *buff, BYTE len) {
00045     BYTE c, i;
00046 
00047     i = 0;
00048     for (;;) {
00049         c = pc.getc();
00050         if (c == '\r') break;
00051         if ((c == '\b') && i) {
00052             i--;
00053             pc.putc('\b');
00054             pc.putc(' ');
00055             pc.putc('\b');
00056         }
00057         if ((c >= ' ') && (i < len - 1)) {
00058             buff[i++] = c;
00059             pc.putc(c);
00060         }
00061     }
00062     buff[i] = 0;
00063     pc.putc('\n');
00064 }
00065 
00066 
00067 
00068 static
00069 void put_dump (const BYTE *buff, DWORD ofs, int cnt) {
00070     BYTE n;
00071 
00072     pc.printf("%08x", ofs);
00073     pc.putc(' ');
00074     for (n = 0; n < cnt; n++) {
00075         pc.putc(' ');
00076         pc.printf("%02x", buff[n]);
00077     }
00078     pc.printf("  ");
00079     for (n = 0; n < cnt; n++) {
00080         pc.putc(((buff[n] < 0x20)||(buff[n] >= 0x7F)) ? '.' : buff[n]);
00081     }
00082     pc.putc('\n');
00083 }
00084 
00085 
00086 
00087 /*-----------------------------------------------------------------------*/
00088 /* Main                                                                  */
00089 
00090 
00091 int main (void) {
00092     char *ptr;
00093     long p1, p2;
00094     BYTE res;
00095     WORD s1, s2, s3, ofs, cnt, w;
00096     PetitFileSystem pfs;
00097     PetitFileSystem::FILINFO fno;
00098 
00099     pc.baud(19200);
00100     pc.printf("\nPFF test monitor\n");
00101 
00102     for (;;) {
00103         pc.putc('>');
00104         get_line(Line, sizeof(Line));
00105         ptr = Line;
00106 
00107         switch (*ptr++) {
00108             case '?' :
00109                 pc.printf("di                - Initialize physical drive\n");
00110                 pc.printf("dd <sector> <ofs> - Dump partial secrtor 128 bytes\n");
00111                 pc.printf("fi                - Mount the volume\n");
00112                 pc.printf("fo <file>         - Open a file\n");
00113                 pc.printf("fd                - Read the file 128 bytes and dump it\n");
00114                 pc.printf("fw <len> <val>    - Write data to the file\n");
00115                 pc.printf("fp                - Write console input to the file\n");
00116                 pc.printf("fe <ofs>          - Move file pointer of the file\n");
00117                 pc.printf("fl [<path>]       - Directory listing\n");
00118                 break;
00119             case 'd' :
00120                 switch (*ptr++) {
00121                     case 'i' :    /* di - Initialize physical drive */
00122                         res = disk_initialize();
00123                         put_drc(res);
00124                         break;
00125                     case 'd' :    /* dd <sector> <ofs> - Dump partial secrtor 128 bytes */
00126                         if (2 != sscanf(ptr, "%x %x", &p1, &p2)) {
00127                             break;
00128                         }
00129                         s2 = p2;
00130                         res = disk_readp((BYTE*)Line, p1, s2, 128);
00131                         if (res) {
00132                             put_drc(res);
00133                             break;
00134                         }
00135                         s3 = s2 + 128;
00136                         for (ptr = Line; s2 < s3; s2 += 16, ptr += 16, ofs += 16) {
00137                             s1 = (s3 - s2 >= 16) ? 16 : s3 - s2;
00138                             put_dump((BYTE*)ptr, s2, s1);
00139                         }
00140                         break;
00141                 }
00142                 break;
00143             case 'f' :
00144                 switch (*ptr++) {
00145 
00146                     case 'i' :    /* fi - Mount the volume */
00147                         put_rc(pfs.mount());
00148                         break;
00149 
00150                     case 'o' :    /* fo <file> - Open a file */
00151                         while (*ptr == ' ') ptr++;
00152                         put_rc(pfs.open(ptr));
00153                         break;
00154 #if _USE_READ
00155                     case 'd' :    /* fd - Read the file 128 bytes and dump it */
00156                         ofs = pfs.getofs();
00157                         res = pfs.read(Line, sizeof(Line), &s1);
00158                         if (res != PetitFileSystem::FR_OK) {
00159                             put_rc((PetitFileSystem::FRESULT)res);
00160                             break;
00161                         }
00162                         ptr = Line;
00163                         while (s1) {
00164                             s2 = (s1 >= 16) ? 16 : s1;
00165                             s1 -= s2;
00166                             put_dump((BYTE*)ptr, ofs, s2);
00167                             ptr += 16;
00168                             ofs += 16;
00169                         }
00170                         break;
00171 #endif
00172 #if _USE_WRITE
00173                     case 'w' :    /* fw <len> <val> - Write data to the file */
00174                         if (2 != sscanf(ptr, "%x %x", &p1, &p2)) {
00175                             break;
00176                         }
00177                         for (s1 = 0; s1 < sizeof(Line); Line[s1++] = (BYTE)p2) ;
00178                         p2 = 0;
00179                         while (p1) {
00180                             if ((UINT)p1 >= sizeof(Line)) {
00181                                 cnt = sizeof(Line);
00182                                 p1 -= sizeof(Line);
00183                             } else {
00184                                 cnt = (WORD)p1;
00185                                 p1 = 0;
00186                             }
00187                             res = pfs.write(Line, cnt, &w);    /* Write data to the file */
00188                             p2 += w;
00189                             if (res != PetitFileSystem::FR_OK) {
00190                                 put_rc((PetitFileSystem::FRESULT)res);
00191                                 break;
00192                             }
00193                             if (cnt != w) break;
00194                         }
00195                         res = pfs.write(0, 0, &w);        /* Finalize the write process */
00196                         put_rc((PetitFileSystem::FRESULT)res);
00197                         if (res == PetitFileSystem::FR_OK)
00198                             pc.printf("%lu bytes written.\n", p2);
00199                         break;
00200                     case 'p' :    /* fp - Write console input to the file */
00201                         pc.printf("Type any line to write. A blank line finalize the write operation.\n");
00202                         for (;;) {
00203                             get_line(Line, sizeof(Line));
00204                             if (!Line[0]) break;
00205                             strcat(Line, "\r\n");
00206                             res = pfs.write(Line, strlen(Line), &w);    /* Write a line to the file */
00207                             if (res) break;
00208                         }
00209                         res = pfs.write(0, 0, &w);        /* Finalize the write process */
00210                         put_rc((PetitFileSystem::FRESULT)res);
00211                         break;
00212 #endif
00213 #if _USE_LSEEK
00214                     case 'e' :    /* fe <ofs> - Move file pointer of the file */
00215                         if (1 != sscanf(ptr, "%x", &p1)) {
00216                             break;
00217                         }
00218                         res = pfs.lseek(p1);
00219                         put_rc((PetitFileSystem::FRESULT)res);
00220                         if (res == PetitFileSystem::FR_OK) {
00221                             pc.printf("OK\n");
00222                         } else {
00223                             pc.printf("NG\n");
00224                         }
00225                         break;
00226 #endif
00227 #if _USE_DIR
00228                     case 'l' :    /* fl [<path>] - Directory listing */
00229                         while (*ptr == ' ') ptr++;
00230                         res = pfs.opendir(ptr);
00231                         if (res) {
00232                             put_rc((PetitFileSystem::FRESULT)res);
00233                             break;
00234                         }
00235                         s1 = 0;
00236                         for (;;) {
00237                             res = pfs.readdir(&fno);
00238                             if (res != PetitFileSystem::FR_OK) {
00239                                 put_rc((PetitFileSystem::FRESULT)res);
00240                                 break;
00241                             }
00242                             if (!fno.fname[0]) break;
00243                             if (fno.fattrib & AM_DIR)
00244                                 pc.printf("   <DIR>   %s\n", fno.fname);
00245                             else
00246                                 pc.printf("%9lu  %s\n", fno.fsize, fno.fname);
00247                             s1++;
00248                         }
00249                         pc.printf("%u item(s)\n", s1);
00250                         break;
00251 #endif
00252                 }
00253                 break;
00254         }
00255     }
00256 
00257 }
00258 
00259 
00260