Doh!
The buffer in FlashDemo.spin2 is declared long!
VAR
long buffer[4096]
So actually 256 bytes are read but only every fourth is displayed. You don't notice the gaps because also only every fourth byte is set to some meaningful value
repeat i from 0 to 255
buffer[i]:= i
This sets byte 0, byte 4, byte 8 etc. to 1, 2, 3 etc. and the rest to 0.
...and I found out that the REP is no good idea. It works with Fastspin and -O1 optimization because the code is automatically copied to LUT RAM. But with no optimization or other compilers it fails because the jump-back of REP confuses the FIFO in hubexec mode and the timing is corrupted.
I'll fix it and make another pull request for GIT.
A tip: the generated code for Read() will get much better if you replace
ORG
drvh #spi_cs
END
with
pinh(spi_cs)
The ORG is inhibiting a number of optimizations -- too many, it seems, and I need to dig into that, but in general the fewer ORGs in your code the better the compiler can do. In fastspin, at least, the pinh(spi_cs) will directly translate into drvh #spi_cs.
I have to confess that this object is one of the first things I wrote for the P2. There was very little documentation available about Spin2 and most examples were in pure assembler. So I took what I could get. Suggestions for improvements are welcome. I've seen that the situation is becoming better.
Eric, I have played a little bit with the Spin2 pin commands after reading the new documentation. The commands are quite powerful and the predefined constants help making the code more clear by avoiding using literal bit patterns. But the optimization is not as good as it could be. Example:
Inlining the PinStart code would be better than a call. It executes faster and consumes less memory as it could be translated to only 4 instructions (WRPIN, WXPIN, WYPIN, DIRH) instead of 6. WRPIN() could also be translated to one single instruction instead of MOV + WRPIN.
Comments
PUB Read (hubAdr, flashAdr, size) ' read any number of bytes test[0]:= size Spi_Init () test[1]:= size Spi_Cmd32 (Read_Data, flashAdr) test[2]:= size repeat size byte[hubAdr++]:= Spi_RdByte () ORG drvh #spi_cs ENDBut when I read out test[] later it's always correct, 3 times 80 for example. So I looked at the generated assembler code. Not optimized very well but looks correct. Size (local3, local4 and local6) is decremented by only #1 per loop iteration.The buffer in FlashDemo.spin2 is declared long! So actually 256 bytes are read but only every fourth is displayed. You don't notice the gaps because also only every fourth byte is set to some meaningful value
repeat i from 0 to 255 buffer[i]:= iThis sets byte 0, byte 4, byte 8 etc. to 1, 2, 3 etc. and the rest to 0.will check it again with VAR byte buffer[256] and report back later !
adding debug output of the buffer content, prior to flashWrite, may have also been a good idea earlier!. Extra Doh!
I'll fix it and make another pull request for GIT.
ORG drvh #spi_cs ENDwith The ORG is inhibiting a number of optimizations -- too many, it seems, and I need to dig into that, but in general the fewer ORGs in your code the better the compiler can do. In fastspin, at least, the pinh(spi_cs) will directly translate into drvh #spi_cs.