title: Cleaner namespaces for microcontroller registers
body: <p>Working with a microcontroller, often you'll have to write and read from registers, to make the code more readable, you'd define the register address along with its bits. This is somewhat fine, but it quickly gets messy when you've got register names that are really similar to each other, as seen here for example</p>
  
  <pre><code>#define SYSAHBCLKCTRL (*(unsigned int*) 0x40048080)
  #define TMR16B0TCR (*(unsigned int*) 0x4000C004)
  #define TMR16B0TC (*(unsigned int*) 0x4000C008)
  #define TMR16B0PR (*(unsigned int*) 0x4000C00C)
  #define TMR16B0MR0 (*(unsigned int*) 0x4000C018) // Match register
  #define ISER1 (*(unsigned int*) 0xE000E104) // Enable IRQ
  #define TMR16B0MCR (*(unsigned int*) 0x4000C014) // Match Control
  #define TMR16B0IR (*(unsigned int*) 0x4000C000) // Interrupt Flag Register
  </code></pre>
  
  <p>At this level, it's still somewhat manageable, but it gets far worse when you start defining the associated flags of each register, for example this one register</p>
  
  <pre><code>#define I2C1CONSET (* (unsigned int *) 0x4005C000)
  #define I2C1CONSET_EN 0x40
  // bit 5, start condition
  #define I2C1CONSET_STA 0x20
  // bit 4, stop condition
  #define I2C1CONSET_STO 0x10
  // bit 2, acknowledge signal
  #define I2C1CONSET_ACK 0x04
  </code></pre>
  
  <p>At this point, I would much rather use something like a namespace, so I can do something like this:</p>
  
  <pre><code>I2C1CONSET |= _I2C1CONSET.EN | _I2C1CONSET.STA; // set EN and STA bits on I2C1CONSET register
  </code></pre>
  
  <p>Are there any clean alternatives to how I'm working right now?</p>