SX/B Shiftin/Shiftout SPI
Rsadeika
Posts: 3,837
I have seen, in other discussions,·where the shiftin/shiftout (shift*) command·can be used to·simulate(?) an SPI protocol. Now, I am wondering if something like that can be done in SX/B. It would be nice if it could be a master/slave multiples. Meaning, master with many slaves or multiple masters and slaves ...
After looking at the asm code for SPI, I noticed that it uses four pins to accomplish the SPI protocol, the shift* commands can only address two pins, one of which is set up as a clk.·Also, what would it take to get the transfer rates up to the one meg level. So, is the answer no, it cannot be done with shift* commands, or will it do it, in a very limited usefullness situation.
If it is possible, then could it be done within an interrupt, or does this mess things up for other commands or the program itself. This seems like it could be a nice solution, no messing around with resistors, just plug wires into the selected pins, and off you go.
Has anybody done something like this, and is willing to share some example code. Or, will the next release of SX/B have the SPI thing covered, and I just sit on my hands till then LOL. Any ideas or input would be appreciated.
Thanks
Ray
After looking at the asm code for SPI, I noticed that it uses four pins to accomplish the SPI protocol, the shift* commands can only address two pins, one of which is set up as a clk.·Also, what would it take to get the transfer rates up to the one meg level. So, is the answer no, it cannot be done with shift* commands, or will it do it, in a very limited usefullness situation.
If it is possible, then could it be done within an interrupt, or does this mess things up for other commands or the program itself. This seems like it could be a nice solution, no messing around with resistors, just plug wires into the selected pins, and off you go.
Has anybody done something like this, and is willing to share some example code. Or, will the next release of SX/B have the SPI thing covered, and I just sit on my hands till then LOL. Any ideas or input would be appreciated.
Thanks
Ray
Comments
As you know, SHIFTIN and SHIFTOUT will cover most applications; if you're after more it's a pretty simple matter to craft it in code. There won't be any changes to SX/B that affect SHIFTIN and SHIFOUT, so you may want to develop something specific for your application. I did this for a Sony PlayStation controller project -- it uses and SPI type protocol where bits are shifted in and out at the same time. There code was very simple to implement (I had even done it on a BASIC Stamp) and the project works well.
Here's how I did it for the PSX:
' Use: {inByte} = PSX_SHIFTIO {outByte}
' -- sends [noparse][[/noparse]optional] "outByte" to PSX
' -- receives [noparse][[/noparse]optional] "inByte" from PSX
PSX_SHIFTIO:
· IF __PARAMCNT = 1 THEN
··· temp3 = __PARAM1······· ' copy output byte
· ELSE
··· temp3 = 0
· ENDIF
· temp4 = 0·················' clear input byte
· FOR temp5 = 1 TO 8····· ··' shift eight bits
··· PsxCmd = temp3.0······ ·' move lsb to Cmd pin
··· temp3 = temp3 >> 1··· ··' shift for next bit
··· PsxClock = 0········· ··' clock the bit
··· WAIT_US 5
··· temp4 = temp4 >> 1···· ·' prep for next bit
··· temp4.7 = PsxData····· ·' get lsb from Data pin
··· PsxClock = 1·········· ·' release clock
··· WAIT_US 5
· NEXT
· RETURN temp4
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Post Edited (Jon Williams (Parallax)) : 2/3/2006 7:23:50 PM GMT
SPI is not well suited for multi-mastering, for one there is more than one data-line Master->Slave and Slave->Master and collision of the Serial Clock is not provided for. Because of this a master would have to be able to access all slave select data pins in order to be absolutely sure the bus was free, the possibility of data and clock collision resulting in garbled transmissions and possible deadlocking of the SPI bus are very high and can arise from many different scenarios.
The SHIFTIN/SHIFTOUT commands are designed for low bandwidth synchronous communications, to achieve 1Mbps throughput, you'd have to write your own assembly routine, speeds approaching OSC/2 are possible.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
Thanks
Ray
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
Thanks
Ray
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Still searching ...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
I must admit that the I²C examples in my book are not the easiest to be followed by a reader who is new to this topic, especially when it comes to multi-master systems, bus arbitration, and all these "nice" events that might happen. But I swear that Chip did not act as a co-author here. If he would, you can be sure that the code would be a lot more efficient, shorter, "more tricky", and almost un-readable.
Just have a look at the UART code most commonly used in SX ISRs. This was originally written by Chip, and I have adapted his version in my book. It is amazing how much can be done with a minimum of SX instructions if you "personally know" each and every bit inside the SX .
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Greetings from Germany,
Günther
Now back to the problem at hand.
Look at the ASM...ENDASM command, you could write entire ISRs in assembly (or the entire program for that matter) with little effort.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
Post Edited (Paul Baker) : 2/4/2006 3:22:23 PM GMT
Thanks
Ray