Probably re-inventing the wheel here but just for the hell of it I decided to try writing a software SPI library.
C++ and Arduino are a relatively new to me (about 18 months now) so I'm not up to speed on all the workings of arduino cores a C++ language.
I feel there must be a more elegant (and maybe faster) way to do the below code that needs to pull bits out of a byte (_data) in either MSB/LSB order. I'm hoping someone has a better way to show me.
if(_bitOrder == LSBFIRST) { // Get bit to send next
tmpBit = _data & 0x01;
_data = _data >> 1;
}
else {
tmpBit = _data >> 7;
_data = _data << 1;
}
The other question is one of timing. Currently I'm coding delayMicroseconds(x); to derive the SPI clock but this may introduce a large time overhead as it's a core call but any other method (ASM loops) would need to be adjusted depending on the code speed of the processor. Without me having to write test code and logic analyse would delayMicroseconds be consistent enough for an SPI clock?
Attached is the (untested) program prior to being broken down into library.
SoftSPI.zip (929 Bytes)