Shop OBEX P1 Docs P2 Docs Learn Events
cog_run — Parallax Forums

cog_run

I am attempting to run my gps driver in a second cog. Starting small by sending the raw sentances or parsing out a single portion. The compliler gives me errors saying that it discards the volatile qualifier for the variable so as to work between cogs. The only examples I can find about passing data between the cogs is in the learn files using a single int. Anybody got any Ideas??
Thanks

Comments

  • The compiler is likely telling you that strcpy and strncmp don't take volatile arguments, so that's where they're getting stripped away. Using volatile on the array itself probably isn't necessary, as the compiler wouldn't normally put those values in a register anyway.

    The way you'd normally do this is to have a buffer that the cog works with that it then transfers to another that the main code uses, and a signal variable of some kind to tell the main code that the values are ready. *That* variable would be declared volatile.

    I've modified your code to demonstrate how I'd do it. (I haven't compiled this)
  • Thanks I will look at it. More information about volatile bool ??? can be found where??
  • Here's a reasonably straight forward answer from StackOverflow which explains the "volatile" keyword

    http://stackoverflow.com/questions/4437527/why-do-we-use-volatile-keyword-in-c

    In short, if you have the same variable accessed from two different cogs, that variable should be marked as volatile (unless it is a constant).
  • Volatile just tells the compiler, any time you access this value, reload it from memory, and any time you assign the value, put it back in memory right away.

    Compilers generally assume your code is a single thread, so only one function can access anything at a time. This means within a single function, they can load a value into a register, then use it from that register for the duration of the function. If another thread can change that variable this will obviously break, so that's what volatile is used for.
  • Jason I understand what you did. When I did this in spin if I remember correctly there was a command that waited if there was nothing available.

    Thanks
  • the compiler does not recognie bool
  • If you put it in c++ mode it should, otherwise just change bool to char - that'll work just as well.
  • JasonDorie wrote: »
    If you put it in c++ mode it should, otherwise just change bool to char - that'll work just as well.

    g++ and gcc both compile C++ code, but they will use the file extension to determine the language. You can override with -xc++ though if you don't want to rename your file from *.c to *.cpp
  • pilot0315 wrote: »
    the compiler does not recognie bool

    It will if you #include <stdbool.h>.

    (Or you can compile in C++ mode, as others have suggested.)
  • Question why is the semicolon placed here???

    while( dataReady == true ) {
    ;
    }
  • The semicolon there on a line by itself isn't necessary. The author of that code probably just put it there to indicate that the loop is supposed to be empty.
  • If I put it in C++ mode I get these errors. and it highlights the cog= cog_run command. If I put it back to C it runs. the word bool does not change in color so the compiler does not see it in C or C++. Still trying to send data between cogs. Still getting fractions of that data and it is intermittant

    Project Directory: F:/GPS WORKS IN C/

    SimpleIDE Version 1.0.2
    C:/Users/martin/Documents/SimpleIDE/Learn/Simple Libraries/
    C:/Users/martin/Documents/SimpleIDE/ Updated on: 2015-11-21

    propeller-elf-gcc.exe -v GCC 4.6.1 (propellergcc_v1_0_0_2408)
    propeller-elf-c++ -I . -L . -I C:/Users/martin/Documents/SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools -L C:/Users/martin/Documents/SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools/cmm/ -I C:/Users/martin/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libsimpletext -L C:/Users/martin/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libsimpletext/cmm/ -I C:/Users/martin/Documents/SimpleIDE/Learn/Simple Libraries/Protocol/libsimplei2c -L C:/Users/martin/Documents/SimpleIDE/Learn/Simple Libraries/Protocol/libsimplei2c/cmm/ -I C:/Users/martin/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libfdserial -L C:/Users/martin/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libfdserial/cmm/ -o cmm/plain gps 2 (1).elf -Os -mcmm -m32bit-doubles -fno-exceptions -fno-rtti -std=c99 plain gps 2 (1).c -lm -lsimpletools -lsimpletext -lsimplei2c -lfdserial -lm -lsimpletools -lsimpletext -lsimplei2c -lm -lsimpletools -lsimpletext -lm -lsimpletools -lm
    cc1plus.exe: warning: command line option '-std=c99' is valid for C/ObjC but not for C++ [enabled by default]
    plain gps 2 (1).c: In function 'int main()':
    plain gps 2 (1).c:49:30: error: invalid conversion from 'void (*)()' to 'void (*)(void*)' [-fpermissive]
    C:/Users/martin/Documents/SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools/simpletools.h:1404:6: error: initializing argument 1 of 'int* cog_run(void (*)(void*), int)' [-fpermissive]
    plain gps 2 (1).c: In function 'void getdata2()':
    plain gps 2 (1).c:94:18: error: invalid conversion from 'volatile char*' to 'const char*' [-fpermissive]
    c:\program files (x86)\simpleide\propeller-gcc\bin\../lib/gcc/propeller-elf/4.6.1/../../../../propeller-elf/include/string.h:14:8: error: initializing argument 2 of 'char* strcpy(char*, const char*)' [-fpermissive]
    plain gps 2 (1).c:97:34: error: invalid conversion from 'char' to 'char*' [-fpermissive]
    c:\program files (x86)\simpleide\propeller-gcc\bin\../lib/gcc/propeller-elf/4.6.1/../../../../propeller-elf/include/string.h:14:8: error: initializing argument 1 of 'char* strcpy(char*, const char*)' [-fpermissive]
    plain gps 2 (1).c: In function 'void print_data()':
    plain gps 2 (1).c:119:11: warning: unknown escape sequence: '\N' [enabled by default]
    Done. Build Failed!

    Click error or warning messages above to debug.
  • thanks electrodude
  • Found out that the c++ compiler does not support cog_run. Per Miguel @ parallax
  • That seems odd... but I haven't tested it myself.

    If you want, you can try PropWare's Runnable class. It does not require any other dependencies from PropWare, so it's very easy to add to your project.

    Here's an example using it to blink pin 16 in a separate cog, while the first cog runs a basic print routine. This example of course uses PropWare's Pin class, but PropWare::Runnable will work just as well with any of the libraries from Simple :)
    #include <PropWare/runnable.h>
    #include <PropWare/printer/printer.h>
    #include <PropWare/pin.h>
    
    class BlinkingThread: public PropWare::Runnable {
        public:
            template<size_t N>
            BlinkingThread(const uint32_t (&stack)[N], const PropWare::Pin::Mask mask)
                : Runnable(stack),
                  m_mask(mask) {
            }
    
            void run() {
                const PropWare::Pin pin(this->m_mask, PropWare::Pin::OUT);
                while (1) {
                    pin.toggle();
                    waitcnt(250 * MILLISECOND + CNT);
                }
            }
    
        private:
            const PropWare::Pin::Mask m_mask;
    };
    
    int main(int argc, char *argv[]) {
        uint32_t       stack[70];
        BlinkingThread blink(stack, PropWare::Pin::P16);
    
        const int8_t cog = PropWare::Runnable::invoke(blink);
    
        while (1) {
            pwOut.printf("Hello from cog %u! %u\n", cogid(), CNT);
            waitcnt(250 * MILLISECOND + CNT);
        }
    }
    

    Full source for PropWare::Runnable in Git.
    And a shortened copy/paste version without comments:
    #include <type_traits>
    #include <propeller.h>
    #include <cstdint>
    #include <stdlib.h>
    
    class Runnable {
        public:
            template<class T>
            static int8_t invoke(T &runnable) {
                static_assert(std::is_base_of<Runnable, T>::value,
                              "Only PropWare::Runnable and its children can be invoked");
                return (int8_t) cogstart((void (*)(void *)) &T::run, (void *) &runnable,
                                         (void *) (runnable.m_stack), runnable.m_stackSizeInBytes);
            }
    
        public:
            virtual void run() = 0;
    
        protected:
            template<size_t N>
            Runnable(const uint32_t (&stack)[N])
                : m_stack(stack), m_stackSizeInBytes(N * sizeof(uint32_t)) {
            }
    
            Runnable(const uint32_t *stack, const size_t stackLength)
                : m_stack(stack), m_stackSizeInBytes(stackLength * sizeof(uint32_t)) {
            }
    
        protected:
            const uint32_t *m_stack;
            size_t         m_stackSizeInBytes;
    };
    
  • Thanks I will look at that. Is there a GUI like the simpleide to work this?
  • In this case, just download the one file, runnable.h, and drop it into your project. Nothing else will need to be done.

    If you wanted to use more of the PropWare libraries, you can use them in SimpleIDE via the instructions at the top of this page.
  • Thanks. Problem is that when I look at the instructions I do not have a project view setting. I am running version 1.02(rc2).
  • I have isolated the runnable.h where do I put it?
  • pilot0315 wrote: »
    I have isolated the runnable.h where do I put it?

    Place it in the same folder as your .cpp and .side files.
    pilot0315 wrote: »
    Thanks. Problem is that when I look at the instructions I do not have a project view setting. I am running version 1.02(rc2).

    It's not a bug. I'm afraid I don't have time this instant to dig up the link to other forum threads that explain the steps, but you search you around you can find other people that have asked how to enable "project view".
  • To enable project mode:

    - Go to Tools -> Properties
    - Click the "View mode" check box on the "General" tab
    - Click Ok
    - Go to Tools -> Set Project View
  • I am using the newest version (RC2) does not have the feature
  • pilot0315 wrote: »
    I am using the newest version (RC2) does not have the feature

    It does actually. It's just very well hidden. I think they hid it so well to ensure folks didn't accidentally make the IDE complex/confusing.
Sign In or Register to comment.