Shop OBEX P1 Docs P2 Docs Learn Events
Newbie COG question — Parallax Forums

Newbie COG question

Shane MShane M Posts: 58
edited 2009-06-29 17:52 in Propeller 1
Hi. This PUB function works perfectly.·

I only want to start this in it's own COG.· Nothing I try works.· I followed the examples.

When I put it in it's own cog, it acts completely different.· I'm guessing I'm missing something key.

How would you start this in it's own COG?

PUB PollSerial
··· serial.RxStr(@strin)··
··· if (not strcomp(@strin,@strlast))
····· emic.say(@strin, true)· 'have emic say text
····· serial.str(@strin)·· ' debug
····· emic.WaitNotBusy···· ' wait for emic
····· CopyString(@strlast,@strin) ' copy current string
··· emic.WaitNotBusy

Here is what I did.

Original Main Loop:
PUB Run    'Main Loop - watch serial/watch pink/watch button
  'initialze Emic,Serial and Network
  Initialize    

  repeat 
    PollSerial ' the function I'll show you next
    PollButton

·
Original PUB:
PUB PollSerial
    serial.RxStr(@strin)   
    if (not strcomp(@strin,@strlast))
      emic.say(@strin, true)  'have emic say text
      serial.str(@strin)   ' debug
      emic.WaitNotBusy     ' wait for emic
      CopyString(@strlast,@strin) ' copy current string 
    emic.WaitNotBusy

Works fine!· THEN I TRIED THIS:

New Main:
PUB Run    'Main Loop - watch serial/watch pink/watch button
  'initialze Emic,Serial and Network
  Initialize    
    
  if (not (cognew(PollSerial, @SqStack) )


  repeat 
    PollButton

New PollSerial:
PUB PollSerial
 repeat ' ONLY LINE I ADDED**   
    serial.RxStr(@strin) ' get serial data
  
    if (not strcomp(@strin,@strlast))
      emic.say(@strin, true)  'have emic say text
      serial.str(@strin)   ' debug
      emic.WaitNotBusy     ' wait for emic
      CopyString(@strlast,@strin) ' copy current string 

    emic.WaitNotBusy

The biggest thing I see is that the serial.RxStr command waits for me to send ONE byte and then loops and loops.· Never again waiting for data.· The Serial object is a spin object I downloaded.· But again, I don't see how this should be so vastly different just because I ran it in a cog.

Any ideas?

Shane Merem
www.websiteforge.com
www.magnusoft.com

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-27 23:28
    You need to post the whole thing (as an attachment to a message). There's so much that's not included. For example, the declaration of SqStack may be important. Your initialization routine may be important. Your PollButton routine may be important. Do you have more than one cog referencing the serial object or the emic object?
  • Shane MShane M Posts: 58
    edited 2009-06-28 03:46
    Thanks.· Here is the file.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-28 05:14
    Is this the version that works? You didn't say.

    I don't have a link to the EMic driver. I suspect the problem is that you're calling the EMic from a cog different from the one it was initialized in. Each cog has a separate set of I/O registers (INA / OUTA / DIRA). Usually the initialization routine sets up the DIRA register and the initial OUTA register. If you try to access those I/O pins from a 2nd cog, they're all set up as inputs and, if you try to set up two cogs so they both try to output to the same I/O pin, one of them will override the other. Any cog that tries to set output mode will override all other cogs that want input mode and any cog that tries to take an output pin high will override all other cogs that try to take the output pin low. In other words, all the DIRA registers are logically or'd together and all the OUTA registers are logically or'd together.
  • Shane MShane M Posts: 58
    edited 2009-06-28 15:28
    I'll switch that around and let you know.. Thanks! I appreciate the assistance.
  • Shane MShane M Posts: 58
    edited 2009-06-28 23:06
    Hello. Turns out the "Stack length" was causing issues. The manual says that they will go over how to determine a cog's needed stack space later... But they don't. Do you know a reference that will help me determine required stack space?
  • WNedWNed Posts: 157
    edited 2009-06-29 00:52
    The reason they don't "get to it later" is because there is no easy explanation. There are a couple of other threads about this, but I haven't got links handy.
    Short version... Writing a program from scratch, I just start with:
    long stack1[noparse][[/noparse]200]
    If the program fails to run for no obvious reason, I increase the stack space until it works. Not very sophisticated, but it works for me, and does not involve any sort of animal sacrifice as other methods do.
    Conversely, if my program works, but it may be included as an object elsewhere, I decrease the stack until the program fails (then increase it just above the threshold) in order to keep the overall project lean.
    And Don't Forget F8. Hitting F8 will show you how your memory usage stacks up...
    (Also available from the Menu under Run/Compile Current/View Info)

    Ned

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "They may have computers, and other weapons of mass destruction." - Janet Reno
  • Shane MShane M Posts: 58
    edited 2009-06-29 13:03
    Thanks! Awesome help guys. I'm a web and database programmer for the most part -- so I'm impatient and alot of this is new. I haven't done any microprocessor work for about 15 years. Back then I took a Motorola HC11 and created a remote controlled 20 channel retail display case alarm. Hand wired it point to point and wrote the code in assembly language. What an education.

    NOW -- I can hardly remember a line of Assembly Language. So I am impatiently crashing through everything trying to come up to speed. The spin language definitely helps.

    On the stack. I basically did what you said. It was at 9. Bumped it to 30. After adding a constant it crashed again. So bumped it to 40. Works great. I'll probably make it a bit bigger later.

    Thanks again.

    Shane Merem
    www.websiteforge.com
    www.magnusoft.com
  • WNedWNed Posts: 157
    edited 2009-06-29 16:28
    Hi Shane,
    It was kind of bizarre reading your last post... you are me, except I never did the alarm thing. Fifteen years ago I was working on flight simulators, and now I'm having to re-learn C and learn Spin and Propeller Assembler. Microcontrollers are *so* much more fun and interesting than database work. Basically what I really got into electronics and computers for in the first place!

    The important thing to remember about Spin as one of your available tools is that it may run much slower than PASM, but it generates much more compact code, so never discount it as being a crutch to avoid PASM, it has its place. PASM code can run 10 times faster than Spin, but but takes up roughly 8 times the space of the equivalent code in Spin. The reason is that Spin gets compiled down into 8 bit tokens that are run by the Spin interpreter (small code with delay), while PASM compiles down to 32 bit machine code directly (hefty code size, but fast). Fortunately, you can use spin for most of your code, and create PASM code "modules" for parts that really need to be fast.
    As for your current project, if you've found a stack size that works, there's no need to make it larger later. There's no dynamic allocation of space, so if it works, it works... don't go back and "fix it".

    Ned

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "They may have computers, and other weapons of mass destruction." - Janet Reno

    Post Edited (WNed) : 6/29/2009 4:36:59 PM GMT
  • Shane MShane M Posts: 58
    edited 2009-06-29 17:52
    Awesome. Thanks for the advice.

    Like you mentioned. This suff is WAY funner. I went to school for my EE. I'm in Michigan so I found out it's hard to be your own boss in that field (automotive industry was going downhill even 15 years ago -- just nobody noticed).... So I ended up in the computer field to "make a living". I have done well -- but my true love is digital electronics.

    From designing and etching my own circuit boards to the programming! It's great!

    Thanks again

    Shane Merem
    www.websiteforge.com
    www.magnusoft.com
Sign In or Register to comment.