    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: A Technique for Register Access in C++</title>
        <link>https://members.accu.org/index.php/articles/281</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #68 - Aug 2005</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c144/">68</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-144/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+144/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;A Technique for Register Access in C++</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 02 August 2005 05:00:00 +01:00 or Tue, 02 August 2005 05:00:00 +01:00</p>
<p><strong>Summary:</strong>&nbsp;This article discusses a C++ scheme for accessing hardware registers in an optimal way.</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e23" id="d0e23"></a></h2>
<div>
<h3>Exploiting C++'s features for efficient and
safe hardware register access.</h3>
</div>
</div>
<p><span class="emphasis"><em>This article originally appeared in
C/C++ Users Journal, and is reproduced by kind
permission.</em></span></p>
<p>Embedded programmers traditionally use C as their language of
choice. And why not? It's lean and efficient, and allows you to get
as close to the metal as you want. Of course C++, used properly,
provides the same level of efficiency as the best C code. But we
can also leverage powerful C++ features to write cleaner, safer,
more elegant low-level code. This article demonstrates this by
discussing a C++ scheme for accessing hardware registers in an
optimal way.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e30" id="d0e30"></a>Demystifying
Register Access</h2>
</div>
<p>Embedded programming is often seen as black magic by those not
initiated into the cult. It does require a slightly different
mindset; a resource constrained environment needs small, lean code
to get the most out of a slow processor or a tight memory limit. To
understand the approach I present we'll first review the mechanisms
for register access in such an environment. Hardcore embedded
developers can probably skip ahead; otherwise here's the view from
10,000 feet.</p>
<p>Most embedded code needs to service hardware directly. This
seemingly magical act is not that hard at all. Some kinds of
register need a little more fiddling to get at than others, but you
certainly don't need an eye-of-newt or any voodoo dances. The exact
mechanism depends on how your circuit board is wired up. The common
types of register access are:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p><span class="bold"><b>Memory mapped I/O</b></span> The hardware
allows us to communicate with a device using the same instructions
as memory access. The device is wired up to live at memory address
n; register 1 is mapped at address n, register 2 is at n+1,
register 3 at n+2, and so on.</p>
</li>
<li>
<p><span class="bold"><b>Port mapped I/O</b></span> Certain devices
present pages of registers that you have to map into memory by
selecting the correct device 'port'. You might use specific
input/output CPU instructions to talk to these devices, although
more often the port and its selector are mapped directly into the
memory address space.</p>
</li>
<li>
<p><span class="bold"><b>Bus separated</b></span> It's harder to
control devices connected over a non-memory mapped bus.
I<sup>2</sup>C and I2S are common peripheral connection buses. In
this scenario you must either talk to a dedicated I<sup>2</sup>C
control chip (whose registers are memory mapped), telling it what
to send to the device, or you manipulate I<sup>2</sup>C control
lines yourself using GPIO<sup>[<a name="d0e62" href="#ftn.d0e62"
id="d0e62">1</a>]</sup> ports on some other memory mapped
device.</p>
</li>
</ul>
</div>
<p>Each device has a data sheet that describes (amongst other
things) the registers it contains, what they do, and how to use
them. Registers are a fixed number of bits wide - this is usually
determined by the type of device you are using. This is an
important fact to know: some devices will lock up if you write the
wrong width data to them. With fixed width registers, many devices
cram several bits of functionality into one register as a 'bitset'.
The data sheet would describe this diagrammatically in a similar
manner to Figure 1.</p>
<div class="figure"><a name="d0e68" id="d0e68"></a>
<div class="mediaobject c5"><img src=
"/var/uploads/journals/resources/goodliffe%20table.png" align="middle" alt=
"Registers in a sample UART line driver device"></div>
<p class="title c6">Figure 1. Registers in a sample UART line
driver device</p>
</div>
<p>So what does hardware access code look like? Using the simple
example of a fictional UART line driver device presented in Figure
1, the traditional C-style schemes are:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p><span class="bold"><b>Direct memory pointer access</b></span>.
It's not unheard of to see register access code like Listing 1, but
we all know that the perpetrators of this kind of monstrosity
should be taken outside and slowly tortured. It's neither readable
nor maintainable.</p>
<div class="sidebar">
<pre class="programlisting">
*((volatile uint32_t *)0xfffe0004) = 10;
*((volatile uint8_t  *)0xfffe0001) = 3;
</pre>
<p>Listing 1</p>
</div>
<div class="sidebar">
<p class="title c6">Using Volatile</p>
<p>This low-level purgatory is where we use C's volatile keyword.
volatile signals to the compiler that a value may change under the
code's feet, that we can make no assumptions about it, and the
optimiser can't cache it for repeated use.</p>
<p>This is just the behaviour we need for hardware register access.
Every time we write code that accesses a register we want it to
result in a real register access. Don't forget the volatile
qualification!</p>
</div>
<p>Pointer usage is usually made bearable by defining a macro name
for each register location. There are two distinct macro flavours.
The first macro style defines bare memory addresses (as in Listing
2). The only real advantage of this is that you can share the
definition with assembly code parsed using the C preprocessor. As
you can see, its use is long-winded in normal C code, and prone to
error - you have to get the cast right each time. The alternative,
in Listing 3, is to include the cast in the macro itself; far nicer
in C. Unless there's a lot of assembly code this latter approach is
preferable.</p>
<div class="sidebar">
<pre class="programlisting">
#define UART_TXBUF 0xfffe0004
#define UART_TXCTL 0xfffe0001
*(volatile uint32_t *)UART_TXBUF = 10;
*(volatile uint8_t  *)UART_TXCTL = 3;
</pre>
<p>Listing 2</p>
</div>
<div class="sidebar">
<pre class="programlisting">
#define UART_TXBUF ((volatile uint32_t*) 0xfffe0004)
#define UART_TXCTL ((volatile uint8_t*)  0xfffe0001)
*UART_TXBUF = 10;
*UART_TXCTL = 3;
</pre>
<p>Listing 3</p>
</div>
<p>We use macros because they have no overhead in terms of code
speed or size. The alternative, creating a physical pointer
variable to describe each register location, would have a negative
impact on both code performance and executable size. However,
macros are gross and C++ programmers are already smelling a rat
here. There are plenty of problems with this fragile scheme. It's
programming at a very low level, and the code's real intent is not
clear- it's hard to spot all register accesses as you browse a
function.</p>
</li>
<li>
<p><span class="bold"><b>Deferred assignment</b></span> is a cute
technique that allows you to write code like Listing 4, defining
the register location values at link time. This is not commonly
used; it's cumbersome when you have a number of large devices, and
not all compilers provide this functionality. It requires you to
run a flat (non virtual) memory model.</p>
<div class="sidebar">
<pre class="programlisting">
extern volatile uint32_t UART_TXBUF;
extern volatile uint8_t  UART_TXCTL;
UART_TXBUF = 10;
UART_TXCTL = 3;

// compile this with:
//   gcc listing4.c
//     -gUART_UART_TXBUF=0xfffe0004 
//     -gUART_TXCTL=0xfffe0001
</pre>
<p>Listing 4</p>
</div>
</li>
<li>
<p>Use a <tt class="literal">struct</tt> to describe the register
layout in memory, as in Listing 5. There's a lot to be said for
this approach - it's logical and reasonably readable. However, it
has one big drawback: it is not standards-compliant. Neither the C
nor C++ standards specify how the contents of a struct are laid out
in memory. You are guaranteed an exact ordering, but you don't know
how the compiler will pad out non-aligned items. Indeed, some
compilers have proprietary extensions or switches to determine this
behaviour. Your code might work fine with one compiler and produce
startling results on another.</p>
<div class="sidebar">
<pre class="programlisting">
struct uart_device_t
{
  uint8_t STATUS;
  uint8_t TXCTL;
 .. and so on ...
};
static volatile uart_device_t * 
    const uart_device
    = reinterpret_cast
    &lt;volatile uart_device_t *&gt;(0xfffe0000);
uart_device-&gt;TXBUF = 10;
uart_device-&gt;TXCTL = 3;
</pre>
<p>Listing 5</p>
</div>
</li>
<li>
<p>Create a <tt class="literal">function</tt> to access the
registers and hide all the gross stuff in there. On less speedy
devices this might be prohibitively slow, but for most applications
it is perfectly adequate, especially for registers that are
accessed infrequently. For port mapped registers this makes a lot
of sense; their access requires complex logic, and writing all this
out longhand is tortuous and easy to get wrong.</p>
</li>
</ul>
</div>
<p>It remains for us see how to manipulate registers containing a
bitset. Conventionally we write such code by hand, something like
Listing 6. This is a sure-fire way to cause yourself untold grief,
tracking down odd device behaviour. It's very easy to manipulate
the wrong bit and get very confusing results.</p>
<div class="sidebar">
<pre class="programlisting">
#define UART_RX_BYTES 0x0e
uint32_t uart_read()
{
  while ((*UART_RXCTL &amp; UART_RX_BYTES) == 0) 
      // manipulate here
  {
    ; // wait
  }
  return *UART_RXBUF;
}
</pre>
<p>Listing 6</p>
</div>
<p>Does all this sound messy and error prone? Welcome to the world
of hardware devices. And this is just addressing the device: what
you write into the registers is your own business, and part of what
makes device control so painful. Data sheets are often ambiguous or
miss essential information, and devices magically require registers
to be accessed in a certain order. There will never be a silver
bullet and you'll always have to wrestle these demons. All I can
promise is to make the fight less biased to the hardware's
side.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e144" id="d0e144"></a>A More Modern
Solution</h2>
</div>
<p>So having seen the state of the art, at least in the C world,
how can we move into the 21st century? Being good C++ citizens we'd
ideally avoid all that nasty preprocessor use and find a way to
insulate us from our own stupidity. By the end of the article
you'll have seen how to do all this and more. The real beauty of
the following scheme is its simplicity. It's a solid, proven
approach and has been used for the last five years in production
code deployed in tens of thousands of units across three
continents. Here's the recipe&hellip;</p>
<p>Step one is to junk the whole preprocessor macro scheme, and
define the device's registers in a good old-fashioned enumeration.
For the moment we'll call this enumeration Register. We immediately
lose the ability to share definitions with assembly code, but this
was never a compelling benefit anyway. The enumeration values are
specified as offsets from the device's base memory address. This is
how they are presented in the device's datasheet, which makes it
easier to check for validity. Some data sheets show byte offsets
from the base address (so 32-bit register offsets increment by 4
each time), whilst others show 'word' offsets (so 32-bit register
offsets increment by 1 each time). For simplicity, we'll write the
enumeration values however the datasheet works.</p>
<p>The next step is to write an inline regAddress function that
converts the enumeration to a physical address. This function will
be a very simple calculation determined by the type of offset in
the enumeration. For the moment we'll presume that the device is
memory mapped at a known fixed address. This implies the simplest
MMU configuration, with no virtual memory address space in
operation. This mode of operation is not at all uncommon in
embedded devices. Putting all this together results in Listing
7.</p>
<div class="sidebar">
<pre class="programlisting">
static const unsigned int baseAddress =
      0xfffe0000;
enum Registers
{
  STATUS = 0x00, // UART status register
  TXCTL  = 0x01, // Transmit control
  RXCTL  = 0x02, // Receive control
  . and so on ...
};
inline volatile uint8_t *regAddress
      (Registers reg)
{
  return reinterpret_cast&lt;volatile 
        uint8_t*&gt;(baseAddress + reg);
}
</pre>
<p>Listing 7</p>
</div>
<p>The missing part of this jigsaw puzzle is the method of
reading/writing registers. We'll do this with two simple inline
functions, <tt class="function">regRead</tt> and <tt class=
"function">regWrite</tt>, shown in Listing 8. Being inline, all
these functions can work together to make neat, readable register
access code with no runtime overhead whatsoever. That's mildly
impressive, but we can do so much more.</p>
<div class="sidebar">
<pre class="programlisting">
inline uint8_t regRead(Registers reg)
{
  return *regAddress(reg);
}

inline void regWrite
      (Registers reg, uint8_t value)
{
  *regAddress(reg) = value;
}
</pre>
<p>Listing 8</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e171" id="d0e171"></a>Different
Width Registers</h2>
</div>
<p>Up until this point you could achieve the same effect in C with
judicious use of macros. We've not yet written anything
groundbreaking. But if our device has some 8-bit registers and some
32-bit registers we can describe each set in a different
enumeration. Let's imaginatively call these Register8 and
Register32. Thanks to C++'s strong typing of enums, now we can
overload the register access functions, as demonstrated in Listing
9.</p>
<div class="sidebar">
<pre class="programlisting">
// New enums for each register width
enum Registers8
{
  STATUS = 0x00, // UART status register
  ... and so on ...
};
enum Registers32
{
  TXBUF = 0x04,  // Transmit buffer
    ... and so on ...
};

// Two overloads of regAddress
inline volatile uint8_t *regAddress
      (Registers8 reg)
{
  return reinterpret_cast&lt;volatile uint8_t*&gt;
        (baseAddress + reg);
}
inline volatile uint32_t *regAddress
      (Registers32 reg)
{
  return reinterpret_cast&lt;volatile uint32_t*&gt;
        (baseAddress + reg);
}
// Two overloads of regRead

inline uint8_t regRead(Registers8 reg)
{
  return *regAddress(reg);
} 
inline uint32_t regRead(Registers32 reg)
{
  return *regAddress(reg);
}
..similarly for regWrite ...
</pre>
<p>Listing 9</p>
</div>
<p>Now things are getting interesting: we still need only type
regRead to access a register, but the compiler will automatically
ensure that we get the correct width register access. The only way
to do this in C is manually, by defining multiple read/write macros
and selecting the correct one by hand each time. This overloading
shifts the onus of knowing which registers require 8 or 32-bit
writes from the programmer using the device to the compiler. A
whole class of error silently disappears. Marvellous!</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e183" id="d0e183"></a>Extending to
Multiple Devices</h2>
</div>
<p>An embedded system is composed of many separate devices, each
performing their allotted task. Perhaps you have a UART for
control, a network chip for communication, a sound device for
audible warnings, and more. We need to define multiple register
sets with different base addresses and associated bitset
definitions. Some large devices (like super I/O chips) consist of
several subsystems that work independently of one another; we'd
also like to keep the register definitions for these parts
distinct.</p>
<p>The classic C technique is to augment each block of register
definition names with a logical prefix. For example, we'd define
the UART transmit buffer like this:</p>
<pre class="programlisting">
#define MYDEVICE_UART_TXBUF 
      ((volatile uint32_t *)0xffe0004)
</pre>
<p>C++ provides an ideal replacement mechanism that solves more
than just this aesthetic blight. We can group register definitions
within namespaces. The nest of underscored names is replaced by
<tt class="literal">::</tt> qualifications - a better, syntactic
indication of relationship. Because the overload rules honour
namespaces, we can never write a register value to the wrong device
block: it's a syntactic error. This is a simple trick, but it makes
the scheme incredibly usable and powerful.</p>
<div class="sidebar">
<p class="title c6">Proof of Efficiency</p>
<p>Perhaps you think that this is an obviously a good solution, or
you're just presuming that I'm right. However, a lot of old-school
embedded programmers are not so easily persuaded. When I introduced
this scheme in one company I met a lot of resistance from C
programmers who could just not believe that the inline functions
resulted in code as efficient as the proven macro technique.</p>
<div class="informaltable">
<table border="1" cellspacing="0">
&lt;colgroup&gt;
&lt;col width=&quot;33%&quot;&gt;
&lt;col width=&quot;33%&quot;&gt;
&lt;col width=&quot;34%&quot;&gt;&lt;/colgroup&gt;
&lt;thead&gt;
<tr>
<th rowspan="2">Register access method</th>
<th colspan="2">Results (object file size in bytes)</th>
</tr>
<tr>
<th>Unoptimised</th>
<th>Optimised</th>
</tr>
&lt;/thead&gt;
&lt;tbody&gt;
<tr>
<td>C++ inline function scheme</td>
<td>1087</td>
<td>551</td>
</tr>
<tr>
<td>C++ using #defines</td>
<td>604</td>
<td>551</td>
</tr>
<tr>
<td>C using #defines</td>
<td>612</td>
<td>588</td>
</tr>
&lt;/tbody&gt;
</table>
</div>
<p>The only way to persuade them was with hard data - I compiled
equivalent code using both techniques for the target platform (gcc
targeting a MIPS device). The results are listed in the table
below. An inspection of the machine code generated for each kind of
register access showed that the code was identical. You can't argue
with that! It's particularly interesting to note that the #define
method in C is slightly larger than the C++ equivalent. This is a
peculiarity of the gcc toolchain - the assembly listing for the two
main functions is identical: the difference in file size is down to
the glue around the function code.</p>
</div>
<p>Namespacing also allows us to write more readable code with a
judicious sprinkling of using declarations inside device setup
functions. Koenig lookup combats excess verbiage in our code. If we
have register sets in two namespaces <tt class="literal">DevA</tt>
and <tt class="literal">DevB</tt>, we needn't quality a <tt class=
"function">regRead</tt> call, just the register name. The compiler
can infer the correct <tt class="function">regRead</tt> overload in
the correct namespace from its parameter type. You only have to
write:</p>
<pre class="programlisting">
uint32_t value = regRead(DevA::MYREGISTER); 
    // note: not DevA::regRead(...)
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e258" id="d0e258"></a>Variable Base
Addresses</h2>
</div>
<p>Not every operating environment is as simplistic as we've seen
so far. If a virtual memory system is in use then you can't
directly access the physical memory mapped locations - they are
hidden behind the virtual address space. Fortunately, every OS
provides a mechanism to map known physical memory locations into
the current process' virtual address space.</p>
<p>A simple modification allows us to accommodate this memory
indirection. We must change the <tt class=
"varname">baseAddress</tt> variable from a simple static const
pointer to a real variable. The header file defines it as extern,
and before any register accesses you must arrange to define and
assign it in your code. The definition of <tt class=
"varname">baseAddress</tt> will be necessarily system specific.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e271" id="d0e271"></a>Other
Usage</h2>
</div>
<p>Here are a few extra considerations for the use of this register
access scheme:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Just as we use namespaces to separate device definitions, it's a
good idea to choose header file names that reflect the logical
device relationships. It's best to nest the headers in directories
corresponding to the namespace names.</p>
</li>
<li>
<p>A real bonus of this register access scheme is that you can
easily substitute alternative regRead/regWrite implementations.
It's easy to extend your code to add register access logging, for
example. I have used this technique to successfully debug hardware
problems. Alternatively, you can set a breakpoint on register
access, or introduce a brief delay after each write (this quick
change shows whether a device needs a pause to action each register
assignment).</p>
</li>
<li>
<p>It's important to understand that this scheme leads to larger
<span class="emphasis"><em>unoptimised</em></span> builds. Although
it's remarkably rare to not optimise your code, without
optimisation inline functions are not reduced and your code will
grow.</p>
</li>
<li>
<p>There are still ways to abuse this scheme. You can pass the
wrong bitset to the wrong register, for example. But it's an order
of magnitude harder to get anything wrong.</p>
<div class="sidebar">
<pre class="programlisting">
// A macro that defines enumeration values for
// a bitset
// You supply the start and end bit positions

#define REG_BIT_DEFN(start, end)
       ((start&lt;&lt;16)|(end-start+1))

enum STATUS_bits
{
  TX_BUFFER_EMPTY = REG_BIT_DEFN(0, 0),
  RX_BUFFER_EMPTY = REG_BIT_DEFN(1, 1), 
  TX_UNDERRUN     = REG_BIT_DEFN(2, 2), 
  RX_OVERFLOW     = REG_BIT_DEFN(3, 3)
};
... similarly for other bitsets ...

#undef REG_BIT_DEFN

inline uint32_t bitRead(Registers32 reg, 
                        uint32_t bits)
{
  uint32_t       regval = *regAddress(reg);
  const uint32_t width  = bits &amp; 0xff;
  const uint32_t bitno  = bits &gt;&gt; 16;
  regval &gt;&gt;= bitno;
  regval  &amp;= ((1&lt;&lt;width)-1);
  return regval;
}

inline void bitWrite(Registers32 reg, 
                     uint32_t bits, 
                     uint32_t value)
{
  uint32_t          regval = *regAddress(reg);
  const uint32_t    width  = bits &amp; 0xff;
  const uint32_t    bitno  = bits &gt;&gt; 16;
  regval &amp;= ~(((1&lt;&lt;width)-1) &lt;&lt; bitno);
  regval |=  value &lt;&lt; bitno;
  *regAddress(reg) = regval;
}
</pre>
<p>Listing 10</p>
</div>
</li>
<li>
<p>A small sprinkling of template code allows us to avoid repeated
definition of <tt class="function">bitRead</tt>/<tt class=
"function">bitWrite</tt>. This is shown in Listing 11.</p>
<div class="sidebar">
<pre class="programlisting">
// Template versions of bitRead/Write - put 
// them at global scope and you don't have to
// copy bitRead/Write into every device 
// namespace

template &lt;typename RegType&gt;
inline uint32_t bitRead
      (RegType reg, uint32_t bits)
{
  uint32_t       regval = *regAddress(reg);
  const uint32_t width  = bits &amp; 0xff;
  const uint32_t bitno  = bits &gt;&gt; 16;
  regval &gt;&gt;= bitno;
  regval  &amp;= ((1&lt;&lt;width)-1);
  return regval;
}
template &lt;typename RegType&gt;
inline void bitWrite(RegType reg, 
      uint32_t bits, uint32_t value)
{
  uint32_t          regval = *regAddress(reg);
  const uint32_t    width  = bits &amp; 0xff;
  const uint32_t    bitno  = bits &gt;&gt; 16;
  regval &amp;= ~(((1&lt;&lt;width)-1) &lt;&lt; bitno);
  regval |=  value &lt;&lt; bitno;
  *regAddress(reg) = regval;
}
</pre>
<p>Listing 11</p>
</div>
</li>
</ul>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e311" id=
"d0e311"></a>Conclusion</h2>
</div>
<p>OK, this isn't rocket science, and there's no scary template
metaprogramming in sight (which, if you've seen the average
embedded programmer, is no bad thing!) But this is a robust
technique that exploits a number of C++'s features to provide safe
and efficient hardware register access. Not only is it supremely
readable and natural in the C++ idiom, it prevents many common
register access bugs and provides extreme flexibility for hardware
access tracing and debugging.</p>
<p>I have a number of proto-extensions to this scheme to make it
more generic (using a healthy dose of template metaprogramming,
amongst other things). I'll gladly share these ideas on request,
but would welcome some discussion about this.</p>
<p>Do Overload readers see any ways that this scheme could be
extended to make it simpler and easier to use?</p>
</div>
<div class="footnotes"><br>
<hr class="c7" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e62" href="#d0e62" id=
"ftn.d0e62">1</a>]</sup> General Purpose Input/Output - assignable
control lines not specifically designed for a particular data
bus.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
