Simple SPI Flash program
AGCB
Posts: 327
in Propeller 1
I'm trying to learn to use a SPI Flash device (Winbond 25Q32FVSIG) and simply write and read a few addresses to get me started. I'm sure Mike Green's Winbond objects are high class but they are well beyond my abilities at present.
So I was trying to use SPI_SPIN to make a simple R/W program. The demo for that object in the Demo Library is for a temperature sensor and quite a bit different that the flash.
Is this possible?
Is there a simple example to get me started?
Your recommendations and help are greatly appreciated!
Thanks
Aaron
So I was trying to use SPI_SPIN to make a simple R/W program. The demo for that object in the Demo Library is for a temperature sensor and quite a bit different that the flash.
Is this possible?
Is there a simple example to get me started?
Your recommendations and help are greatly appreciated!
Thanks
Aaron
Comments
Modes A-D?
In the SPI_SPIN there are also Modes 4 & 5. How do these relate?
Here is a little demo code (untested) that should read the device ID of the Flash: See the datasheet of the Flash chip for the values you should get. For the Winbond 25Q32 I think it is: $EF,$40,$16.
Andy
Got that to work but not with less than CLOCKDELAY of 7
Aaron
Thanks
Aaron
You define the WrData Command in the highest byte, but send only the lowest byte as the command (which is 00).
You can send the command and the address in one spi call, then it makes even sense how you have defined the commands: If you make a page write, you must not deselect the CS until all bytes are written.
For the Read: As you have it now, you deselect CS after the read command and the address, this can not work. CS must go low before the command, and high after the end of the read(s).
Hope this helps
Andy
Yes, I think so.
RdData | address builds a 32bit value with the command in the highest byte and the address in the lower 3 bytes: and then the whole 32bit value is shifted out, which is the same as if you shift 4 times a 8 bit value.
Andy
I thought of splitting it into 2 programs, Write and read. But that does not change anything. It all still has to be perfect or nothing.
I probably would have been very happy w/ Mike Green's "Winbond Driver" but I could not find a demo of it and trying to use it myself proved beyond my abilities. If you have used that object for a project, I would be grateful if you would share some of your code.
The demo for SPI in the library only uses a temperature sensor and has nothing to do w/ addresses or multi byte reads or writes.
I have also spent much time searching the Obex and the web for examples/code.
Thanks
Aaron
Using the SPI_Spin object is so horrible inefficient that I wrote my own little SPI function, that does shift-in and shift-out at the same time.
It shows the flash-ID, erases a sector of the flash, writes some data and reads them back - all works as intended.
Have you connected two pullup resistors at the Hold and the WE pin of the flash?
Here is the code:
Andy
I was about to give up and use a SD card which I have done before but now I will pursue this flash learning theme some more.
Thanks Andy
Aaron
The SPI subroutine (methode) shifts up to 32 bit out at the MOSI pin and produces a clock High-Low for every shifted bit. If you shift a variable to the left, you get a gap on the right. Normally this gap is filled with a Zero, but here I fill it with the level at the MISO pin, so the subroutine can be used to shift out and shift in at the same time (a full duplex spi).
To read a byte you just send dummy data out, I used Zero: spi(0,8). This sends 8 zeroes at MOSI and produces 8 clocks at CLK, and shifts in 8 times the state from MISO.
If you only need to write (shift out) then you just ignore the shifted in data.
Hope this helps
Andy
I don't understand it the way you do because SPI is a new concept to me. One thing I noticed was there was no baud rate in the CLK. This makes sense because the CLK will not go low again until the bit is shifted out. I'm stuck on the 'rotate left' operator "<-" What does that do?
I have to learn SPI to operate a pair of nrf24L01 modules which are an inexpensive substitute for Xbee. ( Very little code can be found for the Propeller. I have to study datasheets.)
I think baud rate will be necessary to synchronize the transmitter to the reciever because it's wireless. The nrf24L01 is a duplex tx/rx transceiver but it will take some time before I can take advantage of that capability.
obex.parallax.com/object/430
obex.parallax.com/object/432
Andy
Where could inquiring forth minds find those neat spi flash WORDS?
They are in EXTEND-TF2.fth in the Tachyon Dropbox folder under P2 but just for convenience here it is. The SFWR and SFRD are coded in assembler but the source here includes the high-level version in the IFNDEF sections which works just fine.
This, for me, is a crash course but you've helped in a big way. My previous Spin projects were easy.
I had to become a student of computer architecture. I had to understand hexadecimal addressing and I had to have a clearer understanding of memory addresses and registers.
It finally occurred to me that in SPI, the data bit is interpreted when the clock is in a specific phase.
I had 'struggled' with how a wireless connection differed from a hard wired connection. It took a while but the light is going on in my head.
Thanks.