S. Marsanne

Sharing some coding and debugging style (part 2)

Discussion created by S. Marsanne Employee on Dec 23, 2016

When writing some code, it is useful to think how the compiler and linker proceed to generate the flash file.

 

The most time consuming part of the firmware (FW) is not the coding, it's the debugging!

Everyone has its own style and method, why not sharing them so people can pick what suits them? Here we go:

 

Debugging and debugger

 

============ 8>< ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

Disabling a piece of code:

 

Each editor has some specific trick to add // in multiple line of code, the following techniques are valid too:

 

#if 0

here the disabled code

#endif

 

This is a classic. Another one where you can turn on or off the code dynamically during debug (avoiding recompiling is fun)

 

volatile int skipcode = 0; // could be a switch case statement too

 

void myfunction(...) {

 

...

while(skipcode) {

 here the optional code

};

 

}

 

This way, when launching the debugger, the code is turned off. Put a breakpoint, change the skipcode variable in a watch window and continue the code... an easy way to activate/deactivate pieces of code. Can be used also in switch/case to run different tests or example of code every reset/run sequence.

 

Why the volatile? To avoid the compiler optimize by removing the while loop!

 

============ 8>< ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

To be continued...

Outcomes