<?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>engin aydogan &#187; embedded</title>
	<atom:link href="http://engin.bzzzt.biz/tag/embedded/feed/" rel="self" type="application/rss+xml" />
	<link>http://engin.bzzzt.biz</link>
	<description>&#039;s journal</description>
	<lastBuildDate>Thu, 02 Feb 2012 21:22:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>DHCP Servers: Stock routers&#8217;, Microsoft Windows ICS&#8217;s, Mac OS X&#8217;s</title>
		<link>http://engin.bzzzt.biz/2011/03/02/dhcp-servers-stock-routers-microsoft-windows-icss-mac-os-xs/</link>
		<comments>http://engin.bzzzt.biz/2011/03/02/dhcp-servers-stock-routers-microsoft-windows-icss-mac-os-xs/#comments</comments>
		<pubDate>Wed, 02 Mar 2011 14:52:03 +0000</pubDate>
		<dc:creator>engin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[dhcp]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[enda]]></category>

		<guid isPermaLink="false">http://engin.bzzzt.biz/?p=407</guid>
		<description><![CDATA[MS Windows and Mac OS X let&#8217;s you share your internet from one network interface to another one. i.e. You can share your Wireless internet to your Ethernet port. In Windows it is called ICS (Internet Connection Sharing). In Mac OS X, you can do it in the Sharing section for your settings. Not  [...]]]></description>
			<content:encoded><![CDATA[<p>MS Windows and Mac OS X let&#8217;s you share your internet from one network interface to another one. i.e. You can share your Wireless internet to your Ethernet port. In Windows it is called ICS (Internet Connection Sharing). In Mac OS X, you can do it in the Sharing section for your settings. Not surprisingly, Mac OS X UI is much more intuitive.</p>
<p>In a few days, we&#8217;ll be in an industrial automation fair and only available internet connection there will be Wireless. We want to connect our devices to internet there but our devices only have Ethernet port. So we&#8217;ll rely on connection sharing of one of these OSes. The problem is that their DHCP servers are acting a bit different than a stock router you&#8217;d have.</p>
<p>Windows does not give responses quickly, after my DHCP REQUEST message, it takes a like 1-2 seconds to get ACK message back. And Windows&#8217;s DHCP server keeps sending OFFERs and ACKs like crazy for the previously received messages even though an IP address is acquired already. Tweaking the timeouts and making sure my client does not honor OFFERs once that state is passed fixed the issues with Windows&#8217; DHCP server.</p>
<p>However the issue with the Mac was a more subtle one. My device couldn&#8217;t acquire IP address at all. I checked if my Windows laptop could get an IP from it and, not surprisingly, it could! My device couldn&#8217;t even get an OFFER for its DISCOVER message. I compared Windows&#8217; DISCOVER message with mine to find the differences. There were two differenes:</p>
<ol>
<li>Seconds field</li>
<li>Options field</li>
</ol>
<div>First I tried to make the Options field identical. Of course, Murphy&#8217;s laws applied and it turned out that it is because of Seconds field. When I changed my Seconds field from 0 to 4 (like Windows do) it all worked out with Mac&#8217;s DHCP server.</div>
<div>According to <a href="http://tools.ietf.org/html/rfc2131">RFC 2131</a>&#8216;s Section 2, page 10, the Seconds field is for:</div>
<blockquote><p>secs          2  Filled in by client, seconds elapsed since client began address acquisition or renewal process.</p></blockquote>
<p>I don&#8217;t see how this affects the behavior but it fixes the issue.</p>
<p>See also <a href="http://engin.bzzzt.biz/2010/07/20/zero-configuration-connectivity-on-embedded-computers/">previous mistake I made about DHCP</a>.</p>
<p>Cheers</p>
]]></content:encoded>
			<wfw:commentRss>http://engin.bzzzt.biz/2011/03/02/dhcp-servers-stock-routers-microsoft-windows-icss-mac-os-xs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Expansion Module Architecture Design Experiences of ENDA Devices</title>
		<link>http://engin.bzzzt.biz/2010/11/25/expansion-module-architecture-design-experiences-of-enda-devices/</link>
		<comments>http://engin.bzzzt.biz/2010/11/25/expansion-module-architecture-design-experiences-of-enda-devices/#comments</comments>
		<pubDate>Thu, 25 Nov 2010 10:05:37 +0000</pubDate>
		<dc:creator>engin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[electronics]]></category>
		<category><![CDATA[embedded]]></category>

		<guid isPermaLink="false">http://engin.bzzzt.biz/?p=242</guid>
		<description><![CDATA[UPDATE: Please see TX FIFO clear problem solved
Implementing Expansion Modules support in PLC firmware was pretty easy, since from the eyes of the PLC (Main Device) all the Expansion Modules look like ONE device. It turned out that with LM3S8738 MCU, it is a challenging task to implement Expansion  [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;"><strong>UPDATE</strong>: Please see <a title="Permanent Link: TX FIFO clear problem solved" rel="bookmark" href="http://engin.bzzzt.biz/2010/12/31/tx-fifo-clear-problem-solved/">TX FIFO clear problem solved</a></p>
<p style="text-align: left;">Implementing Expansion Modules support in PLC firmware was pretty easy, since from the eyes of the PLC (Main Device) all the Expansion Modules look like ONE device. It turned out that with LM3S8738 MCU, it is a challenging task to implement Expansion Module communication logic in the Expansion Modules. The reason is that LM3S8738 has RX and TX FIFOs for to be used in SSI but there&#8217;s basically no control over the FIFOs. So I was not able to control the SSI peripheral as much as I wanted. Here&#8217;s the problem and design principles of the expansion modules.</p>
<p style="text-align: center;"><img class="aligncenter" style="border: 0px initial initial;" src="http://e2e.ti.com/cfs-filesystemfile.ashx/__key/CommunityServer-Components-ImageFileViewer/CommunityServer-Discussions-Components-Files-471/6138.expansion.png_2D00_600x900.png" border="0" alt="" /></p>
<p style="text-align: center;">Basic logic</p>
<p style="text-align: left;">
<p>All devices are connected to a bus and driven by the SSI CLK generated by the  SSI Master which is the Main Device (MD from now on).  When a clock is generated each expansion device  (XD from now on) transmits data to the next XD &#8212; except the last XD which transmits data back to MD and completes the loop. For instance in the first clock, MD sent 1 bit to XD A,  XD A sent 1 bit to XD B, XD B sent 1 bit to XD C and XD C sents 1 bit to MD. And all this transmission is enabled when the ENABLE line is LO. When the ENABLE line is HI all the XD&#8217;s know that the transmission is over and the last N bytes they received are intended for themselves.</p>
<p>So imagine this hypothetical scenario. Each XD&#8217;s message size is 1 byte and 3 XD&#8217;s are connected to the MD. For MD to send query to these XDs, it should send 3 bytes. And MD will receive 3 bytes of answers for the previous query. So here&#8217;s the work flow;</p>
<p>1. ENABLE line is LO</p>
<p>2. XDs put their answers for the previous query to TX FIFO. <span style="color: #ff0000;"><span style="font-size: xx-small;">(First I need to clear TX FIFO but I can&#8217;t &#8212; explained below)</span></span></p>
<p><img style="border: 0px initial initial;" src="http://e2e.ti.com/cfs-filesystemfile.ashx/__key/CommunityServer-Components-ImageFileViewer/CommunityServer-Discussions-Components-Files-471/8422.expansion_5F00_state1.png_2D00_600x0.png" border="0" alt="" /></p>
<p>3. MD sends 1 byte Query C to XD A.</p>
<p>4. XD A sends Response<sub>n-1</sub>A to XD B and puts Query C into its TX FIFO.</p>
<p>5. XD B sends Response<sub>n-1</sub>B to XD C and puts Response<sub>n-1</sub>A to its TX FIFO.</p>
<p>6. XD C sends Response<sub>n-1</sub>C  to MD and puts Response<sub>n-1</sub>B to its TX FIFO.</p>
<p>(Note that all steps from 3 to 7 happened simultaneously when MD is sending 1 byte, as required by the nature of SSI. )</p>
<p><img style="border: 0px initial initial;" src="http://e2e.ti.com/cfs-filesystemfile.ashx/__key/CommunityServer-Components-ImageFileViewer/CommunityServer-Discussions-Components-Files-471/5126.expansion_5F00_state2.png_2D00_600x0.png" border="0" alt="" /></p>
<p>7. MD sends 1 byte (Query B) to XD A.</p>
<p>8. XD A sends Query C to XD B and puts Query B to its TX FIFO.</p>
<p>9. XD B sends Responsen-1A to XD C and puts Query C to its TX FIFO.</p>
<p>10. XD C sends Responsen-1B to MD and puts Responsen-1A to TX FIFO.</p>
<p><img style="border: 0px initial initial;" src="http://e2e.ti.com/cfs-filesystemfile.ashx/__key/CommunityServer-Components-ImageFileViewer/CommunityServer-Discussions-Components-Files-471/2620.expansion_5F00_state3.png_2D00_600x0.png" border="0" alt="" /></p>
<p>11. MD sends 1 byte Query A to XD A</p>
<p>12. XD A sends Query B to XD B and puts Query A in its TX FIFO</p>
<p>13. XD B sends Query C to XD C and puts Query B to its TX FIFO.</p>
<p>14. XD C sends Responsen-1A to MD and puts Query C to its TX FIFO.</p>
<p><img style="border: 0px initial initial;" src="http://e2e.ti.com/cfs-filesystemfile.ashx/__key/CommunityServer-Components-ImageFileViewer/CommunityServer-Discussions-Components-Files-471/5657.expansion_5F00_state4.png_2D00_600x0.png" border="0" alt="" /></p>
<p>15. ENABLE line is HI.</p>
<p>16. Each XD looks for the last 1 byte they received and that&#8217;s their query to process for the next round.</p>
<p><strong><span style="font-size: medium;">The Problem</span></strong></p>
<p>Since XDs don&#8217;t know the total number of XDs connected to the bus they don&#8217;t know how many bytes they will receive hence they rely on the ENABLE line. Whent he ENABLE line is HI, the last byte they got is their query. Since they don&#8217;t know the total frame length (encapsulated by the ENABLE line) they don&#8217;t know when not to put data to TX FIFO. <strong><span style="text-decoration: underline;">So there&#8217;s a garbage byte in the TX FIFO when the transmission is over.</span></strong></p>
<p>Here&#8217;re the things I could do.</p>
<p>1. Clear the TX FIFO each time ENABLE is LO, just before actual transmission begins &#8212; there&#8217;s no clean way to do this in LM3S8738 apparently.</p>
<p>2. Introduce another protocol element to tell XDs the size of the bus, so they&#8217;ll know when not to put <strong>garbage data</strong> to TX FIFO.</p>
<p>3. Put XDs into SSI Master mode for a very short time to clean the TX FIFO. <em><span style="text-decoration: underline;">(I&#8217;m doing this right now, it works but it is not elegant).</span></em></p>
<p>4. Disable/Enable SSI peripheral to reset the TX FIFO. (I could not manage to do SysCtlPeripheralDisable, the CPU hangs, probably gets into a BUS FAULT).</p>
<p><span style="font-size: medium;"><strong>Conclusion</strong></span></p>
<p>In XDs we implemented with low level 8-bit MCUs, it has more granular control over the SSI peripheral &#8212; which is simpler of course. There&#8217;s no TX/RX FIFO, and you can just overwrite the outstanding data to be sent. So when ENABLED line is low, you just put your answer there and it overwrites the garbage, and you are done with it.</p>
<p>With LM3S8738 I&#8217;m still looking for more elegant ways to tackle this problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://engin.bzzzt.biz/2010/11/25/expansion-module-architecture-design-experiences-of-enda-devices/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>zero-configuration connectivity on embedded computers</title>
		<link>http://engin.bzzzt.biz/2010/07/20/zero-configuration-connectivity-on-embedded-computers/</link>
		<comments>http://engin.bzzzt.biz/2010/07/20/zero-configuration-connectivity-on-embedded-computers/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 10:33:39 +0000</pubDate>
		<dc:creator>engin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[dhcp]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[enda]]></category>
		<category><![CDATA[plc]]></category>
		<category><![CDATA[uip]]></category>

		<guid isPermaLink="false">http://engin.bzzzt.biz/?p=207</guid>
		<description><![CDATA[Our device is networked. It is supposed to:

Work when you plug it into any traditional Ethernet network (i.e. with router with embedded DHCP server).
It is also supposed to work when you directly connect it to another node (i.e. a computer which you&#8217;re want to configure the device with &#8212; where no  [...]]]></description>
			<content:encoded><![CDATA[<p>Our device is networked. It is supposed to:</p>
<ul>
<li>Work when you plug it into any traditional Ethernet network (i.e. with router with embedded DHCP server).</li>
<li>It is also supposed to work when you directly connect it to another node (i.e. a computer which you&#8217;re want to configure the device with &#8212; where no DHCP server is available).</li>
</ul>
<p>So our goal is:</p>
<ul>
<li>Zero-configuration connectivity on above two scenarios.</li>
</ul>
<p>Our requirement:</p>
<ul>
<li>Severely scarce resources. Firmware should fit in a 64kb space.</li>
</ul>
<p>Most such networked devices comes with a static IP already set and the node which is supposed to configure them (usually a PC) is supposed to be set to a specific IP address in the same subnet to configure them. This has several drawbacks:</p>
<ul>
<li>Users need to have administration privilleges</li>
<li>Users need to have experience with computers</li>
<li>And this is OS dependent.</li>
</ul>
<p>There are other zero-configuration technology stacks, even though I don&#8217;t have a scientific fact, from their specs I think they&#8217;d take up significant code space.</p>
<p>So here&#8217;s what I&#8217;ve did</p>
<ol>
<li>During device boot I assign it a link-local IP address (i.e. AUTOIP or <a href="http://tools.ietf.org/html/rfc3927">http://tools.ietf.org/html/rfc3927</a>). I did not implement RFC3927 and just generated a fixed IP address according to MAC of the device &#8212; partly because I don&#8217;t have a quality random number generator, as there&#8217;s no clock on most of the devices and reading analog inputs often give same results.</li>
<li>Then I start DHCP negotiations</li>
</ol>
<p>With the scenario the device is able to communicate with another node which also have been assigned to a link-local address without waiting for a DHCP timeout. So when you plug your device directly to your computer you can communicate with the device as soon as your computer also assigned itself a link-local address. This varies from OS to OS but we&#8217;re pleased with the results. Our customers are able to connect to their devices without any configuration.</p>
<p>But there&#8217;s a problem! Even though this configuration worked on our network at the office and many other places where we sold the devices to, it didn&#8217;t work on my home network. It turned out that my linksys router in my home network does not honor DHCP DISCOVER requests if their source IP addresses are not 0.0.0.0</p>
<p>Problem:</p>
<ul>
<li>Some DHCP servers does not honor DHCP DISCOVER requests from 169.254.C.D. They strictly require source IP of the requests to be 0.0.0.0</li>
</ul>
<p>To solve this problem I could either</p>
<ul>
<li>First assign my device IP address 0.0.0.0 and then wait for DHCP timeout, then fallback to AUTO IP. This way DHCP DISCOVER request would work with all routers but other party would have to wait for my device to timeout DHCP and fallback to link-local IP before starting communicating with it &#8212; this is a serious tradeoff.</li>
<li>Or Add UDP IP Spoofing on top of our current implementation hence just make the DHCP DISCOVER source IP 0.0.0.0 and keep the rest the same.</li>
</ul>
<p>Obviously I chose the second approach and patched uIP with the following code:</p>
<pre>$ svn diff
Index: uip/uip.c
===================================================================
--- uip/uip.c   (revision 226)
+++ uip/uip.c   (working copy)
@@ -1181,7 +1181,17 @@
   BUF-&gt;srcport  = uip_udp_conn-&gt;lport;
   BUF-&gt;destport = uip_udp_conn-&gt;rport;

-  uip_ipaddr_copy(BUF-&gt;srcipaddr, uip_hostaddr);
+  extern int g_udp_spoof;
+  extern uip_ipaddr_t g_udp_spoof_ip;
+  if(!g_udp_spoof)
+  {
+    uip_ipaddr_copy(BUF-&gt;srcipaddr, uip_hostaddr);
+  }
+  else
+  {
+       g_udp_spoof = 0;
+       uip_ipaddr_copy(BUF-&gt;srcipaddr, g_udp_spoof_ip);
+  }
   uip_ipaddr_copy(BUF-&gt;destipaddr, uip_udp_conn-&gt;ripaddr);

   uip_appdata = &amp;uip_buf[UIP_LLH_LEN + UIP_IPTCPH_LEN];</pre>
<p>Now our device apparently has best of both worlds.</p>
<ul>
<li>It can communicate with any node with link-local IP address right away at boot time &#8212; without waiting for any sort of (DHCP) timeout.</li>
<li>While link-local communication is working, it tries to acquire an IP address from DHCP server in parallel &#8212; thanks to source IP &#8220;spoofing&#8221;.</li>
</ul>
<p>I&#8217;ll see if further testing will reveal any side effects.</p>
<p>See also:<br />
DHCP RFC: <a href="http://tools.ietf.org/html/rfc2131">http://tools.ietf.org/html/rfc2131</a><br />
Link-local IP Address (AUTO IP): <a href="http://tools.ietf.org/html/rfc3927">http://tools.ietf.org/html/rfc3927</a></p>
]]></content:encoded>
			<wfw:commentRss>http://engin.bzzzt.biz/2010/07/20/zero-configuration-connectivity-on-embedded-computers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Some declaration details for pointers in C/C++</title>
		<link>http://engin.bzzzt.biz/2009/12/03/some-declaration-details-for-pointers-in-cc/</link>
		<comments>http://engin.bzzzt.biz/2009/12/03/some-declaration-details-for-pointers-in-cc/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 09:13:23 +0000</pubDate>
		<dc:creator>engin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[embedded]]></category>

		<guid isPermaLink="false">http://engin.bzzzt.biz/?p=7</guid>
		<description><![CDATA[First of all let&#8217;s remember what a pointer is. A pointer is an address pointing to an object. &#8220;Address&#8221; term is open for debate, some pedantic assess would argue against it.
So let&#8217;s start giving examples.
const char* str ="Foo";
str[0] = 'B'; // error: assignment of read-only location
str = "Bar";  [...]]]></description>
			<content:encoded><![CDATA[<p>First of all let&#8217;s remember what a pointer is. A pointer is an address pointing to an object. &#8220;Address&#8221; term is open for debate, some pedantic assess would argue against it.</p>
<p>So let&#8217;s start giving examples.</p>
<pre>const char* str ="Foo";
str[0] = 'B'; // error: assignment of read-only location
str = "Bar"; // Perfectly OK</pre>
<p>So, it is obvious that first line declares <em>str </em>as n pointer pointing to a constant object and the object is not modifiable but the pointer itself is.</p>
<p>Let&#8217;s review another example.</p>
<pre >char * const str = "Foo";
str[0] = 'B'; // Perfectly OK (See the node below)
str = "Bar"; // error: assignment of read-only variable</pre>
<p>Here we can see that the declaration tells compiler that the pointer value (address) is constant but the object it is pointing to can be modified. Please note that even though the above assignment &#8220;str[0] = &#8216;B&#8217;&#8221; will compile just fine, it will most probably result in a segmentation fault during runtime since your compiler will most likely put it in read only memory region.</p>
<p>Now let&#8217;s make another example.</p>
<pre>const char * const str = "Foo";
str[0] = 'B'; // error: assignment of read-only location
str = "Bar"; // error: assignment of read-only variable</pre>
<p>This declaration tells the compiler that neither the object the pointer is pointing to can be modified, nor the pointer value (address) itself. i.e. nothing is modifiable :)</p>
<p><strong><span style="text-decoration: underline;">Conclusion</span></strong></p>
<p><strong>So, we can conclude that qualifiers (const, volatile) that are placed before the * applies to the object the pointer is pointing to, and qualifiers placed after the * applies to the pointer value itself, i.e. the address.</strong></p>
<p>So we can have weird looking declaration like this one:</p>
<pre>extern volatile const char * volatile const str = "Foo";</pre>
<p>I know it looks a bit unusual but it is perfectly valid.</p>
<p>Note: For hardware programming volatile keyword is a must. If you don&#8217;t know what it is, read this <a title="what is volatile keyword used for" href="http://wiki.answers.com/Q/Volatile_example_code_sample_coding_of_volatile_pointer">article</a> at wiki.answers.com first. Here&#8217;s the <a href="http://backupurl.com/ksfqr7">backup</a>, just in case.</p>
]]></content:encoded>
			<wfw:commentRss>http://engin.bzzzt.biz/2009/12/03/some-declaration-details-for-pointers-in-cc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

