Shop OBEX P1 Docs P2 Docs Learn Events
cognew program halt — Parallax Forums

cognew program halt

I have got 2 Spin objects: main.spin, serialInterface.spin

in main i'm calling
serInt.Start
...
display result

Which should fire up a cog and listen on the serial interface for communication. But as long as i have the "_CogNumber := cognew(init, @_StackSpace)" in serialInterface.spin program execution not goes over that instruction. Can anyone please tell me wath im doing wrong?


serialInterface.spin:

OBJ

pst : "Parallax Serial Terminal"

VAR
long _StackSpace[8]
byte _CogNumber

PUB Start | i
Stop
'_CogNumber := cognew(init, @_StackSpace)

PUB Stop
if _CogNumber > -1
cogstop(_CogNumber)

PRI init
pst.Start(115_200)

repeat
pst.StrIn(@serialIn)
handleInput

PRI handleInput
if(serialIn == startSequence)
pst.Str(String("init_ok"))
else
pst.Str(String("uC"))

DAT
startSequence byte "init"
serialIn byte 50

Comments

  • Your _StackSpace array is very short. Try lengthening it to 30.

    -Phil
  • Hello Phil thank you again for the quick answer!

    First of all you where right, increasing the stack was needed.

    But the program halted because i was to stupid:
    In the Start- function the first thing i did was calling Stop which then stopped the cog if _CogNumber was > -1. In my case the _CogNumber was 0 and it stopped my main cog :)

    Thank you one more time for your Help!
  • ElectrodudeElectrodude Posts: 1,657
    edited 2016-07-21 13:44
    Something that is often done is to add 1 to the value stored in CogNumber. This gets around the problem of cog starting out as 0 and not 01. An example is below. For more examples of this, see "Parallax Serial Terminal.spin" and many of the other objects in the Spin library that do this.
    VAR
      long stack[32]
      byte cog
    
    PUB start : ok
    
      stop
      ok := cog := cognew(cogcode, @stack) + 1
    
    PUB stop
    
      if cog
        cogstop(cog~ - 1) ' stop cog - 1, then set cog := 0
    
    PRI cogcode
    
      repeat
        'blah blah blah
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2016-07-22 03:08
    You have several issues with your current program.

    The line "if(serialIn == startSequence)" only compares the first character of the received data.

    As your code presently stands, there's no need to start another cog. You could just call "Init" from the "Start" method. As it is now you allow the initial cog (cog #0) to die.

    You pass a pointer to "serialIn" to PST in order to store a string but the "serialIn" is only one byte long.
    CON
    
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000    
    
    OBJ
    
    pst     : "Parallax Serial Terminal"
        
    PUB Start
      
      init
    
    PRI init
      pst.Start(115_200)
    
      repeat
        pst.StrIn(@serialIn)
        handleInput
    
    PRI handleInput
      if strcomp(@serialIn, @startSequence)
        pst.Str(String(11, 13, "init_ok"))
      else
        pst.Str(String(11, 13, "uC"))  
    
    DAT
      startSequence         byte "init", 0 ' This needed a terminating zero.
      serialIn              byte 0[Pst#MAXSTR_LENGTH + 1]
    

    I think the above code is close to what you trying to accomplish.

    Edit: I missed the part about this being a child object. In the case of a child object you may well want to start another cog. A lot depends on what the parent object is doing. Because of the mistakes made in the object you posted, I'm afraid you may also have serious errors in your parent object. If you want to post your complete program we could take a look at it. Use the Propeller Tool's "archive" feature to archive all the objects used in a program.
  • thank you Duane Degn,

    thats a good advice, i will remeber it! :)
Sign In or Register to comment.