Hi all,
I would like to know some general good coding practices with, especially in regard to stack and heap management for MSP430 C Code.
As of now, I am following the guidelines on this application note: http://www.ti.com/lit/an/slaa294a/slaa294a.pdf
At the beginning of my code, I turn off the watchdog timer, initialize registers & variables, enable interrupts and enter low power mode.
Generally my ISRs set a flag and wake up the CPU to go to the main routine. The main routine checks which flag was set, and then runs the relevant code. The relevant code consists of running through some functions and then going back to low power mode. For these functions, in order to minimize the stack size, I try to avoid using local variables where I can.
As with most MSP430-based systems, I run an event-driven system. Some of my events happen deterministically (let’s say for example at a rate of x Hz), but others are non-deterministic -- they are based on sensor values or user input.
I am writing the code in C using IAR Embedded Workbench. Based on others’ experience, is there a recommended level of optimization I should use with the compiler? I realize too much optimization may actually cause an unintended implementation of code, but at the same time realize there some benefits to certain aspects of optimization.
On another note, when would one use heap in an MSP430 embedded system? From my understand and from posts I have read, it is best not to use heap in an embedded system unless you have some special application: http://stackoverflow.com/questions/1026730/can-i-write-a-c-application-without-using-the-heap
In my application, I don’t actually use the functions malloc, calloc, etc, but I do use pointers for reading/writing to flash, as well as to send/receive I2C bytes. I already know the maximum size of buffers and such, and create pre-defined array sizes. From my understanding, this isn’t really dynamic memory management and therefore does not fall under using the heap. So hopefully I should not have to worry too much about memory holes and other problems related to heap management. Is this correct?
Sincerely,
Mehdi