Page 3 of 5

Re: C++ and STM32

Posted: Sun Sep 04, 2011 8:12 pm
by Badger
Thanks for that pointer Giovanni, increasing the main and process stack sizes has fixed it :D

Re: C++ and STM32

Posted: Sun Sep 04, 2011 8:17 pm
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

Re: C++ and STM32

Posted: Sun Sep 04, 2011 8:31 pm
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!

Re: C++ and STM32

Posted: Thu Dec 01, 2011 6:50 pm
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.

Re: C++ and STM32

Posted: Thu Dec 01, 2011 7:10 pm
by Giovanni
Temporary workaround, that C++ wrapper needs improvements.

Giovanni

Re: C++ and STM32

Posted: Wed Dec 07, 2011 1:10 am
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?

Re: C++ and STM32

Posted: Tue Dec 13, 2011 9:50 am
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.

Re: C++ and STM32

Posted: Sun Jan 01, 2012 4:42 pm
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

Re: C++ and STM32

Posted: Sun Jan 01, 2012 4:58 pm
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

Re: C++ and STM32

Posted: Sun Jan 01, 2012 5:08 pm
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.