Electronic – Should I define default register values in start up code

armmicrocontrollerprogramming

It has come to my attention that some ARM programmers set some CPU registers with values that are the same default values of the register on microcontroller reset.

For example: the default value of IOCON_PIO2_9, parameter MODE on LCP1114 is 0x02 (pull-up resistor enabled). In Olimex's example code this value is set to 0x02 in software initialization but this is already the default value.

This behavior occurs with many other registers.

Why would someone do that? In my opinion, this extra code just pollutes everything and make code readability worse. Is this a good programming practice?

Best Answer

  1. To make code modular. If your A/D routine, for example, always sets up the A/D completely for whatever mode it uses it in, then that routine can be called at any time whether immediately after powerup or after the A/D was used in some other way.
  2. Code may jump back to the startup location to do a software reset. In that case, the settings may not be at the powerup default values.
  3. The powerup defaults may be different on different processors, so setting them in code makes the code more portable.
  4. It documents what the settings are. If these registers are explicitly set on startup, then someone looking at the code doesn't have to go digging in the datasheet to see what they are set to.
  5. It documents that you care. Let the hardware default the settings you aren't using, but explicitly set the ones you rely on. This may alert some future maintainer to the importance of these particular settings.