Kye
05-30-2010, 10:41 PM
I need some help trying to get some SPI rountines for any SD card to work.
Basically, I made a general purpose SPI function that can run at any speed from 1 to about 3 Mhz. The code for that is below.
... CTRA is set to NCO mode (%00100) and its output is the clock pin.
... The SPITiming is either:
· fastTiming := ((constant(2_968_750 << 9) / clkfreq) << 23)
· slowTiming := ((constant(234_375 << 13) / clkfreq) << 19)
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
' Read SPI
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
readSPI mov SPICounter, #8 ' Setup counter to read in 1 - 32 bits.
mov SPIDataIn, #0 wc '
mov phsa, #0 ' Start clock low.
mov frqa, SPITiming '
readSPILoop waitpne clockPin, clockPin ' Get bit.
rcl SPIDataIn, #1 '
waitpeq clockPin, clockPin '
test dataOutPin, ina wc '
djnz SPICounter, #readSPILoop ' Loop.
mov frqa, #0 ' Stop clock high.
rcl SPIDataIn, #1 '
readSPI_ret ret ' Return.
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
' Write SPI
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
writeSPI mov SPICounter, #8 ' Setup counter to write out 1 - 32 bits.
ror SPIDataOut, SPICounter '
mov phsa, #0 ' Start clock low.
mov frqa, SPITiming '
writeSPILoop shl SPIDataOut, #1 wc ' Set bit.
waitpne clockPin, clockPin '
muxc outa, dataInPin '
waitpeq clockPin, clockPin '
djnz SPICounter, #writeSPILoop ' Loop.
mov frqa, #0 ' Stop clock high.
or outa, dataInPin '
writeSPI_ret ret ' Return.
The nice thing about the above piece of code is that I can run the clock at many different speeds and still read the data correctly. However, I need to increase the preformance of my driver and thus I need the above code to go faster.
So after looking through the new FSRW block driver I came up with this below:
... CTRA is set to·Duty mode (%00110) and its output is the clock pin.
... The SPITiming is either:
· fastTiming := ((constant(2_968_750 << 9) / clkfreq) << 23)
· slowTiming := ((constant(234_375 << 13) / clkfreq) << 19)
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
' Read SPI
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
readSPI mov SPICounter, #8 ' Setup counter to read in 1 - 32 bits.
mov SPIDataIn, #0 '
mov frqa, SPITiming
readSPILoop waitpeq clockPin, clockPin
test dataOutPin, ina wc
rcl SPIDataIn, #1
djnz SPICounter, #readSPILoop
mov frqa, #0
readSPI_ret ret ' Return.
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
' Write SPI
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
writeSPI mov SPICounter, #8 ' Setup counter to write out 1 - 32 bits.
ror SPIDataOut, SPICounter '
mov frqa, SPITiming
writeSPILoop shl SPIDataOut, #1 wc
muxc outa, dataInPin
waitpeq clockPin, clockPin
djnz SPICounter, #writeSPILoop
mov frqa, #0
or outa, dataInPin
writeSPI_ret ret ' Return.
And with this above code I should be able to go faster than 3Mhz but I still should be able to slow the clock down when I want to at will.
However, the above code does not work with the SD card and refuses to work at all. I do not know what I am doing wrong.
...
I've attached the source code and all the files need to run the demo I'm working on. The file with the SPI rountines is the SD2.0_FATEngine.spin
Thank you for any help,
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
Basically, I made a general purpose SPI function that can run at any speed from 1 to about 3 Mhz. The code for that is below.
... CTRA is set to NCO mode (%00100) and its output is the clock pin.
... The SPITiming is either:
· fastTiming := ((constant(2_968_750 << 9) / clkfreq) << 23)
· slowTiming := ((constant(234_375 << 13) / clkfreq) << 19)
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
' Read SPI
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
readSPI mov SPICounter, #8 ' Setup counter to read in 1 - 32 bits.
mov SPIDataIn, #0 wc '
mov phsa, #0 ' Start clock low.
mov frqa, SPITiming '
readSPILoop waitpne clockPin, clockPin ' Get bit.
rcl SPIDataIn, #1 '
waitpeq clockPin, clockPin '
test dataOutPin, ina wc '
djnz SPICounter, #readSPILoop ' Loop.
mov frqa, #0 ' Stop clock high.
rcl SPIDataIn, #1 '
readSPI_ret ret ' Return.
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
' Write SPI
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
writeSPI mov SPICounter, #8 ' Setup counter to write out 1 - 32 bits.
ror SPIDataOut, SPICounter '
mov phsa, #0 ' Start clock low.
mov frqa, SPITiming '
writeSPILoop shl SPIDataOut, #1 wc ' Set bit.
waitpne clockPin, clockPin '
muxc outa, dataInPin '
waitpeq clockPin, clockPin '
djnz SPICounter, #writeSPILoop ' Loop.
mov frqa, #0 ' Stop clock high.
or outa, dataInPin '
writeSPI_ret ret ' Return.
The nice thing about the above piece of code is that I can run the clock at many different speeds and still read the data correctly. However, I need to increase the preformance of my driver and thus I need the above code to go faster.
So after looking through the new FSRW block driver I came up with this below:
... CTRA is set to·Duty mode (%00110) and its output is the clock pin.
... The SPITiming is either:
· fastTiming := ((constant(2_968_750 << 9) / clkfreq) << 23)
· slowTiming := ((constant(234_375 << 13) / clkfreq) << 19)
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
' Read SPI
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
readSPI mov SPICounter, #8 ' Setup counter to read in 1 - 32 bits.
mov SPIDataIn, #0 '
mov frqa, SPITiming
readSPILoop waitpeq clockPin, clockPin
test dataOutPin, ina wc
rcl SPIDataIn, #1
djnz SPICounter, #readSPILoop
mov frqa, #0
readSPI_ret ret ' Return.
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
' Write SPI
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
writeSPI mov SPICounter, #8 ' Setup counter to write out 1 - 32 bits.
ror SPIDataOut, SPICounter '
mov frqa, SPITiming
writeSPILoop shl SPIDataOut, #1 wc
muxc outa, dataInPin
waitpeq clockPin, clockPin
djnz SPICounter, #writeSPILoop
mov frqa, #0
or outa, dataInPin
writeSPI_ret ret ' Return.
And with this above code I should be able to go faster than 3Mhz but I still should be able to slow the clock down when I want to at will.
However, the above code does not work with the SD card and refuses to work at all. I do not know what I am doing wrong.
...
I've attached the source code and all the files need to run the demo I'm working on. The file with the SPI rountines is the SD2.0_FATEngine.spin
Thank you for any help,
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,