C3 SPI Drivers - part 3 ( read bits )

Now we can build up the two basic access routines: Read routine.

The concept of the reading method is the same, but the way bits are feed is different.

You need to read a port and mask the value with the pin bit, then this bit must be added to the result after having shifted it of 1 bit to the left.

Lukily there's a special instruction on propeller that will help us so that we will be able to read a bit with the same amount of cycles used by writing routine.


bitLoopR  and   outa, _SPI_SCK_MASK     ' drop clock 
          test  _SPI_MISO, ina       wc ' this is a little bit tricky: the operation will and
                                        ' the ina with the bit of MISO pin. On c flag will be
                                        ' stored an odd number of bits "1" in the result of
                                        ' and operation. in this case it can only contain
                                        ' 1 or 0 because the result can have only one bit set
                                        ' So c can be 0 or 1 according to the bit MISO! 

          rcl   resultValue, #1         ' another great instruction of propeller! The value in
                                        ' c is put in resultValue, but after having rotated
                                        ' resultvalue by 1 bit to the left! GREAT! 

          or    outa, _SPI_SCK          ' raise clock 

          djnz  _bits, #bitLoopR        ' decrement the counter ( bits ) and loops if not
                                        ' reached 0

 
The same if you want you can optimize the routine for 8 bit reads like we did wth write routine.  Now we only need to put all together and the core of SPI is done.