Shop OBEX P1 Docs P2 Docs Learn Events
Need help with startcog in LMM mode c++ — Parallax Forums

Need help with startcog in LMM mode c++

AnubispodAnubispod Posts: 42
edited 2013-03-21 04:55 in Propeller 1
Hi here is my my code what i want to get to run in c++.

So to explain a bit , i have 2 spin files what i have run trough spin2cpp and got 4 new files
See attachments i converted it with --main to make the MPU6050_Demo as main file to run from .
All works well till i get to the point to start the loop from MPU6050_XS in a new cog like i do in spin .

My spin code works well i get in the cog 150 reads of sensor data per sec and stored in the buffer
But i would like to get it to run in gcc to see if it makes speed difference
int32_t MPU6050_XS::Start_loop(void) {    int pin =0;   
      int foo= 0;
      int stacksize = sizeof(_thread_state_t)+sizeof(int)*3;
      int *stack = (int*) malloc(stacksize);
      int cog = cogstart(&MPU6050_XS::Loop, (void*) pin, stack, stacksize);
      return 0;
 }

int32_t MPU6050_XS::Loop(int32_t Ptr) {
   int32_t _parm__0000[4];
   _parm__0000[0] = Ptr;
   while (1) {
     _parm__0000[1] = _i2cack;
     if (I2cstarted == -1) {
       I2cstart();
       _parm__0000[1] = ((_parm__0000[1] << 1) | I2cwrite((0xd0 | 0x0), 8));
       _parm__0000[1] = ((_parm__0000[1] << 1) | I2cwrite((Ra_accel_xout_h << 24), 0));
       I2cstart();
       _parm__0000[1] = ((_parm__0000[1] << 1) | I2cwrite((0xd0 | 0x1), 8));
       _parm__0000[2] = 0;
       _parm__0000[3] = 13;
       {
         int32_t _idx__0001;
         _idx__0001 = _parm__0000[3];
         do {
           ((uint8_t *)(int32_t)(&_parm__0000[0]))[_parm__0000[2]] = I2cread(_i2cack);
           (_parm__0000[2]++);
           _idx__0001 = (_idx__0001 + -1);
         } while (_idx__0001 >= 1);
       }
       ((uint8_t *)(int32_t)(&_parm__0000[0]))[_parm__0000[2]] = I2cread(_i2cnak);
       I2cstop();
     } else {
       _parm__0000[1] = _i2cnak;
     }
     Lastackbit = _parm__0000[1];
   }
   return 0;
 }

Error log
int32_t MPU6050_XS::Loop(int32_t Ptr) {
   int32_t _parm__0000[4];
   _parm__0000[0] = Ptr;
   while (1) {
     _parm__0000[1] = _i2cack;
     if (I2cstarted == -1) {
       I2cstart();
       _parm__0000[1] = ((_parm__0000[1] << 1) | I2cwrite((0xd0 | 0x0), 8));
       _parm__0000[1] = ((_parm__0000[1] << 1) | I2cwrite((Ra_accel_xout_h << 24), 0));
       I2cstart();
       _parm__0000[1] = ((_parm__0000[1] << 1) | I2cwrite((0xd0 | 0x1), 8));
       _parm__0000[2] = 0;
       _parm__0000[3] = 13;
       {
         int32_t _idx__0001;
         _idx__0001 = _parm__0000[3];
         do {
           ((uint8_t *)(int32_t)(&_parm__0000[0]))[_parm__0000[2]] = I2cread(_i2cack);
           (_parm__0000[2]++);
           _idx__0001 = (_idx__0001 + -1);
         } while (_idx__0001 >= 1);
       }
       ((uint8_t *)(int32_t)(&_parm__0000[0]))[_parm__0000[2]] = I2cread(_i2cnak);
       I2cstop();
     } else {
       _parm__0000[1] = _i2cnak;
     }
     Lastackbit = _parm__0000[1];
   }
   return 0;
 }



Best regards Oliver. R

Comments

  • SRLMSRLM Posts: 5,045
    edited 2013-03-19 17:19
    It might help us if you explain what is not working, and what you have tried.
  • jazzedjazzed Posts: 11,803
    edited 2013-03-19 18:40
    int32_t MPU6050_XS::Loop(int32_t Ptr) // IIRC the function should be static.
  • SRLMSRLM Posts: 5,045
    edited 2013-03-19 23:55
    jazzed wrote: »
    int32_t MPU6050_XS::Loop(int32_t Ptr) // IIRC the function should be static.

    I don't think it needs to be static, at least if you are compiling with g++. But it reminds me: I think the function prototype should be something like:
    void name(void * param){
    
    
    }
    

    You should also include a cogstop(cogid()) as the very last line of the function, otherwise your cog will keep running (waiting) and take up resources.
  • ersmithersmith Posts: 6,092
    edited 2013-03-20 05:11
    jazzed wrote: »
    int32_t MPU6050_XS::Loop(int32_t Ptr) // IIRC the function should be static.

    Yes, I think you're right Steve. If it isn't static then there's an implicit first argument (the "this" pointer) which the cogstart function won't set up correctly.

    Eric
  • AnubispodAnubispod Posts: 42
    edited 2013-03-20 08:44
    Ahhh ok i got it , not the way i first intendet but hey as long it runs ...

    I have rewritten the spin code from the loop i wanted to run in the cog to pasm and now the spin2cpp did a better job.
    So for other on the hunt for a starting point for the MPU6050 can look at those files. i included spin_pasm and c++ for c just shoot it trough the spin2cpp and it works.

    Best regards
    Oliver.R
  • jazzedjazzed Posts: 11,803
    edited 2013-03-20 09:46
    SRLM wrote: »
    I think the function prototype should be something like:
    void name(void * param){
    
    }
    

    Yes.
    SRLM wrote: »
    You should also include a cogstop(cogid()) as the very last line of the function, otherwise your cog will keep running (waiting) and take up resources.

    That's probably true for functions that terminate. It never occurred to me.
    I've updated propeller-gcc cogstart page entry. https://sites.google.com/site/propellergcc/documentation/libraries/propeller-h-library?pli=1#TOC-cogstart

    Thanks!
  • SRLMSRLM Posts: 5,045
    edited 2013-03-20 21:52
    ersmith wrote: »
    Yes, I think you're right Steve. If it isn't static then there's an implicit first argument (the "this" pointer) which the cogstart function won't set up correctly.

    Eric

    Well, I checked, and here is what I have:
    1. A "void functionName(void * param)" function, in the global namespace
    2. The propeller-elf-g++ compiler
    3. Called cogstart from the main() function.
    4. It seems to work.

    I don't know if there is an implicit "this" pointer, however. And I haven't tried putting the cogstart'd function in a class, so it may be an issue there. I'd be interested to hear if it is.
  • SRLMSRLM Posts: 5,045
    edited 2013-03-20 21:55
    jazzed wrote: »

    Is that sites.google.com page generated from a C/C++ source file, or is it edited directly from the sites.google.com editor? I don't want to add additions to it if it's generated from source.

    ps: I've also added an FAQ related to cogstart.
  • jazzedjazzed Posts: 11,803
    edited 2013-03-20 22:43
    SRLM wrote: »
    Is that sites.google.com page generated from a C/C++ source file, or is it edited directly from the sites.google.com editor? I don't want to add additions to it if it's generated from source.

    ps: I've also added an FAQ related to cogstart.

    Most of the propeller.h page was generated using Doxygen and fixed up by hand. The code was created first and pasted into a google text gadget. You can click on the properties to bring up a regular text editor on the site.

    Thanks for working on the FAQ. It has lots of good info in it.
  • ersmithersmith Posts: 6,092
    edited 2013-03-21 04:55
    SRLM wrote: »
    Well, I checked, and here is what I have:
    1. A "void functionName(void * param)" function, in the global namespace
    2. The propeller-elf-g++ compiler
    3. Called cogstart from the main() function.
    4. It seems to work.

    I should have been clearer -- the "static" is necessary only for class members. Global functions will be fine without it (there's no "this" for a global function). But class members will need to be "static", which for a class indicates that it behaves the same for all objects.
Sign In or Register to comment.