SD Read and Write for Propeller

1.png

 I recently read two great tutorials from gadget gangster on how to write to and read from SD card. Now this is great if you want to write pre-set text. But doesn't work out so well if you want to send text that is writen as the chip is running. This code shows a simple "word processer" that can write to, append, read and delete a single file using the Parallax Serial Terminal. Please comment with any questions.

 

 Instructions:

  • My DO, CLK, DI and CS are set for the Gadget Gangster Propeller Platform USB, you may need to set them to modify them
  • the byte "text" is set to 1000 characters. You may need to add more for your desired document length
  • You may want to read up on the object it uses before modifying the code

 For now, I have to type out the code (It can't be attatched). Any other sugestions are welcome. Enjoy:

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

CON

 _clkmode = xtal1 + pll16x

 _xinfreq = 5_000_000

 

 DO  = 0

 CLK = 1

 DI  = 2

 CS  = 3

 

VAR

 byte text[1000]

 

OBJ

 sdfat : "fsrw"

 pst : "parallax serial terminal"

 

PUB Initiate  | mount

 pst.start(115_200)

 waitcnt(clkfreq*4 + cnt)

 

 mount := \sdfat.mount_explicit(DO, CLK, DI, CS)

 

 if mount < 0

    pst.str(string("Failed to mount", 13))

    abort

 

 pst.str(string("SD card was found and mounted successfully!", 13))

 

 pst.str(string("you have four basic options to edit the file - test.txt:", 13, 13, "write", 13, "append", 13, "read", 13, "delete", 13, 13))

 

 getcode

 

PUB getcode

 bytefill(@text, 0, 1000)

 pst.newline

 pst.StrIn(@text)

 RW

 

PUB RW  | counter, r

 

 if strcomp(@text, string("write"))

    bytefill(@text, 0, 1000)

    sdfat.popen(string("test.txt"), "w")

    pst.str(string("Text to write:", 13))

    pst.strin(@text)

    sdfat.pputs(@text)

    sdfat.pputs(string(13, 10))

    sdfat.pclose

    pst.str(string("you wrote: "))

    pst.str(@text)

    pst.newline

    getcode

 

 if strcomp(@text, string("append"))

    bytefill(@text, 0, 1000)

    sdfat.popen(string("test.txt"), "a")

    pst.str(string("Text to add:", 13))

    pst.strin(@text)

    sdfat.pputs(@text)

    sdfat.pputs(string(13, 10))

    sdfat.pclose

    pst.str(string("you added: "))

    pst.str(@text)

    pst.newline

    getcode

 

 if strcomp(@text, string("delete"))

    bytefill(@text, 0, 1000)

    sdfat.popen(string("test.txt"), "d")

    pst.str(string("File has been deleted", 13))

    sdfat.pclose

    getcode

 

 if strcomp(@text, string("read"))

    bytefill(@text, 0, 1000)

    pst.str(string("Text from file:", 13))

    sdfat.popen(string("test.txt"), "r") 

    counter := 0

    repeat

        r := sdfat.pgetc

 

        if r < 0

          pst.newline

          quit

 

        if r == 44 or r == 13

          pst.str(@text)

          pst.newline

          bytefill(@text, 0, 1000)

          counter := 0

 

        if r > 44

          text[counter] := text[counter] + r

          counter++

    sdfat.pclose

    pst.str(string("end of file has been reached, now closing", 13))

    getcode

 

 if strcomp(@text, string("end"))

    pst.str(string(13, "now terminating program"))

    sdfat.unmount

    abort

 

 getcode