Shop OBEX P1 Docs P2 Docs Learn Events
Cog 1 doesn't want to play — Parallax Forums

Cog 1 doesn't want to play

evil_aaronmevil_aaronm Posts: 11
edited 2013-09-08 21:03 in Learn with BlocklyProp
Hi there! New to the Propeller - mine's a Rev A QuickStart - but not new to micros or programming in general, C in particular. I have the following program but, for some reason, Cog 1 doesn't want to participate. I'd expect all 8 cogs to run, but here's the output I get, and the LED for Cog 1 stays dark. Otherwise, this has a nice twinkle effect.

Output:
Started on cog 2
Started on cog 3
Started on cog 4
Started on cog 5
Started on cog 6
Started on cog 7
Started on cog -1

Also, in the on-line example code, the demo stack size is "40 + 25" or 65 bytes. Using trial and error, I found that cogstart() failed (returned -1) for any stack size less than 180. Does this comport with others' experience, or am I doing something wrong?

Thanks for any assistance! :smile:
/*
  Blank Simple Project.c
  http://learn.parallax.com/propeller-c-tutorials 
*/
#include "simpletools.h"                      // Include simple tools

void cogFlashLight( void * );

int randNum( int upperLimit )
{
  unsigned long long int c;
  c = CNT;
  return( c % upperLimit );
}

void cogFlashLight( void *par )
{
  int led = cogid() + 16;  // 16 is the base pin
  int n;
  //pause( (led - 16) * 100 );
  //printf( "In cogid %d\n", led - 16 );
  while ( 1 ) {
    n = randNum( 10 );
    high( led );
    pause( (n + 1) * 100 );  // tenths of a second
    n = randNum( 10 );
    low( led );
    pause( (n + 1) * 100 );
  }
}

//#define STACKSIZE (40 + 25)
//#define STACKSIZE 65
#define STACKSIZE 180
#define COGS 7
unsigned int stack[COGS][STACKSIZE];
//unsigned int stack[STACKSIZE];

int main()                                    // Main function
{
  int i;
  int n;
  printf( "%c", CLS );
  pause( 500 );

  for ( i = 0; i < COGS; i++ ) {
    n = cogstart( &cogFlashLight, NULL, stack[i], STACKSIZE );
    printf( "Started on cog %d\n", n );
  }
  cogFlashLight( NULL );
}

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2013-09-07 20:08
    FWIW, the stack size is indeed expected in bytes but in your example you use it as the number of longs/ints on the stack (unsigned int stack[COGS]COLOR="#FFA500"]STACKSIZE[/COLOR; ). So you have to decide which style you use and stick with it. From other (SimpleIDE) examples I see that usually sizeof(stack) is used as the last parameter. In your case that would be sizeof(stack) which will then pass the right size to the cogstart function.

    As for not being able to utilise all 8 cogs, the others may simply be busy/in use by the system. One is certainly used to run your program. Not sure what the second one is used for (could be a driver or runtime support).
  • evil_aaronmevil_aaronm Posts: 11
    edited 2013-09-07 21:22
    Ah! Good catch. I was using this as my base example - http://learn.parallax.com/propeller-c-functions/multicore-example - and simply added "40 + 25" without considering the fundamental data size. Pilot error...

    As for cog 1, the main program falls into the same LED flashing routine as the others, after kicking them off, so they should all be flashing. When I was testing with just one cog, the "printf's" showed that cog 0 was running the main loop. I've no idea what cog 1 could be doing.
  • kuronekokuroneko Posts: 3,623
    edited 2013-09-07 21:29
    As for cog 1, the main program falls into the same LED flashing routine as the others, after kicking them off, so they should all be flashing. When I was testing with just one cog, the "printf's" showed that cog 0 was running the main loop. I've no idea what cog 1 could be doing.
    OK, I missed that bit. So the other 6 cogs deal with pins 18..23 but your current cog never actually drives pin 16 (assuming cog 0 is running the program)? What happens when you only start one other cog and then call the flash function? Is pin 16 functional?

    Just had to try this myself. And it works. Could it be a side effect of your stack (mis)management? I don't use any console output which seems to give me 7 other cogs plus the primary one. All of them blinking (QS, CMM).
    /*
      Blink Light with Simple Multicore.c   
      Multicore example for use with CMM or LMM memory models.
    
    */
    
    #include "simpletools.h"                      // Include simpletools library 
    
    // For LMM cog 160 bytes declared as int
    // Add enough for stack & calcs (50 bytes declared as int 
    unsigned int stack[8][(160 + (50 * 4)) / 4];
    
    // Declare volatile variables that will not be optimized
    // to one variable for both cogs to share.
    volatile unsigned int tdelay;
    
    // Function prototype
    void blink(void *par);
    
    // Cog 0 starts here
    int main()
    {
      int i = 8;
      tdelay = 200;
      // Pass function pointer, optional parameter value, 
      // stack array address, and the stack's size to cogstart.
      while(i--)
        cogstart(&blink, NULL, stack[i], sizeof(stack[i]));
      // Cog 0 continues here
      pause(3000);
      blink(NULL);
    }  
    
    // Cog 1 starts executing code in this function in
    // parallel.  Hooraaay!
    void blink(void *par)
    {
      while(1)
      {
        high(cogid()+16);
        pause((cogid()+1)*tdelay);
        low(cogid()+16);
        pause(tdelay);    
      }
    }
    
  • ersmithersmith Posts: 6,068
    edited 2013-09-08 06:06
    Sounds like Cog 1 may be running the fdserial driver to handle console output.

    Eric
  • jazzedjazzed Posts: 11,803
    edited 2013-09-08 07:36
    @Kuroneko, that works for me too. Thanks.

    There are no extra cogs being started in the background for the code. Parallax Education has insisted that anything added would be obvious. For example, a full-duplex serial can be added with #include "fdserial.h" and fdserial_open(...) if necessary.
  • evil_aaronmevil_aaronm Posts: 11
    edited 2013-09-08 10:19
    ersmith wrote: »
    Sounds like Cog 1 may be running the fdserial driver to handle console output.

    Eric

    We have a winner! I commented out the printfs and all 8 leds are flickering. Thanks, Eric.
  • jazzedjazzed Posts: 11,803
    edited 2013-09-08 11:02
    We have a winner! I commented out the printfs and all 8 leds are flickering. Thanks, Eric.

    This doesn't make any sense. The only way that printf should be using another cog is if you explicitly changed stdio to use FullDuplexserial using the _Device array or if you added fpucog.c to your project.
  • evil_aaronmevil_aaronm Posts: 11
    edited 2013-09-08 13:43
    jazzed wrote: »
    This doesn't make any sense. The only way that printf should be using another cog is if you explicitly changed stdio to use FullDuplexserial using the _Device array or if you added fpucog.c to your project.

    I did neither, so I don't know. The code above is cut-and-paste. Project options uses CMM Main RAM Compact memory model, Size optimization. Compiler uses 32 bit doubles, all warnings, "-std=c99" options. Linker uses Math lib. Otherwise, it's a completely stock SimpleIDE installation.
  • jazzedjazzed Posts: 11,803
    edited 2013-09-08 15:30
    Ok, here's the story.

    The simpletools.h library includes fpucog. When you add printf and enable the math library when using simpletools.h, fpucog gets included in the build. This is normally a good thing if you need floating point. In this case, you don't need the math library. Some of the Simple Workspace examples have Math checked by default which is probably a mistake.

    How can you find out this stuff? The SimpleIDE project manager has options like "Show Map File" and "Show Assembly". If you right-click on any project file, you can see the popup menu. The resulting "Show Map File" tells you everything about a successfully compiled and linked program.
  • evil_aaronmevil_aaronm Posts: 11
    edited 2013-09-08 17:22
    Thanks for the info. Unrelated question: how do I mark this thread as "solved"? I didn't see any mechanism for that.
  • kuronekokuroneko Posts: 3,623
    edited 2013-09-08 17:24
    Unrelated question: how do I mark this thread as "solved"?
    Edit the first port in this thread, go to advanced mode. There you can change the tag.
  • evil_aaronmevil_aaronm Posts: 11
    edited 2013-09-08 21:03
    Two thumbs up! Thanks!
Sign In or Register to comment.