Shop OBEX P1 Docs P2 Docs Learn Events
starting smart pins from p2spin — Parallax Forums

starting smart pins from p2spin

@JonTitus
@JonnyMac
@evanh
Or anybody.
I have been following the document by @JonTitus regarding the smart pins. Unlocked the word doc to fix the blocked out verbage because of the pdf maker shifted some stuff and making notes to my self. Getting them to work and beginning t understand them much better.
Been looking at the Spin2 documentation and have some confusion as well as sifting through the forums for something simple.
Example code attached.
I am looking of how in Spin2 to simply call the pins from a Spin2 pub(). Bascally start it from spin2 versus directly as the code is written.
Any simple examples and suggestions would be appreciated as well as some links to code that calls stuff from spin2 in pasm as well as passing parameters.
Thank you in advance.
Martin

Comments

  • evanhevanh Posts: 15,910
    edited 2024-10-21 06:40

    The Spin2 provided function is called pinstart(). Not a lot is said about it in the docs. Description is simply PINSTART(PinField, Mode, Xval, Yval)
    PINCLEAR(PinField) is its partner function for returning it back to a plain pin.

    These are listed under section "Built-In Methods" in the official Spin2 manual. I've attached a PDF of the current version 44.

  • evanhevanh Posts: 15,910
    edited 2024-10-21 07:48

    If you've got an oscilloscope, here's a demo you can watch using it:

    CON
    '    _xinfreq = 20_000_000
        _xtlfreq = 20_000_000
        _clkfreq = 20_000_000
    
        PIN = 56
        WIDTH = 500
    
    
    PUB  smartpin_demo()
    
        pinstart( PIN, P_TRANSITION | P_OE, WIDTH, 0 )
    
        repeat
            waitms( 500 )
            wypin( PIN, 5 )
    

    The smartpin mode is one that pulses the pin output for a specified number of steps (transitions) and then holds the output there. I've setup a loop that tells it to make 5 steps, waits half a second, then repeats.

  • JonnyMacJonnyMac Posts: 9,102
    edited 2024-10-21 15:03

    I learned to code by studying working examples. Start with a few simple things that might be useful to what you're doing. It takes a minute to understand the smart pins, but after some time experimenting, it will start to become clear. I'm with Evan: Connect a 'scope or logic analyzer; this will help you make sense of things.

    I've attached three simple objects that use smart pins -- I use these objects frequently.

  • pilot0315pilot0315 Posts: 910
    edited 2024-10-23 10:15

    @evanh your example helps thanks. What is the P_OE stand for?
    @JonnyMac do you have anything a bit less complicated at this time for me to build from. I need to start at a lower level and build up. Thanks for the help to both.
    Martin
    @JonTitus
    Your document is now making sense.
    I will look at the email you sent shortly.
    Martin

  • evanhevanh Posts: 15,910
    edited 2024-10-23 12:29

    @pilot0315 said:
    @evanh your example helps thanks. What is the P_OE stand for?

    When a smartpin is configured, with PINSTART() for example, it diverts its associated DIR bit to become its reset control. The regular DIR pin drive is then done via one bit in PINSTART's mode word - Bit6. The symbol name for setting of bit6 is P_OE. OE is short for Output Enable. Any smartpin mode that has an output to its pin would be expected to set this P_OE bit, otherwise the physical pin stays as just an input.

    There is a full list of these symbols in the Spin2 Doc under the section name "Built-In Symbols for Smart Pin Configuration"

  • @evanh

    Thanks for the definition. Easy. I will look at the built in symbols list.
    Btw, quesiton. I am assuming the the smart pins are fire and forget like the counters on the P1. Is this the basic case?
    My first two mini projects is running servos and also the X-band radar module.
    Any simple examples of passing parameters from pasm2 to spin2?
    Thanks again.
    Martin

  • ersmithersmith Posts: 6,051

    @pilot0315 said:
    Any simple examples of passing parameters from pasm2 to spin2?

    You cannot (practically) call spin2 functions from pasm. Why are you trying to use pasm anyway? For someone just starting out, spin2 is vastly easier. If you find spin2 is not fast enough, then you can use inline pasm within spin2 to optimize critical functions, or you can use flexspin to compile the whole program to pasm.

  • JonnyMacJonnyMac Posts: 9,102
    edited 2024-10-28 12:48

    In the P2, basic servo control is accomplished with PWM smart pin mode. The attached P2 servo object works with analog (50Hz) and digital (300Hz) servos. If you want to do servo ramping (i.e., speed control of movement), that requires an extra cog or running in a timed loop that allows you to refresh the servo position to intermediate positions moving to the desired end point (I have an 8-channel servo controller with movement speed control for the P2 that uses a cog, but doesn't use PWM mode, it uses PULSE mode).

    The attached demo uses PWM to generate a 1kHz PWM output on one pin which is fed into another (jumper wire). The second pin is setup to count positive edges using a smart pin mode. The code shows how to setup the pin to count edges, how to read the count, and how to reset the count.

      pwm.startx(PWM_OUT, 10, 100, 1000, 0)                         ' output 1kHz (10% duty)
      pinstart(CNT_IN, p_count_rises, 0, 0)                         ' set to count pos edges
    
      t := getct()
      repeat
        waitct(t += CLK_FREQ)                                       ' acculate edges for 1 second
        c := rdpin(CNT_IN)                                          ' get the count
        pinfloat(CNT_IN)                                            ' clear/reset count
        pinlow(CNT_IN)                                              ' re-enable counting
        term.fstr1(@"Counts = %d\r", c)
    

    Not that -- as has been mentioned in this thread -- when in smart pin mode the dir bit of the pin is used to enable/reset the smart pin function. The pinfloat command sets the pin to an input, clearing the dir bit to 0 which resets/disables the smart pin mode. The pinlow command makes the pin an output which sets the dir bit to 1 which re-enables the smart pin. The use of the dir bit for on/off control of the smart pin is why you will see p_oe in some smart pin modes (e.g., PWM); this enables smart pin output.

    When running on my computer, the output looks like this.

    Any simple examples of passing parameters from pasm2 to spin2?

    It's no different than in the P1: You have to establish a shared location in the hub that the Spin2 and PASM2 cogs know about. In the P2 the initial pointer (par in P1, ptra in P2) can be used with offsets which can make data exchange between PASM2 and Spin2 nicer.

    For example:

    pasm_main               rdlong    t1, ptra[0]
                            rdlong    t2, ptra[1]
                            mov       t3, t1
                            adds      t3, t2
                            wrlong    t3, ptra[2]
    

    I use offsets with ptra in jm_fullduplexserial.spin2 (in the attached archive).

  • evanhevanh Posts: 15,910

    @pilot0315 said:
    Btw, quesiton. I am assuming the the smart pins are fire and forget like the counters on the P1. Is this the basic case?

    The nature of hardware is it does tend to do its own thing, yes. Both wheels on a bike rotate as needed without having to alternately micro-manage them one by one.

  • @JonnyMac

    Thanks for the help I am making progress.

Sign In or Register to comment.