UPDATE: this post has been updated with a newer version and firmware: 11/29/2010. This newer version provides SoftSPI like capabilities.
In my quest to make my HT1632 display useable I have developed a generic bit banger driver.
The attached archive unzips into the "MicroFrameworkPK_v4_1\Solutions\Netduino" dir.
If you want to build this yourselves you will have to build the managed solution and generate the client assemblies and then build the firmware as usual.
The speed increase in amazing when driving the HT1632 and I know that variable SPI is around the corner, but I thought this might be useful in a more generic context (the code should be device neutral).
The bit banger is constructed with a configuration class that describes two pins, one clock and one data.
Ownership of the pins then belongs to the BitBanger class.
public class Config { public Config( Cpu.Pin clockPin, bool clockEdgeRising, Cpu.Pin dataPin, Cpu.Pin latchPin, bool latchActiveState ) } BitBang.Config config = new BitBang.Config( clkPin, true, dataPin ); // set data & clock pin modes BitBang bb = new BitBang( config );
Additionally the latchPin and latchActiveState Config members allow for additional flexible latching behaviour.
public enum LatchBehaviour { PerTransfer, Per8Bits, Per16Bits, Per32Bits, Never }
LatchBehaviour is passed to the block blitting function and will toggle a latch line (or CS possibly) in a certain manner.
This is very useful for writing to shift registers in a continuous mode.
The BitBang class interface has some basic functionality:
These two methods bang out a stream of bytes, with each byte written from the MSb (bigEndian) or LSb and with optional latch behaviour.
public void Write( byte[] writeBuffer, int writeOffset, int writeCount, bool bigEndian)
public void Write( byte[] writeBuffer, int writeOffset, int writeCount, bool bigEndian, LatchBehaviour latchBehaviour )
And these two methods bang out up to 32 bits with the same endian functionality as above.
public void Write32( UInt32 data, byte numBits, bool bigEndian )
public void Write32AndLatch( UInt32 data, byte numBits, bool bigEndian )
I have attached 2 files containing the compiled firmare (tinyclr.hex.zip) and the source code (bitbang.zip).
Included in the source code is the compiled client assembly which you will find under the MicroFrameworkPK_v4_1\Solutions\Netduino\ManagedCode\SecretLabs_NETMF_Hardware\bin\Release dir.
You will need to reference this assembly or replace the one provided with the NetDuino SDK in order to use the BitBang class.
Hope this is interesting / useful
-(e)