Shop OBEX P1 Docs P2 Docs Learn Events
filetest.c revisited — Parallax Forums

filetest.c revisited

RsadeikaRsadeika Posts: 3,837
edited 2012-09-11 13:23 in Propeller 1
I am doing a test run with filetest.c. The code below is minimal amount to check and see if the SD card got loaded, and show what is on the card. I have an error for the dfs_mount() command, I can not remember how to fix the problem.

Ray

Board Type - DNA:SQI; Compiler Type - C; Memory Model - CMM; Optimization - Os Size. Nothing else checked in Compiler or Linker.
/**
 * @file filetest.c
 * This is the main filetest program start point.
 */
#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
//#include <cog.h>
#include <propeller.h>
//#include <unistd.h>
//#include <sys/stat.h>
#include <sys/sd.h>
#include <dirent.h>


extern _Driver _FileDriver;

char *FindChar(char *ptr, int val);

FILE *stdinfile;
FILE *stdoutfile;

void List(int argc, char **argv)
{
    int i, j;
    char *ptr;
    char fname[13];
    int32_t count = 0;
    uint32_t filesize;
    uint32_t longflag = 0;
    char *path;
    char drwx[5];
    int column;
    int prevlen;
    DIR *dirp;
    struct dirent *entry; /* <dirent.h> */
    
    for (j = 1; j < argc; j++)
    {
        if (argv[j][0] == '-')
        {
            if (!strcmp(argv[j], "-1"))
                longflag = 1;
            else
                printf("Unknown option \"%s\"\n", argv[j]);
        }
        else
            count++;
    }
    //List directories
    for (j = 1; j < argc || count == 0; j++)
    {
        if (count == 0)
        {
            count--;
            path = "./";
        }
        else if (argv[j][0] == '-')
            continue;
        else
            path = argv[j];
        if (count >= 2)
            fprintf(stdoutfile, "\n%s:\n", path);
        dirp = opendir(path);
        
        if (!dirp)
        {
            perror(path);
            continue;
        }
        
        column = 0;
        prevlen = 14;
        while (entry = readdir(dirp))
        {
            if (entry->name[0] == '.') continue;
            ptr = fname;
            for (i = 0; i < 8; i++)
            {
                if (entry->name[i] == ' ') break;
                *ptr++ = tolower(entry->name[i]);
            }
            if (entry->name[8] != ' ')
            {
                *ptr++ = '.';
                for (i = 8; i < 11; i++)
                {
                    if (entry->name[i] == ' ') break;
                    *ptr++ = tolower(entry->name[i]);
                }
            }
            *ptr = 0;
            filesize = entry->filesize_3;
            filesize = (filesize << 8) | entry->filesize_2;
            filesize = (filesize << 8) | entry->filesize_1;
            filesize = (filesize << 8) | entry->filesize_0;
            strcpy(drwx, "-rw-");
            if (entry->attr & ATTR_READ_ONLY)
                drwx[2] = '-';
            if (entry->attr & ATTR_ARCHIVE)
                drwx[3] = 'x';
            if (entry->attr & ATTR_DIRECTORY)
            {
                drwx[0] = 'd';
                drwx[3] = 'x';
            }
            if (longflag)
                fprintf(stdoutfile, "%s %8d %s\n", drwx, filesize, fname);
            else if (++column == 5)
            {
                for (i = prevlen; i < 14; i++) fprintf(stdoutfile, " ");
                fprintf(stdoutfile, "%s\n", fname);
                column = 0;
                prevlen = 14;
            }
            else
            {
                for (i = prevlen; i < 14; i++) fprintf(stdoutfile, " ");
                prevlen = strlen(fname);
                fprintf(stdoutfile, "%s", fname);
            }
        }
        closedir(dirp);
        if (!longflag && column)
            fprintf(stdoutfile, "\n");
    }
}

char *SkipChar(char *ptr, int val)
{
    while (*ptr)
    {
        if (*ptr != val) break;
        ptr++;
    }
    return ptr;
}

char *FindChar(char *ptr, int val)
{
    while (*ptr)
    {
        if (*ptr == val) break;
        ptr++;
    }
    return ptr;
}

int tokenize(char *ptr, char *tokens[])
{
    int num = 0;
    while (*ptr)
    {
        ptr = SkipChar(ptr, ' ');
        if (*ptr == 0) break;
        if (ptr[0] == '>')
        {
            ptr++;
            if (ptr[0] == '>')
            {
                tokens[num++] = ">>";
                ptr++;
            }
            else
                tokens[num++] = ">";
            continue;
        }
        if (ptr[0] == '<')
        {
            ptr++;
            tokens[num++] = "<";
            continue;
        }
        tokens[num++] = ptr;
        ptr = FindChar(ptr, ' ');
        if (*ptr) *ptr++ = 0;
    }
    return num;
}

/**
 * Main program function.
 */
int main(void)
{
    int num;
    char *tokens[20];
    uint8_t buffer[80];  /* propeller.h */
    
    stdinfile = stdin;
    stdoutfile = stdout;

    buffer[0] = 0;
    buffer[1] = 1;
    buffer[2] = 2;
    buffer[3] = 3;
    LoadSDDriver(buffer);
    
    dfs_mount();
    
    waitcnt(CLKFREQ/6+CNT);
    while(1)
    {        
        printf(">");
        fflush(stdout);
        gets(buffer);
        num = tokenize(buffer, tokens);
        if (num == 0) continue;
        if (!strcmp(tokens[0], "quit"))
            break;
        else if (!strcmp(tokens[0], "ls"))
            List(num, tokens);
        else
        {
            printf("Invalid command\n");        
        }
    }

    
    printf("\nSystem Stop");
    return 0;
}


Build Status
Project Directory: E:/PropGCC_SimpleIDE/PropGCC/filetest/

propeller-elf-gcc.exe -o a.out -Os -mcmm -I . -fno-exceptions filetest.c
filetest.c: In function 'main':

filetest.c:199:5: error: too few arguments to function 'dfs_mount'

c:\propgcc\bin\../lib/gcc/propeller-elf/4.6.1/../../../../propeller-elf/include/sys/sd.h:118:10: note: declared here

Done. Build Failed!

Click error or warning messages above to debug.

Comments

  • RsadeikaRsadeika Posts: 3,837
    edited 2012-09-11 05:43
    I found the missing code, now the program below will mount and show what is on the SD card. ls - lists the files, and quit - quits the program. The added code is set up for the DNA:SQI board (pins 0,1,2,3), so it should work for any board that has the SD reader on those pins.

    So far the Code Size is 18,584 bytes (19,372 total), not sure if this is good or bad for CMM memory model.

    Ray
    /**
     * @file filetest.c
     * This is the main filetest program start point.
     */
    #include <stdio.h>
    //#include <stdlib.h>
    //#include <string.h>
    //#include <cog.h>
    #include <propeller.h>
    //#include <unistd.h>
    //#include <sys/stat.h>
    #include <sys/sd.h>
    #include <dirent.h>
    
    
    extern _Driver _FileDriver;
    
    char *FindChar(char *ptr, int val);
    
    FILE *stdinfile;
    FILE *stdoutfile;
    
    void List(int argc, char **argv)
    {
        int i, j;
        char *ptr;
        char fname[13];
        int32_t count = 0;
        uint32_t filesize;
        uint32_t longflag = 0;
        char *path;
        char drwx[5];
        int column;
        int prevlen;
        DIR *dirp;
        struct dirent *entry; /* <dirent.h> */
        
        for (j = 1; j < argc; j++)
        {
            if (argv[j][0] == '-')
            {
                if (!strcmp(argv[j], "-1"))
                    longflag = 1;
                else
                    printf("Unknown option \"%s\"\n", argv[j]);
            }
            else
                count++;
        }
        //List directories
        for (j = 1; j < argc || count == 0; j++)
        {
            if (count == 0)
            {
                count--;
                path = "./";
            }
            else if (argv[j][0] == '-')
                continue;
            else
                path = argv[j];
            if (count >= 2)
                fprintf(stdoutfile, "\n%s:\n", path);
            dirp = opendir(path);
            
            if (!dirp)
            {
                perror(path);
                continue;
            }
            
            column = 0;
            prevlen = 14;
            while (entry = readdir(dirp))
            {
                if (entry->name[0] == '.') continue;
                ptr = fname;
                for (i = 0; i < 8; i++)
                {
                    if (entry->name[i] == ' ') break;
                    *ptr++ = tolower(entry->name[i]);
                }
                if (entry->name[8] != ' ')
                {
                    *ptr++ = '.';
                    for (i = 8; i < 11; i++)
                    {
                        if (entry->name[i] == ' ') break;
                        *ptr++ = tolower(entry->name[i]);
                    }
                }
                *ptr = 0;
                filesize = entry->filesize_3;
                filesize = (filesize << 8) | entry->filesize_2;
                filesize = (filesize << 8) | entry->filesize_1;
                filesize = (filesize << 8) | entry->filesize_0;
                strcpy(drwx, "-rw-");
                if (entry->attr & ATTR_READ_ONLY)
                    drwx[2] = '-';
                if (entry->attr & ATTR_ARCHIVE)
                    drwx[3] = 'x';
                if (entry->attr & ATTR_DIRECTORY)
                {
                    drwx[0] = 'd';
                    drwx[3] = 'x';
                }
                if (longflag)
                    fprintf(stdoutfile, "%s %8d %s\n", drwx, filesize, fname);
                else if (++column == 5)
                {
                    for (i = prevlen; i < 14; i++) fprintf(stdoutfile, " ");
                    fprintf(stdoutfile, "%s\n", fname);
                    column = 0;
                    prevlen = 14;
                }
                else
                {
                    for (i = prevlen; i < 14; i++) fprintf(stdoutfile, " ");
                    prevlen = strlen(fname);
                    fprintf(stdoutfile, "%s", fname);
                }
            }
            closedir(dirp);
            if (!longflag && column)
                fprintf(stdoutfile, "\n");
        }
    }
    
    char *SkipChar(char *ptr, int val)
    {
        while (*ptr)
        {
            if (*ptr != val) break;
            ptr++;
        }
        return ptr;
    }
    
    char *FindChar(char *ptr, int val)
    {
        while (*ptr)
        {
            if (*ptr == val) break;
            ptr++;
        }
        return ptr;
    }
    
    int tokenize(char *ptr, char *tokens[])
    {
        int num = 0;
        while (*ptr)
        {
            ptr = SkipChar(ptr, ' ');
            if (*ptr == 0) break;
            if (ptr[0] == '>')
            {
                ptr++;
                if (ptr[0] == '>')
                {
                    tokens[num++] = ">>";
                    ptr++;
                }
                else
                    tokens[num++] = ">";
                continue;
            }
            if (ptr[0] == '<')
            {
                ptr++;
                tokens[num++] = "<";
                continue;
            }
            tokens[num++] = ptr;
            ptr = FindChar(ptr, ' ');
            if (*ptr) *ptr++ = 0;
        }
        return num;
    }
    
    /**
     * Main program function.
     */
    int main(void)
    {
        int num;
        char *tokens[20];
        uint8_t buffer[80];  /* propeller.h */
        
        stdinfile = stdin;
        stdoutfile = stdout;
    
    _SD_Params* mountParams = (_SD_Params*)-1;   
    static _SD_Params params =
    {
        AttachmentType: _SDA_SingleSPI,
        pins:
        {
            SingleSPI:
            {
                MISO: 0,
                CLK:  1,
                MOSI: 2,
                CS:   3
            }
        }
    };
    mountParams = &params;
    
    //    buffer[0] = 0;
    //    buffer[1] = 1;
    //    buffer[2] = 2;
    //    buffer[3] = 3;
    //    LoadSDDriver(buffer);
        
    //    dfs_mount();
        
        waitcnt(CLKFREQ/6+CNT);
        while(1)
        {        
            printf(">");
            fflush(stdout);
            gets(buffer);
            num = tokenize(buffer, tokens);
            if (num == 0) continue;
            if (!strcmp(tokens[0], "quit"))
                break;
            else if (!strcmp(tokens[0], "ls"))
                List(num, tokens);
            else
            {
                printf("Invalid command\n");        
            }
        }
    
        
        printf("\nSystem Stop");
        return 0;
    }
    
    
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-09-11 06:31
    Ray, that looks good. I get 17,076 bytes for the original filetest program with CMM, but that's using the printf.c file included in the demos directory. Since you're using the standard printf the program size will be larger. BTW, the dirent struct now includes the standard d_name element, which contains the properly formated 8.3 file name. I plan on changing filetest.c to use d_name instead of name. This will eliminate the code needed to convert from the directory entry format to the normal file name format, and it makes things a bit more standard.
  • RsadeikaRsadeika Posts: 3,837
    edited 2012-09-11 12:47
    The attached file is a slight modification of the original filetest.c. I added a quit command, and a definition for the DNA board SD card reader. Basically you will be able to run this in CMM or XMM mode. If you want to change the board type, you have to go to the mount() function and change the #define to the board that you want to use.

    <edit> You may want to change the name of the file, it may get confusing with two filetest.c programs floating around. <edit>

    Ray
  • RsadeikaRsadeika Posts: 3,837
    edited 2012-09-11 13:23
    I just added printf.c to my version of filetest.c and the numbers are:

    CMM - Code Size 17,668 bytes (18,456 total)
    LMM - Code Size 30,008 bytes (30,796 total)
    XMMC - Code Size 33,508 bytes (34,296 total)

    Again, remember that I am using the DNA board.

    Ray
Sign In or Register to comment.