Shop OBEX P1 Docs P2 Docs Learn Events
Cogs not running simultaneously — Parallax Forums

Cogs not running simultaneously

DavidZemonDavidZemon Posts: 2,973
edited 2012-02-07 13:55 in Propeller 1
Hello all. I'm just getting started with the propeller. I'm playing around with some code right now, just familiarizing myself with the language. In my first attempt at parallel processing, I failed. The program runs, but it's running in series, not parallel. The cognew() function calls the toggle method, but the next line of code doesn't run until the first LED has flashed 5 times. Anyone see where I'm going wrong?

Here's an object, Toggle:
PUB toggle (pin, delayCnt, repetition) | x
  
  x := clkfreq/100
  dira[pin]~~
  if repetition == 0
    repeat
      !outa[pin]
      waitcnt(x*delayCnt + cnt)
  else       
    repeat repetition*2
      !outa[pin]
      waitcnt(x*delayCnt + cnt)
  outa[pin] := 1

And here's the Top-Level file:
{{ learning.spin  File to practice my Spin  }}
  
CON
  _clkmode = xtal1
  _xinfreq = 20_000_000


OBJ
  pst   :       "Parallax Serial Terminal"
  tog   :       "toggle"


Var
  long Stack1[8]


PUB Main
  cognew(tog.toggle(15, 50, 5), @Stack1)                'Have cog1 begin flashing a power LED
  tog.toggle(14, 10, 10)    
  pst.start(9_600)                                      'Start serial terminal
  tog.toggle(14, 10, 10)
  pst.str(string("Hello world!"))

Comments

  • CircuitsoftCircuitsoft Posts: 1,166
    edited 2012-02-06 23:32
    8 longs may not be enough stack. Start with 64; you're not exactly pressed for ram right now...
  • kuronekokuroneko Posts: 3,623
    edited 2012-02-06 23:35
    cognew(tog.toggle(15, 50, 5), @Stack1)
    
    You simply can't start a SPIN cog like this (not counting manual stack setup and starting the interpreter yourself). The first parameter has to be a method name without any prefix and/or other attachments (expressions, brackets etc., parameters are fine though). Usually you invoke some form of start method on the child object which encapsulates the cognew business. Otherwise, you'd need a local method which invokes tog.toggle().

    I added an example (using different pins and clock setup). Note this is not suited for multiple start calls as they would all use the same stack.
  • pedwardpedward Posts: 1,642
    edited 2012-02-07 00:18
    If it isn't totally clear, the cognew call has to be made from the same calling frame (inside the class). That's why the "Start" method has been standardized. You allocate an object, call Start of the object, and that calls cognew.
  • Heater.Heater. Posts: 21,230
    edited 2012-02-07 01:16
    This comes up often enough here that the compiler should probably issue a warning about incorrect first parameter to cognew/cog start.
  • DavidZemonDavidZemon Posts: 2,973
    edited 2012-02-07 13:55
    This fixed it. Thanks guys.

    Also... this thing is awesome :D
Sign In or Register to comment.