Sample Code 1 - Demo of most commands in module
Device = 18F4620
Clock = 40
Config OSC = HSPLL
#option SD_SPI = MSSP
#option SD_SPI_SPEED = spiOscDiv4
#option SD_SUPPORT_SUB_DIRECTORIES = True
#option USART_BRGH = True
#option USART_BRG16 = True
Include "USART.bas"
Include "SDFileSystem.bas"
Include "String.bas"
Include "Convert.bas"
Const ClockDiv = _clock * 1000000
Dim MenuOption As String
Dim Timer As TMR1L.AsWord
Dim TimerOn As T1CON.Booleans(0)
Dim TimerInterruptsEnabled As PIE1.Booleans(0)
Dim Counter As LongWord
Dim CounterUpper As Counter.Word1
Interrupt OnTimer()
Inc(CounterUpper)
PIR1.0 = 0
End Interrupt
Sub TimerStart()
Timer = 0
CounterUpper = 0
TimerOn = True
End Sub
Sub TimerStop()
TimerOn = False
Counter.Word0 = Timer
End Sub
Sub NewLine()
USART.Write(13,10)
End Sub
Sub DisplayLine(pString As String)
Dim Spaces As Byte
USART.Write("* ", pString)
Spaces = 70 - Length(pString)
Repeat
USART.Write(" ")
Dec(Spaces)
Until Spaces = 0
USART.Write("*")
NewLine()
End Sub
Sub SeparatorLine()
Dim Spaces As Byte
Spaces = 74
Repeat
USART.Write("*")
Dec(Spaces)
Until Spaces = 0
NewLine
End Sub
Sub Menu()
DisplayLine("SDFileSystem Test Program Menu - Select Option:")
SeparatorLine()
DisplayLine("A: Init I: OpenFile... Q: DiskMounted Y: Format")
DisplayLine("B: QuickFormat J: DeleteFile... R: DiskSizeKB Z: SerialNumber")
DisplayLine("C: Dir K: FileExists... S: FreeSpaceKB 1: MemoryType")
DisplayLine("D: ChDir... L: DirExists... T: IsOpen 2: FATType")
DisplayLine("E: ChDir(Root) M: MkDir... U: IsRootDir 3: DestroyFormat")
DisplayLine("F: ChDir(Up) N: RmDir... V: RWError 4: FileCount")
DisplayLine("G: NewFile... O: Rename... W: NewFile(1MB) 5: DirCount")
DisplayLine("H: AppendFile... P: DiskFull X: SaveFile...")
SeparatorLine()
End Sub
Sub DisplayResponse(pResponse As Byte)
Select pResponse
Case errOK
DisplayLine(">: Response: errOK")
Case errDiskFull
DisplayLine(">: Response: errDiskFull")
Case errNotFound
DisplayLine(">: Response: errNotFound")
Case errRootDirFull
DisplayLine(">: Response: errRootDirFull")
Case errDirNotEmpty
DisplayLine(">: Response: errDirNotEmpty")
Case errExists
DisplayLine(">: Response: errExists")
Case errInUse
DisplayLine(">: Response: errInUse")
Case errRWError
DisplayLine(">: Response: errRWError")
Case errFileNotOpen
DisplayLine(">: Response: errFileNotOpen")
Case errBeyondEOF
DisplayLine(">: Response: errBeyondEOF")
Case errNoResponse
DisplayLine(">: Response: errNoResponse")
Case errInvalidFormat
DisplayLine(">: Response: errInvalidFormat")
Case errInvalidPath
DisplayLine(">: Response: errInvalidPath")
EndSelect
SeparatorLine()
End Sub
Sub DisplayResponse(pResponseB As Boolean)
Select pResponseB
Case True
DisplayLine(">: Response: True")
Case False
DisplayLine(">: Response: False")
EndSelect
SeparatorLine()
End Sub
Sub DisplayTime()
Dim Time As Float
Time = 4 * Counter / ClockDiv
DisplayLine(">: Time Taken: " + FloatToStr(Time,4,3) + " sec")
SeparatorLine()
End Sub
Function GetName(pPrompt As String) As String * 24
DisplayLine("?: " + pPrompt + ":")
SeparatorLine()
USART.Read(GetName)
End Function
Sub SDCode(pMenuOption As String)
Dim Index As LongWord
Dim Name As String
Dim NameNew As String
Dim FileName As String(13)
Dim Response As Byte
If pMenuOption <> " " Then
SeparatorLine()
DisplayLine(">: Option: " + pMenuOption)
EndIf
SeparatorLine()
Select pMenuOption
Case " "
Menu()
Case "A"
TimerStart()
DisplayResponse(SD.Init())
TimerStop()
DisplayTime()
Case "B"
TimerStart()
DisplayResponse(SD.QuickFormat)
TimerStop()
DisplayTime()
Case "C"
DisplayLine(">: Directory of SD/MMC:")
TimerStart()
FileName = SD.Dir(dirFirst, sdFile)
While FileName <> Null
DisplayLine(">: " + FileName)
FileName = SD.Dir(dirNext, sdFile)
Wend
FileName = SD.Dir(dirFirst, sdDirectory)
While FileName <> Null
DisplayLine(">: " + FileName + " <DIR>")
FileName = SD.Dir(dirNext, sdDirectory)
Wend
TimerStop()
DisplayLine(">: " + DecToStr(SD.FileCount, 2) + " File(s)")
DisplayLine(">: " + DecToStr(SD.DirCount, 2) + " Directory(s)")
SeparatorLine()
DisplayTime()
Case "D"
Name = GetName("Enter Directory Name")
TimerStart()
DisplayResponse(SD.ChDir(Name))
TimerStop()
DisplayTime()
Case "E"
TimerStart()
DisplayResponse(SD.ChDir(cdRoot))
TimerStop()
DisplayTime()
Case "F"
TimerStart()
DisplayResponse(SD.ChDir(cdUp))
TimerStop()
DisplayTime()
Case "G"
Name = GetName("NewFile - Enter File Name")
Response = SD.NewFile(Name)
DisplayResponse(Response)
If Response = errOK Then
Index = 1
TimerStart()
Repeat
SD.Write("SDFileSystemTest - Section 1 - Line ",DecToStr(Index,4),13,10)
Inc(Index)
Until Index > 300
SD.CloseFile
TimerStop()
DisplayLine(">: File Closed")
SeparatorLine()
DisplayTime()
EndIf
Case "H"
Name = GetName("AppendFile - Enter File Name")
Response = SD.AppendFile(Name)
DisplayResponse(Response)
If Response = errOK Then
Index = 1
TimerStart()
Repeat
SD.Write("SDFileSystemTest - Section 2 - Line ",DecToStr(Index,4),13,10)
Inc(Index)
Until Index > 300
SD.CloseFile()
TimerStop()
DisplayLine(">: File Closed")
SeparatorLine()
DisplayTime()
EndIf
Case "I"
Name = GetName("OpenFile - Enter File Name")
Response = SD.OpenFile(Name)
DisplayResponse(Response)
If Response = errOK Then
NewLine()
TimerStart()
Repeat
USART.Write(SD.ReadByte())
Until SD.EOF
SD.CloseFile
TimerStop()
NewLine()
SeparatorLine()
DisplayLine(">: File Closed")
SeparatorLine()
DisplayTime()
EndIf
Case "J"
Name = GetName("DeleteFile - Enter File Name")
TimerStart()
DisplayResponse(SD.DeleteFile(Name))
TimerStop()
DisplayTime()
Case "K"
Name = GetName("FileExists - Enter File Name")
TimerStart()
DisplayResponse(SD.FileExists(Name))
TimerStop()
DisplayTime()
Case "L"
Name = GetName("DirExists - Enter Directory Name")
TimerStart()
DisplayResponse(SD.DirExists(Name))
TimerStop()
DisplayTime()
Case "M"
Name = GetName("MkDir - Enter Directory Name")
TimerStart()
DisplayResponse(SD.MkDir(Name))
TimerStop()
DisplayTime()
Case "N"
Name = GetName("RmDir - Enter Directory Name")
TimerStart()
DisplayResponse(SD.RmDir(Name))
TimerStop()
DisplayTime()
Case "O"
Name = GetName("Rename - Enter Old File Name")
NameNew = GetName("Rename - Enter New File Name")
TimerStart()
DisplayResponse(SD.Rename(Name, NameNew, sdFile))
TimerStop()
DisplayTime()
Case "P"
TimerStart()
DisplayResponse(SD.DiskFull)
TimerStop()
DisplayTime()
Case "Q"
TimerStart()
DisplayResponse(SD.DiskMounted)
TimerStop()
DisplayTime()
Case "R"
TimerStart()
DisplayLine(">: Disk Size: " + DecToStr(SD.DiskSizeKB()) + " KB")
TimerStop()
SeparatorLine()
DisplayTime()
Case "S"
TimerStart()
DisplayLine(">: Free Space: " + DecToStr(SD.FreeSpaceKB()) + " KB")
TimerStop()
SeparatorLine()
DisplayTime()
Case "T"
TimerStart()
DisplayResponse(SD.IsOpen)
TimerStop()
DisplayTime()
Case "U"
TimerStart()
DisplayResponse(SD.IsRootDir)
TimerStop()
DisplayTime()
Case "V"
TimerStart()
DisplayResponse(SD.RWError)
TimerStop()
DisplayTime()
Case "W"
Name = GetName("NewFile(1MB) - Enter File Name")
Response = SD.NewFile(Name)
DisplayResponse(Response)
If Response = errOK Then
Index = 1
TimerStart()
Repeat
SD.Write(".")
Inc(Index)
Until Index > 1048576
SD.CloseFile
TimerStop()
DisplayLine(">: File Closed")
SeparatorLine()
DisplayTime()
EndIf
Case "X"
Name = GetName("SaveFile - Enter File Name")
Response = SD.NewFile(Name)
DisplayResponse(Response)
If Response = errOK Then
TimerStart()
Index = 1
Repeat
SD.Write("SDFileSystemTest - Section 1 - Line ",DecToStr(Index,4),13,10)
Inc(Index)
Until Index > 100
SD.SaveFile
Index = 1
Repeat
SD.Write("SDFileSystemTest - Section 2 - Line ",DecToStr(Index,4),13,10)
Inc(Index)
Until Index > 100
SD.SaveFile
Index = 1
Repeat
SD.Write("SDFileSystemTest - Section 3 - Line ",DecToStr(Index,4),13,10)
Inc(Index)
Until Index > 100
SD.SaveFile
Index = 1
Repeat
SD.Write("SDFileSystemTest - Section 4 - Line ",DecToStr(Index,4),13,10)
Inc(Index)
Until Index > 100
SD.CloseFile
TimerStop()
DisplayLine(">: File Closed")
SeparatorLine()
DisplayTime()
EndIf
Case "Y"
TimerStart()
DisplayResponse(SD.Format(1))
TimerStop()
DisplayTime()
Case "Z"
TimerStart()
DisplayLine(">: SerialNumber: " + HexToStr(SD.SerialNumber()))
TimerStop()
SeparatorLine()
DisplayTime()
Case "1"
TimerStart()
Select SD.MemoryType()
Case sdMMC Name = "MMC"
Case sdSD Name = "SD"
Case sdSDHC Name = "SDHC"
EndSelect
DisplayLine(">: MemoryType: " + Name)
TimerStop()
SeparatorLine()
DisplayTime()
Case "2"
TimerStart()
Select SD.FATType()
Case 1 Name = "FAT16"
Case 2 Name = "FAT32"
EndSelect
DisplayLine(">: FATType: " + Name)
TimerStop()
SeparatorLine()
DisplayTime()
Case "3"
TimerStart()
DisplayResponse(SD.DestroyFormat)
TimerStop()
DisplayTime()
Case "4"
TimerStart()
DisplayLine(">: FileCount: " + DecToStr(SD.FileCount()))
TimerStop()
SeparatorLine()
DisplayTime()
Case "5"
TimerStart()
DisplayLine(">: DirCount: " + DecToStr(SD.DirCount()))
TimerStop()
SeparatorLine()
DisplayTime()
Else
DisplayLine(">: Option Not Recognised")
SeparatorLine()
EndSelect
End Sub
SetBaudrate(br115200)
USART.ReadTerminator = #13
DelayMS(1000)
TimerInterruptsEnabled = True
Enable(OnTimer)
SDCode(" ")
NewLine()
While True
USART.Read(MenuOption)
SDCode(Str.Uppercase(MenuOption))
NewLine()
Wend