Why does the SPI library not work on an Attiny?... Answered


Author Options:

Why does the SPI library not work on an Attiny?.. I found some code on gethub to get around this but... Can anyone point me to a good reference (guide, reference book, etc) for programming that might answer these strange truths that I am oblivious to. My background is Mechanical Engineering and Arduino. thanks!

this picture is fromĀ randofo's helpful arduino ISP attiny programming instructable.


SPI in most ATXXX processors is done with special hardware - basically push a byte into a register and leave it. After a period determined by the clock speed, a little flag comes up and says "OK I sent it", and you can either ignore it, or use it to say "Ready for NEXT byte"

In the ATtiny, there is no dedicated SPI, so you have to use "Bitbanging", where you have to decompose the byte into individual bits, stick the bit onto a port pin, toggle another port pin as a clock, then go back for the next bit of the byte.

You have to handle bit order, clockspeed and polarity all on your lonesome......

This is, by far, the best answer I've found on this subject - anywhere. The data sheets mention 'SPI' for the 'Tiny' range, but it's not handled the same as it is for the Mega328P for example. So 'bitbanging' on your lonesome is the only way to go.