<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Peter's Blog - Nodes for firmware</title>
    <link>http://www.petersblog.org/</link>
    <description>Nodes containing the tag firmware</description>
    <item>
      <title>SDCC interrupt woes</title>
      <link>http://www.petersblog.org/node/view/988</link>
      <description>&lt;p&gt;
If anyone else has been trying to figure out why the &lt;a href="/tag/sdcc"&gt;SDCC&lt;/a&gt; 8051 compiler doesn't seem to generate interrupt code that works, the secret is that if the function is defined is a file other than the main program file then a prototype of the interrupt vector function has to be declared in the main program file: 
&lt;/p&gt;
&lt;p&gt;
timer.c: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Keyword"&gt;void&lt;/span&gt; TimerInterrupt( &lt;span class="Keyword"&gt;void&lt;/span&gt;) interrupt &lt;span class="Constant"&gt;3&lt;/span&gt;
{
   blah();
}
&lt;/pre&gt;
&lt;p&gt;
main.c: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Keyword"&gt;extern&lt;/span&gt; &lt;span class="Keyword"&gt;void&lt;/span&gt; TimerInterrupt( &lt;span class="Keyword"&gt;void&lt;/span&gt;) interrupt &lt;span class="Constant"&gt;3&lt;/span&gt;;
&lt;/pre&gt;
&lt;p&gt;
This causes sdcc to generate an interrupt vector table. 
&lt;/p&gt;
&lt;p&gt;
I found this &lt;a href="http://www.maxim-ic.com/appnotes.cfm/appnote_number/3477"&gt;here&lt;/a&gt;. It is reassuring to me that a company like Maxim is endorsing this compiler. 
&lt;/p&gt;&lt;p&gt;Related Posts: &lt;a href="/tag/c"&gt;c&lt;/a&gt; &lt;a href="/tag/firmware"&gt;firmware&lt;/a&gt; &lt;a href="/tag/sdcc"&gt;sdcc&lt;/a&gt;&lt;/p&gt;</description>
      <guid>http://www.petersblog.org/node/view/988</guid>
      <category domain="http://www.technorati.com/tag">c</category>
      <category domain="http://www.technorati.com/tag">firmware</category>
      <category domain="http://www.technorati.com/tag">sdcc</category>
    </item>
    <item>
      <title>Protothreads</title>
      <link>http://www.petersblog.org/node/view/981</link>
      <description>&lt;p&gt;
I am about to start work on a couple of embedded firmware projects and I just happened to come across &lt;a href="http://www.sics.se/~adam/pt/"&gt;protothreads&lt;/a&gt; which use a clever C trick to implement pseudo-threading. Each thread uses only two bytes of memory (thats two: the threads do not need a stack) so it should be great on a 8051. 
&lt;/p&gt;
&lt;p&gt;
The trick relies on a quirk of C where it is possible for a switch statement to jump to case labels inside control statements like for loops. This code very cleverly uses the line number within the file (i.e. __LINE__) as the target for a later switch jump. Hence to 'resume' a thread the code just uses a switch statement to jump to the place within the function where it last yielded. 
&lt;/p&gt;
&lt;p&gt;
This offers the hope of efficient multitasking &lt;i&gt;without&lt;/i&gt; loathsome state machines. Nice sequential coding: bliss. Compared to 'real' threads or interrupt service routines, these protothreads should not be too sensitive to obscure bugs as you know where task switching is likely to happen. This means you don't necessarily need mutexes and whatnot scatterred around. I've always been a believer in doing as much as possible in the foreground and as little as possible under interrupts and this technique will make it easier for me to do this. 
&lt;/p&gt;
&lt;p&gt;
The only shortcomings of the trick (apart from relying on am obscure quirk of the compiler) are that you cannot use switch statements as your thread is based on a switch statement, there are limitations regarding blocking in subroutines and local variables do not hold their values (which also applies to state machines). 
&lt;/p&gt;
&lt;p&gt;
I'm tempted to use the &lt;a href="http://sdcc.sourceforge.net/"&gt;SDCC&lt;/a&gt; 8051 compiler as the old version of the Keil compiler we have at work does not run on Windows XP. It remains to be seen whether this trick will work with this compiler: I have no hardware to test it and I cannot get the sdcdb emulator to support serial IO so I could only tell if it worked by setting breakpoints and seeing if they are reached. 
&lt;/p&gt;
&lt;p&gt;
Thought: if the thread was within the first 255 lines of the source file, each thread would only need one byte to store it's state. 
&lt;/p&gt;&lt;p&gt;Related Posts: &lt;a href="/tag/c"&gt;c&lt;/a&gt; &lt;a href="/tag/firmware"&gt;firmware&lt;/a&gt; &lt;a href="/tag/sdcc"&gt;sdcc&lt;/a&gt;&lt;/p&gt;</description>
      <guid>http://www.petersblog.org/node/view/981</guid>
      <category domain="http://www.technorati.com/tag">c</category>
      <category domain="http://www.technorati.com/tag">firmware</category>
      <category domain="http://www.technorati.com/tag">sdcc</category>
    </item>
  </channel>
</rss>
