C++ and STM32

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: C++ and STM32

Post by Giovanni »

HI,

The C++ demo is in the latest unstable release, note that there is a new linker script (ch.ld) and a new crt0 module. Make sure to use the files from the latest unstable version.

Giovanni
User avatar
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Re: C++ and STM32

Post by Badger »

I'm having some problems with using c++. Basically my blinker thread doesn't seem to work! if I uncomment the blinking code in the main thread then it blinks, so it isn't a problem with the board setup. But it is so simple I can't see any problems with it..

Code: Select all

#include "ch.hpp"
#include "hal.h"

using namespace chibios_rt;

/*
 * Tester thread class. This thread executes the test suite.
 */
class BlinkerThread : public EnhancedThread<128> {

protected:
  virtual msg_t Main(void) {
    while(true) {
      palClearPad(IOPORT2, 15);
        chThdSleepMilliseconds(200);
        palSetPad(IOPORT2, 15);
        chThdSleepMilliseconds(200);
    }
  }

public:
  BlinkerThread(void) : EnhancedThread<128>("blinker") {
  }
};


/*
 * Application entry point.
 */
int main(void) {
  halInit();
  System::Init();
 
  BlinkerThread Blinker(void);

  while (true) {
    chThdSleepMilliseconds(1000);
    /*palSetPad(IOPORT2, 15);
      chThdSleepMilliseconds(200);
      palClearPad(IOPORT2, 15);
      chThdSleepMilliseconds(200);*/
  }

  return 0;
}


Anyone have any ideas?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: C++ and STM32

Post by Giovanni »

Not sure, it looks correct. I will give it a try in the weekend if you don't find the problem. Does the thread start? you may place a breakpoint and verify this.

Giovanni
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: C++ and STM32

Post by Giovanni »

Weird, it seems the compiler entirely removes the code for BlinkerThread for some reason, I am still investigating.

Giovanni
User avatar
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Re: C++ and STM32

Post by Badger »

Hi Giovanni,

I was trying to get my debugger working with SWD, but haven't managed that yet. I did some further experiment and found that the problem is caused because there are no parameters passed to the constructor. With parameters, it works fine!

So this works:

Code: Select all

#include "ch.hpp"
#include "hal.h"

using namespace chibios_rt;

/*
 * Tester thread class. This thread executes the test suite.
 */
class BlinkerThread : public EnhancedThread<128> {

protected:
  virtual msg_t Main(void) {
     int i = 10;
     i = i*2;
      while(true) {
       palClearPad(IOPORT2, 15);
        chThdSleepMilliseconds(200);
        palSetPad(IOPORT2, 15);
        chThdSleepMilliseconds(200);
      }
  }

public:
  BlinkerThread(int ignore) : EnhancedThread<128>("blinker") {
  }
};


/*
 * Application entry point.
 */
int main(void) {
  halInit();
  System::Init();
 
  BlinkerThread blinker(10);

  while (true) {
    chThdSleepMilliseconds(1000);
    /*palSetPad(IOPORT2, 15);
      chThdSleepMilliseconds(200);
      palClearPad(IOPORT2, 15);
      chThdSleepMilliseconds(200);*/
  }

  return 0;
}
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: C++ and STM32

Post by Giovanni »

I just found the same and was about to post it :-)

Not sure why however.

Giovanni
User avatar
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Re: C++ and STM32

Post by Badger »

I've found some more oddities with the c++ interface.

I've got a very simple test application:

Code: Select all


#include "ch.hpp"
#include "hal.h"
#include "util.h"

#include "i2c/lsm303.hpp"

using namespace chibios_rt;

static const I2CConfig i2cfg = {
   OPMODE_I2C, 400000, FAST_DUTY_CYCLE_2, 0, 0, 0, 0
};

/*
 * Tester thread class. This thread executes the test suite.
 */
class BlinkerThread : public EnhancedThread<128> {

protected:
  virtual msg_t Main(void) {
      while(true) {
       palClearPad(IOPORT2, 15);
        chThdSleepMilliseconds(200);
        palSetPad(IOPORT2, 15);
        chThdSleepMilliseconds(200);
      }
     
      return 0;
  }

public:
  BlinkerThread(int ignore) : EnhancedThread<128>("blinker") {
  }
};


class SensorsThd : public EnhancedThread<128> {

protected:
  virtual msg_t Main(void) {
      while(true) {
        chThdSleepMilliseconds(200);
      }
     
      return 0;
  }

public:
  SensorsThd(int ignore) : EnhancedThread<128>("sensors2") {
  }
};

/*
 * Application entry point.
 */
int main(void) {
  halInit();
  System::Init();
 
  i2cStart(&I2CD1, &i2cfg);
  i2cStart(&I2CD2, &i2cfg);
 
  BlinkerThread blinker(1);
 
  SensorsThd sensors(10);
  //SensorsThread sensors2(&I2CD1, &I2CD2);

  while (true) {
    chThdSleepMilliseconds(1000);
  }

  return 0;
}


However if I increase the thread memory size of SensorsThd to 512 or greater than the blinkerThread stops running. Hmmmm!
User avatar
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Re: C++ and STM32

Post by Badger »

It also seems that if I create instances of more than 2x128 byte c++ threads then the system hangs. As far as I can tell it isn't a case of running out of memory, and I am able to allocate a large array (stkalign_t s[1024 / sizeof(stkalign_t)];) without problems, but once I enable more threads (or increase the stack size) the blinking stops.

I suppose it is time I worked out how to setup the new eclipse plugin :D
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: C++ and STM32

Post by Giovanni »

It is possible this is another compiler problem. I would also verify if there is a stack alignment problem, the stacks must be 8 bytes aligned and I am not sure the compiler enforces this when the stack is allocated within an object. You could try to enforce the alignment of the thread object using an attribute.

If you don't find the problem I will give this more time after the current work on the STM32 drivers.

Giovanni
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: C++ and STM32

Post by Giovanni »

Probably I understood the problem.

Threads instantiated as automatic objects into the main() function are allocated into the main stack, you must increase the main stack size to accommodate the other threads (in the scatter file, you may put a customized scatter file into your project directory), this is why it stops working when you go past a certain stack size for the threads.
My previous comment regarding alignment stands, it is not the cause of this problem but potentially still a problem.

Anyway, an update to that C++ wrapper is long overdue. it born just as an experiment.

Giovanni
Post Reply