Shop OBEX P1 Docs P2 Docs Learn Events
SD Card Example Code — Parallax Forums

SD Card Example Code

JefffJefff Posts: 1
edited 2008-01-06 01:58 in General Discussion
I have been attempting for the past week to get the SX48 to interface with an SD card. After trying to follow many different (and often contradicting) specs for SD cards, and trying multiple different brands of SD cards, I've come to the conclusion I must be missing something. Does anyone have any sample code for the SX48 (or something similar enough where porting would be straight forward) that can read data from an SD card? Writing isn't important for what I'm trying to do. The only sample code I have been able to find is for other microcontrollers that have a built in SPI interface, which doesn't help in any way, because I think the problem is with my implementation of the SPI interface.

Any help would be greatly appreciated, or a purposed alternative to easily (and cheaply, I don't have $70 to spare cry.gif) storing about 1MB of data to be accessed from an SX48.

Comments

  • BeanBean Posts: 8,129
    edited 2008-01-02 12:38
    Jefff,
    · If you post what code you have so far, I'm sure we can help you. If you only need to read, that shouldn't be too hard. As long as you are using FAT16 and NOT using long file names.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.iElectronicDesigns.com

    ·
  • JDOhioJDOhio Posts: 72
    edited 2008-01-06 01:58
    I am using the SD Module from Rogue Robotics. The module provides a pretty simple serial interface and can run up to 115.2 kbaud (haven't tried it yet). In my code, I set up a queue of commands that write out the information I want to store. In this manner, the command set is open the file, write the file, close the file. Each command terminates with a carriage return (I use < to represent that below).

    My queue looks like this:

    'Typical Queue
    ' |O| |1| |A| |/|t|.|g|<|W| |1| |xx|xx|<|xx|xx|xx|xx|,|xx|xx|;|C| |1|<|
    ' | open command | write command |tick and data |close command

    The Open command is simply O, the file handle, and A for append. This is followed by the name of the file. I use short file names to shorten the communication time. The next command, W, writes the data to the file handle. The write command must know the number of bytes to write. Once the write command is sent, the SD Module expect to receive the exact number of bytes you told it in the write command. The last command is C for close, and the file handle.

    Once the queue is set up, the commands to communicate become pretty simple. I use SEROUT and SERIN.

    SDMWrite:
    'Use
    '    SDMWrite dataIndex, number of data bytes including separators and terminator
    
        temp2=__PARAM1
        temp3=__PARAM2 + SDMSystemTickLength
    
        'There are four parts to a message: Open, Write, Data, Close.  The commands
        'terminate with SDMCmdTerminate; the data is a specific length.
        FOR temp4=1 TO 4
            IF temp4<>3 THEN
                temp1=0
                DO
                    GOSUB SDMWriteSupport
                LOOP UNTIL temp1=SDMCmdTerminate
            ELSE
                FOR temp5=0 TO temp3
                    GOSUB SDMWriteSupport
                NEXT
            ENDIF
            SDMRead
        NEXT
        
    RETURN temp2
    
    SDMWriteSupport:
        temp1=SDMQueue(temp2)
        GOSUB UpdateIndex
        SEROUT SDMWritePort, T9600, temp1    
    RETURN
    
    UpdateIndex:
        INC temp2
        IF temp2 >= SDMQueueMax THEN
            temp2=temp2-SDMQueueMax
        ENDIF
    RETURN
    
    SDMRead:
    'TODO: this needs to watch for errors: a reply of space, E indicates an error
    
        temp7=0
        DO
            SERIN SDMReadPort,T9600,temp6,SDMReadTimeOut,SDMReadExit
            IF temp6=SDMCmdPrompt THEN
                EXIT
            ELSE
                SDMReplyQueue(temp7)=temp6
                INC temp7
            ENDIF
        LOOP
    SDMReadExit:
    
    
    RETURN
    
    



    SDMWrite uses SDMWriteSupport to write each byte, and UpdateIndex to march through the queue. At the end of each command, the SD Module returns a carriage return as a prompt. SDMWrite uses SDMRead to watch for the prompt.

    Joe
Sign In or Register to comment.