Shop OBEX P1 Docs P2 Docs Learn Events
PropGCC equivalent of someVar := long[someAddress]? — Parallax Forums

PropGCC equivalent of someVar := long[someAddress]?

JonnyMacJonnyMac Posts: 9,107
edited 2014-02-02 14:07 in Propeller 1
As I stumble around PropGCC I found a bit of code that looks like this
void blinker(void *par) 
{ 
  while(1) {
    high(26);
    pause(1000);  
    low(26);
    pause(1000);
  }
}


I'm assuming that an address can be passed in *par -- how can I extract that like I would with long[parVal] in Spin?

Comments

  • Roy ElthamRoy Eltham Posts: 3,000
    edited 2014-02-02 12:52
    You can change the 'void *par' to be whatever you want as long as it's a pointer. So you could change it to 'int *par', then in the body of the function you can use it via pointer dereferencing like this: someVar = *par;

    In my code, I usually define a struct to hold all the data I want to pass via par, and then pass a pointer to that struct in.
  • SRLMSRLM Posts: 5,045
    edited 2014-02-02 13:46
    That looks like something for cogstart. You can take a look at the documentation for that here: https://sites.google.com/site/propellergcc/documentation/libraries/propeller-h-library#TOC-cogstart

    I use the C++ feature of PropGCC, and that's a really handy way to pass an entire object to the new cog. I use it like this:

    main.cpp:
    void MyClassCogRunner(void * parameter) {
        MyClass::Configuration * temp = (MyClass::Configuration *) parameter;
        RecordFile(*temp);
        waitcnt(CLKFREQ / 10 + CNT); //Let everything settle down
        cogstop(cogid());
    }
    
    
    ...
    ...
    <elsewhere...>
    ...
    
            MyClass::Configuration config;
            config.unitNumber = unitNumber;
            config.timestamp = CNT;
    
            cogstart(MyClassCogRunner, &config, myclassStack, stacksize);
    
    


    MyClass.h:
    class MyClass {
    public:
        struct Configuration {
            int unitNumber;
            int timestamp;
        };
       
        <class functions>
    
    }
    

    The problem with cogstart for C++ is that C++ class members have an implicit *this, which is lost with the cogstart. So, you have to cogstart a little runner function.
  • jazzedjazzed Posts: 11,803
    edited 2014-02-02 13:59
    volatile int ledarray[] = { 26, 1000 }; // must be volatile so optimizer doesn't throw code away
    int bstack[50];
    void blinker(void *par) ;
    
    void main(void)
    {
      cogstart(blinker, ledarray, bstack, sizeof(bstack));
    }
    
    void blinker(void *par) 
    { 
      int *array = (int*) par;
      while(1) {
        high(array[0]);
        pause(array[1]);  
        low(array[0]);
        pause(array[1]);
      }
    }
    
    Data type void * is a generic pointer. Think of "void *par" like "somevar" in python, "var par" in javascript, "dim bas" in visual basic, or even "$var" in TCL/Perl.

    The only difference is you need to cast void *par. As long as you always agree on the type (like in spin), it can be anything including a pointer to a structure, a pointer to a pointer, a pointer to an array, or single number.
  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-02-02 14:01
    Thanks, Steve, that's what I was looking for.
  • jazzedjazzed Posts: 11,803
    edited 2014-02-02 14:07
    Jon, I made 2 mistakes.

    This line "int ledarray = { 26, 1000 };"
    should be "volatile int ledarray[] = { 26, 1000 };"

    Sorry, I'm a little sick this weekend.

    Using volatile says: "Look compiler, I don't care if you think what I'm doing with ledarray is wasteful. Do what I want."

    int ledarray[] needs brackets [] to declare it an array. The brackets can have a number. I.E. int ledarray[2] = { 26, 1000 };
Sign In or Register to comment.