SDCard Help please
Chip Cox
Posts: 73
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"
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.