Shop OBEX P1 Docs P2 Docs Learn Events
Is there a code example to understand how to use cogs? — Parallax Forums

Is there a code example to understand how to use cogs?

I am unable to find a useful example explaining how to use cogs. Is there a code example to understand how to use cogs? One example using one specific cog. and one example with multiple. My background is from 8051 C and assembly.

Comments

  • evanhevanh Posts: 15,252
    edited 2023-11-11 03:49

    You are always using at least one cog. Cog and core are interchangeable terms here.

    Cog 0 is the initial boot up core. First program code will be running on cog 0.

    Info: The eight cogs are physically real cores, not logical threads. Each cog has its own limited internal RAM that is both general register space (512 of them!) and also can execute code from its registers ('twas the only way in the Prop1). Self modifying code is acceptable practice.

    With the Prop2, the ability to execute directly from main memory was added. This is a welcome addition and is the default for compiled C on the Prop2.

    To start up a second cog there is two general forms:

    • Either as a high-level-language function/method starting as a new task. This requires allotting a fresh stack area for said task.
    • Or as a low-level-pasm-block that usually lives within that cog. A parameter or mailbox block is often assigned to such, but is entirely optional.
  • evanhevanh Posts: 15,252

    There is two active C development environments established on the Propeller. Both support Prop1 and Prop2 compiling.

    There is an effort to integrate these with VS Code too. They are concentrating on Spin and I haven't looked at that to know how well the C side works - https://forums.parallax.com/discussion/173429/visual-studio-code-supports-p2-development-on-windows-mac-linux-yes-rpi/p1

  • JonnyMacJonnyMac Posts: 8,953
    edited 2023-11-11 03:42

    Here's a very simple blinky light demo that will show you the basics if you're going to use Spin and PASM. If you're going to use one of the C compilers, you'll need to consult the docs for that compiler.

    Note that I've departed from the norm a bit in the demo. For example, this line:

      bcog := cogspin(NEWCOG, spin_blinker(LED2, 167, 333), stack) 
    

    ...would normally be:

      bcog := cogspin(NEWCOG, spin_blinker(LED2, 167, 333), stack) + 1
    

    We do this so that the cog id is !0 if the cog loaded. I'm using debug() to show the cog ids so I left this out to prevent confusion -- you'll see ids 0, 1, and 2 instead of 0, 2, and 3.

  • @mashaikh23 said:
    I am unable to find a useful example explaining how to use cogs. Is there a code example to understand how to use cogs? One example using one specific cog. and one example with multiple. My background is from 8051 C and assembly.

    You don't mention what tools or languages you're using. For FlexProp there are several examples in the samples folder, including blink2.bas, blink_cog.c, blink_all_cogs.spin (simple examples of blinking LEDs with multiple COGs in BASIC, C, and Spin), the LED server samples (more sophisticated LED blinking in a COG, with inter-cog communication), and Multi-Language/mandelbrot.c (drawing a Mandelbrot set using multiple COGs).

  • @ersmith said:

    @mashaikh23 said:
    I am unable to find a useful example explaining how to use cogs. Is there a code example to understand how to use cogs? One example using one specific cog. and one example with multiple. My background is from 8051 C and assembly.

    You don't mention what tools or languages you're using. For FlexProp there are several examples in the samples folder, including blink2.bas, blink_cog.c, blink_all_cogs.spin (simple examples of blinking LEDs with multiple COGs in BASIC, C, and Spin), the LED server samples (more sophisticated LED blinking in a COG, with inter-cog communication), and Multi-Language/mandelbrot.c (drawing a Mandelbrot set using multiple COGs).

    I have started with P2 Edge with FlexProp to code using C for most part but spin2 to get some good understanding. I am currently reviewing blink_all_cogs.spin and blink_cog.c .

  • Thanks all who responded.

  • @evanh said:
    You are always using at least one cog. Cog and core are interchangeable terms here.

    Cog 0 is the initial boot up core. First program code will be running on cog 0.

    Info: The eight cogs are physically real cores, not logical threads. Each cog has its own limited internal RAM that is both general register space (512 of them!) and also can execute code from its registers ('twas the only way in the Prop1). Self modifying code is acceptable practice.

    With the Prop2, the ability to execute directly from main memory was added. This is a welcome addition and is the default for compiled C on the Prop2.

    To start up a second cog there is two general forms:

    • Either as a high-level-language function/method starting as a new task. This requires allotting a fresh stack area for said task.
    • Or as a low-level-pasm-block that usually lives within that cog. A parameter or mailbox block is often assigned to such, but is entirely optional.

    so can load all 8 cogs with code at initial startup?

  • evanhevanh Posts: 15,252
    edited 2023-11-12 23:09

    A program can start up all eight cogs for itself immediately, yes, There is no OS occupying one. The language environment has some overheads, like needing stack space. They generally all require some cogRAM as well. One built-in service that is provided by the high level languages is initial sys-clock PLL frequency setting.

    If you want totally everything to yourself then there is also native Pasm2 for that.

  • @evanh said:
    A program can start up all eight cogs for itself immediately, yes, There is no OS occupying one. The language environment has some overheads, like needing stack space. They generally all require some cogRAM as well. One built-in service that is provided by the high level languages is initial sys-clock PLL frequency setting.

    If you want totally everything to yourself then there is also native Pasm2 for that.

    I found some documentation came along with FlexProp /flexprop/doc/c.html#flex-c. Its seems a good start with inline PASM2.

  • evanhevanh Posts: 15,252

    Oh, yes, "inline" Pasm is very nice to use. FlexC has a bunch of ways to manage how cogRAM is used for this - https://forums.parallax.com/discussion/comment/1555036/#Comment_1555036

Sign In or Register to comment.