Shop OBEX P1 Docs P2 Docs Learn Events
What does a setcommand statement do? — Parallax Forums

What does a setcommand statement do?

JonTitusJonTitus Posts: 193
edited 2020-07-06 22:35 in Propeller 2
Several SPIN/assembly-language programs include statements such as this one from an SPI object:
PUB SHIFTOUT(Dpin, Cpin, CSpin, Bits, Value)             
    setcommand(1, @Dpin)
What does this instruction do? Why do programmers use it? I've assumed the @Dpin points to the starting address of the arguments. That's as far as I got. The setcommand statement isn't documented in the Propeller Manual v. 1.2 and I can't find an explanation--and a clear example in the Forum. Thanks for your help. --Jon

Comments

  • setcommand is simply a method that would have been defined elsewhere in the SPI object.
    Look for something like this in the code.
    PUB setcommand(c,p)
    
  • JonnyMacJonnyMac Posts: 9,104
    edited 2020-07-07 04:00
    setcommand is simply a method that would have been defined elsewhere in the SPI object.
    Indeed, it is.
    PRI setcommand(cmd, argptr)        
        command := cmd << 16 + argptr  
        repeat while command  
    
    You're right, Jon: @Dpin is passing the hub address of the parameters which are used by the PASM cog. A non-zero value in the cmd parameter of setcommand() triggers the operation.

    I haven't yet developed a comfort with the SPI modes of the smart pins, so I'm working on an inline P2ASM program. This is the shiftout() function from that code. I really like that the P2 allows us to blend Spin2 and P2ASM without invoking a cog. I still need to wring-out my shiftin() and shiftio() functions -- then I will send to Parallax.
    pub shiftout(outval, bits, mode) | x, clk, do, di, tix
    
    '' TX bits from outval using defined SPI pins
    '' -- mode determines bit order and clock phase
    ''    * 0 = LSBPRE, 1 = MSBPRE, 2 = LSBPOST, 3 = MSBPOST
    ''    * modes 0..1 are CPHS0; modes 2..3 ard CPHS1
    
      longmove(@clk, @sck, 4)                                       ' copy pins & timing
    
      org
                    drvl      do                                    ' make sdo output if shared
    
                    test      mode, #%01                    wc      ' check for lsb mode
        if_nc       rev       outval                                ' lsbfirst -- reverse the bits
        if_c        mov       x, #32
        if_c        sub       x, bits
        if_c        shl       outval, x                             ' msbfirst -- slide into position
                               
                    test      mode, #%10                    wz      ' check clock mode
    
                    rep       #9, bits                              ' shift out the bits
                     rol      outval, #1                    wc      ' c <- outval.[31]
        if_z         drvc     do                                    ' c -> sdo (CPHS0)
                     waitx    tix                                   ' let sdo settle
                     drvnot   clk                                   ' toggle sclk
                     waitx    tix
        if_nz        drvc     do                                    ' c -> sdo (CPHS1)        
                     waitx    tix
                     drvnot   clk
                     waitx    tix
                   
    so_exit         drvl      do                                    ' leave sdo low
                               
      end
    
  • AribaAriba Posts: 2,690
    Please don't just port P1 objects to P2 without rethinking it. Starting another cog for PASM code is not necessary for the P2. Spin2 is nearly as fast as PASM on the P1, and if you need the speed, you can use inline PASM, or smart pins.

    Andy
  • JonnyMacJonnyMac Posts: 9,104
    edited 2020-07-07 01:05
    +1. I love inline P2ASM for things like this. Last week I ported by PASM code for 1-Wire coms to inline P2ASM; works great, and I save a cog.
  • I agree, Jon. Nice to use the Smart-Pin capabilities, too, and free software from a lot of bit bashing and overhead. Are you working on an SPI object for Prop-2? --Jon
  • JonnyMacJonnyMac Posts: 9,104
    edited 2020-07-07 03:54
    Are you working on an SPI object for Prop-2? --Jon
    Yes; that inline P2ASM code is from my P2 SPI object. The methods are:
    -- start() (setup pins, clock mode, and clock timing)
    -- shiftout() (as above, works in all modes)
    -- shiftin() (needs testing -- waiting on some shift registers)
    -- shiftio() (needs testing)

    At the moment, I'm not convinced using smart pins is the best way for me to do SPI. That said, I reserve the right to be wrong and to change my mind down the line.
Sign In or Register to comment.