<?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>Eli Bendersky's website &#187; EE / Embedded</title>
	<atom:link href="http://eli.thegreenplace.net/category/ee-embedded/feed/" rel="self" type="application/rss+xml" />
	<link>http://eli.thegreenplace.net</link>
	<description>Eli Bendersky's personal website</description>
	<lastBuildDate>Fri, 30 Jul 2010 12:30:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Introducing Luz</title>
		<link>http://eli.thegreenplace.net/2010/05/05/introducing-luz/</link>
		<comments>http://eli.thegreenplace.net/2010/05/05/introducing-luz/#comments</comments>
		<pubDate>Wed, 05 May 2010 17:43:38 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[Assembly]]></category>
		<category><![CDATA[EE / Embedded]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=2164</guid>
		<description><![CDATA[OK, so the documentation still isn&#8217;t complete, but I can&#8217;t wait to introduce my newest concoction &#8211; Luz. Luz is a pure-Python implementation of a MIPS-like CPU (as a simulator, of course). This CPU is programmable in an assembly language, a complete assembler for which has been implemented, along with a linker that takes together [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/03/13/python-documentation-annoyance/' rel='bookmark' title='Permanent Link: Python documentation annoyance'>Python documentation annoyance</a> <small>Edit: I&#8217;ve actually started working on fixing this annoyance in...</small></li><li><a href='http://eli.thegreenplace.net/2005/02/20/mix-implementation-in-perl-completed/' rel='bookmark' title='Permanent Link: MIX implementation in Perl completed !'>MIX implementation in Perl completed !</a> <small>I&#8217;ve recently completed my Perl implementation of Knuth&#8217;s MIX/MIXAL (Perlmix)....</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>OK, so the documentation still isn&#8217;t complete, but I can&#8217;t wait to introduce my newest concoction &#8211; <a href="http://code.google.com/p/luz-cpu/">Luz</a>. Luz is a pure-Python implementation of a MIPS-like CPU (as a simulator, of course). This CPU is programmable in an assembly language, a complete assembler for which has been implemented, along with a linker that takes together several object files and creates an executable image to run on the simulator. Oh, and did I mention that it also includes a rudimentary debugger and disassembler? All of this is Luz:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/uploads/2010/05/luz_proj_toplevel.png" alt="" title="luz_proj_toplevel" width="437" height="952" class="aligncenter size-full wp-image-2165" /></p>
<p>To call Luz new is a bit of a stretch, because I started working on it more than two years ago. It has been a jagged road, with occasional spurts of productivity, but now Luz is finally in a presentable form.</p>
<p>I&#8217;ll paste from its &#8220;getting started guide&#8221;:</p>
<blockquote><p>
<strong>What is Luz useful for?</strong></p>
<p>I don&#8217;t know yet. It&#8217;s a self-educational project of mine, and I learned a lot by working on it. I suppose that Luz&#8217;s main value is as an educational tool. Its implementation focuses on simplicity and modularity, and is done in Python, which is a portable and very readable high-level language.</p>
<p>Luz can serve as a sample of implementing a complete assembler, a complete linker, a complete CPU simulator. Other such tools exist, but usually not in the clean and self-contained form offered by Luz. In any case, if you&#8217;ve found Luz iseful, I&#8217;d love to receive feedback.</p></blockquote>
<p>This summarizes it, really. Not much more to add, except that Luz is available from its <a href="http://code.google.com/p/luz-cpu/">Google Code project</a> in source-only form for now, so you&#8217;ll have to check it out from SVN or just look at the sources in the online browser. Checking the source out is recommended because it allows one to view the documentation in nice HTML format. A few example programs in Luz assembly are available. Luz requires Python 2.6 or higher and the PLY module installed. I tested it on Windows XP and Ubuntu.</p>
<p>I&#8217;ve written <a href="http://eli.thegreenplace.net/2005/02/20/mix-implementation-in-perl-completed/">an assembler and a CPU simulator before</a>, but that was for a very weird architecture (Knuth&#8217;s MIX from TAOCP). Luz is a much more useful beast &#8211; the CPU is not far from real modern CPUs (the embedded kind, mostly), the assembly language is familiar and best of all, Luz also includes a linker, which will make it much easier to compile C for it in the future.</p>
<p>I&#8217;ll write more about Luz in sometime later, when I find the time to work on its documentation.</p>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=2164&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/03/13/python-documentation-annoyance/' rel='bookmark' title='Permanent Link: Python documentation annoyance'>Python documentation annoyance</a> <small>Edit: I&#8217;ve actually started working on fixing this annoyance in...</small></li><li><a href='http://eli.thegreenplace.net/2005/02/20/mix-implementation-in-perl-completed/' rel='bookmark' title='Permanent Link: MIX implementation in Perl completed !'>MIX implementation in Perl completed !</a> <small>I&#8217;ve recently completed my Perl implementation of Knuth&#8217;s MIX/MIXAL (Perlmix)....</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2010/05/05/introducing-luz/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Framing in serial communications</title>
		<link>http://eli.thegreenplace.net/2009/08/12/framing-in-serial-communications/</link>
		<comments>http://eli.thegreenplace.net/2009/08/12/framing-in-serial-communications/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 03:16:47 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[EE / Embedded]]></category>
		<category><![CDATA[Serial port]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1872</guid>
		<description><![CDATA[
Introduction
In the previous post we&#8217;ve seen how to send and receive data on the serial port with Python and plot it live using a pretty GUI.
Notice that the sender script (sender_sim.py) is just sending one byte at a time. The &#34;chunks&#34; of data in the protocol between the sender and receiver are single bytes. This [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/08/20/frames-and-protocols-for-the-serial-port-in-python/' rel='bookmark' title='Permanent Link: Frames and protocols for the serial port &#8211; in Python'>Frames and protocols for the serial port &#8211; in Python</a> <small> Some preliminaries If you&#8217;ve been following this blog recently,...</small></li><li><a href='http://eli.thegreenplace.net/2005/11/22/when-bit-endianness-matters/' rel='bookmark' title='Permanent Link: when bit endianness matters'>when bit endianness matters</a> <small>The smallest addresable information unit in a computer is a...</small></li><li><a href='http://eli.thegreenplace.net/2006/02/13/once-again-perl-serial-ports-and-whats-between-them/' rel='bookmark' title='Permanent Link: once again: perl, serial ports and what&#8217;s between them'>once again: perl, serial ports and what&#8217;s between them</a> <small>As I wrote here, I managed to avoid using the...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<div class="section" id="introduction">
<h3>Introduction</h3>
<p>In the <a class="reference external" href="http://eli.thegreenplace.net/2009/08/07/a-live-data-monitor-with-python-pyqt-and-pyserial/">previous post</a> we&#8217;ve seen how to send and receive data on the serial port with Python and plot it live using a pretty GUI.</p>
<p>Notice that the sender script (<a class="reference external" href="http://eli.thegreenplace.net/wp-content/uploads/2009/08/sender_sim.py">sender_sim.py</a>) is just sending one byte at a time. The &quot;chunks&quot; of data in the protocol between the sender and receiver are single bytes. This is simple and convenient, but hardly sufficient in the general sense. We want to be able to send multiple-byte data frames between the communicating parties.</p>
<p>However, there are some challenges that arise immediately:</p>
<ul class="simple">
<li>The receiver is just receiving a stream of bytes from the serial port. How does it know when a message begins or ends? How does it know how long the message is?</li>
<li>Even more seriously, we can not assume a noise-free channel. This is real, physical hardware stuff. Bytes and whole chunks can and will be lost due to electrical noise. Worse, other bytes will be distorted (say, a single bit can be flipped due to noise).</li>
</ul>
<p>To see how this can be done in a safe and tested manner, we first have to learn about the basics of the Data Link Layer in computer networks.</p>
</div>
<div class="section" id="data-link-layer">
<h3>Data Link Layer</h3>
<p>Given a physical layer that can transmit signals between devices, the job of the Data Link Layer <a class="footnote-reference" href="#id9" id="id1">[1]</a> is (roughly stated) to transmit whole frames of data, with some means of assuring the integrity of the data (lack of errors). When we use sockets to communicate over TCP or UDP on the internet, the framing is taken care of deep in the hardware, and we don&#8217;t even feel it. On the serial port, however, we must take care of the framing and error handling ourselves <a class="footnote-reference" href="#id10" id="id2">[2]</a>.</p>
<div class="section" id="framing">
<h4>Framing</h4>
<p>In chapter 3 of his <a class="reference external" href="http://eli.thegreenplace.net/2009/08/08/book-review-computer-networks-4th-edition-by-andrew-tanenbaum/">&quot;Computer Networks&quot;</a> textbook, Tanenbaum defines the following methods of framing:</p>
<ol class="arabic simple">
<li>Inserting time gaps between frames</li>
<li>Physical layer coding violations</li>
<li>Character count</li>
<li>Flag bytes with byte stuffing</li>
<li>Flag bytes with bit stuffing</li>
</ol>
<p>Methods (1) and (2) are only suitable for a hardware-implemented data link layer <a class="footnote-reference" href="#id11" id="id3">[3]</a>. It is very difficult (read: impossible) to ensure timing when multiple layers of software (running on Windows!) are involved. (2) is an interesting hardware method &#8211; but out of the scope of this article.</p>
<p>Method (3) means specifying in the frame header the number of bytes in the frame. The trouble with this is that the count can be garbled by a transmission error. In such a case, it&#8217;s very difficult to &quot;resynchronize&quot;. This method is rarely used.</p>
<p>Methods (4) and (5) are somewhat similar. In this article I&#8217;ll focus on (4), as (5) is not suitable for serial port communications.</p>
</div>
<div class="section" id="flag-bytes-with-byte-stuffing">
<h4>Flag bytes with byte stuffing</h4>
<p>Let&#8217;s begin with a simple idea and develop it into a full, robust scheme.</p>
<p><em>Flag bytes</em> are special byte values that denote when a frame begins and ends. Suppose that we want to be able to send frames of arbitrary length. A special start flag byte will denote the beginning of the frame, and an end flag byte will denote its end.</p>
<p><img alt="http://eli.thegreenplace.net/wp-content/uploads/2009/08/flags_data.png" src="http://eli.thegreenplace.net/wp-content/uploads/2009/08/flags_data.png" /></p>
<p>A question arises, however. Suppose that the value of the end flag is 0&#215;98. What if the value 0&#215;98 appears somewhere in the data? The protocol will get confused and end the message.</p>
<p>There is a simple solution to this problem that will be familiar to all programmers who know about escaping quotes and special characters in strings. It is called <em>byte stuffing</em>, or <em>octet stuffing</em>, or simply <em>escaping</em> <a class="footnote-reference" href="#id12" id="id4">[4]</a>. The scheme goes as follows:</p>
<ul class="simple">
<li>Whenever a flag (start or end) byte appears in the data, we shall insert a special escape byte (ESC) before it. When the receiver sees an ESC, it knows to ignore it and not insert it into the actual data received (de-stuffing).</li>
<li>Whenever ESC itself has to appear in the data, another ESC is prepended to it. The receiver removes the first one but keeps the second one <a class="footnote-reference" href="#id13" id="id5">[5]</a>.</li>
</ul>
<p>Here are a few examples:</p>
<p><img alt="http://eli.thegreenplace.net/wp-content/uploads/2009/08/escaping.png" src="http://eli.thegreenplace.net/wp-content/uploads/2009/08/escaping.png" /></p>
<p>Note that we didn&#8217;t specify what the data is &#8211; it&#8217;s arbitrary and up the the protocol to decide. The only really required part of the data is some kind of error checking &#8211; a checksum, or better yet a CRC. This is customarily the last byte (or last word) of the frame, referring to all the bytes in the frame (in its un-stuffed form).</p>
<p>This scheme is quite robust: any lost byte (be it a flag, an escape, a data byte or a checksum byte) will cause the receiver to lose just one frame, after which it will resynchronize onto the start flag byte of the next one.</p>
</div>
<div class="section" id="ppp">
<h4>PPP</h4>
<p>As a matter of fact, this method is a slight simplification of the <a class="reference external" href="http://en.wikipedia.org/wiki/Point-to-Point_Protocol">Point-to-Point Protocol</a> (PPP) which is used by most ISPs for providing ADSL internet to home users, so there&#8217;s a good chance you&#8217;re using it now to surf the net and read this article! The framing of PPP is defined in <a class="reference external" href="http://tools.ietf.org/html/rfc1662">RFC 1662</a>.</p>
<p>In particular, PPP does the following:</p>
<ul class="simple">
<li>Both the start and end flag bytes are 0&#215;7E (they shouldn&#8217;t really be different, if you think about it)</li>
<li>The escape byte is 0&#215;7D</li>
<li>Whenever a flag or escape byte appears in the message, it is escaped by 0&#215;7D and the byte itself is XOR-ed with 0&#215;20. So, for example 0&#215;7E becomes 0&#215;7D 0&#215;5E. Similarly 0&#215;7D becomes 0&#215;7D 0&#215;5D. The receiver unsuffs the escape byte and XORs the next byte with 0&#215;20 again to get the original <a class="footnote-reference" href="#id14" id="id6">[6]</a>.</li>
</ul>
</div>
</div>
<div class="section" id="an-example">
<h3>An example</h3>
<p>Let&#8217;s now see a completely worked-out example that demonstrates how this works.</p>
<p>Suppose we define the following protocol:</p>
<ul class="simple">
<li>Start flag: 0&#215;12</li>
<li>End flag: 0&#215;13</li>
<li>Escape (DLE): 0&#215;7D</li>
</ul>
<p>And the sender wants to send the following data message (let&#8217;s ignore its contents for the sake of the example &#8211; they&#8217;re really not that important). The original data is in <strong>(a)</strong>:</p>
<p><img alt="http://eli.thegreenplace.net/wp-content/uploads/2009/08/example1.png" src="http://eli.thegreenplace.net/wp-content/uploads/2009/08/example1.png" /></p>
<p>The data contains two flags that need to be escaped &#8211; an end flag at position 2 (counting from 0, of course!), and a DLE at position 4.</p>
<p>The sender&#8217;s data link layer <a class="footnote-reference" href="#id15" id="id7">[7]</a> turns the data into the frame shown in <strong>(b)</strong> &#8211; start and end flags are added, and in-message flags are escaped.</p>
<p>Let&#8217;s see how the receiver handles such a frame. For demonstration, assume that the first byte the receiver draws from the serial port is not a real part of the message (we want to see how it handles this). In the following diagram, &#8216;Receiver state&#8217; is the state of the receiver <em>after</em> the received byte. &#8216;Data buffer&#8217; is the currently accumulated message buffer to pass to an upper level:</p>
<p><img alt="http://eli.thegreenplace.net/wp-content/uploads/2009/08/example1_rcv.png" src="http://eli.thegreenplace.net/wp-content/uploads/2009/08/example1_rcv.png" /></p>
<p>A few things to note:</p>
<ul class="simple">
<li>The &quot;stray&quot; byte before the header is ignored: according to the protocol each frame has to start with a header, so this isn&#8217;t part of the frame.</li>
<li>The start and end flags are not inserted into the data buffer</li>
<li>Escapes (DLEs) are correctly handled by a special state</li>
<li>When the frame is finished with an end flag, the receiver has a frame ready to pass to an upper level, and comes back waiting for a header &#8211; a new frame.</li>
</ul>
<p>Finally, we see that the message received is exactly the message sent. All the protocol details (flags, escapes and so on) were transparently handled by the data link layer <a class="footnote-reference" href="#id16" id="id8">[8]</a>.</p>
</div>
<div class="section" id="conclusion">
<h3>Conclusion</h3>
<p>There are several methods of handling framing in communications, although most are unsuitable to be used on top of the serial port. Among the ones that are suitable, the most commonly used is <em>byte stuffing</em>. By defining a couple of &quot;magic value&quot; flags and careful rules of escaping, this framing methods is both robust and easy to implement as a software layer. It is also widely used as PPP depends on it.</p>
<p>Finally, it&#8217;s important to remember that for a high level of robustness, it&#8217;s required to add some kind of error checking into the protocol &#8211; such as computing a CRC on the message and appending it as the last word of the message, which the receiver can verify before deciding that the message is valid.</p>
<div align="center" class="align-center"><img alt="http://eli.thegreenplace.net/wp-content/uploads/hline.jpg" class="align-center" src="http://eli.thegreenplace.net/wp-content/uploads/hline.jpg" style="width: 320px; height: 5px;" /></div>
<table class="docutils footnote" frame="void" id="id9" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id1">[1]</a></td>
<td>The Data Link Layer is layer 2 in the <a class="reference external" href="http://en.wikipedia.org/wiki/OSI_model">OSI model</a>. In the <a class="reference external" href="http://en.wikipedia.org/wiki/TCP/IP_model">TCP/IP model</a> it&#8217;s simply called the &quot;link layer&quot;.</td>
</tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id10" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id2">[2]</a></td>
<td>The serial port can be configured to add parity bits to bytes. These days, this option is rarely used, because:</td>
</tr>
</tbody>
</table>
<ul class="simple">
<li>A single parity bit isn&#8217;t a very strong means of detecting errors. 2-bit errors fool it.</li>
<li>Error handling is usually done by stronger means at a higher level.</li>
</ul>
<table class="docutils footnote" frame="void" id="id11" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id3">[3]</a></td>
<td>For example Ethernet (802.3) uses 12 octets of idle characters between frames.</td>
</tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id12" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id4">[4]</a></td>
<td>You might run into the term DLE &#8211; Data Link Escape, which means the same thing. I will use the acronyms DLE and ESC interchangeably.</td>
</tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id13" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id5">[5]</a></td>
<td>Just like quotes and escape characters in strings! In C: <tt class="docutils literal"><span class="pre">&quot;I</span> <span class="pre">say</span> <span class="pre">\&quot;Hello\&quot;&quot;</span></tt>. To escape the escape, repeat it: <tt class="docutils literal"><span class="pre">&quot;Here</span> <span class="pre">comes</span> <span class="pre">the</span> <span class="pre">backslash:</span> <span class="pre">\\</span> <span class="pre">-</span> <span class="pre">seen</span> <span class="pre">it?&quot;</span></tt></td>
</tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id14" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id6">[6]</a></td>
<td>I&#8217;d love to hear why this XOR-ing is required. One simple reason I can think of is to prevent the flag and escape bytes appearing &quot;on the line&quot; even after they&#8217;re escaped. Presumably this improves resynchronization if the escape byte is lost?</td>
</tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id15" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id7">[7]</a></td>
<td>Which is just a fancy way to say &quot;a protocol wrapping function&quot;, since the layer is implemented in software.</td>
</tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id16" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id8">[8]</a></td>
<td>Such transparency is one of the greatest ideas of layered network protocols. So when we implement protocols in software, it&#8217;s a good thing to keep in mind &#8211; transparency aids modularity and decoupling, it&#8217;s a <em>good thing</em>.</td>
</tr>
</tbody>
</table>
</div>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=1872&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/08/20/frames-and-protocols-for-the-serial-port-in-python/' rel='bookmark' title='Permanent Link: Frames and protocols for the serial port &#8211; in Python'>Frames and protocols for the serial port &#8211; in Python</a> <small> Some preliminaries If you&#8217;ve been following this blog recently,...</small></li><li><a href='http://eli.thegreenplace.net/2005/11/22/when-bit-endianness-matters/' rel='bookmark' title='Permanent Link: when bit endianness matters'>when bit endianness matters</a> <small>The smallest addresable information unit in a computer is a...</small></li><li><a href='http://eli.thegreenplace.net/2006/02/13/once-again-perl-serial-ports-and-whats-between-them/' rel='bookmark' title='Permanent Link: once again: perl, serial ports and what&#8217;s between them'>once again: perl, serial ports and what&#8217;s between them</a> <small>As I wrote here, I managed to avoid using the...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2009/08/12/framing-in-serial-communications/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Book review: &#8220;Tab electronics guide to understanding Electricity and Electronics&#8221; by G. Randy Slone</title>
		<link>http://eli.thegreenplace.net/2009/07/10/book-review-tab-electronics-guide-to-understanding-electricity-and-electronics-by-g-randy-slone/</link>
		<comments>http://eli.thegreenplace.net/2009/07/10/book-review-tab-electronics-guide-to-understanding-electricity-and-electronics-by-g-randy-slone/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 08:28:41 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[Book reviews]]></category>
		<category><![CDATA[EE / Embedded]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1781</guid>
		<description><![CDATA[This book has two main aims. One is to teach the basics of electronics. The other is to serve as a guide to electronics hobbyists for setting up a lab to build stuff and experiment with circuits. It is the second aim which spurred me to purchase it. 
I think the best way to present [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2005/08/07/book-review-designing-embedded-hardware-by-john-catsoulis/' rel='bookmark' title='Permanent Link: Book review: &#8220;Designing embedded hardware&#8221; by John Catsoulis'>Book review: &#8220;Designing embedded hardware&#8221; by John Catsoulis</a> <small>This book aims at a very wide domain &#8211; embedded...</small></li><li><a href='http://eli.thegreenplace.net/2003/05/13/book-review-the-designers-guide-to-vhdl-by-p-ashenden/' rel='bookmark' title='Permanent Link: Book review &#8211; The Designer&#8217;s Guide to VHDL (by P. Ashenden)'>Book review &#8211; The Designer&#8217;s Guide to VHDL (by P. Ashenden)</a> <small>I used VHDL on a beginner&#8217;s level a couple of...</small></li><li><a href='http://eli.thegreenplace.net/2005/06/13/book-review-the-pocket-guide-to-thailand-by-berlitz/' rel='bookmark' title='Permanent Link: Book review: &#8220;The pocket guide to Thailand&#8221; by Berlitz'>Book review: &#8220;The pocket guide to Thailand&#8221; by Berlitz</a> <small>This small booklet was recommented by a friend who used...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>This book has two main aims. One is to teach the basics of electronics. The other is to serve as a guide to electronics hobbyists for setting up a lab to build stuff and experiment with circuits. It is the second aim which spurred me to purchase it. </p>
<p>I think the best way to present this review is as a list of pros and cons of the book. Pros:</p>
<ol>
<li>As far as I can tell (mainly from work experience) without actually building anything following the book&#8217;s directions, the advice given on setting up a workbench is correct, interesting and easy to follow. Since this is one of the main goals of the book, it&#8217;s an important point in its favor.</li>
<li>Basic electronics, up to and including BJTs are explained quite well &#8211; I think that any intelligent beginner can learn a lot from this book, even without prior experience. The author manages to leverage intuition in useful ways whenever possible, and doesn&#8217;t get into the complex math you often see in textbooks on the subject.</li>
<li>The book contains a lot of interesting and useful circuits to build, in specially designated &#8220;Circuit Porpourri&#8221; sections. After some point in the book, the author presents several circuits at the end of each chapter, explaining in brief what they do and specifying which components to purchase, and how to connect everything together.</li>
</ol>
<p>Cons:</p>
<ol>
<li>The author has made an unfortunate decision to use electron current flow (current flows from negative to positive) instead of the conventional current flow used almost universally throughout the industry. While electron flow is admittedly more correct in most cases, convention plays a strong role here, and the author should&#8217;ve followed it. Doesn&#8217;t it feel funny for a beginner to read that &#8220;in the transistor, the current goes against the arrow&#8221;? Why against?
</li>
<li>Although the second edition is from 1990, the book is dated. This is clearly seen in the equipment photographs the author uses, as well as in some advice. For instance, AFAIK, no one uses paper catalogs for components these days &#8211; datasheets are downloaded from the web and catalogs are online too.
</li>
<li>After the chapter on BJTs, the wind ran out in the author&#8217;s sails for clear explanations. MOSFETs are explained in a very sketchy way, and op-amps are left practically without any explanation at all. Nevertheless the author keeps piling up circuits assuming the reader will just learn this stuff in some other place?
</li>
</ol>
<p>That said, for $16 (darn cheap for books on Electronics) the book isn&#8217;t a bad deal. Especially if you&#8217;re looking into setting up your own electronics lab, this book can be a valuable first guide. I wouldn&#8217;t recommend seriously attempting to learn electronics with it as the only source, though. In this aspect, this book can serve as only a basic introduction, with more serious texts being a must for deeper understanding of the more advanced concepts circuits.</p>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=1781&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2005/08/07/book-review-designing-embedded-hardware-by-john-catsoulis/' rel='bookmark' title='Permanent Link: Book review: &#8220;Designing embedded hardware&#8221; by John Catsoulis'>Book review: &#8220;Designing embedded hardware&#8221; by John Catsoulis</a> <small>This book aims at a very wide domain &#8211; embedded...</small></li><li><a href='http://eli.thegreenplace.net/2003/05/13/book-review-the-designers-guide-to-vhdl-by-p-ashenden/' rel='bookmark' title='Permanent Link: Book review &#8211; The Designer&#8217;s Guide to VHDL (by P. Ashenden)'>Book review &#8211; The Designer&#8217;s Guide to VHDL (by P. Ashenden)</a> <small>I used VHDL on a beginner&#8217;s level a couple of...</small></li><li><a href='http://eli.thegreenplace.net/2005/06/13/book-review-the-pocket-guide-to-thailand-by-berlitz/' rel='bookmark' title='Permanent Link: Book review: &#8220;The pocket guide to Thailand&#8221; by Berlitz'>Book review: &#8220;The pocket guide to Thailand&#8221; by Berlitz</a> <small>This small booklet was recommented by a friend who used...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2009/07/10/book-review-tab-electronics-guide-to-understanding-electricity-and-electronics-by-g-randy-slone/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Solution to the RC circuit puzzle</title>
		<link>http://eli.thegreenplace.net/2008/12/26/solution-to-the-rc-circuit-puzzle/</link>
		<comments>http://eli.thegreenplace.net/2008/12/26/solution-to-the-rc-circuit-puzzle/#comments</comments>
		<pubDate>Fri, 26 Dec 2008 08:21:45 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[EE / Embedded]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1254</guid>
		<description><![CDATA[Here, as promised, is the solution to the RC circuit puzzle I posted earlier this week.
Let&#8217;s look at the circuit again:

The problem with my reasoning was the direction of current in the capacitor. I&#8217;ve quietly assumed that:

But this is wrong for the circuit above. Why? Because we must obey the voltage &#038; current directions we&#8217;ve [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2008/12/22/an-rc-circuit-puzzle/' rel='bookmark' title='Permanent Link: An RC circuit puzzle'>An RC circuit puzzle</a> <small>If you&#8217;re interested in electronics, you&#8217;ll find the following simple...</small></li><li><a href='http://eli.thegreenplace.net/2009/07/10/book-review-tab-electronics-guide-to-understanding-electricity-and-electronics-by-g-randy-slone/' rel='bookmark' title='Permanent Link: Book review: &#8220;Tab electronics guide to understanding Electricity and Electronics&#8221; by G. Randy Slone'>Book review: &#8220;Tab electronics guide to understanding Electricity and Electronics&#8221; by G. Randy Slone</a> <small>This book has two main aims. One is to teach...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Here, as promised, is the solution to the <a href="http://eli.thegreenplace.net/2008/12/22/an-rc-circuit-puzzlean-rc-circuit-puzzle/">RC circuit puzzle</a> I posted earlier this week.</p>
<p>Let&#8217;s look at the circuit again:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/uploads/2008/12/cap_resistor.png" alt="RC circuit" /></p>
<p>The problem with my reasoning was the direction of current in the capacitor. I&#8217;ve quietly assumed that:</p>
<p><center><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-83f048e5b2d5ee7a404a1e96521b5fcf.gif" alt="I_{c}(t) = C\frac{dV_{c}(t)}{dt}" title="I_{c}(t) = C\frac{dV_{c}(t)}{dt}" style="vertical-align: -6px; border: none;"/></center></p>
<p>But this is wrong for the circuit above. Why? Because we must obey the voltage &#038; current directions we&#8217;ve chosen. In passive elements, the positive current flows from the higher voltage to the lower voltage, meaning that in our circuit:</p>
<p><center><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-1561b8629446e85c174f0fd2a2ca6e46.gif" alt="I_{c}(t) = -C\frac{dV_{c}(t)}{dt}" title="I_{c}(t) = -C\frac{dV_{c}(t)}{dt}" style="vertical-align: -6px; border: none;"/></center></p>
<p>This small minus sign makes all the difference, and now the solution will be correct.</p>
<p>Physically, the intuition is that the current here flows from a discharging capacitor, hence it&#8217;s &#8220;against&#8221; the voltage direction. Had it been a capacitor-charging circuit, there would be no confusion.</p>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=1254&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2008/12/22/an-rc-circuit-puzzle/' rel='bookmark' title='Permanent Link: An RC circuit puzzle'>An RC circuit puzzle</a> <small>If you&#8217;re interested in electronics, you&#8217;ll find the following simple...</small></li><li><a href='http://eli.thegreenplace.net/2009/07/10/book-review-tab-electronics-guide-to-understanding-electricity-and-electronics-by-g-randy-slone/' rel='bookmark' title='Permanent Link: Book review: &#8220;Tab electronics guide to understanding Electricity and Electronics&#8221; by G. Randy Slone'>Book review: &#8220;Tab electronics guide to understanding Electricity and Electronics&#8221; by G. Randy Slone</a> <small>This book has two main aims. One is to teach...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2008/12/26/solution-to-the-rc-circuit-puzzle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An RC circuit puzzle</title>
		<link>http://eli.thegreenplace.net/2008/12/22/an-rc-circuit-puzzle/</link>
		<comments>http://eli.thegreenplace.net/2008/12/22/an-rc-circuit-puzzle/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 19:16:05 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[EE / Embedded]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1241</guid>
		<description><![CDATA[If you&#8217;re interested in electronics, you&#8217;ll find the following simple &#8220;paradox&#8221; amusing. It&#8217;s the usual case of &#8220;proving that 2+2=5&#8243;. The fun is finding where the mistake in the reasoning is.
Consider the following circuit:

Assume that the capacitor is charged to some initial voltage before the switch is closed. At  time 0, the switch is [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2008/12/26/solution-to-the-rc-circuit-puzzle/' rel='bookmark' title='Permanent Link: Solution to the RC circuit puzzle'>Solution to the RC circuit puzzle</a> <small>Here, as promised, is the solution to the RC circuit...</small></li><li><a href='http://eli.thegreenplace.net/2003/10/24/a-probability-paradox/' rel='bookmark' title='Permanent Link: a probability paradox'>a probability paradox</a> <small>This paradox is very well known, but today I gave...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re interested in electronics, you&#8217;ll find the following simple &#8220;paradox&#8221; amusing. It&#8217;s the usual case of &#8220;proving that 2+2=5&#8243;. The fun is finding where the mistake in the reasoning is.</p>
<p>Consider the following circuit:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/uploads/2008/12/cap_resistor.png" alt="RC circuit" /></p>
<p>Assume that the capacitor is charged to some initial voltage before the switch is closed. At  time 0, the switch is closed. What is the current in the circuit as a function of time ? </p>
<p>Let&#8217;s solve it using the familiar RC circuit methods. We know that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-24e2ed277c1ac5782b8b3fa7cd3c90ff.gif" alt="V_{c}(t) = V_{R}(t)" title="V_{c}(t) = V_{R}(t)" style="vertical-align: -4px; border: none;"/> because of Kirchoff&#8217;s voltage law. We&#8217;ll differentiate both sides by time:</p>
<p><center><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-2b4f8936578370384679e2f68dc682c7.gif" alt="\dot{V}_{c}(t) = \dot{V}_{R}(t)" title="\dot{V}_{c}(t) = \dot{V}_{R}(t)" style="vertical-align: -4px; border: none;"/></center></p>
<p>We know that for a capacitor, the relation between current and voltage is:</p>
<p><center><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-237c808abd9927c3f0b1ed1c361c93e7.gif" alt="I_{c}(t) = C\frac{dV_{c}(t)}{dt}=\dot{V}_{c}(t)" title="I_{c}(t) = C\frac{dV_{c}(t)}{dt}=\dot{V}_{c}(t)" style="vertical-align: -6px; border: none;"/></center></p>
<p>Substituting it into the equation above and also recalling that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-4e0050996d5e50832a6b5e43ca175dc1.gif" alt="V_{R}(t)=RI_{R}(t)" title="V_{R}(t)=RI_{R}(t)" style="vertical-align: -4px; border: none;"/>, we get:</p>
<p><center><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-89b98a8f3417417a630f4d8f42a79dac.gif" alt="\frac{1}{C}I_{c}(t)=R\dot{I}_{R}(t)" title="\frac{1}{C}I_{c}(t)=R\dot{I}_{R}(t)" style="vertical-align: -6px; border: none;"/></center></p>
<p>But the current through the capacitor and resistor is the same current, so this can be rewritten simply as:</p>
<p><center><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-0cf0ca71e94be77f0ba08892e7d8c8a9.gif" alt="RC\dot{I}(t)-I(t)=0" title="RC\dot{I}(t)-I(t)=0" style="vertical-align: -4px; border: none;"/></center></p>
<p>This is a simple first order differential equation, the solution of which is:</p>
<p><center><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-7d4b8bc474cbec4e60aad293e54e5778.gif" alt="I(t)=I_{0}e^{\frac{t}{RC}}" title="I(t)=I_{0}e^{\frac{t}{RC}}" style="vertical-align: -4px; border: none;"/></center></p>
<p>For some initial current <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-bc970f9dbea49e7128d48e7cb09fff61.gif" alt="I_{0}" title="I_{0}" style="vertical-align: -3px; border: none;"/>. But wait a second, how can the exponent be positive, won&#8217;t it grow to infinity with time ? There&#8217;s obviously a mistake here, somewhere. Can you find it ?</p>
<p>This problem gave me some headache last night, and today I&#8217;ve successfully stumped a few co-workers with it. I&#8217;ll post a solution in a couple of days.</p>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=1241&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2008/12/26/solution-to-the-rc-circuit-puzzle/' rel='bookmark' title='Permanent Link: Solution to the RC circuit puzzle'>Solution to the RC circuit puzzle</a> <small>Here, as promised, is the solution to the RC circuit...</small></li><li><a href='http://eli.thegreenplace.net/2003/10/24/a-probability-paradox/' rel='bookmark' title='Permanent Link: a probability paradox'>a probability paradox</a> <small>This paradox is very well known, but today I gave...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2008/12/22/an-rc-circuit-puzzle/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>memmgr &#8211; a fixed-pool memory allocator</title>
		<link>http://eli.thegreenplace.net/2008/10/17/memmgr-a-fixed-pool-memory-allocator/</link>
		<comments>http://eli.thegreenplace.net/2008/10/17/memmgr-a-fixed-pool-memory-allocator/#comments</comments>
		<pubDate>Fri, 17 Oct 2008 09:44:17 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[EE / Embedded]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1062</guid>
		<description><![CDATA[In embedded systems, it is common to write code that runs on &#8220;bare metal&#8221;, i.e. without an operating system. On one hand, it is very empowering. When you write your main function (assuming it&#8217;s C, of course, but that&#8217;s a safe assumption for 95% of embedded code), you know it has the full control of [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2006/02/07/sloppy-code-sourceforge-cvs-memory-manager/' rel='bookmark' title='Permanent Link: sloppy code, SourceForge CVS, memory manager'>sloppy code, SourceForge CVS, memory manager</a> <small>Some random stuff: Sloppy code I hate code that looks...</small></li><li><a href='http://eli.thegreenplace.net/2009/10/30/handling-out-of-memory-conditions-in-c/' rel='bookmark' title='Permanent Link: Handling out-of-memory conditions in C'>Handling out-of-memory conditions in C</a> <small> We&#8217;ve all been taught that when malloc returns 0,...</small></li><li><a href='http://eli.thegreenplace.net/2009/12/21/book-review-memory-management-algorithms-and-implementation-in-cc-by-bill-blunden/' rel='bookmark' title='Permanent Link: Book review: &#8220;Memory management: Algorithms and Implementation in C/C++&#8221; by Bill Blunden'>Book review: &#8220;Memory management: Algorithms and Implementation in C/C++&#8221; by Bill Blunden</a> <small>Memory allocation is a fascinating area, ripe in trade-offs and...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>In embedded systems, it is common to write code that runs on &#8220;bare metal&#8221;, i.e. without an operating system. On one hand, it is very empowering. When you write your <code>main</code> function (assuming it&#8217;s C, of course, but that&#8217;s a safe assumption for 95% of embedded code), you know it has the full control of the processor. Your program is the brains of the chip &#8211; whatever you write, the chip performs, without any external code getting in your way.</p>
<p>On the other hand, code running this way misses many of the benefits operating systems provide. Process control, memory management, file system, and so on.</p>
<p>When writing code to run on bare metal, there are some special precautions one must take. One important point to consider is the heap &#8211; dynamic memory allocation. An embedded system (think of the safety controller of a Boeing plane) can&#8217;t just fail because the heap runs out. When <code>malloc</code> returns 0 to your desktop-application code, in most cases you will just bail out, because most probably it&#8217;s the system&#8217;s fault, and you don&#8217;t have much choice. In an embedded controller, this is not an option. There is nowhere to bail out to, and in any case, that heap memory ran out is <em>your fault</em>, a bug in your design or code.</p>
<p>To help managing these complications, embedded programmers often avoid heap allocation altogether, and only use static allocation (i.e. arrays allocated at compile (or more accurately &#8211; link/load) time). However, sometimes this is less than optimal, because:</p>
<ol>
<li>Dynamic allocation helps write code in a more convenient and reusable way.</li>
<li>You may be using some 3rd party code that uses dynamic allocation</li>
</ol>
<p>The solutions to this problem are numerous, but as any self-respecting embedded programmer, I wrote my own fixed-pool memory allocator. It provides a pair of functions:</p>
<pre><code>
// 'malloc' clone
//
void* memmgr_alloc(ulong nbytes);

// 'free' clone
//
void memmgr_free(void* ap);
</code></pre>
<p>That can be used as a drop-in replacement for <code>malloc</code> and <code>free</code>, but with a twist. There is no heap involved. All the memory is allocated from, and returned to, a fixed pool of memory that&#8217;s allocated at link time (in simpler terms: a static array). This way, you know the maximal amount of space your heap will take even before running the program, and can use these functions to test that your program indeed doesn&#8217;t allocate more than you assumed.</p>
<p>Moreover, the library allows a printout of allocation statistics (which you can enhance, the code is open) that will help diagnose allocation problems and memory leaks.</p>
<p>The library (350 LOC of ANSI C) can be downloaded <a href="http://eli.thegreenplace.net/files/prog_code/memmgr.zip">from here</a>. Let me know if you&#8217;ve found it useful.</p>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=1062&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2006/02/07/sloppy-code-sourceforge-cvs-memory-manager/' rel='bookmark' title='Permanent Link: sloppy code, SourceForge CVS, memory manager'>sloppy code, SourceForge CVS, memory manager</a> <small>Some random stuff: Sloppy code I hate code that looks...</small></li><li><a href='http://eli.thegreenplace.net/2009/10/30/handling-out-of-memory-conditions-in-c/' rel='bookmark' title='Permanent Link: Handling out-of-memory conditions in C'>Handling out-of-memory conditions in C</a> <small> We&#8217;ve all been taught that when malloc returns 0,...</small></li><li><a href='http://eli.thegreenplace.net/2009/12/21/book-review-memory-management-algorithms-and-implementation-in-cc-by-bill-blunden/' rel='bookmark' title='Permanent Link: Book review: &#8220;Memory management: Algorithms and Implementation in C/C++&#8221; by Bill Blunden'>Book review: &#8220;Memory management: Algorithms and Implementation in C/C++&#8221; by Bill Blunden</a> <small>Memory allocation is a fascinating area, ripe in trade-offs and...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2008/10/17/memmgr-a-fixed-pool-memory-allocator/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>TFTP</title>
		<link>http://eli.thegreenplace.net/2007/04/03/tftp/</link>
		<comments>http://eli.thegreenplace.net/2007/04/03/tftp/#comments</comments>
		<pubDate>Tue, 03 Apr 2007 10:42:39 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[EE / Embedded]]></category>
		<category><![CDATA[Internet]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/2007/04/03/tftp/</guid>
		<description><![CDATA[Some time ago I heard about TFTP &#8211; something I&#8217;ve never encountered before. TFTP is an acronym for Trivial File Transfer Protocol. Yes, like FTP, just Trivial. TFTP is a much watered down version of FTP &#8211; its only command is to transfer a file from a place to a place &#8211; no directory listing, [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/05/28/book-review-tcpip-illustrated-volume-1-the-protocols-by-w-richard-stevens/' rel='bookmark' title='Permanent Link: Book review: &#8220;TCP/IP Illustrated, Volume 1: The Protocols&#8221; by W. Richard Stevens'>Book review: &#8220;TCP/IP Illustrated, Volume 1: The Protocols&#8221; by W. Richard Stevens</a> <small>This book is arguably the de-facto bible of the TCP/IP...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Some time ago I heard about TFTP &#8211; something I&#8217;ve never encountered before. TFTP is an acronym for Trivial File Transfer Protocol. Yes, like FTP, just Trivial. TFTP is a much watered down version of FTP &#8211; its only command is to transfer a file from a place to a place &#8211; no directory listing, deleting, renaming, user authentication. What is it useful for, one may wonder.</p>
<p>Well, not every computer is a PC. In recent years more and more small embedded devices are becoming networked, and one of the best forms of networking is TCP / UDP / IP &#8211; the same set of protocols the Internet works on. </p>
<p>TFTP works on top of UDP, as opposed to FTP which works on TCP. UDP is a far simpler protocol than TCP, since it is a &#8220;send and forget&#8221; concept, without ensuring the correct arrival of data, in order, like TCP does. As a result, it is much easier to implement which leads to an implementation with a smaller footprint, and this is important for embedded devices. TFTP itself is also much simpler than FTP. It ensures the correct transfer of data by employing a simple <a href="http://www.erg.abdn.ac.uk/users/gorry/course/arq-pages/saw.html">stop and wait</a> protocol on top of UDP. I assume that it also makes it slower than FTP on non-congested networks, since FTP&#8217;s reliability is achieved on the TCP level which works in <a href="http://www.erg.abdn.ac.uk/users/gorry/course/arq-pages/sr.html">selective repeat</a>. However, simplicity is often more important than performance, especially for embedded devices with small amounts of ROM. </p>
<p>So TFTP is perfect for embedded devices to transfer data to and from each other (and PCs) in a reliable, quick way (UDP / IP on Ethernet is far faster than serial RS232 / RS485 communication, the most common interconnection method of embedded devices).</p>
<p>The research into TFTP led me through a few interesting sources of information, on Wikipedia, HowStuffWorks and RFCs. <a href="http://tools.ietf.org/html/rfc1180">RFC 1180</a> is especially helpful &#8211; it&#8217;s a tutorial written in a very readable style that explains the basics of IP, ARP, routing tables and TCP / UDP. <a href="http://tools.ietf.org/html/rfc1350">RFC 1350</a> describes TFTP. <a href="http://tools.ietf.org/html/rfc1123">RFC 1123</a> is a thorough collection of all Internet related protocols with cross references to other relevant RFCs.</p>
<p>The TCP / UDP / IP network stack is one of the nicest examples of sound engineering, and IMHO it is beneficial to get at least a superficial understanding of how these things work under the hood.</p>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=686&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/05/28/book-review-tcpip-illustrated-volume-1-the-protocols-by-w-richard-stevens/' rel='bookmark' title='Permanent Link: Book review: &#8220;TCP/IP Illustrated, Volume 1: The Protocols&#8221; by W. Richard Stevens'>Book review: &#8220;TCP/IP Illustrated, Volume 1: The Protocols&#8221; by W. Richard Stevens</a> <small>This book is arguably the de-facto bible of the TCP/IP...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2007/04/03/tftp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>On my way to Boston</title>
		<link>http://eli.thegreenplace.net/2006/09/21/on-my-way-to-boston/</link>
		<comments>http://eli.thegreenplace.net/2006/09/21/on-my-way-to-boston/#comments</comments>
		<pubDate>Thu, 21 Sep 2006 15:57:26 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[EE / Embedded]]></category>
		<category><![CDATA[Travel]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/2006/09/21/on-my-way-to-boston/</guid>
		<description><![CDATA[In a few hours I&#8217;ll take off from Tel Aviv towards USA, in order to attend the Embedded Systems Conference in Boston on September 25 &#8211; 28. The conference looks very interesting and there are a lot of sessions I&#8217;d like to visit &#8211; hopefully I&#8217;ll come back with new and valuable information, or at [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2006/09/26/boston-days-3-and-4/' rel='bookmark' title='Permanent Link: Boston days 3 and 4'>Boston days 3 and 4</a> <small>Sunday, Early morning When I woke up (at about 6...</small></li><li><a href='http://eli.thegreenplace.net/2006/09/22/boston-day-1/' rel='bookmark' title='Permanent Link: Boston &#8211; day 1'>Boston &#8211; day 1</a> <small>Here&#8217;s what I&#8217;ve been up to in the past 24...</small></li><li><a href='http://eli.thegreenplace.net/2006/09/24/boston-day-2/' rel='bookmark' title='Permanent Link: Boston &#8211; day 2'>Boston &#8211; day 2</a> <small>The rest of yesterday Just as I finished typing yesterday&#8217;s...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>In a few hours I&#8217;ll take off from Tel Aviv towards USA, in order to attend the <a href="http://www.embedded.com/esc/boston/">Embedded Systems Conference</a> in Boston on September 25 &#8211; 28. The conference looks very interesting and there are a lot of sessions I&#8217;d like to visit &#8211; hopefully I&#8217;ll come back with new and valuable information, or at least will get a feel of how things are done &#8220;in the outer world&#8221;. </p>
<p>Before the conference starts I&#8217;ll have a couple of days off to travel around Boston &#8211; which should be fun.</p>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=639&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2006/09/26/boston-days-3-and-4/' rel='bookmark' title='Permanent Link: Boston days 3 and 4'>Boston days 3 and 4</a> <small>Sunday, Early morning When I woke up (at about 6...</small></li><li><a href='http://eli.thegreenplace.net/2006/09/22/boston-day-1/' rel='bookmark' title='Permanent Link: Boston &#8211; day 1'>Boston &#8211; day 1</a> <small>Here&#8217;s what I&#8217;ve been up to in the past 24...</small></li><li><a href='http://eli.thegreenplace.net/2006/09/24/boston-day-2/' rel='bookmark' title='Permanent Link: Boston &#8211; day 2'>Boston &#8211; day 2</a> <small>The rest of yesterday Just as I finished typing yesterday&#8217;s...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2006/09/21/on-my-way-to-boston/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Antialiasing filters and multirate systems</title>
		<link>http://eli.thegreenplace.net/2006/05/10/antialiasing-filteres-and-multirate-systems/</link>
		<comments>http://eli.thegreenplace.net/2006/05/10/antialiasing-filteres-and-multirate-systems/#comments</comments>
		<pubDate>Wed, 10 May 2006 15:27:30 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[EE / Embedded]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/2006/05/10/antialiasing-filteres-and-multirate-systems/</guid>
		<description><![CDATA[What is this about ?
Antialiasing is an important topic to understand when dealing with digital processing of data. In this article I concentrate on the various methods used to combat this phenomenon, and try to explain what is multirate filtering and how it is related to antialiasing.
Sampling
Both analog and digital signals can be sampled. If [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2004/01/07/spam-filters-on-output/' rel='bookmark' title='Permanent Link: spam-filters on output'>spam-filters on output</a> <small>Spam filters are common nowadays. My free Yahoo mail account...</small></li><li><a href='http://eli.thegreenplace.net/2004/11/05/book-review-signals-and-systems-made-ridiculously-simple/' rel='bookmark' title='Permanent Link: Book review: &#8220;Signals and Systems, made ridiculously simple&#8221;'>Book review: &#8220;Signals and Systems, made ridiculously simple&#8221;</a> <small>by Zoher Z. Karu This book began as MIT course...</small></li><li><a href='http://eli.thegreenplace.net/2005/03/29/application-of-combinations/' rel='bookmark' title='Permanent Link: application of combinations'>application of combinations</a> <small>The text buffewring widget I&#8217;m building allows the user to...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<h4>What is this about ?</h4>
<p>Antialiasing is an important topic to understand when dealing with digital processing of data. In this article I concentrate on the various methods used to combat this phenomenon, and try to explain what is multirate filtering and how it is related to antialiasing.</p>
<h4>Sampling</h4>
<p>Both analog and digital signals can be sampled. If we use an ADC that runs at 10^6 samples per second, then we can say that the analog signal on the ADCs input is sampled at frequency Fs = 1 MHz. Each digital signal has some sampling frequency tied to it &#8211; the frequency at which it was sampled <a href="#note1">[1]</a>. If we take a digital signal that was sampled at frequency Fs, and grab each 4th sample discarding the others, we get a digital signal sampled at Fs/4.</p>
<h4>What is aliasing ?</h4>
<p>I won&#8217;t go too much into mathematical details here, as I assume that the basics are well known. Whenever some signal (either analog or digital) is sampled at frequency Fs, aliasing will occur if the original signal had harmonics at Fs/2 or higher (that is, if the sampling frequency was below the Nyquist frequency of the signal). See the links section in the end of this articles for more details. Aliasing is &#8220;bad for you&#8221;, it distorts a signal in a way that can&#8217;t really be fixed, so engineers to their best to avoid it. Fortunately, it is quite simple, using antialias filters.</p>
<h4>Antialias filters</h4>
<p>So, we have a signal we want to sample, and we want to avoid aliasing. What should be done ? Generally, given that our sampling rate is Fs, we just need to make sure that there are no harmonics faster than Fs/2 in the signal. How can we assure this ? By using a lowpass filter that cuts all the harmonics above Fs/2. Such a lowpass filter is called an &#8220;antialias filter&#8221;.</p>
<p/>
Say that the input to your system is an analog signal. You know that it has no &#8220;important&#8221; information at harmonics above 10 KHz, so you can safely sample it with an ADC at 20 KHz. However, although nothing above 10 KHz interests you, the signal might (and <b>will</b>, in a real-world system) have some power at harmonics above 10 KHz, mostly because of noise and the imperfect nature of analog signals. So, how do you avoid aliasing ? Right, by using an antialias filter. And how is that done ? Exactly &#8211; just prepend a lowpass filter to the ADC, which cuts off at 10 KHz, and viola, you&#8217;ll have a clean sampled signal.</p>
<h4>Analog antialias filters</h4>
<p>This is nice in theory, but in the real world, such an implementation poses some serious difficulties. To get a clean signal, you must use a very accurate lowpass filter, one that passes everything below 10 KHz and nothing above 10 KHz. In the DSP jargon such a filter is called a &#8220;brick-wall&#8221; filter, since it looks like a brick wall with completely right angles. </p>
<p/>
The sad truth is that such filters are impossible. They are unreal &#8211; a theoretical delirium. We can get quite close though, but constructing an analog filter that is close to a brick wall requires an accurate, high-order filtering circuitry, which is difficult and quite expensive. Fortunately, there is hope &#8211; multirate filtering to the rescue !</p>
<h4>Digital antialias filters</h4>
<p>I will discuss the general topic of multirate filtering below, but for now I want to explain how it helps with digital antialias filtering.</p>
<p/>
Consider the following solution to the problem presented in the last section: We know that there&#8217;s interesting information at up to 10 KHz, so we should sample the signal at <i>at least</i> 20 KHz, according to the sampling theorem <a href="#note2">[2]</a>. But nothing prevents us from sampling it at a much higher frequency, and gain an important advantage by doing so.</p>
<p/>
Suppose we sample the signal at 100 KHz instead of 20 KHz. Now, to avoid antialiasing in this sampling, we must attach a lowpass filter before the ADC that cuts off at 50 KHz. Note, however, that it is not obliged to be a brick wall filter, since 50 KHz is very far from 10 KHz where the information is, so we don&#8217;t mind for some useless frequencies at 40+KHz to be attenuated. Hence, we can attach a very simple analog filter before the ADC &#8211; a RC for example, tuned to 50 KHz. This helps with the antialiasing of the 100 KHz sample, but it doesn&#8217;t ensure a clean signal, since frequencies between 10 and 50 KHz still pass through, disrupting information that is stored at below 10 KHz.</p>
<p/>
To solve this problem, we now apply another antialias filter on the sampled data. We can now apply a digital lowpass filter tuned to cut-off at 10 KHz. Digital filters also can&#8217;t be brick wall, but they can easily approach it, at a fraction of the cost of an analog filter with the same specification !</p>
<p/>
So, the full solution is: sample the input signal at 100 KHz with an ADC, which has a simple RC filter at its input configured to cut off at 50 KHz. Next, we apply an &#8220;almost brick wall&#8221; digital lowpass filter configured to cut off at 10 KHz. Then, we can resample our 100 KHz signal to 20 KHz (by simply discarding 4 out of each 5 samples) and yay &#8211; we have a clean 10 KHz signal sampled at 20 KHz, no aliasing and no noise disrupting the information.</p>
<h4>Multirate systems</h4>
<p>&#8220;Multirate&#8221; simply means <i>multiple sampling rates</i>. A multirate DSP system uses multiple sampling rates within the system. In the example above, we have a multirate system because the signal is first sampled at 100 KHz and later re-sampled at 20 KHz. Generally, if we can allow to increase the initial sampling frequency of the analog signal (which is called <i>oversampling the signal</i>), we can lower the overall cost of the system because the analog part becomes much simpler.</p>
<h4>Decimation, Interpolation and Resampling</h4>
<p><i>Decimation</i> is decreasing the sampling rate of a signal. In our example, after the digital antialias filter is applied, the signal is decimated by a factor of 5 from 100 KHz to 20 KHz. Another common use for decimation is decreasing the sampling rate to ease on the computation. Suppose you just need to sample an audio signal, for which 44 KHz is usually enough, but you only have a 10 MHz ADC. Why overwhelm your processor with so much samples, when decimating by a factor of 100 would be just fine.</p>
<p/>
<i>Interpolation</i> is the reverse process &#8211; increasing the sampling rate of a signal. This is usually done by inserting a certain amount of zeros between each sample of a signal (inserting N zeros means a N + 1 times increase in the signal&#8217;s frequency) and passing the signal through a digital lowpass filter. The aim is often to generate an input for a system with a faster sampling rate.</p>
<p/>
<i>Resampling</i> is a combination of Decimation and Interpolation. If you have a signal with sampling frequency Fs and you want to have a signal with a sampling frequency of 2.5 * Fs, you can first interpolate the signal by a factor of 5 and then decimate it by a factor of 2.</p>
<p/>
Note: in some digital FIR filter generation tools, it is often possible to combine a FIR with decimation and / or interpolation. This is because the combination allows for a more efficient implementation than separate stages of filtering and resampling.</p>
<h4>Digital-only antialias filtering</h4>
<p>A common misconception seems to be that it is possible to implement antialias filtering without analog circuitry. This is false. Between a real world analog signal and a digital system there must, somewhere, lie the brink where the analog signal is sampled to turn it into the digital signal. And in real physical signals, wherever there is sampling, there is aliasing. So analog filtering is essential, unless you are very sure that your analog signal really doesn&#8217;t have any power at above Fs/2, which is rarely the case.</p>
<p/>
A good rule of thumb is: whenever you sample an analog signal for digital processing with an ADC at rate Fs, attach a simple RC lowpass filter configured to cut off at Fs/2 before the ADC (it is best to make is a little less than Fs/2 to account for the very imperfect performance of a RC filter). This assures that the sampled signal is free of aliases. Later, you can apply multirate techniques with digital filters to further shape your digital signal.</p>
<p/>
So remember, digital antialias filtering works only for digital signals.</p>
<h4>Links</h4>
<p>These links were active at the time I wrote the article. If you find a dead link, let me know. In any case, Googling for the link&#8217;s title may bring you to its new location and other related sources.</p>
<ol>
<li><a href="http://en.wikipedia.org/wiki/Aliasing">Wikipedia entry on aliasing</a>
<li><a href="http://www.bores.com/courses/intro/basics/1_alias.htm">Introduction to DSP &#8211; aliasing</a>
<li><a href="http://www.netrino.com/Publications/Glossary/Filters.html">Introduction to digital filters</a>
<li><a href="http://en.wikipedia.org/wiki/Nyquist-Shannon_sampling_theorem">Wikipedia entry on the Nyquist-Shannon sampling theorem</a>
<li><a href="http://www.dspguru.com/info/faqs/mrfaq.htm">Multirate FAQ</a>
<li><a href="http://www.dspguide.com/pdfbook.htm">The Scientist and Engineer&#8217;s Guide to Digital Signal Processing</a> &#8211; A free book
</ol>
<p/>
<h4>Notes</h4>
<p><a name="note1">[1]</a> &#8211; For simplicity it is sometimes useful to assume that an analog signal is just a digital signal sampled at a very high frequency, say 10^50 Hz. </p>
<p/>
<a name="note2">[2]</a> &#8211; The sampling theorem states that in order for a band limited (at Fv) signal to be reconstructed fully, it must be sampled at a rate Fs >= 2*Fv. </p>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=598&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2004/01/07/spam-filters-on-output/' rel='bookmark' title='Permanent Link: spam-filters on output'>spam-filters on output</a> <small>Spam filters are common nowadays. My free Yahoo mail account...</small></li><li><a href='http://eli.thegreenplace.net/2004/11/05/book-review-signals-and-systems-made-ridiculously-simple/' rel='bookmark' title='Permanent Link: Book review: &#8220;Signals and Systems, made ridiculously simple&#8221;'>Book review: &#8220;Signals and Systems, made ridiculously simple&#8221;</a> <small>by Zoher Z. Karu This book began as MIT course...</small></li><li><a href='http://eli.thegreenplace.net/2005/03/29/application-of-combinations/' rel='bookmark' title='Permanent Link: application of combinations'>application of combinations</a> <small>The text buffewring widget I&#8217;m building allows the user to...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2006/05/10/antialiasing-filteres-and-multirate-systems/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>once again: perl, serial ports and what&#8217;s between them</title>
		<link>http://eli.thegreenplace.net/2006/02/13/once-again-perl-serial-ports-and-whats-between-them/</link>
		<comments>http://eli.thegreenplace.net/2006/02/13/once-again-perl-serial-ports-and-whats-between-them/#comments</comments>
		<pubDate>Mon, 13 Feb 2006 20:06:00 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[EE / Embedded]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Serial port]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/2006/02/13/once-again-perl-serial-ports-and-whats-between-them/</guid>
		<description><![CDATA[As I wrote here, I managed to avoid using the Win32::SerialPort module for sending data to the serial port (COM) by employing a C++ slave program that listens to a socket and transmits whatever it gets to the serial port.
Today, I finally perfected this scheme. Now my C++ program (104 Kb !) is a full-duplex [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2005/12/04/perl-master-c-slave-bound-for-serial-port-programming/' rel='bookmark' title='Permanent Link: perl master, C++ slave, bound for serial port programming'>perl master, C++ slave, bound for serial port programming</a> <small>Introduction I wrote about this topic *SO* much before, but...</small></li><li><a href='http://eli.thegreenplace.net/2006/04/07/non-blocking-socket-access-on-windows/' rel='bookmark' title='Permanent Link: Non-blocking socket access on Windows'>Non-blocking socket access on Windows</a> <small>As I mentioned several times in this blog, one of...</small></li><li><a href='http://eli.thegreenplace.net/2004/12/24/dissapointed-with-perl/' rel='bookmark' title='Permanent Link: dissapointed with Perl'>dissapointed with Perl</a> <small>This is more of a rant than anything else. Feel...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>As I wrote <a href="http://use.perl.org/~spur/journal/27856" rel="nofollow">here</a>, I managed to avoid using the Win32::SerialPort module for sending data to the serial port (COM) by employing a C++ slave program that listens to a socket and transmits whatever it gets to the serial port.</p>
<p>Today, I finally perfected this scheme. Now my C++ program (104 Kb !) is a full-duplex serial port &lt;-&gt; socket bridge. It is created by the Perl &#8216;master&#8217; script and starts listening to the socket and to the serial port. When it gets something from the socket (sent by the Perl script) it transmits it to the serial port. When it gets something from the serial port, it sends it to the Perl script using the socket.</p>
<p>The Perl script can now either send serial data by shoving it into the socket or query it, in a convenient non-blocking way (thank you ioctl(), see <a href="http://www.perlmonks.org/?node_id=529812" rel="nofollow">this</a>), to see if anything new has arrived.</p>
<p>The C++ bridge works in two threads. One waits (using recv()) on the socket to get data from the master and transmit it to the serial port. The other waits on the serial port and transmits what it gets into the socket.</p>
<p>For now I&#8217;m actually doing this using two sockets, one for each way (master -&gt; slave and slave -&gt; master). In theory this is doable with a single bi-directional socket, though I may leave it as it is to allow future flexibility.
</p>
<p>Update (05.03.2008): I&#8217;ve released this to the Programs and Code section. Here&#8217;s a <a href="http://eli.thegreenplace.net/files/prog_code/perl_serial_comm.zip">direct ink</a>.</p>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=537&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2005/12/04/perl-master-c-slave-bound-for-serial-port-programming/' rel='bookmark' title='Permanent Link: perl master, C++ slave, bound for serial port programming'>perl master, C++ slave, bound for serial port programming</a> <small>Introduction I wrote about this topic *SO* much before, but...</small></li><li><a href='http://eli.thegreenplace.net/2006/04/07/non-blocking-socket-access-on-windows/' rel='bookmark' title='Permanent Link: Non-blocking socket access on Windows'>Non-blocking socket access on Windows</a> <small>As I mentioned several times in this blog, one of...</small></li><li><a href='http://eli.thegreenplace.net/2004/12/24/dissapointed-with-perl/' rel='bookmark' title='Permanent Link: dissapointed with Perl'>dissapointed with Perl</a> <small>This is more of a rant than anything else. Feel...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2006/02/13/once-again-perl-serial-ports-and-whats-between-them/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
