<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <title>Jacques Fortier</title>
    <link>None</link>
    <description></description>
    <pubDate>Sat, 23 Apr 2011 22:23:30 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Running Mac OS X's built-in DHCP server</title>
      <link>http://www.jacquesf.com/2011/04/mac-os-x-dhcp-server</link>
      <pubDate>Sun, 10 Apr 2011 00:00:00 EDT</pubDate>
      <category><![CDATA[Mac OS X]]></category>
      <guid>N9ZVrUV6Bd39ba-yqBrvzDzE0Io=</guid>
      <description>Running Mac OS X's built-in DHCP server</description>
      <content:encoded><![CDATA[<p>It turns out that Mac OS X comes with a DHCP server built-in. There don't seem to be any good and simple instructions out there on how to use it. Or at least, there weren't any.... until now!</p>
<p>The server is called bootpd and does both DHCP and BOOTP. These instructions just describe using it for DHCP, however.</p>
<p>To start, you need to create a configuration file for the server. The file should be stored in <code>/etc/bootpd.plist</code>. Here's a sample configuration file:</p>
<div class="pygments_murphy"><pre><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br/><span class="cp">&lt;!DOCTYPE plist PUBLIC &quot;-//Apple//DTD PLIST 1.0//EN&quot; &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;</span><br/><span class="nt">&lt;plist</span> <span class="na">version=</span><span class="s">&quot;1.0&quot;</span><span class="nt">&gt;</span><br/><span class="nt">&lt;dict&gt;</span><br/>    <span class="nt">&lt;key&gt;</span>bootp_enabled<span class="nt">&lt;/key&gt;</span><br/>    <span class="nt">&lt;false/&gt;</span><br/>    <span class="nt">&lt;key&gt;</span>detect_other_dhcp_server<span class="nt">&lt;/key&gt;</span><br/>    <span class="nt">&lt;integer&gt;</span>1<span class="nt">&lt;/integer&gt;</span><br/>    <span class="nt">&lt;key&gt;</span>dhcp_enabled<span class="nt">&lt;/key&gt;</span><br/>    <span class="nt">&lt;array&gt;</span><br/>        <span class="nt">&lt;string&gt;</span>en0<span class="nt">&lt;/string&gt;</span><br/>    <span class="nt">&lt;/array&gt;</span><br/>    <span class="nt">&lt;key&gt;</span>reply_threshold_seconds<span class="nt">&lt;/key&gt;</span><br/>    <span class="nt">&lt;integer&gt;</span>0<span class="nt">&lt;/integer&gt;</span><br/>    <span class="nt">&lt;key&gt;</span>Subnets<span class="nt">&lt;/key&gt;</span><br/>    <span class="nt">&lt;array&gt;</span><br/>        <span class="nt">&lt;dict&gt;</span><br/>            <span class="nt">&lt;key&gt;</span>allocate<span class="nt">&lt;/key&gt;</span><br/>            <span class="nt">&lt;true/&gt;</span><br/>            <span class="nt">&lt;key&gt;</span>lease_max<span class="nt">&lt;/key&gt;</span><br/>            <span class="nt">&lt;integer&gt;</span>86400<span class="nt">&lt;/integer&gt;</span><br/>            <span class="nt">&lt;key&gt;</span>lease_min<span class="nt">&lt;/key&gt;</span><br/>            <span class="nt">&lt;integer&gt;</span>86400<span class="nt">&lt;/integer&gt;</span><br/>            <span class="nt">&lt;key&gt;</span>name<span class="nt">&lt;/key&gt;</span><br/>            <span class="nt">&lt;string&gt;</span>192.168.33<span class="nt">&lt;/string&gt;</span><br/>            <span class="nt">&lt;key&gt;</span>net_address<span class="nt">&lt;/key&gt;</span><br/>            <span class="nt">&lt;string&gt;</span>192.168.33.0<span class="nt">&lt;/string&gt;</span><br/>            <span class="nt">&lt;key&gt;</span>net_mask<span class="nt">&lt;/key&gt;</span><br/>            <span class="nt">&lt;string&gt;</span>255.255.255.0<span class="nt">&lt;/string&gt;</span><br/>            <span class="nt">&lt;key&gt;</span>net_range<span class="nt">&lt;/key&gt;</span><br/>            <span class="nt">&lt;array&gt;</span><br/>                <span class="nt">&lt;string&gt;</span>192.168.33.2<span class="nt">&lt;/string&gt;</span><br/>                <span class="nt">&lt;string&gt;</span>192.168.33.254<span class="nt">&lt;/string&gt;</span><br/>            <span class="nt">&lt;/array&gt;</span><br/>        <span class="nt">&lt;/dict&gt;</span><br/>    <span class="nt">&lt;/array&gt;</span><br/><span class="nt">&lt;/dict&gt;</span><br/><span class="nt">&lt;/plist&gt;</span><br/></pre></div>

<p>This file sets up the DHCP server to run on the interface named <code>en0</code>, which is typically the (non-wireless) Ethernet port. It assumes that that port has been configured with the IP address <code>192.168.33.1</code>, and dishes out addresses from <code>192.168.33.2</code> to <code>192.168.33.254</code>.</p>
<p>To get more information on editing this file, take a look at the bootpd manfile:</p>
<div class="pygments_murphy"><pre>man bootpd<br/></pre></div>

<p>To start the server, run the following command:</p>
<div class="pygments_murphy"><pre>sudo /bin/launchctl load -w /System/Library/LaunchDaemons/bootps.plist<br/></pre></div>

<p>Stopping the server is very similar:</p>
<div class="pygments_murphy"><pre>sudo /bin/launchctl unload -w /System/Library/LaunchDaemons/bootps.plist<br/></pre></div>

<p>If you want to create static assignments, so that a given device always has the same IP address, you need to create a file called <code>/etc/bootptab</code>. There's a small sample of the file below. For more information, just do <code>man bootptab</code></p>
<div class="pygments_murphy"><pre>%%<br/># machine entries have the following format:<br/>#<br/># hostname      hwtype  hwaddr              ipaddr          bootfile<br/>client1         1       00:01:02:03:04:05   10.0.0.20<br/>client2         1       00:a0:b2:ef:ff:0a   10.0.0.20<br/></pre></div>

<p>Make sure to include the <code>%%</code> at the top of the file. It's safe to leave the bootfile field empty because we're just using bootpd as a DHCP server, not a bootp server.</p>]]></content:encoded>
    </item>
    <item>
      <title>In Defense of the C99 Boolean Type</title>
      <link>http://www.jacquesf.com/2011/04/in-defense-of-the-c99-boolean-type</link>
      <pubDate>Sat, 09 Apr 2011 00:00:00 EDT</pubDate>
      <category><![CDATA[Embedded Software]]></category>
      <guid>xpLfbeC2F96Jmix1MB159dYWh3g=</guid>
      <description>In Defense of the C99 Boolean Type</description>
      <content:encoded><![CDATA[<p>I really like a lot of the features in C99, including the new <code>bool</code> type. A lot of people have
dismissed it as syntactic sugar or language bloat, but it does bring something new to the table.</p>
<p>First, how do you use <code>bool</code>? The best way is to include the <code>stdbool.h</code> header, which defines the
bool type and the constants true and false as follows:</p>
<div class="pygments_murphy"><pre><span class="cp">#define bool    _Bool</span><br/><span class="cp">#define true    1</span><br/><span class="cp">#define false   0</span><br/></pre></div>

<p>To maintain compatibility, the actual new keyword added to the language was <code>_Bool</code>. <code>stdbool.h</code>
then defines a macro that allows you to use the slightly friendlier <code>bool</code> type. That way
code that defines its own, incompatible <code>bool</code> type can continue to work by just not including <code>stdbool.h</code>.</p>
<p>So far, there's nothing special here. The real excitement is buried in the ISO C99 standard. Here's
section 6.3.1.2 of the n1256 ISO committee draft (yes, I'm too cheap to shell out $40 for the offical
version of the standard).</p>
<blockquote>
<p>When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1</p>
</blockquote>
<p>Exciting, isn't it? Before the days of C99, programmers would typically represent boolean values
as an <code>int</code>. That would cause all kinds of problems such as:</p>
<div class="pygments_murphy"><pre><span class="k">if</span><span class="p">(</span> <span class="n">somevalue</span> <span class="o">==</span> <span class="n">TRUE</span> <span class="p">)</span><br/><span class="p">{</span><br/>    <span class="cm">/* do something */</span><br/><span class="p">}</span><br/></pre></div>

<p>This would cause unexpected behavior because <code>TRUE</code> would typically be defined to be
1, but somevalue wasn't guaranteed to just be 0 or 1. Now,
if somevalue is a C99 <code>bool</code>, there is that guarantee. Of course, the code above is still
dumb because it's excessively verbose.</p>
<p>Another problem was packing "boolean" values into flags. To get around the problem of
"booleans" not being limited to 0 and 1, you would end up writing code like</p>
<div class="pygments_murphy"><pre><span class="n">flags</span> <span class="o">=</span> <span class="p">(</span><span class="n">condition1</span> <span class="o">?</span> <span class="mi">1</span> <span class="o">:</span> <span class="mi">0</span> <span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="mi">1</span> <span class="o">|</span> <span class="p">(</span><span class="n">condition2</span> <span class="o">?</span> <span class="mi">1</span> <span class="o">:</span> <span class="mi">0</span> <span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="mi">2</span> <span class="o">|</span> <span class="p">(</span><span class="n">condition3</span> <span class="o">?</span> <span class="mi">1</span> <span class="o">:</span> <span class="mi">0</span> <span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="mi">3</span><span class="p">;</span><br/></pre></div>

<p>Now, if the three condition variables above all have type <code>bool</code>, you can make this much more compact:</p>
<div class="pygments_murphy"><pre><span class="n">flags</span> <span class="o">=</span> <span class="p">(</span><span class="n">condition1</span> <span class="o">&lt;&lt;</span> <span class="mi">1</span><span class="p">)</span> <span class="o">|</span> <span class="p">(</span><span class="n">condition2</span> <span class="o">&lt;&lt;</span> <span class="mi">2</span><span class="p">)</span> <span class="o">|</span> <span class="p">(</span><span class="n">condition3</span>  <span class="o">&lt;&lt;</span> <span class="mi">3</span><span class="p">);</span><br/></pre></div>

<p>So, does this actually work? Let's give it a whirl! Here's a silly little test function.</p>
<div class="pygments_murphy"><pre><span class="cp">#include &lt;stdio.h&gt;</span><br/><span class="cp">#include &lt;stdbool.h&gt;</span><br/><br/><span class="n">bool</span> <span class="nf">test</span><span class="p">(</span> <span class="kt">void</span> <span class="p">)</span><br/><span class="p">{</span><br/>        <span class="k">return</span> <span class="n">printf</span><span class="p">(</span> <span class="s">&quot;blah&quot;</span> <span class="p">);</span><br/><span class="p">}</span><br/></pre></div>

<p>Here's the disassembly of that function (compiled for ARM, because I hate reading x86 assembly
language). Notice addresses 10 and 14. Address 10 subtracts 0 from r0 and stores the result in
r0 (on ARM r0 holds the return value from a function, which is <code>printf</code> in this case). That's setting
up a comparison to 0 for address 14, which sets r0 to 1 if the return value from <code>printf</code> was not
zero. If the return value from <code>printf</code> was 0, then r0 already has 0 in it, so there's nothing to do
(clever optimizer!). Of course, r0 then becomes the return value from the <code>test</code> function,
so this does exactly what we expect: It returns 0 if <code>printf</code> returned a value of 0, and 1 otherwise.</p>
<div class="pygments_murphy"><pre> 0:   e52de004    push    {lr}        ; (str lr, [sp, #-4]!)<br/> 4:   e59f0018    ldr r0, [pc, #24]   ; 24 &lt;test+0x24&gt;<br/> 8:   e24dd004    sub sp, sp, #4  ; 0x4<br/> c:   ebfffffe    bl  0 &lt;printf&gt;<br/>10:   e2500000    subs    r0, r0, #0  ; 0x0<br/>14:   13a00001    movne   r0, #1  ; 0x1<br/>18:   e28dd004    add sp, sp, #4  ; 0x4<br/>1c:   e49de004    pop {lr}        ; (ldr lr, [sp], #4)<br/>20:   e12fff1e    bx  lr<br/></pre></div>

<p>And there you have it. The <code>bool</code> type isn't the most ground-breaking feature in C,
but it isn't completely useless either.</p>]]></content:encoded>
    </item>
    <item>
      <title>Consistent Overhead Byte Stuffing</title>
      <link>http://www.jacquesf.com/2011/03/consistent-overhead-byte-stuffing</link>
      <pubDate>Sat, 26 Mar 2011 00:00:00 EDT</pubDate>
      <category><![CDATA[Embedded Software]]></category>
      <guid>ciIHPnD0H4E94t2wPzxQPrZzK88=</guid>
      <description>Consistent Overhead Byte Stuffing</description>
      <content:encoded><![CDATA[<p>Framing packets transmitted on a UART serial link (such as RS-232 or RS-485) is a common problem in embedded programming. The typical approach is to put framing characters at the beginning and end of the packet. If a framing character occurs in the packet, it is escaped using a technique called byte stuffing. This sucks for a few reasons, but there's a better way. The recently developed algorithm called <a href="http://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing">Consistent Overhead Byte Stuffing</a> is a great solution that should be in every embedded software engineer's toolbox.</p>
<p>First, a bit of background. I'm working on a project where I have to transmit data between two microcontrollers using <a href="http://en.wikipedia.org/wiki/RS-422">RS-422</a>. RS-422 is a differential signalling standard that is often used in place of <a href="http://en.wikipedia.org/wiki/RS-232">RS-232</a> to connect two UARTs when electrical isolation and/or noise immunity are required.</p>
<p>The question came up, how do we assemble packets on the transmitter and detect packet boundaries on he receiver? Our packets all have the same size and can contain arbitrary binary data. My first thought was to use idle line detection. If our UART detects enough bit times without a start bit, it can give us an idle line interrupt. Because we're feeding the transmitting UART with DMA, we can guarantee that there are no gaps in the middle of the packet that would cause a false idle line to be detected.</p>
<p>My boss pointed out that I was relying on an implementation detail that might not be valid if we ever moved to a really cheap microcontroller. He encouraged me to come up with a solution that put the framing into the packet itself.</p>
<p>The traditional way to do this is with byte stuffing. You designate a character (such as STX, ASCII 0x02) as the start of frame character and another (often ETX, ASCII 0x03) as the end of frame character. If there's a possibility that ETX or STX will occur in your data, you designate a third character as the escape character (ESC, ASCII 0x1F, for example). STX, ETX, and ESC characters in the data are replaced by an ESC followed by mangled version of he character (the character XORed with 0x20 is a popular choice).</p>
<p>There are two problems with this system:</p>
<ul>
<li>
<p>It can add a lot of overhead. In the worst case, the encoded data could be twice the size of the original data. Unless you can be sure this won't happen, you have to design your buffers and bandwidth to handle this worst case.</p>
</li>
<li>
<p>The amount of overhead is variable. If you want to use DMA or FIFO buffers to send and receive your data, dealing with variable length data can be annoying. For example, you can't reliably request an interrupt after a frame's worth of data has been received. When you're transmitting at multiple megabits per second, you really don't want to check for a complete frame after each character is received.</p>
</li>
</ul>
<p>Returning to the story above, I was really annoyed. My boss had a good point, but byte stuffing is just a pain. I did a bunch of googling, and finally came across <a href="http://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing">COBS</a>. It has the following great properties:</p>
<ul>
<li>
<p>It removes all zero bytes from the original message, allowing the zero byte to be used as a frame delimiter.</p>
</li>
<li>
<p>The worst-case overhead is just 1 byte for every 254 bytes of data, or 0.39%.</p>
</li>
<li>
<p>For messages smaller than 254 bytes, the overhead is constant (exactly 1 byte).</p>
</li>
<li>
<p>The encoding process is fairly efficient (no complicated rearranging of input data across multiple bytes).</p>
</li>
</ul>
<p>There's <a href="http://en.m.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing#section_3">sample code</a> on the Wikipedia COBS page. I've improved the Wikipedia code in a few ways:</p>
<ul>
<li>
<p>My encoder and decoder return the length of the output data, because that size varies depending on the input data.</p>
</li>
<li>
<p>My decoder checks the validity of its input data to protect against corrupted or malicious data. The original Wikipedia code would cause a buffer overflow if an overly high block length code occurred at the end of the input data.</p>
</li>
<li>
<p>My decoder doesn't put the imaginary zero byte at the end of its output.</p>
</li>
<li>
<p>My encoder and decoder use modern C99. For example, specifying the restrict qualifier on the input and output pointers means the code can be more heavily optimized (restrict is a promise that the two buffers do not overlap, which helps the optimizer eliminate unneeded loads and stores).</p>
</li>
</ul>
<p>COBS resources:</p>
<ul>
<li>
<p><a href="https://github.com/jacquesf/COBS-Consistent-Overhead-Byte-Stuffing">My C COBS implementation on GitHub</a> (or see below)</p>
</li>
<li>
<p><a href="http://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing">COBS wikipedia page</a></p>
</li>
<li>
<p><a href="http://www.stuartcheshire.org/papers/COBSforToN.pdf">COBS academic paper</a></p>
</li>
<li>
<p><a href="http://pypi.python.org/pypi/cobs/1.0.0">COBS Python implemenatation</a></p>
</li>
</ul>
<p>Here's my C implementation of COBS:</p>
<div class="pygments_murphy"><pre><span class="cm">/* Copyright 2011, Jacques Fortier. All rights reserved.</span><br/><span class="cm"> *</span><br/><span class="cm"> * Redistribution and use in source and binary forms are permitted, with or without modification.</span><br/><span class="cm"> */</span><br/><span class="cp">#include &lt;stdint.h&gt;</span><br/><span class="cp">#include &lt;stddef.h&gt;</span><br/><br/><span class="cm">/* Stuffs &quot;length&quot; bytes of data at the location pointed to by</span><br/><span class="cm"> * &quot;input&quot;, writing the output to the location pointed to by</span><br/><span class="cm"> * &quot;output&quot;. Returns the number of bytes written to &quot;output&quot;.</span><br/><span class="cm"> *</span><br/><span class="cm"> * Remove the &quot;restrict&quot; qualifiers if compiling with a</span><br/><span class="cm"> * pre-C99 C dialect.</span><br/><span class="cm"> */</span><br/><span class="kt">size_t</span> <span class="nf">cobs_encode</span><span class="p">(</span><span class="k">const</span> <span class="kt">uint8_t</span> <span class="o">*</span> <span class="kr">restrict</span> <span class="n">input</span><span class="p">,</span> <span class="kt">size_t</span> <span class="n">length</span><span class="p">,</span> <span class="kt">uint8_t</span> <span class="o">*</span> <span class="kr">restrict</span> <span class="n">output</span><span class="p">)</span><br/><span class="p">{</span><br/>    <span class="kt">size_t</span> <span class="n">read_index</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span><br/>    <span class="kt">size_t</span> <span class="n">write_index</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span><br/>    <span class="kt">size_t</span> <span class="n">code_index</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span><br/>    <span class="kt">uint8_t</span> <span class="n">code</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span><br/><br/>    <span class="k">while</span><span class="p">(</span><span class="n">read_index</span> <span class="o">&lt;</span> <span class="n">length</span><span class="p">)</span><br/>    <span class="p">{</span><br/>        <span class="k">if</span><span class="p">(</span><span class="n">input</span><span class="p">[</span><span class="n">read_index</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span><br/>        <span class="p">{</span><br/>            <span class="n">output</span><span class="p">[</span><span class="n">code_index</span><span class="p">]</span> <span class="o">=</span> <span class="n">code</span><span class="p">;</span><br/>            <span class="n">code</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span><br/>            <span class="n">code_index</span> <span class="o">=</span> <span class="n">write_index</span><span class="o">++</span><span class="p">;</span><br/>            <span class="n">read_index</span><span class="o">++</span><span class="p">;</span><br/>        <span class="p">}</span><br/>        <span class="k">else</span><br/>        <span class="p">{</span><br/>            <span class="n">output</span><span class="p">[</span><span class="n">write_index</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="n">input</span><span class="p">[</span><span class="n">read_index</span><span class="o">++</span><span class="p">];</span><br/>            <span class="n">code</span><span class="o">++</span><span class="p">;</span><br/>            <span class="k">if</span><span class="p">(</span><span class="n">code</span> <span class="o">==</span> <span class="mh">0xFF</span><span class="p">)</span><br/>            <span class="p">{</span><br/>                <span class="n">output</span><span class="p">[</span><span class="n">code_index</span><span class="p">]</span> <span class="o">=</span> <span class="n">code</span><span class="p">;</span><br/>                <span class="n">code</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span><br/>                <span class="n">code_index</span> <span class="o">=</span> <span class="n">write_index</span><span class="o">++</span><span class="p">;</span><br/>            <span class="p">}</span><br/>        <span class="p">}</span><br/>    <span class="p">}</span><br/><br/>    <span class="n">output</span><span class="p">[</span><span class="n">code_index</span><span class="p">]</span> <span class="o">=</span> <span class="n">code</span><span class="p">;</span><br/><br/>    <span class="k">return</span> <span class="n">write_index</span><span class="p">;</span><br/><span class="p">}</span><br/><br/><span class="cm">/* Unstuffs &quot;length&quot; bytes of data at the location pointed to by</span><br/><span class="cm"> * &quot;input&quot;, writing the output * to the location pointed to by</span><br/><span class="cm"> * &quot;output&quot;. Returns the number of bytes written to &quot;output&quot; if</span><br/><span class="cm"> * &quot;input&quot; was successfully unstuffed, and 0 if there was an</span><br/><span class="cm"> * error unstuffing &quot;input&quot;.</span><br/><span class="cm"> *</span><br/><span class="cm"> * Remove the &quot;restrict&quot; qualifiers if compiling with a</span><br/><span class="cm"> * pre-C99 C dialect.</span><br/><span class="cm"> */</span><br/><span class="kt">size_t</span> <span class="nf">cobs_decode</span><span class="p">(</span><span class="k">const</span> <span class="kt">uint8_t</span> <span class="o">*</span> <span class="kr">restrict</span> <span class="n">input</span><span class="p">,</span> <span class="kt">size_t</span> <span class="n">length</span><span class="p">,</span> <span class="kt">uint8_t</span> <span class="o">*</span> <span class="kr">restrict</span> <span class="n">output</span><span class="p">)</span><br/><span class="p">{</span><br/>    <span class="kt">size_t</span> <span class="n">read_index</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span><br/>    <span class="kt">size_t</span> <span class="n">write_index</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span><br/>    <span class="kt">uint8_t</span> <span class="n">code</span><span class="p">;</span><br/>    <span class="kt">uint8_t</span> <span class="n">i</span><span class="p">;</span><br/><br/>    <span class="k">while</span><span class="p">(</span><span class="n">read_index</span> <span class="o">&lt;</span> <span class="n">length</span><span class="p">)</span><br/>    <span class="p">{</span><br/>        <span class="n">code</span> <span class="o">=</span> <span class="n">input</span><span class="p">[</span><span class="n">read_index</span><span class="p">];</span><br/><br/>        <span class="k">if</span><span class="p">(</span><span class="n">read_index</span> <span class="o">+</span> <span class="n">code</span> <span class="o">&gt;</span> <span class="n">length</span> <span class="o">&amp;&amp;</span> <span class="n">code</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">)</span><br/>        <span class="p">{</span><br/>            <span class="k">return</span> <span class="mi">0</span><span class="p">;</span><br/>        <span class="p">}</span><br/><br/>        <span class="n">read_index</span><span class="o">++</span><span class="p">;</span><br/><br/>        <span class="k">for</span><span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">code</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span><br/>        <span class="p">{</span><br/>            <span class="n">output</span><span class="p">[</span><span class="n">write_index</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="n">input</span><span class="p">[</span><span class="n">read_index</span><span class="o">++</span><span class="p">];</span><br/>        <span class="p">}</span><br/>        <span class="k">if</span><span class="p">(</span><span class="n">code</span> <span class="o">!=</span> <span class="mh">0xFF</span> <span class="o">&amp;&amp;</span> <span class="n">read_index</span> <span class="o">!=</span> <span class="n">length</span><span class="p">)</span><br/>        <span class="p">{</span><br/>            <span class="n">output</span><span class="p">[</span><span class="n">write_index</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="sc">&#39;\0&#39;</span><span class="p">;</span><br/>        <span class="p">}</span><br/>    <span class="p">}</span><br/><br/>    <span class="k">return</span> <span class="n">write_index</span><span class="p">;</span><br/><span class="p">}</span><br/></pre></div>]]></content:encoded>
    </item>
    <item>
      <title>Creating Excerpts in Jekyll with Wordpress-style &lt;!--more--&gt; HTML Comments</title>
      <link>http://www.jacquesf.com/2011/03/creating-excerpts-in-jekyll-with-wordpress-style-more-html-comments</link>
      <pubDate>Sun, 06 Mar 2011 00:00:00 EST</pubDate>
      <category><![CDATA[Web Development]]></category>
      <guid>http://www.jacquesf.com/2011/03/creating-excerpts-in-jekyll-with-wordpress-style-more-html-comments</guid>
      <description>Creating Excerpts in Jekyll with Wordpress-style &lt;!--more--&gt; HTML Comments</description>
      <content:encoded><![CDATA[<p>I just finished converting my blog to <a href="http://jekyllrb.com">Jekyll</a>. If you've never heard of Jekyll, it's a
static blog publishing tool.
Unlike Wordpress, where the HTML for your blog is generated on-the-fly by your server every time someone accesses your site,
Jekyll is meant to run on your PC. You write your posts in a simple text format (often <a href="http://daringfireball.net/projects/markdown/">Markdown</a>), run Jekyll on them, and then
upload the posts to your server. The nice thing is that your server dosn't need any PHP or Python or Ruby or anything.
And, if your website gets a ton of traffic, there's no database to crash. In fact, since you don't need any scripting
languages or a database, you can just throw everything on <a href="https://s3.amazonaws.com/">Amazon S3</a> and be guaranteed that your site will never crash.</p>
<p>The downside of Jekyll is that its features are kind of minimalistic. That minimalism is nice because it means
there's less to learn. That minimalism also results in lots of blog posts on the theme of "I switched to Jekyll and
it's awesome now that I've hacked it up to implement this Wordpress feature I couldn't live without." On that note,
here's how you can put a list of post excerpts on your Jekyll blog's front page and archive pages, with the "fold"
marked with a <code>&lt;!--more--&gt;</code> HTML comment just like in Wordpress.</p>
<!--more-->

<p>There are lots of pages out there showing how to do similar things by forking Jekyll. But, with the newish Jekyll plugin
system, there's no need to mess with the Jekyll core. Instead, just add a plugin that adds a new Liquid filter. Here's
postmore.rb, which should go in the _plugins directory in the root of your Jekyll site:</p>


<div class="pygments_murphy"><pre><span class="k">module</span> <span class="nn">PostMore</span>
  <span class="k">def</span> <span class="nf">postmorefilter</span><span class="p">(</span><span class="n">input</span><span class="p">,</span> <span class="n">url</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">input</span><span class="o">.</span><span class="n">include?</span> <span class="s2">&quot;&lt;!--more--&gt;&quot;</span>
      <span class="n">input</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;&lt;!--more--&gt;&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">first</span> <span class="o">+</span> <span class="s2">&quot;&lt;p class=&#39;more&#39;&gt;&lt;a href=&#39;</span><span class="si">#{</span><span class="n">url</span><span class="si">}</span><span class="s2">&#39;&gt;</span><span class="si">#{</span><span class="n">text</span><span class="si">}</span><span class="s2">&lt;/a&gt;&lt;/p&gt;&quot;</span>
    <span class="k">else</span>
      <span class="n">input</span>
    <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="no">Liquid</span><span class="o">::</span><span class="no">Template</span><span class="o">.</span><span class="n">register_filter</span><span class="p">(</span><span class="no">PostMore</span><span class="p">)</span>
</pre></div>



<p>This creates and registers a filter called <code>postmorefilter</code>. It takes two arguments: the URL of the main post
body page, and the text to use on the link to the page. To use it, you just apply it to the post's contents. If the post
containes the <code>&lt;!--more--&gt;</code> fold marker, then the text up to the marker will be output, followed by a
link to the page with the full post. If there is no marker, then the entire post is output without a link. For example:</p>


<div class="pygments_murphy"><pre>{{ page.content | postmorefilter: page.url, &quot;Read the rest of this entry&quot; }}
</pre></div>



<p>The next step is to modify your index.html to output your posts. I wanted to put the most recent few posts on my blog's
front page, and then have archive pages with older posts. To do that, I had to turn on Jekyll's pagination feature by
setting <code>paginate: 6</code> in my _config.yml (where 6 is the number of posts per page). Then, I had to modify my
index.html to use the paginator. Note that if you want to do this in another file, you need to set the <code>paginate_file</code>
setting in your _config.yml. Here's what my index.html looks like:</p>


<div class="pygments_murphy"><pre>---
layout: twocolumn
title: Jacques Fortier
---

<span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;home&quot;</span><span class="nt">&gt;</span>
  <span class="c">&lt;!-- iterate through the posts on this page --&gt;</span>
  {% for page in paginator.posts %}
    <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;post&quot;</span><span class="nt">&gt;</span>
      <span class="nt">&lt;h1&gt;&lt;a</span> <span class="na">href=</span><span class="s">&quot;{{ page.url }}&quot;</span><span class="nt">&gt;</span>{{ page.title }}<span class="nt">&lt;/a&gt;&lt;/h1&gt;</span>
      <span class="nt">&lt;p</span> <span class="na">class=</span><span class="s">&quot;meta&quot;</span><span class="nt">&gt;</span>{{ page.date | date: &quot;%B %e, %Y&quot; }}<span class="nt">&lt;/p&gt;</span>
        {{ page.content | postmorefilter: page.url, &quot;Read the rest of this entry&quot; }}
    <span class="nt">&lt;/div&gt;</span>
  {% endfor %}

  <span class="c">&lt;!-- links to prev and next pages for browsing thru archives --&gt;</span>
  {% if paginator.previous_page == 1 %}
    <span class="nt">&lt;span</span> <span class="na">id=</span><span class="s">&quot;newer&quot;</span><span class="nt">&gt;&lt;a</span> <span class="na">href=</span><span class="s">&quot;/&quot;</span><span class="nt">&gt;</span><span class="ni">&amp;laquo;&amp;laquo;</span> Newer Entries<span class="nt">&lt;/a&gt;&lt;/span&gt;</span>
  {% elsif paginator.previous_page %}
    <span class="nt">&lt;span</span> <span class="na">id=</span><span class="s">&quot;newer&quot;</span><span class="nt">&gt;&lt;a</span> <span class="na">href=</span><span class="s">&quot;/page{{ paginator.previous_page }}&quot;</span><span class="nt">&gt;</span><span class="ni">&amp;laquo;&amp;laquo;</span> Newer Entries<span class="nt">&lt;/a&gt;&lt;/span&gt;</span>
  {% endif %}
  {% if paginator.next_page %}
    <span class="nt">&lt;span</span> <span class="na">id=</span><span class="s">&quot;older&quot;</span><span class="nt">&gt;&lt;a</span> <span class="na">href=</span><span class="s">&quot;/page{{ paginator.next_page }}&quot;</span><span class="nt">&gt;</span>Older Entries <span class="ni">&amp;raquo;&amp;raquo;</span><span class="nt">&lt;/a&gt;&lt;/span&gt;</span>
  {% endif %}

<span class="nt">&lt;/div&gt;</span>
</pre></div>



<p>Of course, you would set the title and layout in the YAML front-matter to whatever you're using on your blog.</p>
<p>To summarize:</p>
<ul>
<li>Put this in your _plugins directory: <a href="/downloads/postmore.rb">postmore.rb</a></li>
<li>Use this as your index.html: <a href="/downloads/index.html.txt">index.html.txt</a></li>
</ul>]]></content:encoded>
    </item>
    <item>
      <title>Adding Titles to iPad UIToolbars</title>
      <link>http://www.jacquesf.com/2011/01/adding-titles-to-ipad-uitoolbars</link>
      <pubDate>Sun, 09 Jan 2011 00:00:00 EST</pubDate>
      <category><![CDATA[iOS Software]]></category>
      <guid>http://www.jacquesf.com/?p=190</guid>
      <description>Adding Titles to iPad UIToolbars</description>
      <content:encoded><![CDATA[<p>I recently had to figure out how to add a title to a UIToolbar at the top of an iPad view. The toolbar was in the “silver” style and the idea was to make the title look like the titles used in navigation controllers. If I just had one button on each side of the title, I could have used a navigation bar. In this case, I needed to have a bunch of buttons in the toolbar, so a UINavigationBar couldn’t work.</p>
<p>The concept is pretty simple: just add a UIBarButtonItem with a label as the custom view. The trickier part is getting everything matched up, pixel-perfect, with the stock controls. After a bunch of fiddling around with <a href="http://iconfactory.com/software/xscope">xScope</a>, here’s what I came with.</p>
<!--more-->

<p>Start by creating a label that’s 23 pixels tall, and wide enough to hold your text. Set its font to <code>boldSystemFontOfSize:20</code> and set the text alignment to <code>UITextAlignmentCentered</code>.</p>
<p>Figuring out the colors was a bit tricky. For an iPad silver-style toolbar (UIBarStyleDefault with no tintColor applied), fiddling around with xScope gave me a text color in HTML #RRGGBB format of #717880 (just divide those values by 255.0 to get the color components expected by UIColor). The shadow color is #e6e7eb. The shadowOffset should be (0, 1), which puts the shadow 1 pixel above the text.</p>
<p>The last detail was getting the label to line up vertically as expected. If you just put the label directly in a UIBarButtonItem it’s close. In fact, it’s just one pixel lower than the title in UINavigationBar. To make it match up perfectly, I ended up putting the label into a container UIView that’s one pixel taller than the label.</p>
<p>And that’s all there is to it. I’ve put this code into a category of UILabel (freely licensed under the <a href="http://sam.zoy.org/wtfpl/">WTFPL</a>). You can use the category like this:</p>
<div class="pygments_murphy"><pre><span class="n">UIBarButtonItem</span> <span class="o">*</span><span class="n">title</span> <span class="o">=</span> <span class="p">[[</span><span class="n">UIBarButtonItem</span> <span class="n">alloc</span><span class="p">]</span> <span class="nl">initWithTitle:</span><span class="s">@&quot;Title&quot;</span> <span class="nl">width:</span><span class="mi">120</span><span class="p">];</span><br/><span class="c1">// Grab the label if you want to change the title later on</span><br/><span class="n">UILabel</span> <span class="o">*</span><span class="n">titleLabel</span> <span class="o">=</span> <span class="p">[</span><span class="n">title</span><span class="p">.</span><span class="n">customView</span><span class="p">.</span><span class="n">subviews</span> <span class="n">lastObject</span><span class="p">];</span><br/></pre></div>

<p>The code is below, or you can grab it from GitHub: <a href="http://gist.github.com/772504">UIBarButtonItem to add title to UIToolbar on iPad</a></p>
<div class="pygments_murphy"><pre><span class="c1">//  UIBarButtonItem+JFAdditions.m</span><br/><span class="c1">//</span><br/><span class="c1">//  Created by Jacques Fortier on 1/10/11.</span><br/><span class="c1">//  Copyright 2011 Jacques Fortier.</span><br/><span class="c1">//  Released under the WTFPL (see LICENSE or http://sam.zoy.org/wtfpl/COPYING)</span><br/><br/><span class="k">@interface</span> <span class="nc">UIBarButtonItem</span><span class="nl">(JFAdditions)</span><br/><br/><span class="o">-</span> <span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nl">initWithTitle:</span><span class="p">(</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span><span class="n">title</span> <span class="nl">width:</span><span class="p">(</span><span class="n">CGFloat</span><span class="p">)</span><span class="n">width</span><span class="p">;</span><br/><br/><span class="k">@end</span><br/><br/><span class="k">@implementation</span> <span class="nc">UIBarButtonItem</span><span class="nl">(JFAdditions)</span><br/><br/><span class="o">-</span> <span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nl">initWithTitle:</span><span class="p">(</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span><span class="n">title</span> <span class="nl">width:</span><span class="p">(</span><span class="n">CGFloat</span><span class="p">)</span><span class="n">width</span> <span class="p">{</span><br/>    <span class="n">UILabel</span> <span class="o">*</span><span class="n">label</span> <span class="o">=</span> <span class="p">[[</span><span class="n">UILabel</span> <span class="n">alloc</span><span class="p">]</span> <span class="nl">initWithFrame:</span><span class="n">CGRectMake</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="mi">23</span><span class="p">)];</span><br/>    <span class="n">label</span><span class="p">.</span><span class="n">text</span> <span class="o">=</span> <span class="n">title</span><span class="p">;</span><br/>    <span class="n">label</span><span class="p">.</span><span class="n">backgroundColor</span> <span class="o">=</span> <span class="p">[</span><span class="n">UIColor</span> <span class="n">clearColor</span><span class="p">];</span><br/>    <span class="n">label</span><span class="p">.</span><span class="n">opaque</span> <span class="o">=</span> <span class="n">NO</span><span class="p">;</span><br/>    <span class="n">label</span><span class="p">.</span><span class="n">textColor</span> <span class="o">=</span> <span class="p">[</span><span class="n">UIColor</span> <span class="nl">colorWithRed:</span><span class="mh">0x71</span><span class="o">/</span><span class="mf">255.0</span> <span class="nl">green:</span><span class="mh">0x78</span><span class="o">/</span><span class="mf">255.0</span> <span class="nl">blue:</span><span class="mh">0x80</span><span class="o">/</span><span class="mf">255.0</span> <span class="nl">alpha:</span><span class="mf">1.0</span><span class="p">];</span><br/>    <span class="n">label</span><span class="p">.</span><span class="n">shadowOffset</span> <span class="o">=</span> <span class="n">CGSizeMake</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span><br/>    <span class="n">label</span><span class="p">.</span><span class="n">shadowColor</span> <span class="o">=</span> <span class="p">[</span><span class="n">UIColor</span> <span class="nl">colorWithRed:</span><span class="mh">0xe6</span><span class="o">/</span><span class="mf">255.0</span> <span class="nl">green:</span><span class="mh">0xe7</span><span class="o">/</span><span class="mf">255.0</span> <span class="nl">blue:</span><span class="mh">0xeb</span><span class="o">/</span><span class="mf">255.0</span> <span class="nl">alpha:</span><span class="mf">1.0</span><span class="p">];</span><br/>    <span class="n">label</span><span class="p">.</span><span class="n">textAlignment</span> <span class="o">=</span> <span class="n">UITextAlignmentCenter</span><span class="p">;</span><br/>    <span class="n">label</span><span class="p">.</span><span class="n">font</span> <span class="o">=</span> <span class="p">[</span><span class="n">UIFont</span> <span class="nl">boldSystemFontOfSize:</span><span class="mf">20.0</span><span class="p">];</span><br/>    <span class="n">UIView</span> <span class="o">*</span><span class="n">labelContainer</span> <span class="o">=</span> <span class="p">[[</span><span class="n">UIView</span> <span class="n">alloc</span><span class="p">]</span> <span class="nl">initWithFrame:</span><span class="n">CGRectMake</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="mi">24</span><span class="p">)];</span><br/>    <span class="p">[</span><span class="n">labelContainer</span> <span class="nl">addSubview:</span><span class="n">label</span><span class="p">];</span><br/><br/>    <span class="n">self</span> <span class="o">=</span> <span class="p">[</span><span class="n">self</span> <span class="nl">initWithCustomView:</span><span class="n">labelContainer</span><span class="p">];</span><br/><br/>    <span class="p">[</span><span class="n">label</span> <span class="n">release</span><span class="p">];</span><br/>    <span class="p">[</span><span class="n">labelContainer</span> <span class="n">release</span><span class="p">];</span><br/><br/>    <span class="k">return</span> <span class="n">self</span><span class="p">;</span><br/><span class="p">}</span><br/><br/><span class="k">@end</span><br/></pre></div>]]></content:encoded>
    </item>
    <item>
      <title>Omega HH506RA Serial Protocol</title>
      <link>http://www.jacquesf.com/2010/03/omega-hh506ra-serial-protocol</link>
      <pubDate>Mon, 22 Mar 2010 00:00:00 EDT</pubDate>
      <category><![CDATA[Embedded Software]]></category>
      <guid>http://www.jacquesf.com/?p=187</guid>
      <description>Omega HH506RA Serial Protocol</description>
      <content:encoded><![CDATA[<p>Today I had to figure out how to read temperature from an Omega HH506RA thermocouple signal conditioner/datalogger. The HH506RA is a nice little unit that reads temperature from two thermocouples (type K, J, N, T, R, S, or E). It can display the temperature on its LCD screen and communicate them to a PC over RS232 or USB. The PC connection is optically isolated, which is very nice because RS232 and USB are both notorious for causing noise problems via ground loops.</p>
<p>I've put information on how to communicate with the unit up here for future reference.</p>
<!--more-->

<p>I found some information <a href="http://forums.ni.com/ni/board/message?board.id=140&amp;thread.id=25547">on an NI forum</a> and on <a href="http://www.mathworks.com/matlabcentral/fileexchange/11739-omega-hh506ra-read">Matlab's file exchange site.</a>. The info on the NI site is a bit hard to read and not quite correct, so here's a quick and clear rundown.</p>
<p><b>Serial Port Setup</b></p>
<p>If you're using the USB cable, its actually just a virtual serial port, which will likely show up on your PC as COM1 to COM4. If you're using the RS232 cable, then obviously you need a serial port on the PC. Either way, you need to set the serial port up for 2400 baud, 7 data bits, even parity, 1 stop bit, and no flow control.</p>
<p><b>Communication Overview</b></p>
<p>To read the temperatures from the unit, send this string over the serial port: <code>#001Nrn</code>. <code>r</code> means the carriage return character (ASCII 13). <code>n</code> means the newline character (ASCII 10). You'll get back a string that follows this pattern: <code>&nbsp;TTTTt TTTTtABrn</code>. The first and second <code>&nbsp;TTTTt</code> groups represent the temperature and thermocouple type for the first and second thermocouple. The <code>AB</code> section is some miscellaneous information.</p>
<p><b>Temperature</b></p>
<p>The <code>&nbsp;TTTT</code> part will  be a hexadecimal string representing the temperature in tenths of a degree Celcius. For example, <code>&nbsp;017A</code> represents 37.8 degrees Celcius. If the number is negative, it will start with a minus sign instead of a space. For example <code>-00C2</code> means -19.4 degrees C.</p>
<p><b>Thermocouple Type</b></p>
<p>The <code>t</code> part represents the thermocouple type (set using the buttons on the HH506RA). The codes go as follows:</p>
<table>
<tbody>
<tr>
<th>Code</th>
<th>Type</th>
</tr>
<tr>
<td>0</td>
<td>K</td>
</tr>
<tr>
<td>1</td>
<td>J</td>
</tr>
<tr>
<td>2</td>
<td>T</td>
</tr>
<tr>
<td>3</td>
<td>E</td>
</tr>
<tr>
<td>4</td>
<td>N</td>
</tr>
<tr>
<td>5</td>
<td>R</td>
</tr>
<tr>
<td>6</td>
<td>S</td>
</tr>
</tbody>
</table>

<p><b>Miscellaneous Info</b></p>
<p>According to the document on the NI site, the <code>A</code> digit above represents Celcius/Fahrenheit while the <code>B</code> character represents battery. I've only ever seen them as 0 in my testing.</p>
<p><b>Example</b></p>
<p>Bringing it all together:<br />
You send: <code>#001Nrn</code><br />
You receive: <code>-00B20 02C1200rn</code><br />
Decoding: The first thermocouple is a type K that is reading -17.8 degrees C. The second thermocouple is a type T reading 70.5 degrees C.</p>
<p><b>Errors</b></p>
<p>If you send a bad command, you'll get a a response of <code>Errrn</code>. I've found that to be a handy way to resynchronize communication in case something goes wrong. Just send <code>rn</code> and read for a response until you see <code>Errrn</code>. Once you get that response, you know the unit is ready to accept a command again.</p>
<p><b>Other Commands</b></p>
<p>The document on the NI forum has some additional details. For example, you can read the unit's ID with the command <code>%IDRrn</code>. The ID is the set of digits you send in the read temperature command <code>#001Nrn</code>. You can change the unit's ID so that it responds to <code>#005Nrn</code> by sending a command along the lines of <code>%001I005rn</code>. I don't really see the point of this, so I haven't bothered testing it. The unit can log data, which can then be read back with the command <code>#001Srn</code>.</p>]]></content:encoded>
    </item>
    <item>
      <title>Watch out for compiler bugs related to volatile variables</title>
      <link>http://www.jacquesf.com/2010/02/watch-out-for-compiler-bugs-related-to-volatile-variables</link>
      <pubDate>Wed, 03 Feb 2010 00:00:00 EST</pubDate>
      <category><![CDATA[Embedded Software]]></category>
      <guid>http://www.jacquesf.com/?p=186</guid>
      <description>Watch out for compiler bugs related to volatile variables</description>
      <content:encoded><![CDATA[<p>At my new job, I've been diving back into embedded software development. During my time as an industrial automation application engineer, I stopped working quite as close to the hardware. It's definitely fun to be digging around in registers and assembly code again.</p>
<p>I came across this article called on <em>Embedded.com</em> called "<a href="http://www.embedded.com/design/testissue/222400497">When good compilers go bad, or What you see is not what you execute</a>". It talks about all kinds of ways that compilers can end up generating incorrect (or sometimes technically correct but unexpected) source code. I've got a rule of thumb when debugging that in that vast majority of cases "you didn't discover a bug in the compiler/operating system". It's good to keep in mind that that isn't always the case.</p>
<p>One reference that especially caught my eye was to a paper entitled "<a href="http://www.cs.utah.edu/~regehr/papers/emsoft08-preprint.pdf">Volatiles Are Miscompiled, and What to Do about It</a>". They looked at 13 different C compilers and found out that all thirteen of them had issues with volatile variables.</p>
<p>Volatile variables are very frequently used in the embedded world. Memory-mapped hardware registers (used to read digital and analog inputs for example) are often accessed via volatile pointers in C. The volatile qualifier tells the C compiler "this variable could change at any time, so every time I ask you to read or write it, I really mean it!"</p>
<p>The authors of the paper found that every single compiler they tested failed to correctly implement the volatile qualifier in at least a few of their test cases. However, there are a few things you can do to help protect yourself:</p>
<ul>
    <li>Don't turn on optimization. They couldn't prove it, but they're pretty sure that all (and definitely most) of these kinds of bugs are caused by the compiler's optimizer.</li>
    <li>Wrap uses of volatile variables in function calls. Because functions are (obviously) used way more than volatile variables, there tend to be very few bugs related to incorrect optimizations of function calls. Hiding volatile accesses in function calls makes it much less likely the access to a volatile variable will get optimized away (assuming the function isn't inlined).</li>
    <li>Don't use volatile variables for inter-thread communication, use semaphores instead. This one deserves a post all of its own.</li>
    <li>When the code just absolutely has to work, verify the assembly or machine code your compiler produces by hand.</li>
</ul>

<p>One thing I really liked about the paper is that they used <a href="http://valgrind.org">valgrind</a> to help automate their testing. When I've run into some really tough to debug memory and pointer issues, valgrind has always been a huge help. I wish it were easier to use it in embedded programming projects.</p>]]></content:encoded>
    </item>
  </channel>
</rss>

