C3 SPI Set Channel

As said before, to access any SPI device on C3 you first need to select its channel. In the C3 there is a clock counter chip that drives a multiplexer according to pulses received from propeller.

For example to select channel 5 you need 5 clock pulses to send to the counter, which will configure its output lines with this value. The multiplexer receives 5 on its input lines and selects its channel 5 and put to low the signal. The cs line of the device connected to that channel will be now active, and the device should be ready to send/receive data. 

The set channel routine has been included in the asm driver to speedup the process.

The concept of sending pulses method is simple... 


_spi_set_ch
            mov  _channel, arg0
            and  outa, _SPI_SEL_CLR_MASK ' send clr pulse to the counter
            or   outa, _SPI_SEL_CLR
            cmp _channel, #0 wz
       if_z jmp  #_spi_set_ch_ret ' if channel is 0 then go back
            mov  ch_cycle, _channel
loopchannel or   outa, _SPI_SEL_CLK ' sent spi clock cycles as much as
' the channel number

            and  outa, _SPI_SEL_CLK_MASK
            djnz ch_cycle, #loopChannel
_spi_set_ch_ret
            ret

' used variables -----
_channel long 0 ' channel setup variable
ch_cycle long 0
 

Set channel 0 and set channel # have been separated to optimize speed, since with channel 0 is not necessary to have pulses but only to reset the Clear line. For the rest nothing in particula, just remember to assign to arg0 your parameter ( the channel number ) if you use this function as a standard one.