<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>State Space</title>
	<atom:link href="http://embeddedgurus.com/state-space/feed/" rel="self" type="application/rss+xml" />
	<link>http://embeddedgurus.com/state-space</link>
	<description>A Blog by Miro Samek</description>
	<lastBuildDate>Mon, 26 Sep 2011 20:24:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>What&#8217;s the state of your Cortex?</title>
		<link>http://embeddedgurus.com/state-space/2011/09/whats-the-state-of-your-cortex/</link>
		<comments>http://embeddedgurus.com/state-space/2011/09/whats-the-state-of-your-cortex/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 20:24:37 +0000</pubDate>
		<dc:creator>Miro Samek</dc:creator>
				<category><![CDATA[Firmware Bugs]]></category>
		<category><![CDATA[MCUs]]></category>
		<category><![CDATA[RTOS Multithreading]]></category>
		<category><![CDATA[ARM Cortex-M]]></category>
		<category><![CDATA[hardware race condition]]></category>

		<guid isPermaLink="false">http://embeddedgurus.com/state-space/?p=118</guid>
		<description><![CDATA[Recently, I&#8217;ve been involved in a fascinating bug hunt related to a very peculiar behavior of the ARM Cortex-M3 core. Given the incredible popularity of this core, I thought that digging a little deeper into the mysteries of ARM Cortex could be interesting and informative. First, I need to provide some background. So, the bug [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I&#8217;ve been involved in a fascinating bug hunt related to a very peculiar behavior of the ARM Cortex-M3 core. Given the incredible popularity of this core, I thought that digging a little deeper into the mysteries of ARM Cortex could be interesting and informative.</p>
<p>First, I need to provide some background. So, the bug was related to the very unique ARM Cortex-M exception type called PendSV. This is an exception triggered by software, but unlike any regular software interrupt, PendSV is an <strong>asynchronous</strong> exception. This means that PendSV typically does not run immediately after it is triggered, but only after the Nested Vectored Interrupt Controller (NVIC) determines that the priority of the currently executing code drops below the priority associated with PendSV.</p>
<p>At this point, you might wonder, why and where would such &#8220;Pended Software Interrupt&#8221; be useful? Well, it turns out that PendSV is the only reliable way on ARM Cortex-M to find out when all (possibly nested) interrupt service routines (ISRs) have completed. And this determination is essential to run the scheduler in any preemptive real time kernel.</p>
<p>Virtually all preemptive RTOSes for ARM Cortex-M processors work as follows. Upon initialization the priority associated with PendSV is set to be the lowest of all exceptions (0xFF). All ISRs in the system, prioritized above PendSV, trigger the PendSV exception by writing 1 to the PENDSVSET bit in the NVIC ICSR register, like this:</p>
<p><code>*((uint32_t volatile *)0xE000ED04) = 0x10000000;<br />
</code><br />
Now, the heavy lifting is left entirely to the NVIC hardware. NVIC will activate PendSV only <strong>after</strong> the last of all nested interrupts completes and is about to return to the preempted task context. This is exactly the right time for a context switch. In other words, the PendSV exception is designed to call the scheduler and perform the task preemption. ARM Cortex is so smart that it eliminates the overhead of exiting one exception (the last nested interrupt) and activating another (the PendSV) in the trick called &#8220;tail-chaining&#8221;.</p>
<p>Everything looks easy so far, but ARM Cortex has one more trick up it&#8217;s sleeve and this optimization, called &#8220;late-arrival&#8221;, has interesting side effects related to PendSV. This subtle interaction between PendSV and late-arrival leads essentially to a hardware race condition I&#8217;ve recently had a pleasure to chase down.</p>
<p>To illustrate the events that lead up to the bug, I&#8217;ve prepared a distilled hardware trace available for viewing at <a title="ARM-Cortex-M3 bug trace" href="http://www.state-machine.com/attachments/ARM-Cortex-M3_bug.txt" target="_blank">ARM-Cortex-M3_bug.txt</a>. Please go ahead and click on this link to follow along.</p>
<p>The trace starts with an interrupt entry (labelled as Exception 83). This system runs under the preemptive kernel called QK, so the ISR calls QK_ISR_ENTRY() and later QK_ISR_EXIT() macros to inform the kernel about the interrupt. At trace index 069545 the QK_ISR_EXIT() macro triggers the PendSV exception by writing 0&#215;10000000 into the ICSR register.</p>
<p>After this, the Exception 83 runs to completion and eventually tail-chains to Exception 14 (PendSV). This is all as expected.</p>
<p>However, the real problem starts at trace index 069618, at which the execution of the first instruction of PendSV (CPSID i) is cancelled due to arrival of a higher-priority Exception 36 (another interrupt).</p>
<p>This cancellation of low-priority Exception 14 in favor of the higher-priority Exception 36 is anotehr ARM Cortex-special called <strong>late arrival</strong>. The ARM core optimizes the interrupt entry (which is identical for all exception), and instead of entering the low-priority exception and than immediately high-priority exception, it simply enters the high-priority exception.</p>
<p>The problem is that just before the late arrival, the PENDSVSET bit in the NVIC-ICSR register is already <em>cleared</em>.</p>
<p>However, the late-arriving Exception 36 sets this bit again in QK_ISR_EXIT(), which is normal for any interrupt (trace index 070126).</p>
<p>The Exception 36 eventually exits to the original PendSV (trace index 070130), but this is <strong>not</strong> the usual tail-chaining (the trace indicates tail-chaining by the pair Exception Exit/Exception Entry). This time around the trace shows only Exception Exit, but no entry.</p>
<p>This difference has very important implication, which is that the PENDSVSET bit in the NVIC-ICSR register is <span style="text-decoration: underline"><span style="color: #ff0000"><strong>not</strong></span></span> cleared (remember that it is set, however).</p>
<p>What unfolds next is the consequence of the PENDSVSET bit being set. PendSV executes, fakes its own return to the QK scheduler, and eventually it unlocks interrupts. But before SVCall (Exception 11) can execute, the PendSV Exception 14 is taken again (because it is triggered by the PENDSVSET bit). This makes no sense and should never happen, because PendSV should never be in the triggered state at this point.</p>
<p>***<br />
So, what are the consequences of this behavior and what is the fix?</p>
<p>Well, as you can see, due to late-arrival PendSV can be occasionally entered with the PENDSVSET bit being set, so it will be triggered again immediately after it completes. This might or might not have grave consequences. In case of the QK kernel, this was unacceptable and led to a Hardware Fault. In other RTOSes it might simply cause another scheduler call, waste of some CPU, and delay of the task-level response, but perhaps not a catastrophic failure.</p>
<p>The actual fix of the problem is very simple. Since you cannot rely on the automatic clearing of the PENDSVSET bit in the NVIC-ICSR register, you need to clear it manually (by writing 1 to the PENDSVCLR bit in the NVIC-ICSR register.) Of course this is wasteful, because only one time in a million this bit is actually not cleared automatically.</p>
<p>Interestingly, I have not seen such writing to the PENDSVCLR bit in open source RTOSes for ARM Cortex-M (such as FreeRTOS.org). Recently, I&#8217;ve come across some posts to the ARM Community Forums that this problem exists for the Frescale MQX RTOS (see <a title="PendSV pending inside PendSV handler?" href="http://forums.arm.com/index.php?/topic/15245-pendsv-pending-inside-pendsv-handler-cortex-m4/">PendSV pending inside PendSV handler? (Cortex-M4)</a>).</p>
<p>If you use a preemptive kernel on ARM Cortex-M0/M3, perhaps you could check how <em>your </em>kernel handles PendSV. If you don&#8217;t see an explicit write to the PENDSVCLR bit, I would recommend that you think through the consequences of re-entering PendSV. I&#8217;d be very interested to collect a survey of how the existing kernels for ARM Cortex-M handle this situation.</p>
]]></content:encoded>
			<wfw:commentRss>http://embeddedgurus.com/state-space/2011/09/whats-the-state-of-your-cortex/feed/</wfw:commentRss>
		<slash:comments>48</slash:comments>
		</item>
		<item>
		<title>On the Origin of Software by Means of Artificial Selection</title>
		<link>http://embeddedgurus.com/state-space/2011/08/on-the-origin-of-software-by-means-of-artificial-selection/</link>
		<comments>http://embeddedgurus.com/state-space/2011/08/on-the-origin-of-software-by-means-of-artificial-selection/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 21:32:44 +0000</pubDate>
		<dc:creator>Miro Samek</dc:creator>
				<category><![CDATA[TDD]]></category>
		<category><![CDATA[embedded books]]></category>
		<category><![CDATA[embedded software development]]></category>

		<guid isPermaLink="false">http://embeddedgurus.com/state-space/?p=102</guid>
		<description><![CDATA[If you haven&#8217;t put your hands on the recent James Grenning&#8217;s book &#8220;Test-Driven Development for Embedded C&#8221; yet, I highly recommend you do. Here is why. First, you need to realize that this book is not really about testing&#8211;it is about software development. The central idea behind TDD (Test-Driven Development) is that software, as any complex [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven&#8217;t put your hands on the recent James Grenning&#8217;s book<em> &#8220;Test-Driven Development for Embedded C&#8221;</em> yet, I highly recommend you do. Here is why.</p>
<p>First, you need to realize that this book is <strong>not</strong> really about testing&#8211;it is about software <strong>development</strong>. The central idea behind TDD (Test-Driven Development) is that software, as any complex system in nature, has to evolve gradually and has to keep <strong>working</strong> throughout all the development stages. This idea is of course not new and goes back all the way to the Darwin&#8217;s &#8220;<em>On the Origin of Species&#8221;</em>. More recently, in his 1977 book &#8220;<em>Systemantics: How Systems Work and Especially How They Fail</em>&#8221; John Gall wrote:</p>
<blockquote><p><em>&#8220;A complex system that works is invariably found to have evolved from a simple system that worked&#8230;. A complex system designed from scratch never works and cannot be patched up to make it work. You have to start over, beginning with a working simple system&#8221;. </em></p></blockquote>
<p>The key point of TDD is to subject the software to constant &#8220;struggle for existence&#8221; to actually see if it indeed <strong>is</strong> still working and in the process weed out any undesired mutations.</p>
<p>Of course, in developing software we don&#8217;t have the deep evolutionary time, so we need to accelerate the pace of software evolution. We do this by <strong>automating</strong> the testing.</p>
<p>For embedded development this means avoiding the target system bottleneck (James calls it DOH-Development On Hardware). The embedded TDD strategy is to develop embedded software on the <strong>desktop</strong> and only occasionally check it on the real embedded hardware. This means that the C/C++ compilers and tools for the desktop (such as Visual C++, MinGW, or Cygwin for Windows and GCC for Linux and Mac OS X) are important for us.</p>
<p>The book comes with testing frameworks (Unity and CppUTest) and plenty of example code. The code works right of the box on Linux, but I had some issues running it on Windows. In the process of learning the tools, I&#8217;ve prepared a small template for Visual C++ 2008, which is available for download from:</p>
<p><a title="tdd_blinky.zip" href="http://www.state-machine.com/attachments/tdd_blinky.zip">http://www.state-machine.com/attachments/blinky_tdd.zip</a></p>
<p>This demo assumes that you download and install the CppUTest framework (<a href="http://sourceforge.net/projects/cpputest/">http://sourceforge.net/projects/cpputest/</a>) and that you define the environment variable CPP_U_TEST to point to the directory where you installed CppUTest. The Visual Studio solution AllTest.sln is located in the blinky\tests directory.</p>
<p>I&#8217;d love to hear about your experiences with TDD in embedded programming. I&#8217;m sure I will blog more about it in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://embeddedgurus.com/state-space/2011/08/on-the-origin-of-software-by-means-of-artificial-selection/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Protothreads versus State Machines</title>
		<link>http://embeddedgurus.com/state-space/2011/06/protothreads-versus-state-machines/</link>
		<comments>http://embeddedgurus.com/state-space/2011/06/protothreads-versus-state-machines/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 17:28:53 +0000</pubDate>
		<dc:creator>Miro Samek</dc:creator>
				<category><![CDATA[Efficient C/C++]]></category>
		<category><![CDATA[event-driven programming]]></category>
		<category><![CDATA[state machines]]></category>
		<category><![CDATA[multitasking]]></category>
		<category><![CDATA[protothreads]]></category>

		<guid isPermaLink="false">http://embeddedgurus.com/state-space/?p=92</guid>
		<description><![CDATA[For a number of years I&#8217;ve been getting questions regarding Protothreads and comparisons to state machines. Here is what I think. Protothreads are an attempt to write event-driven code in a sequential way. To do so, protothreads introduce a concept of &#8220;blocking abstraction&#8221; to event-driven programming&#8211;something that event-driven programming is trying to get rid of [...]]]></description>
			<content:encoded><![CDATA[<p>For a number of years I&#8217;ve been getting questions regarding <a title="Protothreads" href="http://en.wikipedia.org/wiki/Protothreads">Protothreads</a> and comparisons to state machines. Here is what I think.</p>
<p>Protothreads are an attempt to write event-driven code in a sequential way. To do so, protothreads introduce a concept of &#8220;blocking abstraction&#8221; to event-driven programming&#8211;something that event-driven programming is trying to get rid of in the first place.</p>
<p>Obviously, to be compatible with event-driven programming, protothreads cannot *really* block, at least not in the same sense as traditional threads of a conventional blocking RTOS can. Instead, a protothread is still called for every event and still *returns* to the caller without really blocking. However, when a given event does not match the &#8220;blocking abstraction&#8221;, the protothread returns without doing anything and without progressing. Only when the current event matches the &#8220;blocking abstraction&#8221; the protothread advances to the next &#8220;blocking abstraction&#8221; and also returns. Please note that protothreads allow standard flow control statements, such as IF-THEN-ELSE and WHILE loops between any two &#8220;blocking abstractions&#8221;. Therefore the program feels more &#8220;natural&#8221; for designers used to the traditional sequential programming style. You simply see the expected sequence of events.</p>
<p>Protothreads are indeed a simplification, but only for *sequential problems*, in which only specific sequences of events are valid and all other sequences are invalid. Examples for using protothreads include the sequence of events for initializing a radio modem.</p>
<p>However, protothreads lose their advantage entirely if there are many valid sequences of events. This is actually the most common situation in event-driven systems. State machines are capable to handle multiple sequences of events easily. In fact, state machines are getting simpler when the sequence of events matters less. In contrast, protothreads are getting more and more complex when they need to accept more sequences of events.</p>
<p>So, in the end, protothreads are an attempt to replace state machines, which are considered complex and messy by the inventors of the protothread mechanism. But, the &#8220;messiness&#8221; of state machines is obviously a very subjective statement. A good state machine implementation technique can remove many (most) accidental difficulties of coding state machines. I mean, if a state machine as depicted in a state diagram is simple, and if the code does not reflect this simplicity, the problem is with the implementation technique, not with the inherent complexity of a state machine.</p>
<p>The bottom line: good state machine implementation techniques eliminate most reasons for using protothreads. State machines are far more generic and flexible, because they can easily handle multiple sequences of events. In comparison, protothreads are intentionally crippled state machines that transition implicitly from one &#8220;blocking abstraction&#8221; to another executing code in between as the &#8220;actions&#8221; on such implicit transition.</p>
]]></content:encoded>
			<wfw:commentRss>http://embeddedgurus.com/state-space/2011/06/protothreads-versus-state-machines/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Is an RTOS really the best way to design embedded systems?</title>
		<link>http://embeddedgurus.com/state-space/2011/06/is-an-rtos-really-the-best-way-to-design-embedded-systems/</link>
		<comments>http://embeddedgurus.com/state-space/2011/06/is-an-rtos-really-the-best-way-to-design-embedded-systems/#comments</comments>
		<pubDate>Tue, 07 Jun 2011 17:44:23 +0000</pubDate>
		<dc:creator>Miro Samek</dc:creator>
				<category><![CDATA[event-driven programming]]></category>
		<category><![CDATA[RTOS Multithreading]]></category>
		<category><![CDATA[state machines]]></category>
		<category><![CDATA[multitasking]]></category>
		<category><![CDATA[RTOS]]></category>

		<guid isPermaLink="false">http://embeddedgurus.com/state-space/?p=86</guid>
		<description><![CDATA[Recently I&#8217;ve been involved in a discussion on the LinkedIn Real-Time Embedded Engineering group, which I started with the question &#8220;Is an RTOS really the best way to design embedded systems?&#8221;. The discussion, which has swollen to over 0xFF comments by now, has sometimes low signal to noise ratio, but I believe it is still [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.state-machine.com/attachments/linkedin_rte_group.jpg" alt="LinkedIn RTE group" /></p>
<p>Recently I&#8217;ve been involved in a discussion on the <a href="http://www.linkedin.com/groups?gid=102939">LinkedIn Real-Time Embedded Engineering group</a>, which I started with the question &#8220;Is an RTOS really the best way to design embedded systems?&#8221;.</p>
<p>The discussion, which has swollen to over 0xFF comments by now, has sometimes low signal to noise ratio, but I believe it is still interesting.</p>
<p>I consider this discussion to be a continuation of the topic from my April blog post <a href="http://embeddedgurus.com/state-space/2010/04/i-hate-rtoses/">I hate RTOSes</a>.</p>
<p>As before, my main point is centered on the fundamental mismatch of using the sequential programming paradigm (RTOS or superloop) to solve problems that are event-driven by nature.</p>
<p>I&#8217;m really curious what the visitors to <a href="http://EmbeddedGurus.com">EmbeddedGurus</a> think.</p>
]]></content:encoded>
			<wfw:commentRss>http://embeddedgurus.com/state-space/2011/06/is-an-rtos-really-the-best-way-to-design-embedded-systems/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Rapid Prototyping with QP and Arduino</title>
		<link>http://embeddedgurus.com/state-space/2011/02/rapid-prototyping-with-qp-and-arduino/</link>
		<comments>http://embeddedgurus.com/state-space/2011/02/rapid-prototyping-with-qp-and-arduino/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 16:54:50 +0000</pubDate>
		<dc:creator>Miro Samek</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://embeddedgurus.com/state-space/?p=62</guid>
		<description><![CDATA[Arduino (see arduino.cc) is an open-source electronics prototyping platform, designed to make digital electronics more accessible to non-specialists in multidisciplinary projects. Arduino has gained popularity, because it provides both hardware and compatible software focused on developing working prototypes. By making it easier to build the first prototype, Arduino lowers the barrier of entry to the [...]]]></description>
			<content:encoded><![CDATA[<p><img style="float: right;margin-left: 5px" src="http://www.state-machine.com/news/logo_arduino.png" alt="" /></p>
<p>Arduino (see <a href="http://arduino.cc">arduino.cc</a>) is an open-source electronics prototyping platform, designed to make digital electronics more accessible to <strong>non-specialists</strong> in multidisciplinary projects. Arduino has gained popularity, because it provides both hardware and compatible software focused on developing working prototypes. By making it easier to build the first prototype, Arduino lowers the barrier of entry to the field of modern microelectronics and enables a host of new applications.</p>
<p>However, programming the Arduino microcontroller (Atmel AVRmega) remains a challenge. Traditionally, Arduino programs are written in a sequential manner, which means that whenever an Arduino program needs to synchronize with some external event, such as a button press, arrival of a character through the serial port, or a time delay, it explicitly waits in-line for the occurrence of the event. Waiting &#8220;in-line&#8221; means that the Arduino processor spends all of its cycles constantly checking for some condition in a tight loop (called the polling loop).</p>
<p>Although this approach is functional in many situations, it doesn&#8217;t work very well when there are multiple possible sources of events whose arrival times and order you cannot predict and where it is important to handle the events in a timely manner. The fundamental problem is that while a sequential program is waiting for one kind of event (e.g., a button press), it is not doing any other work and is not responsive to other events (e.g., characters from the serial port).</p>
<p>Another big problem with the sequential program structure is wastefulness in terms of power dissipation. Regardless of how much or how little actual work is being done, the Arduino processor is always running at top speed, which drains the battery quickly and prevents you from making truly long-lasting battery-powered devices.</p>
<h3>Event-Driven Programming for Arduino</h3>
<p>For these and other reasons experienced programmers turn to the long-know design strategy called <em>event-driven programming</em>, which requires a distinctly different way of thinking than conventional sequential programs. All event-driven programs are naturally divided into the application, which actually handles the events, and the supervisory event-driven infrastructure (framework), which waits for events and dispatches them to the application. The control resides in the event-driven framework, so from the application standpoint, the control is inverted compared to a traditional sequential program.</p>
<p>It turns out that the <strong>QP/C++</strong> state machine framework beautifully complements the Arduino platform and provides everything you need to build responsive, robust, and power-efficient Arduino programs based on modern hierarchical state machines. In many ways the open source QP/C++ state machine framework is like a modern real-time operating system (RTOS) specifically designed for executing event-driven state machines. The free QM™ graphical modeling tool takes Arduino programming to the next level, by enabling automatic code generation of complete Arduino sketches.</p>
<p>The QP Development Kit (QDK) for Arduino is different from most other QDKs available from <a href="http://www.state-machine.com">state-machine.com</a> in that it is self-contained and includes the simplified and compacted source-code version of the QP/C++ framework. The QDK-Arduino is designed to plug directly into the Arduino IDE to become immediately useful without building a binary library. The extensive Application Note <a href="http://www.state-machine.com/arduino/AN_Event-Driven_Arduino.pdf">&#8220;Event Driven Arduino Programming with QP&#8221;</a> describes the main concepts and how to get started.</p>
<div><a href="http://www.state-machine.com/arduino" target="_blank">QDK-Arduino on state-machine.com<strong>»</strong></a><br />
<a href="http://arduino.cc/playground/Code/QP" target="_blank">QP on Arduino Playground<strong>»</strong></a></div>
]]></content:encoded>
			<wfw:commentRss>http://embeddedgurus.com/state-space/2011/02/rapid-prototyping-with-qp-and-arduino/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Linear statechart notation</title>
		<link>http://embeddedgurus.com/state-space/2010/11/linear-statechart-notation/</link>
		<comments>http://embeddedgurus.com/state-space/2010/11/linear-statechart-notation/#comments</comments>
		<pubDate>Mon, 15 Nov 2010 16:58:43 +0000</pubDate>
		<dc:creator>Miro Samek</dc:creator>
				<category><![CDATA[modeling tool]]></category>
		<category><![CDATA[state machines]]></category>
		<category><![CDATA[UML]]></category>

		<guid isPermaLink="false">http://embeddedgurus.com/state-space/?p=54</guid>
		<description><![CDATA[The traditional fully 2-dimensional structure of UML state diagrams is too much rope to hang yourself with. There is no standard drawing order or pattern; some designers start from the top, some from the middle, and others just &#8220;go with the flow&#8221;. Transitions can originate at any state edge and go in any direction, so they are [...]]]></description>
			<content:encoded><![CDATA[<p>The traditional fully 2-dimensional structure of UML state diagrams is too much rope to hang yourself with. There is no standard drawing order or pattern; some designers start from the top, some from the middle, and others just &#8220;go with the flow&#8221;. Transitions can originate at any state edge and go in any direction, so they are easy to miss in any nontrivial diagram because you don&#8217;t know exactly where to look.</p>
<p>In many ways, UML state diagrams resemble the old way of drawing tree structures (e.g., a family tree) with the root in the middle and branches fanning out in all directions. But today nobody draws hierarchical structures that way anymore. If you look at your file-system explorer you will see the hierarchical structure of directories and files arranged <strong>linearly</strong>, top-to-bottom. The &#8220;linear statechart notation&#8221; that I would like to propose here is based on the same idea.</p>
<p>The goal of the &#8220;linear statechart notation&#8221; is to make the diagrams more structured and legible by reducing the use of horizontal dimension.</p>
<p>As an example of the &#8220;linear notation&#8221; consider the state diagram shown in the screen shot from the <a title="Free QM state machine tool for embedded systems" href="http://embeddedgurus.com/state-space/2010/11/free-state-machine-tool-for-embedded-systems/" target="_self">free QM tool</a>.  (This statechart shows the lifescycle of a space ship in a simple &#8220;Fly &#8216;n&#8217; Shoot&#8221; game.) First please take a look at the hierarchical model explorer pane on the left-side of the screen. You see the Ship object, its attributes and methods followed by the Statechart with the hierarchically arranged elements below. Now, please take a look at the state diagram in the middle. You will see the one-to-one correspondence between the diagram and the explorer view. Please note that the state diagram is essentially 1-dimensional.</p>
<p><img src="http://www.state-machine.com/attachments/linear.jpg" alt="" /></p>
<p>Finally, please take a look at the right-hand side, which is a screenshot of the *generated code* in the Eclipse-based tool (Atollic TrueSTUDIO in this case). The generated code corresponds to the state &#8220;flying&#8221;, which is highlighted in the diagram. Interestingly, the code itself is an extension of the &#8220;linear notation&#8221; that zooms into the &#8220;flying&#8221; state. Again, just go from the top to bottom in the code, inside the &#8220;flying&#8221; state in the diagram, and in the &#8220;flying&#8221; element in the model explorer. You see exactly the same elements represented in the same order, from the entry action, through the events TIME_TICK, PLAYER_TRIGGER, DESTROYED_MINE, HIT_WALL, and HIT_MINE. I think this consistency and tracibility is great.</p>
<p>I&#8217;d like to hear your comments about the proposed notation. I also hope that this post explains a bit how the <a title="Free QM state machine tool for embedded systems" href="http://www.state-machine.com/qm" target="_self">free QM tool</a> works and generates code.</p>
]]></content:encoded>
			<wfw:commentRss>http://embeddedgurus.com/state-space/2010/11/linear-statechart-notation/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Free state machine tool for embedded systems</title>
		<link>http://embeddedgurus.com/state-space/2010/11/free-state-machine-tool-for-embedded-systems/</link>
		<comments>http://embeddedgurus.com/state-space/2010/11/free-state-machine-tool-for-embedded-systems/#comments</comments>
		<pubDate>Sun, 07 Nov 2010 00:22:56 +0000</pubDate>
		<dc:creator>Miro Samek</dc:creator>
				<category><![CDATA[Efficient C/C++]]></category>
		<category><![CDATA[modeling tool]]></category>
		<category><![CDATA[state machines]]></category>
		<category><![CDATA[UML]]></category>
		<category><![CDATA[code generation]]></category>
		<category><![CDATA[graphical tool]]></category>

		<guid isPermaLink="false">http://embeddedgurus.com/state-space/?p=49</guid>
		<description><![CDATA[I realize that I stalled a little my series about RTOSes, event-driven programming, state machines and frameworks for embedded systems. I certainly will come back to this subject, but today I wanted to let you know about a new, free, graphical tool for drawing state machines and generating production-quality embedded code. Traditionally, such tools haven&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>I realize that I stalled a little my series about RTOSes, event-driven programming, state machines and frameworks for embedded systems. I certainly will come back to this subject, but today I wanted to let you know about a new, free, graphical tool for drawing state machines and generating production-quality embedded code.</p>
<p>Traditionally, such tools haven&#8217;t particularly caught on in the embedded space, mainly because too often such tools fail to pull their own weight. Developers find themselves fighting the tool at every step of the way: from drawing the diagrams to trying to live in a straight jacket of the code structure it generates.</p>
<p><img src="http://www.state-machine.com/attachments/qm_gui.jpg" alt="" /></p>
<p>The new, free QM tool from <a href="http://www.state-machine.com">Quantum Leaps</a> (my company) is different, because it was designed from the ground up around the code-centric approach. Unlike other graphical tools, QM gives you complete control over the generated code structure, directory names, file names, and elements that go into every file. You can mix your own code with the synthesized code and use QM to generate as much or as little of the overall code as you see fit. At the low level, QM respects your graphical layout as much as possible and will not re-attach or re-route connectors, resize nodes, or adjust text annotations. You will find that you don&#8217;t need to fight the tool.</p>
<p>Even though a lot of effort went into making QM as UML-compliant, the tool is innovative and might work <strong>differently</strong> than other graphical state machine tools on the market. For example, QM does not use &#8220;pseudostates&#8221;, such as the initial pseudostate or choice point. Instead QM uses higher-level primitives of initial-transition and choice-segment, respectively. This simplifies state diagramming immensely, because you don&#8217;t need to separately position pseudostates and then connect them. Also, QM introduces a new notation for internal transitions, which allows actual drawing of internal transitions (in standard UML notation internal transitions are just text in the state body). This notation enables showing internal transitions with regular state transitions in a choice point&#8211;something that comes up very often in practice and was never addressed well in the standard UML.</p>
<p>The QM tool is available now for a <strong>free</strong> download and free, unrestricted use from <a href="http://www.state-machine.com/qm">state-machine.com/qm</a>. I&#8217;d appreciate any comments about the tool, comparisons to other similar tools, code generation, UML, state machines, etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://embeddedgurus.com/state-space/2010/11/free-state-machine-tool-for-embedded-systems/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>RTOS without blocking?</title>
		<link>http://embeddedgurus.com/state-space/2010/04/rtos-without-blocking/</link>
		<comments>http://embeddedgurus.com/state-space/2010/04/rtos-without-blocking/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 18:13:55 +0000</pubDate>
		<dc:creator>Miro Samek</dc:creator>
				<category><![CDATA[RTOS Multithreading]]></category>

		<guid isPermaLink="false">http://embeddedgurus.com/state-space/?p=40</guid>
		<description><![CDATA[In my previous post, &#8220;I hate RTOSes&#8221;, I have identified blocking as the main cause of the particular brittleness and inflexibility of the programs based on RTOSes. Here I&#8217;d like to discuss techniques of minimizing blocking and eradicating it completely from the application-level code. In other words, I&#8217;d like to show you how to use [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous post, &#8220;I hate RTOSes&#8221;, I have identified blocking as the main cause of the particular brittleness and inflexibility of the programs based on RTOSes. Here I&#8217;d like to discuss techniques of minimizing blocking and eradicating it completely from the application-level code. In other words, I&#8217;d like to show you how to use an RTOS for building responsive event-driven software.</p>
<p>For reasons I&#8217;ve outlined before, experienced RTOS users have learned to be weary of peppering the code with the blocking calls to the RTOS. So, even though every RTOS boasts a plethora of various communication and synchronization mechanisms (all of them based on blocking), advanced real-time developers intentionally limit their designs to just one generic blocking call per task, as shown in the following pseudocode:</p>
<pre>void task_routine(void *arg) {
    while (1) {
        // block on any event designated for this task (generic)
        // process the event *without* further blocking (task specific)
    }
}
</pre>
<p>Most RTOSes provide mechanisms to wait for <strong>multiple</strong> events in a single blocking call, for example: event flags, message mailboxes, message queues, the <code>select()</code> call, condition variables, and many others. From all these possibilities, I&#8217;d like to single out the message queue, because it is the most generic and flexible mechanism. A message posted to a message queue not only unblocks any task that waits on the queue (synchronization), but the message can also contain any information associated with the event (interprocess communication). For example, a message from an analog-to-digital converter (ADC) can signal when the conversion has completed as well as the actual value of the conversion result.</p>
<p>The generic pseudocode of a task based on a message queue looks as follows:</p>
<pre>void task_routine(void *arg) {
    while (1) { // main event loop of the task
        void *event = msg_queue_get(); // wait for event
        // process the event *without* further blocking (task specific)
    }
}
</pre>
<p>The most important premise of this <strong>event-loop</strong> design is that the task-specific code that processes the events obtained from the queue is <strong>not</strong> allowed to block. The event-processing code must execute quickly and return back to the event loop, so that the event loop can check for other events.</p>
<p>This design also automatically guarantees that each event is processed in <strong>run-to-completion</strong> (RTC) fashion. By design, the event loop must necessarily complete processing of the current event before looping back to obtain and process the next event. Also note that the need for queuing events is an immediate consequence of the RTC processing style. Queuing prevents losing events that arrive while the event-loop is executing an RTC step.</p>
<p>The event-loop pseudocode shown above is still task-specific, but it is quite easy to make it completely generic. As shown below, you can combine a message queue and an event-handler pointer-to-function in the TCB structure. A pointer to the TCB struct can be then passed to the task in the argument of the task routine (<code>arg</code>). This is quite easily achieved when the task is created.</p>
<pre>typedef struct {
    MessageQueue queue;        // event queue associated with the task
    void (*handler)(void *event); // event handler pointer-to-function
} TCB;   // task control block

void task_routine(void *arg) {
    while (1) { // main event loop of the task
        void *event = msg_queue_get(((TCB *)arg)-&gt;queue); // wait for event
        (*((TCB *)arg)-&gt;handler)(event);// handle the event without blocking
    }
}
</pre>
<p>The last snippet of code is generic, meaning that this simple event-loop can be used for <strong>all</strong> tasks in you application. So at this point, you can consider the <code>task_routine()</code> function as part of the generic event-driven infrastructure for executing your applications, which consist of event-handler functions.</p>
<p>What this way of thinking gives you is quite significant, because in fact you have just created your first event-driven <strong>framework</strong>.</p>
<p>The distinction between a framework and a toolkit is simple. A toolkit, such as an RTOS, is essentially a collection of functions that <strong>you</strong> can call. When you use a toolkit, <strong>you</strong> write the main body of the application (such as all the task routines) and <strong>you</strong> call the various functions from the RTOS. When you use a framework, you reuse the main body (such as the <code>task_routine()</code> function) and you provide the code that <strong>the framework</strong> calls. In other words, a framework uses <strong>inverted</strong> control compared to a traditional RTOS.</p>
<p>Inversion of control is a very common phenomenon in all event-driven architectures, because it recolonizes the plain fact that the events are controlling the application, not the other way around.</p>
<p>In my next post in the &#8220;I hate RTOSes&#8221; series, I&#8217;ll talk about challenges of programming without blocking. I&#8217;ll explain what you need to sacrifice when you write non-blocking code and why this often leads to &#8220;spaghetti&#8221; code. Stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://embeddedgurus.com/state-space/2010/04/rtos-without-blocking/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>I hate RTOSes</title>
		<link>http://embeddedgurus.com/state-space/2010/04/i-hate-rtoses/</link>
		<comments>http://embeddedgurus.com/state-space/2010/04/i-hate-rtoses/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 18:22:16 +0000</pubDate>
		<dc:creator>Miro Samek</dc:creator>
				<category><![CDATA[Firmware Bugs]]></category>
		<category><![CDATA[RTOS Multithreading]]></category>

		<guid isPermaLink="false">http://embeddedgurus.com/state-space/?p=35</guid>
		<description><![CDATA[I have to confess that I&#8217;ve been experiencing a severe writer&#8217;s block lately. It&#8217;s not that I&#8217;m short of subjects to talk about, but I&#8217;m getting tired of circling around the most important issues that matter to me most and should matter the most to any embedded software developer. I mean the basic software structure. [...]]]></description>
			<content:encoded><![CDATA[<p>I have to confess that I&#8217;ve been experiencing a severe writer&#8217;s block lately. It&#8217;s not that I&#8217;m short of subjects to talk about, but I&#8217;m getting tired of circling around the most important issues that matter to me most and should matter the most to any embedded software developer. I mean the basic software structure.</p>
<p>Unfortunately, I find it impossible to talk about truly important issues without stepping on somebody&#8217;s toes, which means picking a fight. So, in this installment I decided to come out of the closet and say it openly: I hate RTOSes.</p>
<p>The main reason I say so is because a conventional RTOS implies a certain programming paradigm, which leads to particularly brittle designs. I&#8217;m talking about blocking. Blocking occurs any time you wait explicitly in-line for something to happen. All RTOSes provide an assortment of blocking mechanisms, such as various semaphores, event-flags, mailboxes, message queues, and so on. Every RTOS task, structured as an endless loop, must use at least one such blocking mechanism, or else it will take all the CPU cycles. Typically, however, tasks block in many places scattered throughout various functions called from the task routine (the endless loop). For example, a task can block and wait for a semaphore that indicates end of an ADC conversion. In other part of the code, the same task might wait for a timeout event flag, and so on.</p>
<p>Blocking is evil, because it appears to work initially, but quickly degenerates into a unmanageable mess. The problem is that while a task is blocked, the task is not doing any other work and is not responsive to other events. Such task cannot be easily extended to handle other events, not just because the system is unresponsive, but also due to the fact the the whole structure of the code past the blocking call is designed to handle only the event that it was explicitly waiting for.</p>
<p>You might think that difficulty of adding new features (events and behaviors) to such designs is only important later, when the original software is maintained or reused for the next similar project. I disagree. Flexibility is vital from day one. Any application of nontrivial complexity is developed over time by gradually adding new events and behaviors. The inflexibility prevents an application to grow that way, so the design degenerates in the process known as architectural decay. This in turn makes it often impossible to even finish the original application, let alone maintain it.</p>
<p>The mechanisms of architectural decay of RTOS-based applications are manifold, but perhaps the worst is unnecessary proliferation of tasks. Designers, unable to add new events to unresponsive tasks are forced to create new tasks, regardless of coupling and cohesion. Often the new feature uses the same data as other feature in another tasks (we call such features cohesive). But placing the new feature in a different task requires very careful sharing of the common data. So mutexes and other such mechanisms must be applied. The designer ends up spending most of the time not on the feature at hand, but on managing subtle, hairy, unintended side-effects.</p>
<p>For decades embedded engineers were taught to believe that the only two alternatives for structuring embedded software are a &#8220;superloop&#8221; (main+ISRs) or an RTOS. But this is of course not true. Other alternatives exist, specifically event-driven programming with modern state machines is a much better way. It is not a silver bullet, of course, but after having used this method extensively for over a decade I will never go back to a raw RTOS. I plan to write more about this better way, why it is better and where it is still weak. Stay tuned.</p>
]]></content:encoded>
			<wfw:commentRss>http://embeddedgurus.com/state-space/2010/04/i-hate-rtoses/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Free store is not free lunch</title>
		<link>http://embeddedgurus.com/state-space/2010/01/free-store-is-not-free-lunch/</link>
		<comments>http://embeddedgurus.com/state-space/2010/01/free-store-is-not-free-lunch/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 19:27:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Coding Standards]]></category>

		<guid isPermaLink="false">http://embeddedgurus.com/state-space/2010/01/29/free-store-is-not-free-lunch/</guid>
		<description><![CDATA[In my previous post &#8220;A Heap of Problems&#8221; I have compiled a list of problems the free store (heap) can cause in real-time embedded (RTE) systems. This was quite a litany, although I didn’t even touch the more subtle problems yet (for example, the C++ exception handling mechanism can cause memory leaks when a thrown [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous post &#8220;<a href="http://www.embeddedgurus.net/state-space/2010/01/heap-of-problems.html">A Heap of Problems</a>&#8221; I have compiled a list of problems the free store (heap) can cause in real-time embedded (RTE) systems. This was quite a litany, although I didn’t even touch the more subtle problems yet (for example, the C++ exception handling mechanism can cause memory leaks when a thrown exception bypasses memory de-allocation).</p>
<p>But even though the free store is definitely not a free lunch, getting by without the heap is certainly easier said than done. In C, you will have to rethink implementations that use lists, trees, and other dynamic data structures. You’ll also have to severely limit your choice of the third-party libraries and legacy code you want to reuse (especially if you borrow code designed for the desktop). In C++, the implications are even more serious because the object-oriented nature of C++ applications results in much more intensive dynamic-memory use than in applications using procedural techniques. For example, most standard C++ libraries (e.g., STL, Boost, etc.) requrie the heap. Without it, C++ simply does not feel like the same language.</p>
<p>Here are a few common sense guidelines for dealing with the heap:</p>
<p>1. For smaller systems, such as microcontrollers with only on-chip RAM, you probably don&#8217;t want to open the heap can of worms at all. The problems and waste that goes with the heap aren&#8217;t simply worth the trouble.</p>
<p>For systems with sufficient RAM, such as processors with megabytes of external DRAM, trading some of this cheap RAM for convenience in programming might be a reasonable deal. In the following discussion I assume that the system is big enough to run under a preemptive RTOS.</p>
<p>2. The simplest option is to limit the use of the heap to just one task. In this case, heap is not being shared concurrently and does not need any mutual-exclusion protection mechanism. To limit the non-determinism of the heap, I would recommend assigning low priority to the task that uses the heap. The priority should be lower than any real-time task.</p>
<p>3. At the expense of introducing a mutual protection to *all* heap operations (e.g., a mutex), you can allow more than one task to use the heap. However, I would still strongly recommend against using the heap in any tasks with real-time deadlines. All tasks that use the heap should run at a lower priority than any of the real-time tasks.</p>
<p>4. In any case, heap should never be used inside the interrupt service routines (ISRs).</p>
<p>In summary, using the heap in real-time embedded (RTE) systems always requires extra thought and discipline. You should always make sure that the heap is correctly integrated with your runtime environment.</p>
]]></content:encoded>
			<wfw:commentRss>http://embeddedgurus.com/state-space/2010/01/free-store-is-not-free-lunch/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

