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
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Re: C++ and STM32

Post by Badger »

Thanks for that pointer Giovanni, increasing the main and process stack sizes has fixed it :D
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: C++ and STM32

Post by Giovanni »

Heh, careful, the main() function stack is the "process stack", the "main stack" is the stack for interrupts. It is a bit of a mess but those are the names ARM gave them.

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

Re: C++ and STM32

Post by Badger »

Giovanni wrote:Heh, careful, the main() function stack is the "process stack", the "main stack" is the stack for interrupts. It is a bit of a mess but those are the names ARM gave them.

Thanks, that makes sense, I have returned __main_stack_size__ to it's original value!
mobyfab
Posts: 484
Joined: Sat Nov 19, 2011 6:47 pm
Has thanked: 21 times
Been thanked: 31 times

Re: C++ and STM32

Post by mobyfab »

Just a heads up,

It works fine on the STM32F4DISCOVERY with some minor changes of course.

I had to increase the stack size as well. Else I cannot create more than 3x128 bytes threads.

Is this the "normal" way of fixing the issue or just a temporary workaround?

Thanks.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: C++ and STM32

Post by Giovanni »

Temporary workaround, that C++ wrapper needs improvements.

Giovanni
Xamusk
Posts: 81
Joined: Sat Mar 05, 2011 10:47 pm

Re: C++ and STM32

Post by Xamusk »

I, for one, use the C API in my C++ projects. It's just too troublesome to access some functions in the correct scopes. For example, imagine that I create some hardware abstraction class and this class needs the delay functions. From where should I call it? Must I have a pointer to the caller thread object just so I can call the delay function? Should the delay function be a static member? If it is static, why not just call the C API?
noether
Posts: 91
Joined: Wed Nov 16, 2011 3:18 pm

Re: C++ and STM32

Post by noether »

Xamusk wrote:I, for one, use the C API in my C++ projects. It's just too troublesome to access some functions in the correct scopes. For example, imagine that I create some hardware abstraction class and this class needs the delay functions. From where should I call it? Must I have a pointer to the caller thread object just so I can call the delay function? Should the delay function be a static member? If it is static, why not just call the C API?


Hello Xamusk, I faced the same problems in the past mixing C (RTOS) and C++ (my stuff) in microcontrollers. This FAQ solved to me many of these problems,
http://www.parashift.com/c++-faq-lite/m ... d-cpp.html . I hope this could be useful for you.

Cheers.
noether
Posts: 91
Joined: Wed Nov 16, 2011 3:18 pm

Re: C++ and STM32

Post by noether »

I would like to continue this discussion with some issues that I am facing.

I am including Eigen library (http://eigen.tuxfamily.org/index.php?title=Main_Page) just modifying the demo project.

Code: Select all

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

#include "Eigen/Dense"
using namespace Eigen;

static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *arg) {

   (void)arg;
   chRegSetThreadName("blinker");

   int i = 1;
   float valor[2];
   Matrix<float, 3, 3> A, L;
   A = Matrix<float, 3, 3>::Identity(3, 3) +
      Matrix<float, 3, 3>::Constant(1);

   while (TRUE)
   {
      i++;
      L = A.llt().matrixL();
      A(0, 0) += i;
      valor[0] = L(0, 0);
      valor[1] = L(1, 1);

      palSetPad(GPIOD, GPIOD_LED3);       /* Orange.  */
      chThdSleepMilliseconds(500);
      palClearPad(GPIOD, GPIOD_LED3);     /* Orange.  */
      chThdSleepMilliseconds(500);
   }
}

int main(void) {

   halInit();
   chSysInit();

   chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);

   while (TRUE) {
      chThdSleepMilliseconds(500);
   }
}


During the compilation, I do not have any problems if I set the flags to -O2 -DNDEBUG . But once I do not want any optimization for an easy gdb debugging, putting -O0 the linker complains as follows:

Code: Select all

Linking build/ch.elf
/home/noether/workspace/tool-chains/arm-none-eabi-4.6.2/lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib/thumb/cortex-m4/libc.a(lib_a-abort.o): In function `abort':
/home/noether/workspace/tool-chains/summon-arm-toolchain/build/arm-none-eabi/thumb/cortex-m4/newlib/libc/stdlib/../../../../../../../gcc-4.6.2/newlib/libc/stdlib/abort.c:63: undefined reference to `_exit'
/home/noether/workspace/tool-chains/arm-none-eabi-4.6.2/lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib/thumb/cortex-m4/libc.a(lib_a-signalr.o): In function `_kill_r':
/home/noether/workspace/tool-chains/summon-arm-toolchain/build/arm-none-eabi/thumb/cortex-m4/newlib/libc/reent/../../../../../../../gcc-4.6.2/newlib/libc/reent/signalr.c:61: undefined reference to `_kill'
/home/noether/workspace/tool-chains/arm-none-eabi-4.6.2/lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib/thumb/cortex-m4/libc.a(lib_a-signalr.o): In function `_getpid_r':
/home/noether/workspace/tool-chains/summon-arm-toolchain/build/arm-none-eabi/thumb/cortex-m4/newlib/libc/reent/../../../../../../../gcc-4.6.2/newlib/libc/reent/signalr.c:96: undefined reference to `_getpid'
collect2: ld returned 1 exit status
make: *** [build/ch.elf] Error 1


Do you know if I can solve this issue with any flag, or adding another external source like syscalls.c?

If I remove the Matrix operations lines, the code compiles without any problems.

Cheers
Last edited by noether on Sun Jan 01, 2012 4:59 pm, edited 1 time in total.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: C++ and STM32

Post by Giovanni »

Probably without optimizations unused code is not removed, you have to provide stub implementations of those calls in syscall.c (I would put just a chSysHalt() inside for debug).

Giovanni
noether
Posts: 91
Joined: Wed Nov 16, 2011 3:18 pm

Re: C++ and STM32

Post by noether »

Many thanks, it could compiled it. Now let see if the program runs correctly :P

Well, it doesn't xD.
The binary file is close to 130KB (it is 13KB with -O2 -DNDEBUG), and it crashes at the end of vectors.c .

Edit
-------
Already I can get around the problem just with adding some "volatile" for avoiding the optimizations. If you are interested, I can dedicate more time to know what is happening, but I do not know where I have to start.
Post Reply