Thursday, November 28, 2013

Initialization before main

Let's say you have some peripheral, that you want to be up and running in your code, before you enter your main() function.

XC16 has the following attribute for this:

__attribute__((user_init))

Let's what the XC16 Compiler's user guide has to say about it:

But should you really use it with your xc16 or c30 compiler?

The answer is really short: NO

Why not, you may ask? Pretty simple, because the linker will go nuts, if you use the remove unused code option.

You will get a not working code. See my previous post.

By the way, here is a graph showing the benefits of using remove unused code option.

This comes from my recent project. You can clearly see the benefits from using this option, but remember, do not use the following attribute anywhere.
__attribute__((user_init))

As a side note, the user_init attribute is not a part of the standard GCC compiler. At least it is not listed on the official user manual page: GCC Function Attributes Manual
So looks like Microchip impleneted this in their GCC fork. Now I am wondering, how this bug never appeared in their bugtracker.
  1. Developers don't use this attribute
  2. Developers using this attribute think PIC is so damn cheap, that they buy the PIC with the bigger flash without second thoughts, instead of using the remove unused code option
  3. Developers who use this attribute never write unused code, or even if they do, they remove it manually


For the end here is a little video for those who'd like to see what actually happens.





No comments:

Post a Comment