Shop OBEX P1 Docs P2 Docs Learn Events
Hello World SD file access — Parallax Forums

Hello World SD file access

Dr_AculaDr_Acula Posts: 5,484
edited 2012-07-03 03:27 in Propeller 1
Once you have "Hello World" on the screen, time to start loading and saving files to the SD card :)

Grab a board with an SD card. If it is not on the list, take the text below, edit the SD pins to the ones on your board and save it to a file ending in .cfg and put it in the c:\propgcc\propeller-load folder
# [propboe]
# IDE:SDXMMC
    clkfreq: 80000000
    clkmode: XTAL1+PLL16X
    baudrate: 115200
    rxpin: 31
    txpin: 30
    cache-driver: eeprom_cache.dat
    cache-size: 8K
    cache-param1: 0
    cache-param2: 0
    eeprom-first: TRUE
    sd-driver: sd_driver.dat
    sdspi-do: 24
    sdspi-clk: 25
    sdspi-di: 26
    sdspi-cs: 27

Reboot SimpleIDE and your board should be on the list in the dropdown menu.

Change to C++ (my preference)
Memory model to XMMC
Optimization to "speed" (this is going to an SD card so size is not as important as speed).

Run this little program (compiles to about 50k) and it should write some text to a file Test.txt on the SD card.
#include <stdio.h>
#include <propeller.h>
extern _Driver _SimpleSerialDriver;
extern _Driver _FileDriver;
typedef unsigned char UCHAR;
typedef unsigned long DWORD;
typedef unsigned long UINT;
static char    a[80];
static FILE   *fp1;
/* This is a list of all drivers we can use in the
 * program. The default _InitIO function opens stdin,
 * stdout, and stderr based on the first driver in
 * the list (the serial driver, for us)
 */
_Driver *_driverlist[] = {
  &_SimpleSerialDriver,
  &_FileDriver,
  NULL
};
/**
 * Main program function.
 */
int main(int argc, char *argv[])
{
    strcpy(a,"Test.txt"); // open a file for writing
    printf("Opening file Test.txt\n");
    if((fp1=fopen(a,"w"))==0)
        fprintf(stderr,"Can't open file %s\n",a);
    else
        fputs ("This is a line of text",fp1);
    fclose(fp1);
    printf("Text saved and file closed\n");
    while(1);
}

Click the light blue arrow and it compiles and opens the terminal and should display this
Loading cache driver
Initializing SD card
Mounting SD filesystem
Opening AUTORUN.PEX
Loading kernel
Loading cluster map
Initializing cache
Starting program
Opening file Test.txt
Text saved and file closed

Comments

  • jazzedjazzed Posts: 11,803
    edited 2012-07-01 13:06
    Glad you got something working.

    No need to restart the IDE to reload the board types. Just press the puzzle button.

    Cheers.
    --Steve
  • StephenMooreStephenMoore Posts: 188
    edited 2012-07-02 18:51
    Works first time but if I run again I do not get any SimlelDE terminal output
  • jazzedjazzed Posts: 11,803
    edited 2012-07-02 19:44
    Works first time but if I run again I do not get any SimlelDE terminal output

    Sorry you're having trouble. Does the program build and load ok?

    What is the board type set to?
    What is the memory model ?

    Maybe post your build status here?
    Right-click build status to select all, then right-click again to copy.
  • StephenMooreStephenMoore Posts: 188
    edited 2012-07-02 20:35
    C3 with LMM and C compiler,-Os Size.....

    Project Directory: C:/Users/Dad/Documents/myProj/


    propeller-elf-gcc.exe -o a.out -Os -mlmm -I . -fno-exceptions myProj.c
    propeller-elf-objdump -h a.out
    Done. Build Succeeded!


    propeller-load.exe -I C:/propgcc/propeller-load/ -b C3 -p COM3 a.out -r
    Patching __cfg_sdspi_config1 with 090a0b13
    Loading a.out to hub memory


    29632 bytes sent


    Verifying RAM ...
    OK


    First impression as a user is FANTASTIC PROGRAM!

    I am not sure how traditional SPIN or PASM exactly play into this. I really like the program control they offer. Is it possible to build inline code fragments within C?

    How do I utilize OBEX code?

    I know its early but will there be a C library repository?

    sm
  • jazzedjazzed Posts: 11,803
    edited 2012-07-03 03:27
    C3 with LMM and C compiler,-Os Size.....

    Project Directory: C:/Users/Dad/Documents/myProj/


    propeller-elf-gcc.exe -o a.out -Os -mlmm -I . -fno-exceptions myProj.c
    propeller-elf-objdump -h a.out
    Done. Build Succeeded!


    propeller-load.exe -I C:/propgcc/propeller-load/ -b C3 -p COM3 a.out -r
    Patching __cfg_sdspi_config1 with 090a0b13
    Loading a.out to hub memory


    29632 bytes sent


    Verifying RAM ...
    OK

    Hmm. Seems like the download worked.

    What happens if you use a simple hello program?
    #include <stdio.h>
    #include "propeller.h"
    
    void main(void)
    {
        int n = 1;
        while(1) {
            waitcnt(CLKFREQ+CNT);
            printf("Hello World %d\n", n);
            n++;
        }
    }
    


    You might try the SD example here:
    https://sites.google.com/site/propellergcc/documentation/libraries#TOC-Console-and-File-system-driver-initialization

    First impression as a user is FANTASTIC PROGRAM!

    I am not sure how traditional SPIN or PASM exactly play into this. I really like the program control they offer. Is it possible to build inline code fragments within C?

    How do I utilize OBEX code?

    Propeller GCC allows using in-line ASM based on the GAS syntax.

    Propeller GCC can use PASM code written for a traditional mail-box interface.
    Please see the SimpleIDE demo: toggle/pasm_toggle

    Propeller GCC does not support the Spin language as a standard feature.
    It is possible to start a Spin program from C, but it is an advanced topic.
    The code would not be in-line source.
    I know its early but will there be a C library repository?

    Parallax is working on a set of libraries and training materials.
Sign In or Register to comment.