Config Spi = Hard , Data Order = Msb , Master = Yes , Polarity = High , Phase = 1 , Clockrate = 16 , Noss = 1
Declare Sub Sdsendcommand(byval Command As Byte , Byval Argument As Long)
Declare Function Sdreadresponse() As Byte
Declare Sub Getbock(buffer As Byte)
Sdcs Alias Portb.0 'the Slave Select port
Dim Temp As Byte
Dim Idx As Word
Dim Rxblock(512) As Byte
Set Ddrb.0 'make CS = output
Spiinit 'init hardware SPI
Set Sdcs 'make CS = high
Waitms 1000 'wait a second
For Idx = 1 To 10
Temp = Spimove(&Hff) '80 clocks
Next Idx
Print "start INI"
Call Sdsendcommand(0 , 0) 'send command 0 GO_TO_IDLE
Do
Temp = Sdreadresponse() 'get response in temp
Loop Until Temp = 1 'confirm that we are in idle state
'
Do
Call Sdsendcommand(1 , 0) 'send command 1 SEND_OP_COND
Temp = Sdreadresponse()
Loop Until Temp = 0 'wait for 0 to be returned
Print "finished INI"
Print "set block len"
Call Sdsendcommand(16 , 512) 'send command 16 SET_BLOCKLEN=512
Do
Temp = Sdreadresponse()
Loop Until Temp = 0 'wait for 0 to be returned
Print "read block cmd"
Call Sdsendcommand(17 , 0) 'send command 17 READ_SINGLE_BLOCK=0
Do
Temp = Sdreadresponse()
Loop Until Temp = 0 'wait for 0 to be returned
Print "ready for block xfer"
Call Getbock(rxblock(1)) 'get the block & stick it in rxblock
Print "printing block"
For Idx = 1 To 512
Print Hex(rxblock(idx)) ; " " ;
Next Idx
Print
End
Sub Sdsendcommand(byval Command As Byte , Byval Argument As Long)
Local extra As Byte
Extra = &HFF
Set Sdcs 'make CS = high
Spiout Extra , 1 '8 bit delay
Reset Sdcs 'make CS low
Command = Command Or &H40 'set bit 6, host bit, bit 7 should be clear (no commands above &H80)
Extra = &H95 'checksum & end bit for command 0, ignored once in spi mode
Print "Command " ; Hex(command)
Print "Argument " ; Hex(argument)
Print "crc " ; Hex(extra)
Spiout Command , 1 'send command
Spiout Argument , 4 'send argument
Spiout Extra , 1 'send checksum & end bit
End Sub
Function Sdreadresponse() As Byte
Do
Spiin Sdreadresponse , 1
Loop Until Sdreadresponse <> &HFF 'until response not equal to &HFF
Print "Sdreadresponse " ; Sdreadresponse
End Function
Sub Getbock(buffer() As Byte)
Local Rxcrc16 As Word
Spiin Buffer(1) , 512
Spiin Rxcrc16 , 2
Print "CRC " ; Hex(rxcrc16)
End Sub |