Shop OBEX P1 Docs P2 Docs Learn Events
Multicore problem. processes do not run parallel — Parallax Forums

Multicore problem. processes do not run parallel

ErgoBotErgoBot Posts: 4
edited 2015-05-29 11:56 in Propeller 1
Hello,

I want to have the two methods BlinkLEDright and BlinkLEDleft run parallel.
Unfortunatly they run in a sequence instead. First BlinkLEDleft starts, then BlinkLEDright follows once the first one has ended.
Can you please explain to me why this happens? how can I make them run parallel? Thank you very much!


VAR
long stack[100]

OBJ

blinkl: "BlinkLEDleft"
blinkr: "BlinkLEDright"

PUB komplett


coginit(2, blinkl.BlinkLEDleft, @stack[00])
coginit(3, blinkr.BlinkLEDright, @stack[50])


CON

_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000


PUB BlinkLEDright

dira[0] := %11
waitcnt(clkfreq * 2 + cnt)
repeat 5
outa[0] := 1
waitcnt(clkfreq / 3 + cnt)
outa[0] := 0
waitcnt(clkfreq / 3 + cnt)


CON

_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000


PUB BlinkLEDleft

dira[1] := %11
waitcnt(clkfreq * 2 + cnt)
repeat 5
outa[1] := 1
waitcnt(clkfreq / 3 + cnt)
outa[1] := 0
waitcnt(clkfreq / 3 + cnt)

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2015-05-29 04:21
    coginit(n, obj.method, param) doesn't work for SPIN. What happens here is that the result of obj.method (0 in this case) is interpreted as an address to a PASM image which is then subsequently loaded/started. To determine each result value, both methods have to be called which makes you see them being executed in sequence.

    What you should do is either start two wrapper methods in the parent object to call the child methods in the context of a new cog or you move the cognew/coginit into the child objects (there is usually no need for the parent to deal with that, especially stack size & Co).

    Also, code is best posted within [noparse]
    
    [/noparse] tags.                        
  • ErgoBotErgoBot Posts: 4
    edited 2015-05-29 06:00
    Thank you for the answer! it was very helpful
  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-05-29 11:56
    As we say in Hollywood: for your consideration.

    There's no need for an external file as you've shown; you can use an embedded method and even give it flexible parameters. I've done that, but matched the behavior of your original program.
    '' =================================================================================================
    ''
    ''   File.......
    ''   Purpose....
    ''   Author..... 
    ''   E-mail..... 
    ''   Started....
    ''   Updated....
    ''
    '' =================================================================================================
    
    
    con { timing }
    
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000                                           ' use 5MHz crystal
    
      CLK_FREQ = (_clkmode >> 6) * _xinfreq                          ' system freq as a constant
      MS_001   = CLK_FREQ / 1_000                                    ' ticks in 1ms
      US_001   = CLK_FREQ / 1_000_000                                ' ticks in 1us
    
    
    con { io pins }
    
      RX1   = 31                                                     ' programming / terminal
      TX1   = 30
    
      SDA   = 29                                                     ' eeprom / i2c
      SCL   = 28
    
      R_LED = 27
      L_LED = 26
    
    
    var
    
      long  lstack[16]
      long  rstack[16]
    
    
    pub main
    
      cognew(blinker(L_LED, CLK_FREQ << 1, 5, 333, 333), @lstack) 
      cognew(blinker(R_LED, CLK_FREQ << 1, 5, 333, 333), @rstack) 
    
      repeat
        waitcnt(0)
    
    
    pri blinker(pin, delay, cyles, onms, offms) | t
    
      dira[pin] := 1
    
      if (delay > 0)
        waitcnt(cnt + delay) 
    
      t := cnt
      repeat cycles
        outa[pin] := 1
        waitcnt(t += (MS_001 * onms))
        outa[pin] := 0
        waitcnt(t += (MS_001 * offms))
    
      cogstop(cogid)                                                 ' ensure self-termination
    
    
    dat { license }
    
    {{
    
      Terms of Use: MIT License
    
      Permission is hereby granted, free of charge, to any person obtaining a copy of this
      software and associated documentation files (the "Software"), to deal in the Software
      without restriction, including without limitation the rights to use, copy, modify,
      merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
      permit persons to whom the Software is furnished to do so, subject to the following
      conditions:
    
      The above copyright notice and this permission notice shall be included in all copies
      or substantial portions of the Software.
    
      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
      INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
      PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
      HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
      CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
      OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    
    }}
    
Sign In or Register to comment.