Shop OBEX P1 Docs P2 Docs Learn Events
SimpleIDE CogC Stack and Sub routines — Parallax Forums

SimpleIDE CogC Stack and Sub routines

I have been working with developing COGC routines and can get them to work passing a data structure and running everything inside the main routine. However, code is much more efficient if a sub-function or sub-routine can be called to do some data manipulation. I think I need a stack in the COGC to call these sub-functions or sub-routines and a methods to make these functions visible to the main.

Anyone have some advise??



Comments

  • I got it to work. Don't know if it is the proper way but it works.
  • ersmithersmith Posts: 6,053
    edited 2016-04-11 21:23
    COGC code always runs with a stack, so it is OK to make subroutine calls in COGC. In fact the stack is set to whatever pointer you pass in to cognew, and grows down from there. So generally you would set up a structure like:
    /*
     * This is the structure which we'll pass to the C cog.
     * It contains a small stack for working area, and the
     * mailbox which we use to communicate. See toggle.h
     * for the definition/
     */
    struct {
        unsigned stack[STACK_SIZE];
        volatile struct toggle_mailbox m;
    } par;
    

    And then pass &par.m to cognew. See for example the toggle/cog_c_toggle demo; it does set up a stack, even though in fact the toggle_fw.cogc file doesn't need one.

    Another option is to declare functions as _NATIVE. Then they will use the PASM calling convention (storing return address directly in the jump that ends the subroutine) and so won't need a stack for subroutine return addresses. They may still need a stack for data storage though, so it's always best to provide a small one.

  • Got that. My concern was using subroutines called from the cogc main routine. Looks like I can just create the routines in the COGC file and call them. It works but it doesn't conform to my understaning from the documentation on how the PROPELLER works.
  • greybeard wrote: »
    Got that. My concern was using subroutines called from the cogc main routine. Looks like I can just create the routines in the COGC file and call them. It works but it doesn't conform to my understaning from the documentation on how the PROPELLER works.

    Perhaps the documentation needs to be updated. It should say that all of the functions in a COGC file are compiled together and loaded into the same COG, so it's safe for them to call each other. They cannot call functions that are in another COG, including the main COG. What was your understanding?
  • That is what I did and everything works well. I was not clear that a compiled COGC file would keep everything together for the COG to run. I did learn that I can add multiple subroutines until it quit working, then modify the stack size and it began working again. It's called the brail learning technique. I further assume I can created multiple cogs running the same code.

    I assumed that it would not be possible to call function not in the COGC file and all communication with the HUB goes through the passed PAR structure data passed to the cog. The same as spin.

    BTW. The par structure used in the toggle example is defined but not used. It only passes the mailbox structure. A part of my confusion.

    My basic understanding the propeller code architecture is lacking and I'm not sure that what I think I understand is correct....but all that is for another post.

    Thanks for the re;ply
  • I found the answers to my questions here:
    https://github.com/parallaxinc/propgcc-docs/blob/master/doc/InDepth.md

    Makes sense now.
  • greybeard wrote: »
    I found the answers to my questions here:
    https://github.com/parallaxinc/propgcc-docs/blob/master/doc/InDepth.md

    Makes sense now.

    That link got a bit messed up. Here it is: https://github.com/parallaxinc/propgcc-docs/blob/master/doc/InDepth.md
Sign In or Register to comment.