embedded stack collision with the variable-memory

There was a strange bug I found which took me ages to find. I was implementing some stuff on a Atmel A90S8515 microcontroller. This device is quite old and has very limited memory. Both Flash and RAM. The problem occurred when sprintf from the standard library was called. When called,  any internal variables were changed (seemingly randomly). The only reason some I figured it out was because there was a soft-PWM implementation, which changed the color of a RGB led. Reading trough forums I found out that the sprintf-function is quite memory consuming, therefore uses a lot of stack related memory. As a heap is not existent (there is no dynamic data), a good reason for the strange behaviour might be the stack colliding with some global variables. Luckily, this hypothesis was underlined by this article. Therefore, what happened is, that the execution of sprintf (consuming lot of memory on the stack), overwrote global variables in the bss and or the data part of the memory. As the compiler is not able to find these kind of bugs, there is no warning or whatever. The solution was found within seconds. Some very large send/receive  buffers (defined as such in the initial phase of  the project) were just re sized from 128 bytes each to 32 bytes each, making more room for the stack!

my atmel microcontroller is continuously reseting

A problem I had multiple times was: I reuse software from the internet, but when i use the software / simulate it, it is constantly resetting! The reason why this is happening is (or can be) undefined reset vectors. This means, the hardware raises an interrupt, which is not hadled by your software. Therefore, make sure that all interrupts which might occure are written in the software, like UART, ADC, etc. related interrupts! Sometimes you just need an empty interrupt service routine:

The avr-gcc compiler will default your missing vector to the reset-vector, as it is a sign of buggy code! Another approach is to us a “catch-all” interrupt vector

which catches all undefined interrupts.

compile C and C++ code into one binary in Linux

Sometimes you have a project written in C and you want to add some code (may be open source software) which is provided in C++. So you may want to call a function written in C++ from C. For that purpose, you have to change the function prototype of the concerned C++ function like:

The ifdefs are only needed, if you are including the header file containing this code into a C and C++ source file!

When it comes to compiling, use the specific compiler like gcc for C code and g++ for C++ code. Compile each sourcefile(s) into a object file like:

The last line will link the object files together.
A good tutorial is found here.