SDCard Help please
Ok,
This should have been simple. I'm trying to write some information out to an SD card and then read it with my PC to verify it's there. To do this I'm taking the approach that I want to completely re-initialize the card and start from scratch. So I mount the card, format the card, create the new file, open the new file, write a string to it, close the file, unmount the card. Then I move the card to my PC. The card shows up as drive E, the label name is correct, the right file name is even there. But it's empty. Please review and see if you can tell what I am doing wrong here.
Thanks
_clkfreq = 80_000_000
_clkmode = xtal1 + pll16x
_cardDataOutPin = 16
_cardClockPin = 21
_cardDataInPin = 20
_cardChipSelectPin = 19
OBJ
DEBUG : "Parallax Serial Terminal"
STR : "STREngine"
fat : "SD2.0_FATEngine.spin"
dat
vollbl byte "SPINWEB",0
Write_Mode byte "W",0
outfile byte "CONFIG.DAT",0
MAC_HDR byte "MAC",0
var
byte MAC_Address[6],stringPointer
long sdres
PUB main | index, fileSize, token, packetSize,i, startCnt, timeoutCnt, currentCnt, fileName[64] ' 64 longs = 256 bytes
DEBUG.Start(115200)
waitcnt((clkfreq*2)+cnt)
sdres:=fat.FATEngineStart(_cardDataOutPin, _cardClockPin, _cardDataInPin, _cardChipSelectPin, 0, 0)
debug.str(string("Start returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
DEBUG.Str(string("Mounting SD card.", DEBUG#NL, DEBUG#NL))
sdres:=fat.mountPartition(0, stringPointer)
debug.str(string("Mount returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
sdres:=fat.formatPartition(0,@vollbl)
debug.str(string("Format returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
sdres:=fat.newFile(@outfile)
debug.str(string("newFile returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
sdres:=fat.openFile(@outfile,@Write_Mode)
debug.str(string("OpenFile returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
debug.str(string("MAC"))
sdres:=fat.writeString(string("MAC"))
debug.str(string("WriteString returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
{{
MAC_Address[0] := $00
MAC_Address[1] := $08
MAC_Address[2] := $DC
MAC_Address[3] := $16
MAC_Address[4] := $EF
MAC_Address[5] := $56
i:=0
repeat while i < 6
debug.str(string("About to write : "))
debug.dec(i)
debug.str(string(" : "))
debug.hex(MAC_Address,2)
debug.str(string(13))
sdres:=fat.writeByte(MAC_Address)
debug.str(string("write byte returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
i:=i+1
}}
sdres:=fat.closeFile
debug.str(string("Close File returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
sdres:=fat.unmountPartition
debug.str(string("Unmount returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
Debug output I get is
Start returned :
Mounting SD card.
Mount returned : SPINWEB
Format returned : SPINWEB
newFIle returned CONFIG.DAT
OpenFile returned : CONFIG.DAT
MACWriteString returned :
Close File returned :
Unmount returned :
This should have been simple. I'm trying to write some information out to an SD card and then read it with my PC to verify it's there. To do this I'm taking the approach that I want to completely re-initialize the card and start from scratch. So I mount the card, format the card, create the new file, open the new file, write a string to it, close the file, unmount the card. Then I move the card to my PC. The card shows up as drive E, the label name is correct, the right file name is even there. But it's empty. Please review and see if you can tell what I am doing wrong here.
Thanks
_clkfreq = 80_000_000
_clkmode = xtal1 + pll16x
_cardDataOutPin = 16
_cardClockPin = 21
_cardDataInPin = 20
_cardChipSelectPin = 19
OBJ
DEBUG : "Parallax Serial Terminal"
STR : "STREngine"
fat : "SD2.0_FATEngine.spin"
dat
vollbl byte "SPINWEB",0
Write_Mode byte "W",0
outfile byte "CONFIG.DAT",0
MAC_HDR byte "MAC",0
var
byte MAC_Address[6],stringPointer
long sdres
PUB main | index, fileSize, token, packetSize,i, startCnt, timeoutCnt, currentCnt, fileName[64] ' 64 longs = 256 bytes
DEBUG.Start(115200)
waitcnt((clkfreq*2)+cnt)
sdres:=fat.FATEngineStart(_cardDataOutPin, _cardClockPin, _cardDataInPin, _cardChipSelectPin, 0, 0)
debug.str(string("Start returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
DEBUG.Str(string("Mounting SD card.", DEBUG#NL, DEBUG#NL))
sdres:=fat.mountPartition(0, stringPointer)
debug.str(string("Mount returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
sdres:=fat.formatPartition(0,@vollbl)
debug.str(string("Format returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
sdres:=fat.newFile(@outfile)
debug.str(string("newFile returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
sdres:=fat.openFile(@outfile,@Write_Mode)
debug.str(string("OpenFile returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
debug.str(string("MAC"))
sdres:=fat.writeString(string("MAC"))
debug.str(string("WriteString returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
{{
MAC_Address[0] := $00
MAC_Address[1] := $08
MAC_Address[2] := $DC
MAC_Address[3] := $16
MAC_Address[4] := $EF
MAC_Address[5] := $56
i:=0
repeat while i < 6
debug.str(string("About to write : "))
debug.dec(i)
debug.str(string(" : "))
debug.hex(MAC_Address,2)
debug.str(string(13))
sdres:=fat.writeByte(MAC_Address)
debug.str(string("write byte returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
i:=i+1
}}
sdres:=fat.closeFile
debug.str(string("Close File returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
sdres:=fat.unmountPartition
debug.str(string("Unmount returned : "))
debug.str(@byte[sdres][0])
debug.str(string(13))
Debug output I get is
Start returned :
Mounting SD card.
Mount returned : SPINWEB
Format returned : SPINWEB
newFIle returned CONFIG.DAT
OpenFile returned : CONFIG.DAT
MACWriteString returned :
Close File returned :
Unmount returned :
Comments
Here is my SD Card "Hello World"
CON _clkmode = xtal1 + pll16x 'Use the PLL to multiple the external clock by 16 _xinfreq = 5_000_000 'An external clock of 5MHz. is used (80MHz. operation) CON CR = 13 CS = 16 ''CS: Clear Screen CE = 11 ''CE: Clear to End of line CB = 12 ''CB: Clear lines Below LF = 10 NBSP = 32 HTTP_ERROR = 0 HTTP_GET = 1 HTTP_POST = 2 HTTP_10 = 0 HTTP_11 = 1 FS = "/" EQ = "=" AMP = "&" QM = "?" DOT = "." STR_TERM = 0 OBJ pst : "Parallax Serial Terminal.spin" eprom : "data.spin" SDCard : "SD2.0_FATWrapper.spin" str : "ASCII0_STREngine.spin" DAT get byte "GET", 0 post byte "POST", 0 http byte "HTTP/1.1", 0 host byte "Host:", 0 contlen byte "Content-Length:", 0 body byte CR, LF, CR, LF, 0 dynamicpage byte "SHP", 0 defaultpage byte "index.htm", 0, "default.htm", 0 placeholder byte "<!--[content]-->", 0 testfile byte "test.txt", 0 filestack byte $0[12], 0 hostname byte $0[20], 0 dirstack byte $0[256], 0 url byte $0[256], 0 fileBuffer byte $0[2048], 0 clipboard byte $0[1024], 0 varstack byte $0[1024], 0 dynamicContent byte $0[1024], 0 VAR byte method byte httpVersion byte fileType byte directoryStackDepth byte postback long ptrStart long ptrEnd long contentLength long header long directoryStackPtr[10] long fileStackPtr[2] long fileErrorHandle long varstackPtr long dynamicContentPtr PUB Main | i, temp, len, size, offset, s, flag, stringPointer pst.Start(115200) SDCard.Start PauseMSec(2_000) flag := 1 stringPointer := str.tokenizeString(@testfile) pst.Char(CR) pst.Str(string("---------------------")) pst.Char(CR) pst.Str(string("Mount file system")) pst.Char(CR) SDCard.mount(fileErrorHandle) pst.Str(string("Open file")) pst.Char(CR) SDCard.openFile(stringPointer, "r") pst.Str(string("read file")) pst.Char(CR) size := SDCard.getFileSize SDCard.readFromFile(@fileBuffer, size) pst.Str(@fileBuffer) pst.Char(CR) pst.Str(string("--------------")) pst.Char(CR) pst.Str(string("Close file")) pst.Char(CR) SDCard.closeFile pst.Str(string("File Size: ")) pst.Dec(size) pst.Char(CR) pst.Str(string("--------------")) pst.Char(CR) pst.Str(string("Open file test.txt")) pst.Char(CR) SDCard.openFile(stringPointer, "w") pst.Str(string("Fill file")) pst.Char(CR) SDCard.writeData(string("This is a test"), strsize(string("This is a test"))) pst.Str(string("Close file")) pst.Char(CR) SDCard.closeFile pst.Str(string("Open the file again")) pst.Char(CR) SDCard.openFile(stringPointer, "r") size := SDCard.getFileSize SDCard.readFromFile(@fileBuffer, size) pst.Str(@fileBuffer) pst.Char(CR) pst.Str(string("--------------")) pst.Char(CR) pst.Str(string("Delete file - result: ")) SDCard.deleteEntry(stringPointer) pst.dec(temp) pst.Char(CR) pst.Str(string("Create file")) SDCard.newFile(stringPointer) pst.Char(CR) bytefill(@fileBuffer, 0, 1024) pst.Str(string("--------------")) pst.Char(CR) pst.Str(string("Open file: ")) pst.Str(stringPointer) pst.Char(CR) SDCard.openFile(stringPointer, "w") pst.Str(string("Fill file")) pst.Char(CR) SDCard.writeData(string("Hi"), strsize(string("Hi"))) pst.Str(string("Close file")) pst.Char(CR) SDCard.closeFile pst.Str(string("Open the file again")) pst.Char(CR) SDCard.openFile(stringPointer, "r") size := SDCard.getFileSize SDCard.readFromFile(@fileBuffer, size) pst.Str(string("File Size (")) pst.Dec(size) pst.Str(string(") - content:")) pst.Str(@fileBuffer) pst.Char(CR) SDCard.unmount(" ") PRI PauseMSec(Duration) waitcnt(((clkfreq / 1_000 * Duration - 3932) #> 381) + cnt) return
You can do this but...
You should convert the byte to ASCII characters. When you open the file in a text editor the result of fat.writeByte(MAC_Address will look like gibberish or an unexpected value.
When you open the file for writing just pass "W".
It would help you alot if you read the API for each function included with the code.