P2 simple SPI flash memory driver
pic18f2550
Posts: 400
PRI Spi_Init() | div div:= (clkfreq / 100_000_000) #> 4 ' adjust smart pin base period to actual frequency ORG fltl #spi_di ' reset smart pins fltl #spi_do wrpin ##modeClk,#spi_ck ' smart pin transition mode wrpin ##modeTXD,#spi_di ' smart pin synchronous serial transmit wrpin ##modeRXD,#spi_do ' smart pin synchronous serial receive wxpin div,#spi_ck ' clock transition base period drvl #spi_ck ' enable smart pin END
I would like to know with which clock the SD card is driven, if the clkfreq is 256Mhz.
Thanks
Comments
Since it is using transition mode, the effective clock is
clkfreq/(div*2)
.For clkfreq = 256_000_000 that obviously comes out to... 4. Which it will always do since 500_000_000 is not a valid clkfreq.)
So the clock frequency comes out to 32 MHz
wxpin ( P2clk / ( SPIclk * 2 ) ) << 16 , #spi_ck
wxpin (256,000,000 / ( 5,000,000 * 2 ) ) << 16 , #spi_ck
That's what I understood from the documentation.
Is that correct?
No, the *2 is because of the transition mode. As the name implies, the divider values counts the cycles between level transitions (i.e. high-to-low and low-to-high), so the overall period is twice the divider value.
Also, no <<16
Ok
256,000,000÷2÷26 would be the correct way to calculate the value of wxpin? To get a takt of 5.000.000?
Checks out, value should be 26
I'm a simpleton: I prefer to use pulse mode for the SPI clock. This the setup from my simple flash interface object.
This lets you set the number of bits in the transaction very easily
A warning: don't use too low frequency for the flash while SD is inserted. I managed to "unformat" the SD with 2 MHz clock for the flash (sector 0 overwritten). After testing a flash related (working) code there were no more files on the SD.
I raised the flash clock to 20 MHz, no more SD problems.
Use SPI clock mode 3 (CPOL=1, CPHA=1) to avoid incorrect device selection. That keeps the two clocks high during idle, which is important so as not to select the other device when idle. I've converted FlexC's SD driver to do this.
The alternative mode 0 workaround is a convoluted device select sequence for each access to each device.
Jon's code above only needs the clock inverted and it'll work fine as mode 3.
Wuerfel_21 is that correct?
I want to write first of all a sector of a SD card.
What surprises me that in the original only 256 bytes stand should it not be 512?
Source: https://github.com/parallaxinc/propeller/tree/master/libraries/community/p2/All/Simple_SpiFlash
That's a flash driver, not an SD driver
Sorry Wuerfel_21 was already on the other topic, it was about SD cards.