Shop OBEX P1 Docs P2 Docs Learn Events
Call subroutine from within (spin) cog? — Parallax Forums

Call subroutine from within (spin) cog?

ZootZoot Posts: 2,227
edited 2015-08-06 21:30 in Propeller 1
I have a robot program on a Propeller 1 I'm working on.
The main program uses a PWM object that launches it's own cog.
The main program then launches another cog that does some RTC stuff, updating of ramping values, etc.
Can the second cog call methods in the original PWM object? In other words, can the new spin code cog call subroutines from the main program? It compiles, but don't know if this will work in practice.
e.g.
OBJ
  pwm : "pwm.spin" ' launches a cog, but also has some methods for updating PWM duty values and such
PUB Main
  pwm.start  cognew( monitor_cog, @stack)

  repeat
     ' do more stuff

PUB monitor_cog | tmp1
   repeat            tmp1++      if tmp1 > 100         tmp1~      pwm.duty( ledPin, tmp1, 1000 ) ' this method is in the pwm object declared in the main program      waitcnt( clkfreq / 50 + cnt )     ' can you do that from within the cog?
(also, I can't figure out how to post code on the new forums)

Comments

  • DavidZemonDavidZemon Posts: 2,973
    edited 2015-08-02 14:26
    Re: Posting code
    Use HTML tags from the "Show source" button at the right of the toolbar <pre><code>my code here</code></pre>
    [code]
    Any cog can "communicate" with any other cog through hub RAM. Probably, you can do what you want since the PWM core code is already running in another cog. The easiest thing would be to give it a try and if it works, great! However, you could also look through pwm.spin and find pwm.duty. If that method doesn't use any stateful variables (variables that were modified in the Main cog), you'd be good to go.
  • It really depends on the specifics of the object. Some motor control objects set I/O pins from both PASM and Spin. If the start method sets I/O pins with Spin before or after starting a new cog then you probably want to make all calls to that object from the same cog which called the Start method.
    Some motor control objects just pulse the enable pin from PASM but the direction pins are set with Spin methods. The same sort thing happens with one of the OLED display objects. Most the pins are controlled from PASM but the command/data pin and the reset pin are controlled from Spin.
    The safest thing to do is to make calls to the PWM object from the same cog which called the Start method. If you attach an archive of your program we can let you know if you need to worry about which cog calls the object.
  • ZootZoot Posts: 2,227
    OK, so it "works", in the sense that spin figures out where the "subroutine" is and calls it. In this case, the objects all have helper functions (methods) that manipulate variables in the running cog, not pin wiggling, so it should be OK.
    I just wasn't sure if you could even "run" external subroutines from within a spin cog.
    Somewhat related: is it correct that the spin interpreter itself uses up a cog? So if I have 6 spin cogs that I've set in motion, I am really using 7 cogs, because one runs the intrepreter?
  • DavidZemonDavidZemon Posts: 2,973
    edited 2015-08-02 18:09
    The first part is correct, second part, no so much. The Spin interpreter runs in every cog that needs it. There is some onboard ROM for the interpreter that every cog has access to. Spin instructions (tokens) are pulled down from HUB RAM into a cog's memory and then interpreted.
  • A Spin cog is the Spin interpreter. There's not a separate interpreter side-by-side with your Spin code. Spin byte codes reside in the hub only.
    -Phil
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-08-03 03:48
    Cog #0 gets loaded with the Spin interpreter on startup. Any time "cognew" is used to launch a Spin method another copy of the interpreter is loaded (from ROM) into the new cog to execute Spin code from hub RAM. Any "cognew" statements which are used to launch PASM code just run the PASM code.
    The Spin interpreter requires some stack space in the hub which is why the address of stack space is added to "cognew" commands launching Spin code but not PASM code.
  • ZootZoot Posts: 2,227
    Makes perfect sense. Thanks. Now I know why what works... does :)
Sign In or Register to comment.