Faster SPI in C (GCC), how?
Hello,
Is there a way to specify the frequency for the functions?:
shift_out(7, 8, MSBFIRST, 7, 0b1010110);
shift_in(7, 8, MSBPRE, 8);
Using those two functions will start a COG, right? So there should be a way to specify the frequency, am I right?
Thanks in advance.
Is there a way to specify the frequency for the functions?:
shift_out(7, 8, MSBFIRST, 7, 0b1010110);
shift_in(7, 8, MSBPRE, 8);
Using those two functions will start a COG, right? So there should be a way to specify the frequency, am I right?
Thanks in advance.

Comments
Do you want faster or slower? Either way, you can't change the speeds of these functions. They send as fast as they can, which is pretty slow because they use many higher-level functions instead of directly manipulating OUTA, INA, and DIRA. I wrote my own significantly faster one (and it's still sad how slow it is), but I don't have it on this computer and it didn't support all of the different modes - only the ones I needed (but it's pretty easy to change it). There might be other functions in simpletools that you can configure, but I doubt it. Unless you can find someone else's library that's faster than simpletools', your best bet is probably writing your own. If you need somewhere to start, there's a SPI_Spin.spin in the Spin library that comes with the Propeller Tool that you might be able to port to C.
Here's the link to the Spin SPI library: http://obex.parallax.com/object/566. It says it's for a temperature sensor, but that's just the demo. You just want the SPI_Spin.spin in the zip file. It should be pretty easy to port to C. If you've never seen any Spin before, note that it uses indentation for blocks instead of curly brackets.
Here's the class documentation:
http://david.zemon.name/PropWare/classPropWare_1_1SPI.xhtml
Note: shift_*_fast and shift_*_sector are planned methods that have not yet been implemented (I think shift_*_fast just calls the normal shift_* assembly at the moment), but these are the methods that will be capable of achieving 4 MHz.
David
[FONT=courier new]' Simple fast, bare-bones SPI - transmits 8 bits MSB first - ' data must be left justified - data is not discarded ' Usage: $1234 16 SHL SPIWR SPIWR 'transmit 16 bits ' SPIWRX permits variable number of bits if spicnt is set directly with @SPICNT COGREG! ' SPIWR ( data -- data<<8 ) ' SPIWRX could be an instruction if we manually set the spicnt beforehand SPIWRB shl tos,#24 ' left justify and write byte to SPI (2.8us) SPIWR mov spicnt,#8 ' code execution time of 2.25us + overhead SPIWRX rol tos,#1 wc ' assume msb first (140208 update now rotates) muxc OUTA,spiout ' send next bit of data out xor OUTA,spisck ' toggle clock xor OUTA,spisck ' toggle clock djnz spicnt,#SPIwrX jmp unext ' Receive data and shift into existing stack parameter ' If MOSI needs to be in a certain state then this should be set beforehand ' Clock will simply pulse from it's starting state - normally low ' Useage: 0 SPIRD SPIRD SPIRD SPIRD 'read 32-bits as one long ' SPIRD ( data -- data<<8 ) ' 2.8us SPIRD mov spicnt,#8 ' always read back 8-bits SPIRDX xor OUTA,spisck ' toggle clock test spiinp,INA wc ' read data from device rcl tos,#1 xor OUTA,spisck ' toggle clock djnz spicnt,#SPIRDX jmp unext[/FONT]