Shop Learn
[PASM2] Need help configuring Smart Pin for "pulse/cycles" mode — Parallax Forums

[PASM2] Need help configuring Smart Pin for "pulse/cycles" mode

CabbageCabbage Posts: 12
edited 2021-04-29 19:00 in Propeller 2

I'd like to configure Pin 1 as Smart Pin mode "00100". I've been following the latest "Documentation v35" but I'm a bit baffled by pages 73-75. Anyway here's what I've come up with so far, which doesn't seem to work as I'd expected...

DAT
        org

        'select the standard external 25 MHz crystal
        hubset          #$F0

        'configure PIN 1 to be a SMART PIN in mode "pulse/cycle output" (M=00100)

        { syntax
         WRPIN D/#,S/# - Set smart pin S/# mode to D/#, ack pin
        }
        WRPIN           PULSE_CONFIG, PULSE_PIN
        WXPIN           PULSE_X, PULSE_PIN 
        WYPIN           PULSE_Y, PULSE_PIN 






        'just blink a LED on the dev board forever
        rep             @.loop, #0 ' infinite loop
        drvnot          #56
        waitx           ##12_500_000  
.loop

'-------------------

'WRPIN syntax:          %AAAA_BBBB_FFF_PPPPPPPPPPPPP_TT_MMMMM_0
PULSE_CONFIG  long      %0000_0000_000_0000000000000_00_00100_0
PULSE_PIN     long      1
PULSE_X       long      $0010_0100
PULSE_Y       long      $ffff_ffff

I'm running this on a "P2 Eval" board, which appears to have a 25 MHz external crystal. The LED does flash at 1 Hz. I'm using a Saleae Logic to show the pin states.

The problem is that Pin 1 stays HIGH all the time, but I was expecting it to approximate a PWM signal of about 1/16 duty cycle (possibly inverted).

Clearly I've misinterpreted the instructions, what am I doing wrong?

Comments

  • RaymanRayman Posts: 11,988
    edited 2021-04-29 18:39

    That hubset looks like a very old version...
    Also, I think the crystal is 20 MHz.

    You may want to start off with Spin2 first...

  • RaymanRayman Posts: 11,988

    You can set the smartpin in one line of Spin2 or Flexprop C code, like done here for NCO:
    http://www.rayslogic.com/Propeller2/NCO.htm

  • RaymanRayman Posts: 11,988
    edited 2021-04-29 18:38

    Here's a sample of some pure P2 assembly that sets the clock in a way that I think @ozpropdev came up with a long time ago:

    CON  'RJA:  new for real P2 - you can use different xdiv and xmul to set clock frequency:  /10*125 -> 250 MHz or /20*297 = 297 MHz
      _XTALFREQ     = 20_000_000                                    ' crystal frequency
      _XDIV         = 20'2                                            ' crystal divider to give 1MHz
      _XMUL         = 297'25                                          ' crystal / div * mul
      _XDIVP        = 1                                             ' crystal / div * mul /divp to give _CLKFREQ (1,2,4..30)
      _XOSC         = %10                                  'OSC    ' %00=OFF, %01=OSC, %10=15pF, %11=30pF
      _XSEL         = %11                                   'XI+PLL ' %00=rcfast(20+MHz), %01=rcslow(~20KHz), %10=XI(5ms), %11=XI+PLL(10ms)
      _XPPPP        = ((_XDIVP>>1) + 15) & $F                       ' 1->15, 2->0, 4->1, 6->2...30->14
      _CLOCKFREQ    = _XTALFREQ / _XDIV * _XMUL / _XDIVP            ' internal clock frequency                
      _SETFREQ      = 1<<24 + (_XDIV-1)<<18 + (_XMUL-1)<<8 + _XPPPP<<4 + _XOSC<<2  ' %0000_000e_dddddd_mmmmmmmmmm_pppp_cc_00  ' setup  oscillator
      _ENAFREQ      = _SETFREQ + _XSEL                                             ' %0000_000e_dddddd_mmmmmmmmmm_pppp_cc_ss  ' enable oscillator
    
    
    
    DAT 'Start of HyperRAM test code
    orgh  0
    org   0
    '+-------[ Set Xtal ]----------------------------------------------------------+ 
    ' RJA:  New for real P2
                    hubset  #0                              ' set 20MHz+ mode
                    hubset  ##_SETFREQ                      ' setup oscillator
                    waitx   ##20_000_000/100                ' ~10ms
                    hubset  ##_ENAFREQ                      ' enable oscillator
    '+-----------------------------------------------------------------------------+    
    
  • RaymanRayman Posts: 11,988
    edited 2021-04-29 18:50

    Just one other tip...

    In Spin2 mode (and also with FlexProp C), you can do inline assembly, which might be easier.

    In Spin2, this define in a con section sets the clock automatically:

    con
    
      CLK_FREQ = 200_000_000                                      ' system freq as a constant
    
  • @Rayman said:
    That hubset looks like a very old version...

    The hubset example was taken from the v35 documentation (page 58).

    @Rayman said:
    Also, I think the crystal is 20 MHz.

    The crystal is 25 MHz on my board (no idea why, perhaps it's an old-stock board?? I got it from Mouser in the UK, but it shipped from the USA).
    The markings on the crystal aren't very helpful: "6276 T 0806".
    Nevertheless the above code does give a near perfect 1 Hz square wave on the LED.

    @Rayman said:
    You may want to start off with Spin2 first...

    I'm not interested in Spin really, I need the bare-metal performance of PASM for my purposes.
    Ultimately I don't need to to very complex things, but I do need to do simple things on MANY channels at once and do them with near perfect determinism. It's... complicated. :)
    In order to get there, I need to learn the quirks of this platform. I'm familiar with the Prop 1 but this P2 is so wildly different it's like starting from scratch. Fun though.

  • RaymanRayman Posts: 11,988
    edited 2021-04-29 19:24

    Ok, Looks like "hubset #$F0" switches to the internal fast RC oscillator which is somewhere around 20 MHz. Guess it's 25 MHz in your case.

    Doesn't look like you are using the crystal at all.

    I'm only aware of 20 MHz crystals being used on any Parallax boards.

    BTW: I think that RC fast mode is what you are in after booting anyway. Do you get the same thing if you comment out that line?

  • AribaAriba Posts: 2,471

    This PASM code should work: (see comments for explanation)

    CON
      _clkfreq   =  25_000_000
      PULSE_PIN  =  1
    
    DAT
            org
    
            asmclk         'set clock to _clkfreq
    
            'configure PIN 1 to be a SMART PIN in mode "pulse/cycle output" (M=00100)
    
            { syntax
             WRPIN D/#,S/# - Set smart pin S/# mode to D/#, ack pin
            }
            dirl            #PULSE_PIN
            WRPIN           #P_PULSE + P_OE, #PULSE_PIN  'mode with defined constants
            WXPIN           ##2<<16+8, #PULSE_PIN        '2cy low, 8-2=6cy high
            dirh            #PULSE_PIN                   'enable smartpin
            WYPIN           ##-1, #PULSE_PIN             'start pulses
    
            'now Pin 1 should be generating a PWM pattern of something like 1/8 duty cycle
    
    
            'just blink a LED on the dev board forever
            rep             @.loop, #0 ' infinite loop
            drvnot          #56
            waitx           ##12_500_000  'toggle every half second for a 1 Hz square wave on the LED
    .loop
    

    Andy

  • RaymanRayman Posts: 11,988

    Right, that's a new way to set the clock... "asmclk" is a macro that substitutes in some assembly lines, like the ones above.

    Guess that's easier, but I like the @ozpropdev way better...

  • AribaAriba Posts: 2,471
    edited 2021-04-29 19:45

    The same in Spin2:

    CON
      _clkfreq   =  25_000_000
      PULSE_PIN  =  1
    
    pub main()
      pinstart(PULSE_PIN, P_PULSE + P_OE, 2<<16+8, 0)
      wypin(PULSE_PIN, -1)
    
      repeat
        pintoggle(56)
        waitms(500)
    

    The timing is the same as with PASM, just easier to write.
    If the smartpin does the pulses, it does not much matter if you set it with PASM or Spin, and Spin2 is much faster than Spin on the P1.

    Andy

  • Cluso99Cluso99 Posts: 17,731

    If you have a P2-Eval board then it should only be a 20MHz xtal. AFAIK Parallax never used anything other than 20MHz. P2D2 boards used 25MHz originally.

  • evanhevanh Posts: 10,951

    @Cabbage said:

    @Rayman said:
    That hubset looks like a very old version...

    The hubset example was taken from the v35 documentation (page 58).

    Someone else, other than Chip, has added it. The "F" is a hack workaround for a known issue with switching PLL modes.

    The "0" is selecting RCFAST internal R-C oscillator - which is not crystal locked at all. If you look a little closer at your measured frequency you'll see, although it is close, it isn't really 25 MHz. Chip did a good job of making RCFAST temperature stable too.

  • evanhevanh Posts: 10,951
    edited 2021-04-30 06:19

    PS: The mode number to get the external crystal directly is HUBSET #%10_10

    EDIT: And to use it safely you want to also have the requisite stabilisation steps:

        hubset #%10_00    ' startup the crystal oscillator, RCFAST selected
        waitx ##25_000_000/200    ' pause 5 ms, in RCFAST, for crystal oscillator to stabilise
        hubset #%10_10    ' switch over to XI clock source (external crystal)
        ...
    
  • evanhevanh Posts: 10,951

    As for the OP source code. There is two fixes: One is the pin's DIR has to be set high to enable the smartpin. The other is related but more obscure. Because DIR is repurposed from controlling the pin to controlling the smartpin, there is, when a smartpin is operating. a bit in the WRPIN mode word for enabling the pin output drive - TT=%01

    DAT
            org
    
            'select the standard internal 25 MHz RCFAST oscillator
            hubset          #$F0
    
            'configure PIN 1 to be a SMART PIN in mode "pulse/cycle output" (M=00100)
    
            { syntax
             WRPIN D/#,S/# - Set smart pin S/# mode to D/#, ack pin
            }
        DIRL        PULSE_PIN
            WRPIN           PULSE_CONFIG, PULSE_PIN
            WXPIN           PULSE_X, PULSE_PIN 
        DIRH        PULSE_PIN
            WYPIN           PULSE_Y, PULSE_PIN 
    
    
    
    
    
            'just blink a LED on the dev board forever
            rep             @.loop, #0 ' infinite loop
            drvnot          #56
            waitx           ##12_500_000  
    .loop
    
    '-------------------
    
    'WRPIN syntax:          %AAAA_BBBB_FFF_PPPPPPPPPPPPP_TT_MMMMM_0
    PULSE_CONFIG  long      %0000_0000_000_0000000000000_01_00100_0
    PULSE_PIN     long      1
    PULSE_X       long      $0010_0100
    PULSE_Y       long      $ffff_ffff
    
  • evanhevanh Posts: 10,951

    Ariba has also demonstrated using the built-in symbols to get the same. eg: WRPIN #P_PULSE + P_OE, #PULSE_PIN 'mode with defined constants

  • evanhevanh Posts: 10,951

    For future reference: DIR is an important control. The base period timer, of those related smartpin modes, runs its own little dance that takes a little wrangling when you want the pulses coordinated at an exact phase alignment. Further reading - https://forums.parallax.com/discussion/comment/1522036/#Comment_1522036

  • JRoarkJRoark Posts: 728
    edited 2021-04-30 12:31

    EvanH, if you ever decide to write a P2 book, count me in for purchase.

  • evanhevanh Posts: 10,951

    That certainly strokes the ego. :D
    Appreciated.

    I'm way too lazy though. And some credit must go to others' too - For throwing questions and giving their own input. It would be a big list: From Chip's and Eric's tools and docs and Brian's and Cluso's early sources, JMG's curveball questions to Tony's and Andy's snippets and Roger's races with the external RAMs. Even the early streamer work with Mike (msrobot) ... I wouldn't have been motivated without the community here.

    I was also going to say I've learned to write better but that's not really the case. It's all down to lots of testing to know a comprehensive explanation.

  • Thanks all for the tips, I'll try this out over the weekend.

    Is there an ETA on a formal datasheet & manual for the P2?

    Seems like the forum IS the de facto documentation for this chip. That's great for the community, but not so hot for prospective industrial adopters.

    I'm close to being asked to do a formal adoption evaluation of the P2 for work and as it stands I think the current lack of coherent documentation would be a deal-breaker.
    That would suck, because I know for a fact that the P2 would be ideal for some of the more exotic projects we have on the roadmap, but I can't prove that to the boss without hard, documentary evidence and probably a working demo too.

  • JonnyMacJonnyMac Posts: 7,589

    I think @"Jeff Martin" will have an update on documentation during the next P2 Live Forum Discussion on May 5.
    -- https://forums.parallax.com/discussion/171704/propeller-2-live-forum-early-adopter-series-topics-speakers-and-registration/p1

  • Hi Jon,

    Thank you for the link, I've registered. Looks interesting, perhaps I can get a colleague or two of mine to join in as well.

Sign In or Register to comment.