title: How to assign a variable pointer to access hardware register on msp430?
body: <p>For context I'm using the <code>mspgcc</code> and the <code>msp430g2553</code> on the launchpad development board.</p>
  
  <p>I want to create a pointer that can point to various hardware registers.  The standard header provides access to the registers through statements like this:</p>
  
  <pre><code>P1DIR &amp;= 0x08;
  </code></pre>
  
  <p>The above is the direction register for port 1 gpio pins.  <code>P1DIR</code> is a 8bit hardware register.</p>
  
  <p>My attempts to assign a pointer that can be used to modify this are as follows:</p>
  
  <pre><code>volatile unsigned char *reg;
  reg = (unsigned char *) &amp;P1DIR;
  </code></pre>
  
  <p>This does not generate any errors or warnings by gcc however it does not work.  When using the gdb to test for values reg contains the value <code>0xFFFF</code> instead of <code>0x0022</code> (address of <code>P1DIR</code>) as stated in the data sheet.</p>
  
  <p>Here are the lines from the header to help with the problem:</p>
  
  <pre><code>/* External references resolved by a device-specific linker command file */
  #define SFR_8BIT(address)   extern volatile unsigned char address
  ...
  SFR_8BIT(P1DIR);                              /* Port 1 Direction */
  </code></pre>
  
  <p>As far as I could find the linker uses a file called <code>periph.x</code> (located at <code>/usr/local/msp430-uniarch-20110716/msp430/lib/ldscripts/msp430g2553</code>) which contains the line:</p>
  
  <pre><code>__P1DIR = 0x0022;
  </code></pre>