<?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>Things Learned Along the Way</title>
	<atom:link href="http://blog.curioussystem.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.curioussystem.com</link>
	<description>Tips, Tricks and other tidbits for the world</description>
	<lastBuildDate>Wed, 05 Jun 2013 17:55:06 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>These are not the bits you&#8217;re looking for</title>
		<link>http://blog.curioussystem.com/2013/06/these-are-not-the-bits-youre-looking-for/</link>
		<comments>http://blog.curioussystem.com/2013/06/these-are-not-the-bits-youre-looking-for/#comments</comments>
		<pubDate>Wed, 05 Jun 2013 17:52:45 +0000</pubDate>
		<dc:creator>Chad Kidder</dc:creator>
				<category><![CDATA[Circuits]]></category>
		<category><![CDATA[Embedded Systems]]></category>
		<category><![CDATA[misleading measurements]]></category>

		<guid isPermaLink="false">http://blog.curioussystem.com/?p=151</guid>
		<description><![CDATA[I&#8217;ve noticed recently that the number of bits available in an ADC is slowly creeping up.  That makes me excited&#8230; until I look at the system accuracy.  How do you tell what you are actually reading with an integrated ADC?  You measure the internal voltage reference (IVR) usually.  Then you do some math to figure &#8230; </p><p><a class="more-link block-button" href="http://blog.curioussystem.com/2013/06/these-are-not-the-bits-youre-looking-for/">Continue reading &#187;</a>]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve noticed recently that the number of bits available in an ADC is slowly creeping up.  That makes me excited&#8230; until I look at the system accuracy. <span id="more-151"></span> How do you tell what you are actually reading with an integrated ADC?  You measure the internal voltage reference (IVR) usually.  Then you do some math to figure out what your rails are.  For single-ended measurements, this is usually ground and VCC.  So, what good is 12 bits of ADC accuracy if we only have six bits of accuracy with the IVR?  Yes, you heard me right, only six bits.  If you have an IVR with +/- 3 % accuracy, that ends up being about 6 bits of accuracy.  I&#8217;ll walk you through it.</p>
<p>Let&#8217;s say we have a 1.0 V IVR with a +/- 3% tolerance.  That means that it could be anywhere between 0.97 to 1.03 V, for a total range of 0.06 volts.  To find how many bits of resolution that gives us, we say 0.06 V &gt;= VREF+/2<sup>n</sup> where n= number of bits of resolution and VREF- = GND.  If we have VREF+ = 3.3 V, that gives us n = -log<sub>2</sub>(0.06/3.3) = 5.78 bits.  Working this backwards to verify we see that for six bits of resolution, the LSB is 3.3 V / 2<sup>6</sup> = 0.052 V, which is less than our margin of error.  So, we have slightly less than six bits of absolute accuracy.</p>
<p>If you are using your ADC monitoring absolute voltage, any bits after six are waisted, just giving you a false sense of accuracy.  This can be dangerous when we optimize an algorithm for a particular device.  We may assume that the level of precision we have is also the level of accuracy we have.  This will bite us later when the lot-to-lot or device-to-device deviations in manufacturing suddenly move the IVR by 30 mV when we designed our algorithm for 12-bits of precision (which equates to 0.8 mV with a 3.3 V VREF+).</p>
<p>If you need more absolute accuracy, you can get an external precision voltage reference to connect to VREF+.  Depending on your application you may be able to depend on the stability of VCC and assume what it is.  When you are running of a battery and VCC can change, that is not an option.  Now you know.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.curioussystem.com/2013/06/these-are-not-the-bits-youre-looking-for/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Two wrongs make a working prototype</title>
		<link>http://blog.curioussystem.com/2013/06/two-wrongs-make-a-working-prototype/</link>
		<comments>http://blog.curioussystem.com/2013/06/two-wrongs-make-a-working-prototype/#comments</comments>
		<pubDate>Wed, 05 Jun 2013 17:30:29 +0000</pubDate>
		<dc:creator>Chad Kidder</dc:creator>
				<category><![CDATA[Blackfin]]></category>
		<category><![CDATA[embedded C]]></category>
		<category><![CDATA[frustration]]></category>
		<category><![CDATA[troubleshooting]]></category>
		<category><![CDATA[You mean I needed those]]></category>

		<guid isPermaLink="false">http://blog.curioussystem.com/?p=149</guid>
		<description><![CDATA[I had an interesting problem show up when I was working on a project at the beginning of the year.  It involved an Analog Devices Blackfin 51x series processor.  We went to transition to their new IDE and that&#8217;s when we started noticing problems with the Flash RAM. The Backstory I was bringing up a &#8230; </p><p><a class="more-link block-button" href="http://blog.curioussystem.com/2013/06/two-wrongs-make-a-working-prototype/">Continue reading &#187;</a>]]></description>
				<content:encoded><![CDATA[<p>I had an interesting problem show up when I was working on a project at the beginning of the year.  It involved an Analog Devices Blackfin 51x series processor.  We went to transition to their new IDE and that&#8217;s when we started noticing problems with the Flash RAM.<span id="more-149"></span></p>
<h2>The Backstory</h2>
<p>I was bringing up a new board with an Analog Devices&#8217; Blackfin 514 on it the last part of 2012 and the first part of 2013.  We got the code working and got it programming the flash RAM and booting from it using their VisualDSP++ IDE.  Since this was a new design, we wanted to transition to their Cross Core Embedded Studio (CCES) that is based on Eclipse.  We got the base program transitioned over and then tried programming the flash RAM and booting from it.  That didn&#8217;t go so well.  I reprogrammed it and verified what was programmed.  Let&#8217;s try this again&#8230; and again, the old program ran.  We swapped back to VisualDSP++ and it didn&#8217;t see the new code.  What gives?  To understand my level of frustration, realize that this was a period of more than aweek as it was a 24 hour round trip for answers to tech support questions via India (I think, the replies liked to show up at 3 AM MST).</p>
<h2>&#8230; and now, the rest of the story</h2>
<p>Thank you for the introduction Paul Harvey.  We slowly got the issue escalated.  I sent traces from my oscilloscope of the memory lines.  I was finally talking with the author of their flash programming driver.  I sent memory dumps back and forth from VisualDSP++ and CCES.  We did that a few times and had some of their engineers look at our schematic.  We finally had a lead.  We had based our design of the <a href="http://www.analog.com/en/evaluation/bf518f-ezbrd/eb.html">BF518 Ez-board</a>.  If you look at their <a href="imported">documentation</a>, page 94, zone 2A, there are several pull-up resistors on the AMS lines between the switches and flash RAM.  When we designed the board, we <del>assumed</del> thought that the pull-up resistors were there to keep the lines from floating when they were disconnected from the processor.  It turns out that when the processor first resets, some of those lines are not configured as AMS lines, but are floating.  Alright.  Now we know that is a problem.  If that is the case, then why were we able to program the flash RAM in VisualDSP++ successfully.</p>
<p>So, why did VisualDSP++ successfully program the flash RAM?  It turns out that when the code for the flash driver for the BF518 Ez-board was designed just for the BF518, not the BF51x series.  So, when we went in and changed it to compile for the BF514, there was a preprocessor directive that no longer compiled in the code to set all the AMS lines as active.  That was the second &#8220;wrong&#8221;.  We were lucky that with some of the AMS lines floating, they reliably floated in a particular direction so we ended up <em>programming and reading</em> the same 1 MB block of flash memory&#8230; which was not actually the first block of flash memory.  We didn&#8217;t notice this because our program was small enough that we didn&#8217;t need more than the one block of memory.</p>
<p>When transitioning to CCES, we fixed the wrong in the flash RAM driver which caused our error to show up.  We soldered on the pull-up resistors and things started working with CCES&#8230; and failing with VisualDSP++.  We figured out what needed to be changed so the correct portions of the VisualDSP++ driver would compile and work.  Things were good after that.</p>
<p>The moral of the story is, &#8220;Yes we put those there for a reason, but not the one you think.&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.curioussystem.com/2013/06/two-wrongs-make-a-working-prototype/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Freescale KL25 Peripheral Selection Guide</title>
		<link>http://blog.curioussystem.com/2013/06/freescale-kl25-peripheral-selection-guide/</link>
		<comments>http://blog.curioussystem.com/2013/06/freescale-kl25-peripheral-selection-guide/#comments</comments>
		<pubDate>Wed, 05 Jun 2013 16:34:40 +0000</pubDate>
		<dc:creator>Chad Kidder</dc:creator>
				<category><![CDATA[Kinetis]]></category>
		<category><![CDATA[KL25]]></category>
		<category><![CDATA[low power]]></category>
		<category><![CDATA[Wish I'd known that earlier]]></category>

		<guid isPermaLink="false">http://blog.curioussystem.com/?p=148</guid>
		<description><![CDATA[I previously wrote my review of the Freescale KL25.  Since then I&#8217;ve finished my first design with the KL25. The second spin of the board will see several of the peripherals and pins rearranged.  Most of this has to do with things that I glossed over in the documentation desire to get the product to &#8230; </p><p><a class="more-link block-button" href="http://blog.curioussystem.com/2013/06/freescale-kl25-peripheral-selection-guide/">Continue reading &#187;</a>]]></description>
				<content:encoded><![CDATA[<p>I previously wrote my review of the <a href="http://blog.curioussystem.com/2013/02/freescale-kl25-cortex-m-0-review/">Freescale KL25</a>.  Since then I&#8217;ve finished my first design with the KL25. The second spin of the board will see several of the peripherals and pins rearranged.  Most of this has to do with things that I glossed over in the documentation desire to get the product to market.  None were killers, but required some work-around or are being changed to improve functionality.   <span id="more-148"></span></p>
<h2>Keeping I/O Busses Active</h2>
<p>To get the lowest current you try and shut things off.  Well, you only have a finite number of choices for clocking your peripherals.  This becomes especially tricky if you are using the very low power run (VLPR) mode.  First off, pull up table 5-2 (p. 121) from the <a href="http://cache.freescale.com/files/32bit/doc/ref_manual/KL25P80M48SF0RM.pdf">reference manual</a>.  This shows you the clocking options.  Now, pull up table 7-2 (p. 144) to see which peripherals and clocks are available for the low power modes you want to operate in.</p>
<h3>SPI</h3>
<p>The first question is, do you want to have the SPI bus active while in a stop mode?  If so, we have to use partial stop mode (PSTOP2 where the bus clock is active) and SPI0, which is clocked off the bus clock.  Otherwise we can use SPI1 which is clocked off the faster system clock, but is only operable during run or wait states.  The next consideration, will be the desired speed, especially during VLPR mode.  If we are going to use VLPR mode, SPI1 can run at 4x (in reality, closer to 5x) the speed of SPI0 due to limitiation on the bus and system clocks in VLPR mode.  Either SPI bus will work as a slave in stop and VLPS modes, but the bus speed limits are half what they are for masters in the same mode.</p>
<h3>UART</h3>
<p>Do you need more than one UART?  If not, just use UART0.  It has more clocking options and there are no downsides compared to UART1 or UART2.  If you need more than one UART, the question becomes what modes do you want your UARTs active in?  There is one low power UART (UART0) that can be active in stop or VLPS modes.  So, the first question is if you need multiple UARTs active, which one will you need to monitor more than the others?  This device needs to be hooked up to UART0.  If that is not an issue, are you using the VLPR mode?  If so, UART1 &amp; UART2 have a much lower line speed in these modes compared to what UART0 can do.</p>
<h3>I2C</h3>
<p>Do you need more than one I2C bus?  If not, just use I2C0, as it will work in PSTOP2 mode.  (I haven&#8217;t confirmed this.  The documentation leaves me a little unsure here.)</p>
<h3>USB</h3>
<p>What I found out is there is not an internal way to see if VUSB+ is hooked up to the VREGIN pin.  So, throw a voltage divider on the board and hook it up (the divided voltage) to a pin so you can sense if there is a USB connection.  I initially started this project thinking I would use the USB port for communications, but ended up just using it for charging the battery.  That means that I was not running any of the USB stack on the MCU.  The next spin of the board I plan on putting a KL15 on it instead, since I am not actually using the USB interface, except for charging.</p>
<h2>GPIO Interrupts</h2>
<p>The first thing I found, and mentioned in the comments in the <a href="http://blog.curioussystem.com/2013/02/freescale-kl25-cortex-m-0-review/">prior article</a>, only GPIO port A &amp; D have interrupt capability.  On top of that, if you are using any of the low leakage stop modes, if you want to interrupt on a GPIO, it needs to be connected to a pin that is connected to the low leakage wakeup unit (LLWU).  There are only two pins which are able to interrupt while awake or in the low leakage stop modes.  These two pins are PTD4 &amp; PTD6.  They are available on all packages.</p>
<p>Of note is PTA4.  It defaults to NMI.  If you plan on using this as an input that is not the NMI, make sure that you put code before main() to change the pin setup.  If you wait until main to do it, you may trigger the NMI before you get to main() and wonder what in the world is going on.  The same goes for PTA20, which defaults to RESET_b.</p>
<p>If you decide you need to use the pins that SWD is on, don&#8217;t forget to double-check your circuit so that you do not hurt either the programmer or target device when trying to program.  Yes, you already know this, but take a minute and check it for me.  You&#8217;ll thank me one of these times.</p>
<h2>ADC</h2>
<p>If you are using a package that has external VREF +/- pins, make sure to hook them up.  The documentation alludes to multiple options for ADC references, but I was unsuccessful using the alternate VREF setting.  I assumed that the alternate VREF setting was internal connection to VCC &amp; GND.  This isn&#8217;t a big deal as VREF +/- are right next to VCC and GND pins.</p>
<h2>Choosing Pins</h2>
<p>Now that we&#8217;ve talked about peripheral selection, here&#8217;s the general order I would use for selecting what goes on which pins:</p>
<ol>
<li>Which signals need to be on GPIOs that need to interrupt while running and in low leakage stop modes.</li>
<li>Serial busses &#8211; SPI, UART, I2C</li>
<li>other internal peripherals (ADC, DAC, comparitor)</li>
<li>Input GPIOs (put these on extra pins on ports A &amp; D)</li>
<li>Output GPIOs</li>
</ol>
<p>While deciding what goes where, remember, you have to route all these traces so try to keep them as short as possible and away from each other as much as possible.  We don&#8217;t need to add another two layers to the board when it can be avoided.</p>
<h2>Closing Thoughts</h2>
<p>Overall, I am pleased with what I have seen out of the KL25 series of MCUs.  I&#8217;m interested to poke around with the KL05 series to see if it&#8217;s worth replacing some PICs or MSP430s for low end designs.  I&#8217;m not convinced of that, just because of the coding complexity of bringing things up (more time spent with init code) and low power current with SRAM backed up.  We shall see.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.curioussystem.com/2013/06/freescale-kl25-peripheral-selection-guide/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Freescale KL25 Cortex M-0+ Review</title>
		<link>http://blog.curioussystem.com/2013/02/freescale-kl25-cortex-m-0-review/</link>
		<comments>http://blog.curioussystem.com/2013/02/freescale-kl25-cortex-m-0-review/#comments</comments>
		<pubDate>Sat, 09 Feb 2013 16:48:33 +0000</pubDate>
		<dc:creator>Chad Kidder</dc:creator>
				<category><![CDATA[embedded C]]></category>
		<category><![CDATA[Embedded Systems]]></category>
		<category><![CDATA[Kinetis]]></category>
		<category><![CDATA[Cortex-M0+]]></category>
		<category><![CDATA[KL25]]></category>
		<category><![CDATA[low power modes]]></category>

		<guid isPermaLink="false">http://blog.curioussystem.com/?p=144</guid>
		<description><![CDATA[One of the projects I&#8217;m working on I chose to use one of the new Freescale KL25 MCUs.  They have an ARM Cortex-M0+ core and lots of peripherals to boot, including ones specifically designed for low power usage.  Below are some of the reasons why I chose it, and what my experiences with it have &#8230; </p><p><a class="more-link block-button" href="http://blog.curioussystem.com/2013/02/freescale-kl25-cortex-m-0-review/">Continue reading &#187;</a>]]></description>
				<content:encoded><![CDATA[<p>One of the projects I&#8217;m working on I chose to use one of the new Freescale KL25 MCUs.  They have an ARM Cortex-M0+ core and lots of peripherals to boot, including ones specifically designed for low power usage.  Below are some of the reasons why I chose it, and what my experiences with it have been.</p>
<p><span id="more-144"></span></p>
<h2>Why the KL25?</h2>
<p>Initially when I chose the KL25 my criteria was an MCU with a few KB of RAM for text buffers and a timer/counter that can operate as a counter with almost everything else shut off on the chip.  I can get quite a bit of RAM on the KL25 and its low power timer can operate in counter mode with almost everything else shut off on the chip.  The other thing it has going for it is that it has an ARM Cortex core in it.  A proprietary core is a drawback for me when ARM has the one <del>ring</del> core to rule them all.  I don&#8217;t have to learn a new interrupt controller every time or a new whatever.  Don&#8217;t get me wrong, everyone has their own peripherals, but as far as the different Cortex cores go, it&#8217;s just remembering which instructions are in which core.</p>
<h2>Initial Experience</h2>
<p>I picked up one of the <a href="http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=FRDM-KL25Z">FRDM-KL25</a> boards to give it a dry run.  The IDE I used was the CodeWarrior 10.3 beta.  First up, the Processor Expert tool was a life saver.  The clocking on the KL25 is rather involved and it takes up to three different clock modes to get to your final mode.  The complexity comes with the flexibility the processor provides.  Processor Expert is great, even if all you do is cut the code out that it generates and paste it into your own application.  Without it, I don&#8217;t know if i would have made it past setting up the clock.  After that it was a matter of setting up the different peripherals and going.  I didn&#8217;t use DMA or interrupts for my first trial, just polling.</p>
<h2>Onto a Custom Board</h2>
<p>After it working for our initial prototype, we did up a custom board with the KL25 on it.  The only issue I had there was that my clocks wouldn&#8217;t start.  It turns out that I fried the MCU the first time I powered it up.  Four dead chips later I figured out that the example I was coping the RTC code from used a crystal, not an external clock chip.  So, whenever I &#8220;enabled the oscillator,&#8221; the MCU and external clock were fighting who was driving the CLK_IN pin and it killed the clock.  It turns out that step can be omitted when using an external clock.</p>
<h2>Peripheral Thoughts</h2>
<p>I was a little surprised that only two of the five GPIO banks are capable of interrupts.  I had expected all of them to.  That just meant I had to be a little more careful with what i attached to which pin.</p>
<p>The KL25 has an up to 16-bit ADC.  However, the internal voltage reference is only good to +/- 3%.  That gives me six bits of accuracy for the voltage reference&#8230; which gives me six bits of absolute accuracy for a 16-bit measurement unless I attach an external voltage reference that is more precise.  I will say that I am happy with the temperature I am getting out of the internal temperature diode.  The first time I programmed it up I got within about 0.1 °C of what I expected.  One thing I will nit-pick about the ADC documentation is the &#8220;alternate&#8221; voltage reference that can be set.  I tried using it since I did not connect the external analog power pins initially.  I was able to read the internal voltage reference and temperature diode just fine, but external readings were way off.  After I connected the external analog voltage pins, VDDA &amp; VSSA, the external readings were good.  I like that the ADC will do reading averaging by itself without intervention from the core.  I tell it to get me the reading from a channel and put the core into a wait state until the ADC tells me it&#8217;s done with its readings.</p>
<h2>Conclusion</h2>
<p>I like the KL25 and hope that it works out well in the long run.  To finesse the lowest power out of it takes some doing and I figure my code base will undergo several revisions as I push to get the current draw down as low as it can go.  A lot of that will be mastering the clocking of the chip and putting enough intelligence into the program so that it will change the peripheral clocking when the core is put into one of the low power modes.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.curioussystem.com/2013/02/freescale-kl25-cortex-m-0-review/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Editing PIC .hex files</title>
		<link>http://blog.curioussystem.com/2012/12/editing-pic-hex-files/</link>
		<comments>http://blog.curioussystem.com/2012/12/editing-pic-hex-files/#comments</comments>
		<pubDate>Tue, 11 Dec 2012 19:52:06 +0000</pubDate>
		<dc:creator>Chad Kidder</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[PIC]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[CCS]]></category>
		<category><![CDATA[CRC]]></category>
		<category><![CDATA[Undocumented Feature]]></category>

		<guid isPermaLink="false">http://blog.curioussystem.com/?p=142</guid>
		<description><![CDATA[&#8230; or how to edit a .hex file instead of recompiling it for every option. I had a product a while back where the customer needed to program it, but we didn&#8217;t want them to have to hassle with compiling it for every (10,000+) permutation.  I had a whole build system set up when this &#8230; </p><p><a class="more-link block-button" href="http://blog.curioussystem.com/2012/12/editing-pic-hex-files/">Continue reading &#187;</a>]]></description>
				<content:encoded><![CDATA[<p>&#8230; or how to edit a .hex file instead of recompiling it for every option.</p>
<p>I had a product a while back where the customer needed to program it, but we didn&#8217;t want them to have to hassle with compiling it for every (10,000+) permutation.  I had a whole build system set up when this was just a few hundred files that would do all the compilations for me (thank you python!).  That was now out of the question.  There were a few hurdles to do this on a PIC, but we achieved it<span id="more-142"></span></p>
<h2>Outsmarting the Compiler</h2>
<p>We use the CCS compiler for our work on the PIC16 processor.  There is a handy compiler tag to tell it where to put your constant variable, <code class="codecolorer c default"><span class="c"><span style="color: #339933;">#org StartLocation, StopLocation</span></span></code>.  Simple right?  right?  Not so fast. If you just define a constant variable, refer to it once when loading it into the actual variable in RAM, the compiler will compile it out in the name of speed.  How nice.  To outsmart the CCS compiler, one had to create a one element array and initialize it as so:</p>
<div class="codecolorer-container c geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int32</span> SomeConstantVariable<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>SOME_VALUE<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>Now we have the value stored in flash memory as part of the program.</p>
<h2>Overcoming the Architecture</h2>
<p>One interesting tidbit of heretofore insignificant trivia is that the PIC16 uses 14-bit words for instructions.  This now becomes significant because we are saving our variable in code memory.  How do we access it?  How does it show up in 14-bit memory?  It shows up as the lowest eight bits of a 14-bit word.  So, our example four-byte <code class="codecolorer c default"><span class="c"><span style="color: #993333;">unsigned</span> <span style="color: #993333;">int32</span></span></code> takes up eight bytes of space with every other byte <code class="codecolorer c default"><span class="c"><span style="color: #208080;">0x34</span></span></code>.  The higher byte is actually an opcode (RETLW) that returns the lower byte.  The CCS compiler does not access the memory location so much as run the code at the location.  As part of this method there is some overhead associated with storing a value.  There is a one word prefix added to the amount of memory required.  Let me show you how this works.  Say we are trying to store the value 0xFEDCBA98 (4,275,878,552 unsigned) into memory.  The value is stored in memory Little Endian style, so the lowest byte is at the lowest address.</p>
<table align="center">
<caption>Memory Layout</caption>
<tbody>
<tr>
<td>Address (hex)</td>
<td>100</td>
<td>101</td>
<td>102</td>
<td>103</td>
<td>104</td>
</tr>
<tr>
<td>Value (hex)</td>
<td>000B</td>
<td>3498</td>
<td>34AB</td>
<td>34CD</td>
<td>34EF</td>
</tr>
</tbody>
</table>
<p>Now that we&#8217;ve got that figured out, let&#8217;s put it together:</p>
<div class="codecolorer-container c geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#org 0x0100, 0x0104</span><br />
<span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int32</span> SomeConstantVariable<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>SOME_VALUE<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>Notice here that I am stating the length of the array, not the variable index of the array.  I am not assigning a value to array element &#8217;1&#8242;, but initializing an array of size one.</p>
<h2>Editing the Hex File</h2>
<p>To edit the hex file we need to find the memory location we care about and write to it.  One thing to note is that the memory location listed in the .hex file and the memory location listed in the <code class="codecolorer c default"><span class="c"><span style="color: #339933;">#org</span></span></code> is not the same.  The value in the intel hex file is actually twice the value listed in the <code class="codecolorer c default"><span class="c"><span style="color: #339933;">#org</span></span></code> statement.  Why?  Because the intel hex file is addresses bytes, but the <code class="codecolorer c default"><span class="c"><span style="color: #339933;">#org</span></span></code> addresses 14-bit words.  The <a href="http://en.wikipedia.org/wiki/Intel_HEX">intel hex file </a>is a paged file format, so take that into account when looking up addresses.  Now we know the address we need and what to look for in memory, so what&#8217;s left?  Making sure we get the CRC on the .hex file correct.</p>
<h2>Embrace and Extend or, a custom CRC</h2>
<p>At this point we have edited our .hex file and want to load it onto our firmware.  We can do that using the ICD-64U previously mentioned using their handy-dandy command line tool&#8230; but it doesn&#8217;t like the checksum.  Wait you say, there isn&#8217;t a checksum as part of the intel hex file format?  CCS was kind enough to add one for us.  The last line of the file reads: <code class="codecolorer text default"><span class="text">;CRC=5FA2  CREATED=&quot;11-Dec-12 12:13&quot;</span></code>.  The CRC is run on all lines proceeding this line in the file.  The following code, in C#, will generate the CRC for the file:</p>
<div class="codecolorer-container csharp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">ushort</span> CRC16<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> inBytes<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">long</span> length <span style="color: #008000;">=</span> inBytes<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">ushort</span> crc_Dbyte <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">ushort</span> byte_counter <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">byte</span> bit_counter<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">ushort</span> c<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span>byte_counter <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> byte_counter <span style="color: #008000;">&lt;</span> length<span style="color: #008000;">;</span> byte_counter<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>inBytes<span style="color: #008000;">&#91;</span>byte_counter<span style="color: #008000;">&#93;</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">31</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span>inBytes<span style="color: #008000;">&#91;</span>byte_counter<span style="color: #008000;">&#93;</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">127</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ushort</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>inBytes<span style="color: #008000;">&#91;</span>byte_counter<span style="color: #008000;">&#93;</span> <span style="color: #008000;">&lt;&lt;</span> <span style="color: #FF0000;">8</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;</span> 0xFF00<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">for</span><span style="color: #008000;">&#40;</span>bit_counter<span style="color: #008000;">=</span><span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> bit_counter <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">8</span><span style="color: #008000;">;</span> bit_counter<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>crc_Dbyte <span style="color: #008000;">^</span> c<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;</span> 0x8000<span style="color: #008000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; crc_Dbyte <span style="color: #008000;">&lt;&lt;=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; crc_Dbyte <span style="color: #008000;">^=</span> 0x1021<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; crc_Dbyte <span style="color: #008000;">&lt;&lt;=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c <span style="color: #008000;">&lt;&lt;=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">return</span> crc_Dbyte<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>There is also an option in the command line tool to ignore the checksum, but it is generally not good form to have warnings thrown at the end user that occur all the time.  If you&#8217;re wondering why I chose C# for this code it&#8217;s simple.  The programmer runs on windows and it was the easiest way to create it and get it to work.  I don&#8217;t want to have to pack python libraries with it, or have them install the GTK framework, or wxWindows.</p>
<h2>Closing Remarks</h2>
<p>That gives enough information to get you going.  I hope this just saved someone a week of learning the hard way&#8230; and a lot of back and forth with CCS tech support.  If you need some help filling in the gaps, I happen to know an <a href="http://curioussystem.com">engineer who can help</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.curioussystem.com/2012/12/editing-pic-hex-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Case of the Random Lockup</title>
		<link>http://blog.curioussystem.com/2012/05/the-case-of-the-random-lockup/</link>
		<comments>http://blog.curioussystem.com/2012/05/the-case-of-the-random-lockup/#comments</comments>
		<pubDate>Sat, 19 May 2012 17:43:16 +0000</pubDate>
		<dc:creator>Chad Kidder</dc:creator>
				<category><![CDATA[embedded C]]></category>
		<category><![CDATA[Stellaris]]></category>
		<category><![CDATA[interrupts]]></category>
		<category><![CDATA[linker]]></category>
		<category><![CDATA[pain]]></category>

		<guid isPermaLink="false">http://blog.curioussystem.com/?p=141</guid>
		<description><![CDATA[We had a bug that just wouldn&#8217;t go away. Sometimes it would show up quickly, sometimes it would show up after the code had been running for a few days&#8230; but it would show up. What&#8217;s the programmer to do? The Setup So here&#8217;s the background. We have an ARM Cortex-M3 MCU that we are &#8230; </p><p><a class="more-link block-button" href="http://blog.curioussystem.com/2012/05/the-case-of-the-random-lockup/">Continue reading &#187;</a>]]></description>
				<content:encoded><![CDATA[<p>We had a bug that just <em>wouldn&#8217;t go away</em>. Sometimes it would show up quickly, sometimes it would show up after the code had been running for a few days&#8230; but it would show up. What&#8217;s the programmer to do?</p>
<p><span id="more-141"></span></p>
<h2>The Setup</h2>
<p>So here&#8217;s the background. We have an ARM Cortex-M3 MCU that we are using as a controller and IO processor for a device. It&#8217;s coordinating about 4 serial connections. Luckily, only two to three of those are active at a time. We had the proof of concept code running without hangup for over a week. Add a few more functions and things look OK&#8230; then we started getting lockups after the program ran for a few days. The first temptation was to ignore them because they were happening after the device was running for a few days. People wouldn&#8217;t use it that long in the real world, right? Then we had one of the lockups occur after 9 hours. That was a little harder to marginalize&#8230; so down the rabbit hole we go.</p>
<h2>Initial Diagnosis</h2>
<p>When I&#8217;d stop the MCU in the debugger, it was reliably stopped in the same location in memory. A stack trace was not intuitive as well, since the debugger couldn&#8217;t figure out which subroutine we were in&#8230; because we were outside programmed memory. It felt like the Star Trek: The Next Generation episode where they traveled past the edge of the universe. After some slight changes to the code to aid in debugging, it now started hanging at another address reliably. I figured there had to be a reference I had wrong in the code that was causing the jump to nowhere, but how to find it?</p>
<p>A week of code review didn&#8217;t turn it up. I thought it might have something to do with a stack overrun (I had this problem when using sprinf() before) so I pumped the stack up to 3x what I thought it needed. I even went to the trouble to initialize the stack so I could see what was used and while instructive, it showed that stack overflow was not the culprit.</p>
<h2>But where does it start?</h2>
<p>Trying to piece the stack trace together manually, it looked like the code was locking up during a delay loop. Statistically, that was to be expected since that&#8217;s where the processor spends 90%+ of it&#8217;s time. So, was it correlation or coincidence? Was the address it was locking up the first place it went off the reservation, or just where it got stuck. To help with this I made sure that whenever I programmed the MCU, it cleared all the flash memory, not just the portion I was using. That changed where the program was hanging. Now it was hanging on a jump to the end of the 32-bit address space&#8230; where I didn&#8217;t even have memory.</p>
<p>At this point I&#8217;m starting to rip out whole modules of code to try and narrow down what is occurring. We&#8217;re two weeks into trying to track this bug down. It&#8217;s still hanging. Ripping out sections of code hasn&#8217;t solved anything. It did lead to a clue. When I activated the SysTick interrupt I now had the processor reliably hanging less than a second after I enabled interrupts on the MCU. So, this is interrupt related. Hmmm&#8230;</p>
<h2>On the Trail</h2>
<p>I went and checked my vector interrupt table (VIT) and it looked good. I&#8217;m pretty sure it is the interrupts, but what is going on here. How am I getting sent to the end of the know address space when I&#8217;ve looked at the actual values being stored, which are all legit. How to further narrow things down?</p>
<p>I went looking for an opcode I could write across all unused memory to further figure things out. After a lot of digging I found an opcode (breakpoint, 0xbexx) that would pause things when debugging. I went into the linker script and had all extra memory filled with its opcode. Now let&#8217;s see where that gets us.</p>
<h2>A Break in the Case</h2>
<p>When we started up the code this time, it did indeed halt the code&#8230;. at the same address as the value of the opcode I had just overwritten everything extra with. So&#8230; we&#8217;re interrupting and jumping to the address all my unused memory is pointed at. At this point an epiphany occurred. While I hadn&#8217;t made many changes to the c code for the project, I had changed the location of things in flash memory so I could do boot-loading. I had also changed where I stored the VIT. Maybe I was onto something here? I did some digging and while relocatable, I have to tell the MCU where I relocated it to. The software is not smart enough to do that for you. Some of the built-in libraries may do that for you, but if you&#8217;re moving things in the boot loader, good luck.</p>
<h2>The Fix is In</h2>
<p>So, was it that simple, set a register and we&#8217;re good? To test I found said register and set it before any interrupts were enabled&#8230; and let it run&#8230; and run&#8230; and run. After about four days I declared it good&#8230; and then let it run over the weekend just to be sure.</p>
<p>The moral of the story is, just because you tell one piece of software (the linker in this case) what you are doing, don&#8217;t assume that the other piece (the compiler) or the hardware is smart enough to figure out what you are doing. For the gory details see the <a href="http://e2e.ti.com/support/development_tools/code_composer_studio/f/81/p/179877/663147.aspx#663147">e2e.ti.com thread</a>. I hope that this helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.curioussystem.com/2012/05/the-case-of-the-random-lockup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Two clicks to program a PIC</title>
		<link>http://blog.curioussystem.com/2012/02/two-clicks-to-program-a-pic/</link>
		<comments>http://blog.curioussystem.com/2012/02/two-clicks-to-program-a-pic/#comments</comments>
		<pubDate>Tue, 07 Feb 2012 05:41:59 +0000</pubDate>
		<dc:creator>Chad Kidder</dc:creator>
				<category><![CDATA[PIC]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[device programming]]></category>
		<category><![CDATA[firmware]]></category>
		<category><![CDATA[ICD-64U]]></category>
		<category><![CDATA[laziness]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://blog.curioussystem.com/?p=139</guid>
		<description><![CDATA[Sometimes you end up having to program a device with many different types of firmware. For a project I&#8217;ve used PICs on, this was the case. Here&#8217;s how to setup things so that all it requires is two clicks to load the firmware from windows explorer. This trick uses the CCS ICD-64U programmer (it will &#8230; </p><p><a class="more-link block-button" href="http://blog.curioussystem.com/2012/02/two-clicks-to-program-a-pic/">Continue reading &#187;</a>]]></description>
				<content:encoded><![CDATA[<p>Sometimes you end up having to program a device with many different types of firmware.  For a project I&#8217;ve used PICs on, this was the case.  Here&#8217;s how to setup things so that all it requires is two clicks to load the firmware from windows explorer.<br />
<span id="more-139"></span></p>
<p>This trick uses the <a href="http://ccsinfo.com/product_info.php?products_id=icd_u64">CCS ICD-64U</a> programmer (it will probably work with any of their programmers).  We first create a batch file (with a nice name like PIC.bat) in our favorite text editor that calls the loader and then pauses so we can read the output.  You may have to adjust the location of the executable depending on your installation location.</p>
<div class="codecolorer-container dos geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #33cc33;">@</span><a href="http://www.ss64.com/nt/echo.html"><span style="color: #b1b100; font-weight: bold;">echo</span></a> off<br />
&quot;c:\Program Files<span style="color: #33cc33;">(</span>x86<span style="color: #33cc33;">)</span>\PICC\ccsloader.exe&quot; +VERBOSE +WRITE=<span style="color: #33cc33;">%</span><span style="color: #448888;">1</span><br />
<a href="http://www.ss64.com/nt/pause.html"><span style="color: #b1b100; font-weight: bold;">pause</span></a></div></div>
<p>Next copy the batch file into <code class="codecolorer text default"><span class="text">%APPDATA%\Microsoft\Windows\SendTo</span></code> and you&#8217;re done.  Now find a firmware file, right click on it and then you should have the batch file listed as an option.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.curioussystem.com/2012/02/two-clicks-to-program-a-pic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multithreading, Python and passed arguments</title>
		<link>http://blog.curioussystem.com/2012/02/multithreading-python-and-passed-arguments/</link>
		<comments>http://blog.curioussystem.com/2012/02/multithreading-python-and-passed-arguments/#comments</comments>
		<pubDate>Sun, 05 Feb 2012 03:59:25 +0000</pubDate>
		<dc:creator>Chad Kidder</dc:creator>
				<category><![CDATA[computers]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[deep copy]]></category>
		<category><![CDATA[dictionary]]></category>
		<category><![CDATA[multithreading]]></category>
		<category><![CDATA[shallow copy]]></category>

		<guid isPermaLink="false">http://blog.curioussystem.com/?p=137</guid>
		<description><![CDATA[Recently I&#8217;ve had a project that required precompiling the firmware for a device so that the end user could program the device, but not have the source code. We&#8217;re not talking about a few versions of the code, but almost 1000. This is something that no person would want to do, especially since it would &#8230; </p><p><a class="more-link block-button" href="http://blog.curioussystem.com/2012/02/multithreading-python-and-passed-arguments/">Continue reading &#187;</a>]]></description>
				<content:encoded><![CDATA[<p>Recently I&#8217;ve had a project that required precompiling the firmware for a device so that the end user could program the device, but not have the source code.  We&#8217;re not talking about a few versions of the code, but almost 1000.  This is something that no person would want to do, especially since it would have to be redone every time the source code changes.  Python to the rescue.  It was simple enough to write a program that would copy the source code, change a bit of information in a header file, compile it and save the binary to the appropriate location.  Controlling other programs is pretty easy with the <a href="http://docs.python.org/library/subprocess.html">subprocess</a> module.  That&#8217;s great and all, but doing it single-threaded, that&#8217;s so 90s.  Python makes multithreading pretty simple using its <a href="http://docs.python.org/library/multiprocessing.html">multiprocessing</a> library.  The trick is not stepping on any toes when you do it.</p>
<p><span id="more-137"></span></p>
<p>For starters, I have eight (okay, four hyperthreaded) cores.  Since I still have to use the system, I set up a <a href="http://docs.python.org/library/multiprocessing.html#using-a-pool-of-workers">pool</a> to use up to six threads.  The object that is returned from the pool.apply_async() function was arrayed to keep track of all the desired processes.  This is because in the time it takes for a dozen of the compilations to finish, I&#8217;ve already queued up almost 1000 compilations.  At this point the main python thread just sits there checking to see if a task has completed and stores the output information.</p>
<h2>Gotchas</h2>
<h3>Timing</h3>
<p>One of the problems that I encountered was that of making sure that I wasn&#8217;t copying the source code into the same place to compile.  To create unique directories I tried to timestamp them down to the microsecond.  That would have worked wonderfully&#8230; except that the time stamp I was getting out of the time module in windows was only granular to about 10 milliseconds.  That caused some of the threads to fail because they were trying to use the same directory.  I found another way to uniquely name the directories and then we were good./p></p>
<h3>Keeping passed data safe from other threads</h3>
<p>You&#8217;re familiar with the terms shallow and deep copy, right?  Do you know when each one is done in python?  Most of the time it is a shallow copy.  Well, when you pass the same dictionary, with slightly different parameters, as the argument in each pool.apply_async() call&#8230; you can see where this is going.  By the time the dictionary could be acted upon it had been changed in the main thread&#8230; many times.  Luckily, we have the <a href="http://docs.python.org/library/copy.html">copy.deepcopy()</a> function to save the day.  I just made sure that we did a deep copy of the dictionary before passing it and things went much better.</p>
<h2>A skeletal example</h2>
<p>The acutal code was written for a client so I will only show a prototype for you to use here</p>
<div class="codecolorer-container python geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">subprocess</span><br />
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">copy</span> <span style="color: #ff7700;font-weight:bold;">import</span> deepcopy<br />
<span style="color: #ff7700;font-weight:bold;">from</span> multiprocessing <span style="color: #ff7700;font-weight:bold;">import</span> Pool<br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> ThreadFunction<span style="color: black;">&#40;</span>*args<span style="color: #66cc66;">,</span> **kwargs<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#This thread is what is called by the pool</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#subprocess.call(['/path/to/something', 'arg1','arg2'])</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> kwargs<span style="color: black;">&#91;</span><span style="color: #483d8b;">'square'</span><span style="color: black;">&#93;</span>**<span style="color: #ff4500;">2</span> &nbsp; <span style="color: #808080; font-style: italic;">#This gives us something to return</span><br />
<br />
<br />
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">&quot;__main__&quot;</span>:<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#We have to wrap the main body in here for multiprocessing to work</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#because the file is loaded for each thread.</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#If we don't do this, each thread would start executing the whole program</span><br />
&nbsp; &nbsp; pool <span style="color: #66cc66;">=</span> Pool<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span> &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">#This sets up multithreading with up to 6 worker threads</span><br />
&nbsp; &nbsp; PoolResults <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span> &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">#This is where we will keep track of the results</span><br />
&nbsp; &nbsp; ArgDict <span style="color: #66cc66;">=</span> <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">#Dictionary for arguments</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> ii <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2000</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; ArgDict<span style="color: black;">&#91;</span><span style="color: #483d8b;">'square'</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> ii<br />
&nbsp; &nbsp; &nbsp; &nbsp; PoolResults.<span style="color: black;">append</span><span style="color: black;">&#40;</span>pool.<span style="color: black;">apply_async</span><span style="color: black;">&#40;</span>ThreadFunction<span style="color: #66cc66;">,</span> deepcopy<span style="color: black;">&#40;</span>ArgDict<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#now we've got all our tasks queued up, we need to wait until they are</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#done</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; results <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>PoolResults<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">try</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#get the results if they are ready and append to the results array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results.<span style="color: black;">append</span><span style="color: black;">&#40;</span>PoolResults<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span>timeout<span style="color: #66cc66;">=</span><span style="color: #ff4500;">0.01</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PoolResults.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">except</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">pass</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#Now our tasks have completed and we can show the results</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> results</div></div>
<p>You may be asking why I didn&#8217;t use the <a href="http://docs.python.org/library/multiprocessing.html#multiprocessing.pool.multiprocessing.Pool.map">map()</a> function of the pool.  The reason is that the calculations involved in each iteration were sufficiently complex that I didn&#8217;t want to wait until I had all the calculations done for each permutation of code before starting compilation.  The first few versions of the code had the calculations necessary to figure out the code changes taking as long as the compilation.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.curioussystem.com/2012/02/multithreading-python-and-passed-arguments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Of embedded black boxes</title>
		<link>http://blog.curioussystem.com/2012/01/of-embedded-black-boxes/</link>
		<comments>http://blog.curioussystem.com/2012/01/of-embedded-black-boxes/#comments</comments>
		<pubDate>Sat, 07 Jan 2012 15:53:48 +0000</pubDate>
		<dc:creator>Chad Kidder</dc:creator>
				<category><![CDATA[embedded C]]></category>
		<category><![CDATA[Embedded Systems]]></category>
		<category><![CDATA[MSP430]]></category>
		<category><![CDATA[PIC]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Stellaris]]></category>

		<guid isPermaLink="false">http://blog.curioussystem.com/?p=136</guid>
		<description><![CDATA[Over the last few months I&#8217;ve been working on projects using PIC microcontrollers. At first I had a rather negative view of the PIC processors. Since I started coding on them, I have come to realize that it is the compiler that I have issues with much more than the hardware. The compiler we&#8217;ve been &#8230; </p><p><a class="more-link block-button" href="http://blog.curioussystem.com/2012/01/of-embedded-black-boxes/">Continue reading &#187;</a>]]></description>
				<content:encoded><![CDATA[<p>Over the last few months I&#8217;ve been working on projects using PIC microcontrollers.  At first I had a rather negative view of the PIC processors.  Since I started coding on them, I have come to realize that it is the compiler that I have issues with much more than the hardware.  The compiler we&#8217;ve been using by <a href="http://ccsinfo.com" target="_blank">CCS</a> is designed to get projects up and running quickly by abstracting the hardware from the coder.  So, instead of directly writing to the registers, you call one of their functions.  We&#8217;ve found this works well when it is a mature processor and when we&#8217;re not trying to do too many things at once.  Where we ran into lots of frustration was with new processors.  We thought we had the code written correctly, yet it wasn&#8217;t working.  Now instead of just having our code and hardware errata to look at, I also had to figure out what the CCS code was doing&#8230; which means slogging through the disassembled code and looking up the mnemonics for the different assembly operations.</p>
<p>Now, you need to understand where I come from.  I&#8217;m a Professional Engineer.  When I vouch for something, I&#8217;m saying that it works.  When I have the black box of a intrinsic compiler functions, it makes me a little less sure about what is occurring.  That&#8217;s one of the reasons why I like the <a href="http://www.ti.com/msp430" target="_blank">MSP430</a>.  TI does a very good job of letting me know what&#8217;s going on with the chip.  There&#8217;s not much left to the imagination when using it.  The analog functions are well documented and there are good code examples.  The <a href="http://www.ti.com/stellaris" target="_blank">Stellaris</a> line of ARM Cortex-M3 chips tries to make both camps happy.  It has good documentation of things, but it also comes with a large driver library that encapsulates many of the functions for an engineer to rapidly program on it.</p>
<p>My beef with embedded processor abstraction through libraries or intrinsic functions is that it usually doesn&#8217;t keep up with the latest processors and that it is too easy to do something that modifies something else and then you don&#8217;t know where the problem is being generated because the functions work separately or in a different order.  Oh, and they take more memory, on memory constrained processors.  What&#8217;s your experience?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.curioussystem.com/2012/01/of-embedded-black-boxes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cleaning up filenames for transfer to windows</title>
		<link>http://blog.curioussystem.com/2012/01/cleaning-up-filenames-for-transfer-to-windows/</link>
		<comments>http://blog.curioussystem.com/2012/01/cleaning-up-filenames-for-transfer-to-windows/#comments</comments>
		<pubDate>Sat, 07 Jan 2012 15:23:30 +0000</pubDate>
		<dc:creator>Chad Kidder</dc:creator>
				<category><![CDATA[computers]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.curioussystem.com/?p=135</guid>
		<description><![CDATA[For those of you run multiple operating systems, you may have run across the problem where the filenames on one are not valid on the other. Specifically I&#8217;ve had that problem when using NTFS filesystems between Linux and Windows. The NTFS3G drivers on Linux will allow characters in the file names that windows doesn&#8217;t like. &#8230; </p><p><a class="more-link block-button" href="http://blog.curioussystem.com/2012/01/cleaning-up-filenames-for-transfer-to-windows/">Continue reading &#187;</a>]]></description>
				<content:encoded><![CDATA[<p>For those of you run multiple operating systems, you may have run across the problem where the filenames on one are not valid on the other.  Specifically I&#8217;ve had that problem when using NTFS filesystems between Linux and Windows.  The NTFS3G drivers on Linux will allow characters in the file names that windows doesn&#8217;t like.  To solve this, I wrote a quick python script that will make the filenames windows acceptable.  Enjoy.</p>
<p>windows_rename.py</p>
<div class="codecolorer-container python geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">#! /usr/bin/env python</span><br />
<span style="color: #808080; font-style: italic;">#Copyright 2012 Chad Kidder</span><br />
<span style="color: #808080; font-style: italic;">#Released under GPL v3.0</span><br />
<br />
<br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">sys</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">re</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">shutil</span><br />
<br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> SafeNames<span style="color: black;">&#40;</span>location<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> root<span style="color: #66cc66;">,</span> dirs<span style="color: #66cc66;">,</span> files <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">walk</span><span style="color: black;">&#40;</span>location<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> tfile <span style="color: #ff7700;font-weight:bold;">in</span> files:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NewFile <span style="color: #66cc66;">=</span> InvalidCharacters.<span style="color: black;">sub</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;_&quot;</span><span style="color: #66cc66;">,</span>tfile<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> NewFile <span style="color: #66cc66;">&lt;&gt;</span> tfile:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">shutil</span>.<span style="color: black;">move</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>root<span style="color: #66cc66;">,</span> tfile<span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>root<span style="color: #66cc66;">,</span> NewFile<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%s -&gt; %s&quot;</span> % <span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>root<span style="color: #66cc66;">,</span> tfile<span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>root<span style="color: #66cc66;">,</span> NewFile<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> tdir <span style="color: #ff7700;font-weight:bold;">in</span> dirs:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NewDir <span style="color: #66cc66;">=</span> InvalidCharacters.<span style="color: black;">sub</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;_&quot;</span><span style="color: #66cc66;">,</span>tdir<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> NewDir <span style="color: #66cc66;">&lt;&gt;</span> tdir:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%s -&gt; %s&quot;</span> % <span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>root<span style="color: #66cc66;">,</span> tdir<span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>root<span style="color: #66cc66;">,</span> NewDir<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">shutil</span>.<span style="color: black;">move</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>root<span style="color: #66cc66;">,</span> tdir<span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>root<span style="color: #66cc66;">,</span> NewDir<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">&quot;__main__&quot;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">2</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Please enter a directory to fix the file names on&quot;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; InvalidCharacters <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;[<span style="color: #000099; font-weight: bold;">\/</span>?:*|'<span style="color: #000099; font-weight: bold;">\&quot;</span>]&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> location <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SafeNames<span style="color: black;">&#40;</span>location<span style="color: black;">&#41;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.curioussystem.com/2012/01/cleaning-up-filenames-for-transfer-to-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
