<?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 c</title>
    <link>http://www.petersblog.org/</link>
    <description>Nodes containing the tag c</description>
    <item>
      <title>Testing Firmware Code</title>
      <link>http://www.petersblog.org/node/view/1581</link>
      <description>&lt;p&gt;
&lt;a href="/node/1542"&gt;I've described before&lt;/a&gt; how I've been testing Firmware code written in C by compiling it into Ruby Extensions and using ruby's excellent unit testing framework. 
&lt;/p&gt;
&lt;p&gt;
Up till now I've been testing the 'middle tier' algorithmic code but how to go about testing the low level stuff, that which pokes the hardware? 
&lt;/p&gt;
&lt;p&gt;
The firmware I am developing is for the ST ARM750 microcontroller and I am using ST's own standard library which is very useful driver code for all the peripherals embedded on the chip. It is a very useful resource as the peripherals on this chip are very complex and the user manual is written in hardware engineer gobbledygook that you have to study like a legal document, looking for the small print (e.g. running it at 60MHz it kept crashing until I found the small print that advised me to enable 'burst mode'). 
&lt;/p&gt;
&lt;p&gt;
Scanning through the source to this library I saw some #ifdef DEBUG statements that implied that it could be compiled into an emulation library. The code was written very cleanly so it took me very little time to write something in (of course) ruby to scan the header files and generate 5250 lines of C code to emulate this library. Here is an example of one of the generated library functions: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="line-numbers"&gt;   1 &lt;/span&gt; &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;/*&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;   2 &lt;/span&gt; &lt;span class="Comment"&gt; * Emulate the function TIM_ClearFlag&lt;/span&gt;
&lt;span class="line-numbers"&gt;   3 &lt;/span&gt; &lt;span class="Comment"&gt; &lt;span class="Comment"&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;   4 &lt;/span&gt; &lt;span class="Keyword"&gt;void&lt;/span&gt; TIM_ClearFlag(TIM_TypeDef* TIMx, u16 TIM_FLAG)
&lt;span class="line-numbers"&gt;   5 &lt;/span&gt; {
&lt;span class="line-numbers"&gt;   6 &lt;/span&gt;   VALUE nRet;
&lt;span class="line-numbers"&gt;   7 &lt;/span&gt;   VALUE oArgs = rb_ary_new();
&lt;span class="line-numbers"&gt;   8 &lt;/span&gt;   VALUE strFuncName = rb_str_new2( &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;TIM_ClearFlag&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);
&lt;span class="line-numbers"&gt;   9 &lt;/span&gt;   ID nId = rb_intern( &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;Function&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);
&lt;span class="line-numbers"&gt;  10 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;  11 &lt;/span&gt;   rb_ary_push( oArgs, PassStructureArg( (&lt;span class="Keyword"&gt;void&lt;/span&gt; *)TIMx, &lt;span class="Keyword"&gt;sizeof&lt;/span&gt;( TIM_TypeDef)));
&lt;span class="line-numbers"&gt;  12 &lt;/span&gt;   rb_ary_push( oArgs, LONG2NUM( TIM_FLAG));
&lt;span class="line-numbers"&gt;  13 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;  14 &lt;/span&gt;   nRet = rb_funcall( g_oCallBack, nId, &lt;span class="Constant"&gt;2&lt;/span&gt;, strFuncName, oArgs);
&lt;span class="line-numbers"&gt;  15 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;  16 &lt;/span&gt;   &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;/*&lt;/span&gt; nRet is not used &lt;span class="Comment"&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  17 &lt;/span&gt; }
&lt;/pre&gt;
&lt;p&gt;
TIM_ClearFlag is the name of the function in the ST standard library. The emulation code takes the name of the emulated function and the two arguments to the function, converts them into ruby objects and passes them to a method called 'Function' in a ruby object. In the testing case, this object is something that will make sure the functions are being called in the correct sequence and with the correct parameters and will return the appropriate return value. Structure arguments are passed as binary objects which the ruby can decipher from the structure definitions in the ST library header files (also parsed using ruby). 
&lt;/p&gt;
&lt;p&gt;
Note that my system has a few limitations that are acceptable because of the simplicity of the ST library and the way it was coded: 
&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;
The parser I wrote just about handles the coding style of this library 
&lt;/li&gt;
&lt;li&gt;
The calls to ruby do not support the case where the library may poke the values within a structure passed to it by pointer. 
&lt;/li&gt;
&lt;li&gt;
It doesn't handle passing structures by value. 
&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;
Now I have my low level testing library I will be able to do my testing using something I call signature analysis (I'm not sure there is a better software engineering term). I write the code, test it on the real hardware, then I can record the sequence of function calls made including the values of function arguments. Later on, in leiu of real hardware to test it on I can run the test code and compare it against the previously recorded signatures. Et voila, the reassurance that I haven't broken anything and I can sleep at night. 
&lt;/p&gt;
&lt;p&gt;
UPDATE: 
&lt;/p&gt;
&lt;p&gt;
My test library now includes a YamlFaker that can be used in two modes: 
&lt;/p&gt;
&lt;ol&gt;&lt;li&gt;
it will record a sequence of function calls 
&lt;/li&gt;
&lt;li&gt;
it will ensure a sequence of function calls matches a previous recording 
&lt;/li&gt;&lt;/ol&gt;

&lt;p&gt;
The yaml script looks like this: 
&lt;/p&gt;
&lt;div class="verbatim-block"&gt;&lt;pre&gt;#
# This file is maintained by the YamlFaker module DO NOT EDIT
#
---
- test_CommandPWMSet Demand -200:
  - :Comment: |-

      Looking for demand to be correct according to the requested range and the
      scaling factors. Also ensure that for the fifth channel, the period is
      large for small demand as the output of this channel is inverted
  - TIM_SetPulse:
      args:
      - TIMx: TIM0
      - TIM_Channel: &amp;quot;0x2&amp;quot;
      - Pulse: &amp;quot;0x0&amp;quot;
  - TIM_SetPulse:
      args:
      - TIMx: TIM1
      - TIM_Channel: &amp;quot;0x2&amp;quot;
      - Pulse: &amp;quot;0x0&amp;quot;
  - TIM_SetPulse:
      args:
      - TIMx: TIM2
      - TIM_Channel: &amp;quot;0x2&amp;quot;
      - Pulse: &amp;quot;0x0&amp;quot;
  - PWM_SetPulse:
      args:
      - PWM_Channel: &amp;quot;0x2&amp;quot;
      - Pulse: &amp;quot;0x0&amp;quot;
  - PWM_SetPulse:
      args:
      - PWM_Channel: &amp;quot;0x4&amp;quot;
      - Pulse: &amp;quot;0x12c&amp;quot;
- test_CommandPWMSet Demand -100:
  - TIM_SetPulse:
      args:
      - TIMx: TIM0
      - TIM_Channel: &amp;quot;0x2&amp;quot;
      - Pulse: &amp;quot;0x0&amp;quot;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
Most of the entries in the script describe the expected sequence of function calls, complete with arguments. I can ensure that the demand being sent to my PWM channels is what I expect. These recorded scripts could be quite useful, it is a complete log of the interaction with the hardware. If I ever had to change the code that poked the hardware I can compare the script recorded from the new code against my old script and make sure that any changes are what I would expect. This way I can be far more certain that my changes are correct before they hit real hardware. 
&lt;/p&gt;&lt;p&gt;Related Posts: &lt;a href="/tag/c"&gt;c&lt;/a&gt; &lt;a href="/tag/ruby"&gt;ruby&lt;/a&gt; &lt;a href="/tag/testing"&gt;testing&lt;/a&gt;&lt;/p&gt;</description>
      <guid>http://www.petersblog.org/node/view/1581</guid>
      <category domain="http://www.technorati.com/tag">c</category>
      <category domain="http://www.technorati.com/tag">ruby</category>
      <category domain="http://www.technorati.com/tag">testing</category>
    </item>
    <item>
      <title>Problems with Multiple Ruby Extensions</title>
      <link>http://www.petersblog.org/node/view/1572</link>
      <description>&lt;p&gt;
I've been testing my C libraries by compiling them into ruby extensions and then using ruby's unit testing to test them. Under Windows I had it all running nicely and I could run a main test script that would invoke all the other test scripts. Sweet. 
&lt;/p&gt;
&lt;p&gt;
Then it came to getting it running under linux (the target system) and while the unit tests ran fine individually, when I tried my main test script I was getting weird errors from my test extensions, including segment violations (!). 
&lt;/p&gt;
&lt;p&gt;
It took me a while to track down the problem but it turned out to be due to the behaviour of gcc and the linux program loader. All my test libraries were loaded into one ruby process and because they all contained functions with similar names it became a bit of a lottery as to which function in which library was called. I am used to the behaviour of windows dll's where all functions are by default hidden from the outside world unless you explicity export them. gcc/elf/whatever seems to export everything from shared libraries (linux equivalent of a dll) by default with hilarious results. 
&lt;/p&gt;
&lt;p&gt;
In version 4 of gcc they have added a facility for hiding these internal functions. I solved my problem as follows: 
&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;
Add '-fvisibility=hidden' to the gcc arguments to cause all functions to be hidden by default. 
&lt;/li&gt;
&lt;li&gt;
Add this code to my stddefs.h: 
&lt;pre class="lazy"&gt;&lt;span class="line-numbers"&gt;   1 &lt;/span&gt; #&lt;span class="Keyword"&gt;ifdef&lt;/span&gt; _MSC_VER
&lt;span class="line-numbers"&gt;   2 &lt;/span&gt;   #&lt;span class="Keyword"&gt;ifdef&lt;/span&gt; BUILDING_DLL
&lt;span class="line-numbers"&gt;   3 &lt;/span&gt;     #&lt;span class="Keyword"&gt;define&lt;/span&gt; &lt;span class="Entity"&gt;DLLEXPORT&lt;/span&gt; &lt;span class="Entity"&gt;__declsp&lt;span class="Entity"&gt;ec&lt;/span&gt;&lt;/span&gt;(dllexport)
&lt;span class="line-numbers"&gt;   4 &lt;/span&gt;   #&lt;span class="Keyword"&gt;else&lt;/span&gt;
&lt;span class="line-numbers"&gt;   5 &lt;/span&gt;     #&lt;span class="Keyword"&gt;define&lt;/span&gt; &lt;span class="Entity"&gt;DLLEXPORT&lt;/span&gt; &lt;span class="Entity"&gt;__declsp&lt;span class="Entity"&gt;ec&lt;/span&gt;&lt;/span&gt;(dllimport)
&lt;span class="line-numbers"&gt;   6 &lt;/span&gt;   #&lt;span class="Keyword"&gt;endif&lt;/span&gt;
&lt;span class="line-numbers"&gt;   7 &lt;/span&gt;   #&lt;span class="Keyword"&gt;define&lt;/span&gt; &lt;span class="Entity"&gt;DLLLOCAL&lt;/span&gt;
&lt;span class="line-numbers"&gt;   8 &lt;/span&gt; #&lt;span class="Keyword"&gt;else&lt;/span&gt;
&lt;span class="line-numbers"&gt;   9 &lt;/span&gt;   #&lt;span class="Keyword"&gt;define&lt;/span&gt; &lt;span class="Entity"&gt;DLLEXPORT&lt;/span&gt; __attribute__ ((visibility(&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;default&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)))
&lt;span class="line-numbers"&gt;  10 &lt;/span&gt;   #&lt;span class="Keyword"&gt;define&lt;/span&gt; &lt;span class="Entity"&gt;DLLLOCAL&lt;/span&gt; __attribute__ ((visibility(&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;hidden&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)))
&lt;span class="line-numbers"&gt;  11 &lt;/span&gt; #&lt;span class="Keyword"&gt;endif&lt;/span&gt;
&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Change the functions that ruby calls to load the ruby extension to the following form (i.e. add DLLEXPORT): 
&lt;pre class="lazy"&gt;&lt;span class="line-numbers"&gt;   1 &lt;/span&gt; &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;/*&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;   2 &lt;/span&gt; &lt;span class="Comment"&gt; * Initialise the test class.&lt;/span&gt;
&lt;span class="line-numbers"&gt;   3 &lt;/span&gt; &lt;span class="Comment"&gt; &lt;span class="Comment"&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;   4 &lt;/span&gt; DLLEXPORT &lt;span class="Keyword"&gt;void&lt;/span&gt; &lt;span class="Entity"&gt;Init_testtim&lt;span class="Entity"&gt;er&lt;/span&gt;&lt;/span&gt;( &lt;span class="Keyword"&gt;void&lt;/span&gt;)
&lt;span class="line-numbers"&gt;   5 &lt;/span&gt; {
&lt;span class="line-numbers"&gt;   6 &lt;/span&gt;     VALUE oMyClass = rb_define_class( &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;TestTimer&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, rb_cObject);
&lt;span class="line-numbers"&gt;   7 &lt;/span&gt;     rb_define_method( oMyClass, &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;initialize&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, test_initialise, &lt;span class="Constant"&gt;1&lt;/span&gt;);
&lt;span class="line-numbers"&gt;   8 &lt;/span&gt;     rb_define_method( oMyClass, &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;TimerSet&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, test_TimerSet, &lt;span class="Constant"&gt;1&lt;/span&gt;);
&lt;span class="line-numbers"&gt;   9 &lt;/span&gt;     rb_define_method( oMyClass, &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;TimerUpdate&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, test_TimerUpdate, &lt;span class="Constant"&gt;1&lt;/span&gt;);
&lt;span class="line-numbers"&gt;  10 &lt;/span&gt;     rb_define_method( oMyClass, &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;TimerStart&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, test_TimerStart, &lt;span class="Constant"&gt;1&lt;/span&gt;);
&lt;span class="line-numbers"&gt;  11 &lt;/span&gt;     rb_define_method( oMyClass, &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;TimerTest&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, test_TimerTest, &lt;span class="Constant"&gt;0&lt;/span&gt;);
&lt;span class="line-numbers"&gt;  12 &lt;/span&gt; }
&lt;/pre&gt;
&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;
These functions are the only ones that need to be visible outside the library. 
&lt;/p&gt;
&lt;p&gt;
Results: 
&lt;/p&gt;
&lt;div style="text-align:center"&gt;&lt;img src="/images/TestsDone.gif" alt="images/TestsDone.gif"/&gt;&lt;/div&gt; &lt;p&gt;Related Posts: &lt;a href="/tag/c"&gt;c&lt;/a&gt; &lt;a href="/tag/gcc"&gt;gcc&lt;/a&gt; &lt;a href="/tag/linux"&gt;linux&lt;/a&gt; &lt;a href="/tag/ruby"&gt;ruby&lt;/a&gt;&lt;/p&gt;</description>
      <guid>http://www.petersblog.org/node/view/1572</guid>
      <category domain="http://www.technorati.com/tag">c</category>
      <category domain="http://www.technorati.com/tag">gcc</category>
      <category domain="http://www.technorati.com/tag">linux</category>
      <category domain="http://www.technorati.com/tag">ruby</category>
    </item>
    <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>
