<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Embedded Freaks..</title>
	<atom:link href="http://embeddedfreak.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://embeddedfreak.wordpress.com</link>
	<description>Daily embedded life tips and trick</description>
	<lastBuildDate>Sat, 14 Nov 2009 00:47:09 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='embeddedfreak.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/9f1113cc5558fcab720e54b7e5a379eb?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Embedded Freaks..</title>
		<link>http://embeddedfreak.wordpress.com</link>
	</image>
			<item>
		<title>Tracing I2C Communication Direction</title>
		<link>http://embeddedfreak.wordpress.com/2009/11/14/tracing-i2c-communication-direction/</link>
		<comments>http://embeddedfreak.wordpress.com/2009/11/14/tracing-i2c-communication-direction/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 00:13:06 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[embedded-tips]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=487</guid>
		<description><![CDATA[Sometimes, the most difficult part when dealing with I2C is to differentiate the communication direction: which signal is sent to which device. Hence, this simple trick should be useful.

Things to notice is that:

It only works for two devices debugging
The hump that is caused by the resistor should not exceed the threshold of maximum VInput (ie [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=487&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Sometimes, the most difficult part when dealing with I2C is to differentiate the communication direction: which signal is sent to which device. Hence, this simple trick should be useful.</p>
<p><span id="more-487"></span></p>
<div id="attachment_488" class="wp-caption aligncenter" style="width: 441px"><img class="size-full wp-image-488" title="I2C_Debug_1" src="http://embeddedfreak.files.wordpress.com/2009/11/i2c_debug_1.png?w=431&#038;h=425" alt="I2C_Debug_1" width="431" height="425" /><p class="wp-caption-text">I2C Debugging Trick</p></div>
<p>Things to notice is that:</p>
<ul>
<li>It only works for two devices debugging</li>
<li>The hump that is caused by the resistor should not exceed the threshold of maximum VInput (ie not exceeding 0.3V for CMOS device or 0.7V for TTL device)</li>
</ul>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/487/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/487/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/487/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/487/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/487/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/487/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/487/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/487/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/487/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/487/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=487&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2009/11/14/tracing-i2c-communication-direction/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>

		<media:content url="http://embeddedfreak.files.wordpress.com/2009/11/i2c_debug_1.png" medium="image">
			<media:title type="html">I2C_Debug_1</media:title>
		</media:content>
	</item>
		<item>
		<title>LPC1766 Not So Reliable UART0 Loopback</title>
		<link>http://embeddedfreak.wordpress.com/2009/08/16/lpc1766-not-so-reliable-uart0-loopback/</link>
		<comments>http://embeddedfreak.wordpress.com/2009/08/16/lpc1766-not-so-reliable-uart0-loopback/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 05:35:30 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[Cortex-M]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=478</guid>
		<description><![CDATA[Let&#8217;s continue the last post with the real interrupt based program: UART0 interrupt-based loopback. The program example will echoing anything that is sent to the serial port, but since it&#8217;s not checking whether the TX FIFO is empty or not during the loopback, some data may be lost if you sent a file (or sending [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=478&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Let&#8217;s continue the last post with the real interrupt based program: UART0 interrupt-based loopback. The program example will echoing anything that is sent to the serial port, but since it&#8217;s not checking whether the TX FIFO is empty or not during the loopback, some data may be lost if you sent a file (or sending a lot of data).</p>
<p>The whole application can be downloaded <a href="/home/kunil/workspace/LPC1766_Uart_Interrupt-090816_1232.zip">here</a>.</p>
<p><span id="more-478"></span>The UART peripheral on LPC17xx works similar to the other LPC2000 series. The initialisation steps are:</p>
<p> // Change P0.2 and P0.3 mode to TXD0 and RXD0, assuming the each pins are pulled up (PINMODE = 00),<br />
 // and open drain mode is off (PINMODE_OD = 0).<br />
 // If you have to set the PINMODE, don&#8217;t pull down the TXD0 pin.<br />
 PINCON->PINSEL0 = (1 << 4) | (1 << 6);</p>
<p> // Set 8N1 mode<br />
 UART0->LCR &#038;= ~STOP_1_BIT &#038; ~PARITY_NONE;<br />
 UART0->LCR |= CHAR_8_BIT;</p>
<p> // Set the baud rate (ie. fill up the DLM, DLL, and FDR)<br />
 uart_set_baudrate(baudrate);</p>
<p> // Enable TX and RX FIFO. This step is optional, but since UART&#8217;s FIFO is there, and we&#8217;ve paid<br />
 // for it, I&#8217;d like to enable it.<br />
 UART0->FCR |= FIFO_ENABLE;</p>
<p> // Set FIFO to trigger when at least 14 characters available (only needed if FIFO is enabled)<br />
 UART0->FCR |= (3 << 6);</p>
<p> // Enable UART RX interrupt (for LPC17xx UART). Note, there&#8217;s 3 different interrupt under UART0<br />
 // interrupt (TX buffer empty/ THRE, RX buffer full / RDA, or character received timeout / CTI)<br />
 // Read LPC17xx user manual for the details<br />
 UART0->IER = RBR_IRQ_ENABLE;</p>
<p> // Enable the UART interrupt (for Cortex-CM3 NVIC)<br />
 NVIC_EnableIRQ(UART0_IRQn);</p>
<p>If you have work with LPC2000 series before, the whole initialisation code is similar, except for the last part &#8216;NVIC_EnableIRQ(UART0_IRQn)&#8217;. The NVIC_EnableIRQ() is used to enable the UART0 interrupt on NVIC.</p>
<p>The next step, after the UART initialisation, is to enable the global interrupt, like this:</p>
<p>int main(void) {<br />
 uart_init(115200);<br />
 __enable_irq();</p>
<p> while (1) {<br />
  __WFI();<br />
 }</p>
<p> return 0;<br />
}</p>
<p>The __enable_irq() is a macro from &#8216;core_cm3.h&#8217;, which will be expanded to &#8216;asm volatile(&#8220;cpsie i&#8221;)&#8217;. The __WFI() is also a macro for &#8216;asm volatile(&#8220;wfi&#8221;)&#8217;, to enter the sleep mode until the interrupt comes.</p>
<p>Now, how to handle the UART0 interrupt ? Take a look at your startup code:</p>
<p>void __attribute__((weak)) UART0_IRQHandler(void);      // 21: UART0<br />
// &#8230;.<br />
__attribute__ ((section(&#8220;.cs3.interrupt_vector&#8221;)))<br />
void (* const g_pfnVectors[])(void) = {<br />
 //..<br />
 UART0_IRQHandler, // 21: UART0<br />
};<br />
//..<br />
#pragma weak UART0_IRQHandler       = Default_Handler // 21: UART0</p>
<p>The startup code said that UART0 interrupt handler is named as UART0_IRQHandler, and it&#8217;s temporarily linked to Default_Handler(). It means that if you declare a function named: &#8216;void UART0_IRQHandler(void)&#8217;, the temporary link to Default_Handler() will be overridden. That&#8217;s how you&#8217;re handling the interrupt, by creating the same function name as in the startup file:</p>
<p>/*<br />
 * UART0 interrupt handler<br />
 */<br />
void UART0_IRQHandler(void) {<br />
 switch(UART0->IIR &#038; 0&#215;0E) {<br />
 case RDA_INTERRUPT:<br />
 case CTI_INTERRUPT:<br />
  // Loopback to send<br />
  UART0->THR = UART0->RBR;<br />
  break;<br />
 }<br />
}</p>
<p>In the interrupt handler, we still need to read IIR register, since LPC17xx implementation multiplexed several interrupts into a single interrupt.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/478/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/478/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/478/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/478/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/478/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/478/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/478/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/478/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/478/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/478/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=478&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2009/08/16/lpc1766-not-so-reliable-uart0-loopback/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>
	</item>
		<item>
		<title>Cortex-M3 Interrupt/Exception Control</title>
		<link>http://embeddedfreak.wordpress.com/2009/08/14/cortex-m3-global-interruptexception-control/</link>
		<comments>http://embeddedfreak.wordpress.com/2009/08/14/cortex-m3-global-interruptexception-control/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 13:18:57 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[Cortex-M]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=464</guid>
		<description><![CDATA[Cortex-M3 has NVIC (Nested Vector Interrupt Controller), a kind of peripheral, to support up to 255 interrupts.
Other than that, it also has WIC (Wakeup Interrupt Controller), which similar to NVIC, but it doesn&#8217;t need any clock to run. Hence it can be used to detect any interrupt during sleep mode without burning energy (no need [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=464&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Cortex-M3 has NVIC (Nested Vector Interrupt Controller), a kind of peripheral, to support up to 255 interrupts.</p>
<p>Other than that, it also has WIC (Wakeup Interrupt Controller), which similar to NVIC, but it doesn&#8217;t need any clock to run. Hence it can be used to detect any interrupt during sleep mode without burning energy (no need to have any clock running on system at all).</p>
<p>Since WIC is only available on rev2 Cortex-M3 (currently only LPC17xx that have it), this post will only discuss the NVIC control.<span id="more-464"></span></p>
<h4>Configurable Priority vs Unconfigurable Priority Interrupt</h4>
<p>On Cortex-M3, the exceptions / interrupts are divided into two categories, based on their priority: configurable and unconfigurable. The unconfigurable priority interrupts have fixed priority, and consist of 3 interrupts:</p>
<ol>
<li>Reset. Invoked on power on and reset</li>
<li>Non Maskable Interrupt. An external interrupt (activated by external pin) that is unstoppable except by reset.</li>
<li>Hard fault. Any fault that is not handled by other fault handlers (memory management fault, bus fault, usage fault) will activate this interrupt. The interrupt is also activated when the other fault handlers are turned off</li>
</ol>
<p>Those interrupts / exceptions are the basic interrupts that are expected to be handled by the application (read: <strong>MUST</strong> be implemented by application). The priority is started at -3 for Reset, -2 for NMI, and -1 for Hard fault.</p>
<p>The rest of 252 interrupts are priority configurable, starting at 0 (the highest priority) until 252 (the lowest priority).</p>
<h4>Global Interrupt Enable / Disable</h4>
<p>During the initial reset, NVIC is turned off. Therefore, the processor cannot receive any interrupts (except for NMI, Reset interrupt, and hard fault). To turn on the interrupts with configurable priority:</p>
<p>asm volatile (&#8220;cpsie i&#8221;);</p>
<p>&#8220;CPSIE I&#8221; is a assembly instruction to enable the priority configurable interrupts. Actually, it&#8217;s a shortcut to this longer procedure</p>
<p>asm volatile (&#8220;MOVS r0, #0\n\<br />
               MSR PRIMASK, r0&#8243;);</p>
<p>To turn off the priority configurable interrupts:</p>
<p>asm volatile (&#8220;cpsid i&#8221;);</p>
<p>Or, taking the longer non-atomic procedure:</p>
<p>asm volatile (&#8220;MOVS r0, #1\n\<br />
               MSR PRIMASK, r0&#8243;);</p>
<p>Actually, if it is needed, the hard fault interrupt can be turned off as well (along with the priority configurable interrupts) with this command:</p>
<p>asm volatile (&#8220;cpsid f&#8221;);</p>
<p>Or, using longer non-atomic procedure</p>
<p>asm volatile (&#8220;MOVS r0, #1\n\<br />
               MSR FAULTMASK, r0&#8243;);</p>
<p>To turn on the hard fault interrupt with the other priority configurable interrupts, use:</p>
<p>asm volatile (&#8220;cpsie f&#8221;);</p>
<p>Or, using longer non-atomic procedure</p>
<p>asm volatile (&#8220;MOVS r0, #0\n\<br />
               MSR FAULTMASK, r0&#8243;);</p>
<h4>Enabling / Disabling Group of Interrupts</h4>
<p>Since you have up to 255 interrupts, it&#8217;s odd to have just single global interrupt on/off. Hence, ARM created a register to enable/disable group of interrupts, based on their priority, the BASEPRI. If you set the BASEPRI, the interrupts with the same priority level or lower (ie. the priority number is bigger than BASEPRI) will be masked.</p>
<p>Here&#8217;s how to set it:</p>
<p>asm volatile (&#8220;MOVS r0, #5\n\<br />
               MSR BASEPRI, r0&#8243;);</p>
<p>The above code will mask the interrupts with priority 5 or below. To reset/disable the BASEPRI, just replace #5 with #0.</p>
<h4>Enabling / Disabling Specific Interrupts</h4>
<p>This one is very easy. If you look at the<a href="http://infocenter.arm.com/help/topic/com.arm.doc.ddi0337g/DDI0337G_cortex_m3_r2p0_trm.pdf"> Cortex-M3 technical manual</a>, the only thing that you need to do is to set IRQ X &#8216;Set Enable Register&#8217; (ISER) or &#8216;Clear Enable Register&#8217; (ICER). For example, to turn on TIMER0 interrupts (Match0, Match1, Capture0, Capture1) on LPC1766, you can use:</p>
<p>NVIC->ISER[0] = (1 << 1);</p>
<p>// Or if you have downloaded CMSIS library, you can use:</p>
<p>NVIC_EnableIRQ(TIMER0_IRQn);</p>
<p>The NVIC_EnableIRQ() is in core_cm3.h file at<a href="http://www.onarm.com/download/download389.asp"> CMSIS library source code</a>. Don&#8217;t forget to include &#8220;LPC17xx.h&#8221; too, before using these functions from core_cm3.h</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/464/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/464/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/464/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/464/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/464/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/464/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/464/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/464/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/464/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/464/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=464&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2009/08/14/cortex-m3-global-interruptexception-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>
	</item>
		<item>
		<title>Cortex-M3 SysTick peripheral</title>
		<link>http://embeddedfreak.wordpress.com/2009/08/10/cortex-m3-systick-peripheral/</link>
		<comments>http://embeddedfreak.wordpress.com/2009/08/10/cortex-m3-systick-peripheral/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 09:10:31 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[Cortex-M]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=458</guid>
		<description><![CDATA[The interesting part of Cortex-M3 is SysTick, an additional timer, provided by ARM, to switch the process context on RTOS. ARM was hoping that by using SysTick, most of RTOS will be portable from vendor to vendors.
But I&#8217;m a bit curious on how to use it. Hence, here&#8217;s the simple demo on how to use [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=458&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>The interesting part of Cortex-M3 is SysTick, an additional timer, provided by ARM, to switch the process context on RTOS. ARM was hoping that by using SysTick, most of RTOS will be portable from vendor to vendors.</p>
<p>But I&#8217;m a bit curious on how to use it. Hence, here&#8217;s the simple demo on how to use SysTick on Cortex-M3.<span id="more-458"></span>main.c</p>
<p>/*<br />
 * main.c<br />
 *<br />
 *  Created on: 10-Aug-2009<br />
 *      Author: kunil<br />
 */</p>
<p>#include &#8220;LPC17xx.h&#8221;<br />
#include &#8220;core_cm3.h&#8221;</p>
<p>/*<br />
 * Linker file variables<br />
 */<br />
extern void __sidata;<br />
extern void __sdata;<br />
extern void __edata;<br />
extern void __sbss;<br />
extern void __ebss;</p>
<p>static int main(void);<br />
static void setup_SysTick(void);<br />
static void setup_LEDs(void);<br />
static void display_current_time(void);</p>
<p>static unsigned int u32_current_time;</p>
<p>/*<br />
 * Override the standard Reset_Handler() on startup_LPC1766<br />
 */<br />
void Reset_Handler(void) {<br />
 unsigned int *source, *destination;</p>
<p> // Copy data section from ROM to RAM<br />
 source = (unsigned int *) &#038;__sidata;<br />
 for (destination = (unsigned int *) &#038;__sdata; destination<br />
      < (unsigned int *) &#038;__edata; destination++) {<br />
   *destination = *source;<br />
 }</p>
<p> // Clear the BSS section<br />
 for (destination = (unsigned int *) &#038;__sbss; destination<br />
       < (unsigned int *) &#038;__ebss; destination++) {<br />
   *destination = 0;<br />
 }</p>
<p> main();</p>
<p> // If main() ever return, this will hold it<br />
 while(1);<br />
}</p>
<p>/*<br />
 * Override the standard SysTick_Handler() on startup_LPC1766<br />
 */<br />
void SysTick_Handler(void) {<br />
 u32_current_time++;<br />
}</p>
<p>int main(void) {<br />
 setup_SysTick();<br />
 setup_LEDs();<br />
 while (1) {<br />
 /* Wait for interrupt..a.k.a sleep until interrupt */<br />
 asm volatile (&#8220;wfi&#8221;);<br />
 display_current_time();<br />
 }<br />
}</p>
<p>static void setup_SysTick(void) {<br />
 /* The upper limit of SysTick<br />
 *<br />
 * Since the MCU clock will be equal to internal RC (assumming the PLL is not<br />
 * activated), 4000000 value will generate 1s interrupt<br />
 *<br />
 * The value is counted at 1 (not zero). Hence -1 offset is introduced<br />
 */<br />
 SysTick->LOAD = (4000000 &#8211; 1);<br />
 /* SysTick control register<br />
 *<br />
 * SYSTICK_CLKSOURCE = 1 means the timer will use CPU (core)&#8217;s clock<br />
 * SYSTICK_TICKINT = 1 means the SysTick interrupt is activated on NVIC<br />
 * SYSTICK_ENABLE = 1 means enable SysTick<br />
 */<br />
 SysTick->CTRL = (1 << SYSTICK_CLKSOURCE) | (1<<SYSTICK_TICKINT) | (1 << SYSTICK_ENABLE);<br />
}</p>
<p>static void setup_LEDs(void) {<br />
 // Enable P1.28, P1.29, and P1.31 as output<br />
 GPIO1->FIODIR = (1 << 28) | (1 << 29) | (1 << 31);<br />
 // Enable P2.2 &#8211; P2.6 as output<br />
 GPIO2->FIODIR = (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6);<br />
}</p>
<p>static void display_current_time(void) {<br />
 /* Accessing the pins directly<br />
 *<br />
 * u32_current_time[0] = P1.28<br />
 * u32_current_time[1] = P1.29<br />
 * u32_current_time[2] = P1.31<br />
 * u32_current_time[3] = P2.2<br />
 * u32_current_time[4] = P2.3<br />
 * u32_current_time[5] = P2.4<br />
 * u32_current_time[6] = P2.5<br />
 * u32_current_time[7] = P2.6<br />
 */<br />
 GPIO1->FIOPIN = 0;<br />
 GPIO2->FIOPIN = 0;</p>
<p> GPIO1->FIOPIN |= (((u32_current_time &#038; 0&#215;03) >> 0) << 28);<br />
 GPIO1->FIOPIN |= (((u32_current_time &#038; 0&#215;04) >> 2) << 31);<br />
 GPIO2->FIOPIN |= (((u32_current_time &#038; 0xF8) >> 3) <<  2);<br />
}</p>
<p>To use the source code, you will need the &#8216;cm3_core.h&#8217;, &#8217;system_LPC17xx.h and &#8216;LPC17xx.h&#8217; file from CMSIS website. I&#8217;m using ver 1.20 source code from their site, and also my own C start up / linker file.</p>
<p>startup_LPC17xx.c</p>
<p>/*<br />
 * startup_LPC1766.c &#8211; Startup file for LPC1766 Cortex-M3<br />
 *<br />
 * @author: kunilkuda_at_gmail_dot_com<br />
 */<br />
extern void __cs3_stack; ///< This is global variable from linker script</p>
<p>/* We gave &#8216;weak&#8217; attribute, so these functions can be aliased into a single<br />
 * function<br />
 */<br />
void __attribute__((weak)) Reset_Handler(void);     /* Reset Handler */<br />
void __attribute__((weak)) NMI_Handler(void);       /* NMI Handler */<br />
void __attribute__((weak)) HardFault_Handler(void); /* Hard Fault Handler */<br />
void __attribute__((weak)) MemManage_Handler(void); /* MPU Fault Handler */<br />
void __attribute__((weak)) BusFault_Handler(void);  /* Bus Fault Handler */<br />
void __attribute__((weak)) UsageFault_Handler(void);/* Usage Fault Handler */<br />
void __attribute__((weak)) SVC_Handler(void);       /* SVCall Handler */<br />
void __attribute__((weak)) DebugMon_Handler(void);  /* Debug Monitor Handler */<br />
void __attribute__((weak)) PendSV_Handler(void);    /* PendSV Handler */<br />
void __attribute__((weak)) SysTick_Handler(void);   /* SysTick Handler */</p>
<p>/*<br />
 * Ask GCC to put this array into .cs3.interrupt_vector section<br />
 */<br />
__attribute__ ((section(&#8220;.cs3.interrupt_vector&#8221;)))<br />
void (* const g_pfnVectors[])(void) = {<br />
 &#038;__cs3_stack,       // The initial stack pointer<br />
 Reset_Handler,      // The reset handler<br />
 NMI_Handler,        // The NMI handler<br />
 HardFault_Handler,  // The hard fault handler<br />
 MemManage_Handler,  // The MPU fault handler<br />
 BusFault_Handler,   // The bus fault handler<br />
 UsageFault_Handler, // The usage fault handler<br />
 0,                  // Reserved<br />
 0,                  // Reserved<br />
 0,                  // Reserved<br />
 0,                  // Reserved<br />
 SVC_Handler,        // SVCall handler<br />
 DebugMon_Handler,   // Debug monitor handler<br />
 0,                  // Reserved<br />
 PendSV_Handler,     // The PendSV handler<br />
 SysTick_Handler,    // The SysTick handler<br />
};</p>
<p>/* Here&#8217;s how to alias multiple functions into single function<br />
 */<br />
void Default_Handler(void);<br />
#pragma weak NMI_Handler        = Default_Handler /* NMI handler */<br />
#pragma weak HardFault_Handler  = Default_Handler /* Hard Fault handler */<br />
#pragma weak MemManage_Handler  = Default_Handler /* MPU Fault Handler */<br />
#pragma weak BusFault_Handler   = Default_Handler /* Bus Fault Handler */<br />
#pragma weak UsageFault_Handler = Default_Handler /* Usage Fault Handler */<br />
#pragma weak SVC_Handler        = Default_Handler /* SVCall Handler */<br />
#pragma weak DebugMon_Handler   = Default_Handler /* Debug Monitor Handler */<br />
#pragma weak PendSV_Handler     = Default_Handler /* PendSV Handler */<br />
#pragma weak SysTick_Handler    = Default_Handler /* SysTick Handler */</p>
<p>void Default_Handler(void) {<br />
 while (1);<br />
}</p>
<p>extern int main(void);<br />
void Reset_Handler(void) {<br />
 main();</p>
<p> // If main() ever exit, this should hold MCU from running wild<br />
 while(1);<br />
}</p>
<p>LPC1766.ld</p>
<p>/*<br />
 * Define the supported output formats &#8211; elf32-littlearm is the<br />
 *  default<br />
 */  <br />
OUTPUT_FORMAT(&#8220;elf32-littlearm&#8221;, &#8220;elf32-bigarm&#8221;, &#8220;elf32-littlearm&#8221;)   </p>
<p>/* Define the target architecture */  <br />
OUTPUT_ARCH(arm)   </p>
<p>/* Define the system entry point */  <br />
ENTRY(Reset_Handler)   </p>
<p>/* Define the memory layout for the board */<br />
SECTIONS  <br />
{  <br />
 /* LPC1766 flash is at 0&#215;0000_0000 */<br />
 .flash 0&#215;00000000 :<br />
 {<br />
 /* Tell the linker to collect any .cs3.interrupt_vector section to this<br />
 address */  <br />
 KEEP(*(.cs3.interrupt_vector));</p>
<p> /* Collect the executable parts here */<br />
 *(.text .text.*);<br />
 /* Collect the const variables here */<br />
 *(.rodata .rodata.*);</p>
<p> __sidata = .;<br />
 }</p>
<p> /*<br />
 * This section refers to variables with init value. The init values are<br />
 * stored on ROM, but addressed on RAM (otherwise the variables cannot be<br />
 * manipulated).<br />
 *<br />
 * This section will be loaded at 0&#215;10000000, the start of LPC1766 RAM<br />
 */<br />
 .data_at_ram 0&#215;10000000: AT (LOADADDR(.flash) + SIZEOF(.flash))<br />
 {<br />
 __sdata = .;<br />
 *(.data .data.*);<br />
 __edata = .;<br />
 }</p>
<p> .ram :<br />
 {<br />
 __sbss = .;<br />
 /* Collect the global / static variables, without init value here */<br />
 *(.bss .bss.*);<br />
 __ebss = .;<br />
 }</p>
<p> /* Set __cs3_stack to point the end of memory<br />
 * This one depends on your MCU. I&#8217;m using LPC1766 with 256kB flash and<br />
 * 32kB RAM<br />
 */  </p>
<p> __cs3_stack = 0&#215;10000000 + (32 * 1024);<br />
}</p>
<p>To compile the files:</p>
<p># arm-none-eabi-gcc -O2 -ffunction-sections -fdata-sections -g3 -Wall -c -mcpu=cortex-m3 -mthumb -omain.o main.c<br />
# arm-none-eabi-gcc -O2 -ffunction-sections -fdata-sections -g3 -Wall -c -mcpu=cortex-m3 -mthumb -ostartup_LPC1766.o startup_LPC1766.c<br />
# arm-none-eabi-gcc -Wl,&#8211;gc-sections -Wl,-Map,LPC1766_Systick_Interrupt.map -Wl,-T,/home/kunil/workspace/LPC1766_Systick_Interrupt/LPC1766.ld -oLPC1766_Systick_Interrupt.elf  main.o startup_LPC1766.o</p>
<p>To generate the HEX file:</p>
<p># arm-none-eabi-objcopy -j .flash -j .data_at_ram -O ihex LPC1766_Systick_Interrupt.elf LPC1766_Systick_Interrupt.hex</p>
<p>The eclipse project file can be downloaded from <a href="http://www.4shared.com/file/124081219/9e7be612/LPC1766_Systick_Interrupt-090810_1646.html">here</a>.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/458/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=458&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2009/08/10/cortex-m3-systick-peripheral/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>
	</item>
		<item>
		<title>Cortex-M3 Blinky in Assembly</title>
		<link>http://embeddedfreak.wordpress.com/2009/08/09/cortex-m3-blinky-in-assembly/</link>
		<comments>http://embeddedfreak.wordpress.com/2009/08/09/cortex-m3-blinky-in-assembly/#comments</comments>
		<pubDate>Sat, 08 Aug 2009 16:02:13 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[Cortex-M]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=451</guid>
		<description><![CDATA[As tradition for new comers, I created my own &#8216;hello world&#8217; using Cortex-M3&#8217;s assembly using Codesourcery&#8217;s GCC assembler. The code below has been tested on LPC1766 &#8211; Keil&#8217;s MCB1700 board. It will turn on/off the LEDs on the board.
Here&#8217;s the code:
/* Simple startup file for Cortex-M3 */
 /* Thumb-2 instructions are only supported in unified [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=451&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>As tradition for new comers, I created my own &#8216;hello world&#8217; using Cortex-M3&#8217;s assembly using Codesourcery&#8217;s GCC assembler. The code below has been tested on LPC1766 &#8211; Keil&#8217;s MCB1700 board. It will turn on/off the LEDs on the board.</p>
<p>Here&#8217;s the code:<span id="more-451"></span></p>
<p>/* Simple startup file for Cortex-M3 */</p>
<p> /* Thumb-2 instructions are only supported in unified syntax mode */<br />
 .syntax unified</p>
<p>/* Vector table definition */<br />
 .section &#8220;.cs3.interrupt_vector&#8221;<br />
 .long  __cs3_stack                 /* Top of Stack                 */<br />
 .long  Reset_Handler               /* Reset Handler                */<br />
 .long  NMI_Handler                 /* NMI Handler                  */<br />
 .long  HardFault_Handler           /* Hard Fault Handler           */<br />
 .long  MemManage_Handler           /* MPU Fault Handler            */<br />
 .long  BusFault_Handler            /* Bus Fault Handler            */<br />
 .long  UsageFault_Handler          /* Usage Fault Handler          */<br />
 .long  0                           /* Reserved                     */<br />
 .long  0                           /* Reserved                     */<br />
 .long  0                           /* Reserved                     */<br />
 .long  0                           /* Reserved                     */<br />
 .long  SVC_Handler                 /* SVCall Handler               */<br />
 .long  DebugMon_Handler            /* Debug Monitor Handler        */<br />
 .long  0                           /* Reserved                     */<br />
 .long  PendSV_Handler              /* PendSV Handler               */<br />
 .long  SysTick_Handler             /* SysTick Handler              */</p>
<p>/* Vendor specific interrupts<br />
 *  &#8212;- Not implemented<br />
 */</p>
<p> /* Program section */<br />
 .section &#8220;.text&#8221;</p>
<p> /* Declare as thumb function. Otherwise it will not be linked<br />
 * correctly<br />
 */<br />
 .thumb_func<br />
 /* Export the symbol so linker can see this */<br />
 .global Reset_Handler<br />
Reset_Handler:<br />
 /* Jump to main(), a thumb function */<br />
 LDR     R0, =main<br />
 BX      R0<br />
 /* If main() ever exit, this should hold MCU from running wild */<br />
 B       .</p>
<p>/* This is how the lazy guy doing it: by aliasing all the<br />
 * interrupts into single address<br />
 */<br />
.thumb_func<br />
NMI_Handler:<br />
.thumb_func<br />
HardFault_Handler:<br />
.thumb_func<br />
MemManage_Handler:<br />
.thumb_func<br />
BusFault_Handler:<br />
.thumb_func<br />
UsageFault_Handler:<br />
.thumb_func<br />
SVC_Handler:<br />
.thumb_func<br />
DebugMon_Handler:<br />
.thumb_func<br />
PendSV_Handler:<br />
.thumb_func<br />
SysTick_Handler:<br />
 B    . /* while(1); */</p>
<p>The startup file contains the initial vector address. Save it into &#8217;startup_lpc17xx.S&#8217; file, then compile it using:</p>
<p># arm-none-eabi-as -mcpu=cortex-m3 -ostartup_lpc17xx.o startup_lpc17xx.S</p>
<p>Then, here comes the main file to blink the LEDs on MCB1700:</p>
<p>/*<br />
 * Blinky program for MCB1768 &#8211; ARM Cortex-M3<br />
 *<br />
 * The LEDs are at these pins:<br />
 * &#8211; P1.28, P1.29, P1.31<br />
 * &#8211; P2.2, P2.3, P2.4, P2.5, P2.6<br />
 *<br />
 */<br />
 /* Thumb-2 instructions are only supported in unified syntax mode */<br />
 .syntax unified<br />
 /* Put these codes into .text section */<br />
 .section &#8220;.text&#8221;<br />
 /* Declare main() as global..Otherwise the linker won&#8217;t find it */<br />
 .global main<br />
 /* Declare as thumb function..Otherwise it may not linked correctly */<br />
 .thumb_func<br />
main:<br />
 /* Set the pins direction as output */<br />
 LDR     R0, =set_gpio_dir<br />
 BLX     R0<br />
loop:<br />
 LDR     R0, =clear_leds<br />
 BLX     R0<br />
 LDR     R0, =delay<br />
 BLX     R0<br />
 LDR     R0, =set_leds<br />
 BLX     R0<br />
 LDR     R0, =delay<br />
 BLX     R0<br />
 B       loop</p>
<p> .thumb_func<br />
set_gpio_dir:<br />
 /* P1.28, P1.29, and P1.31 direction mode is located at<br />
 * FP1DIR (0&#215;2009_C020).<br />
 *<br />
 * P1.28 -> Bit[28] -> Output dir : 1<br />
 * P1.29 -> Bit[29] -> Output dir : 1<br />
 * P1.31 -> Bit[31] -> Output dir : 1<br />
 */<br />
 MOVW    R3, #0xC020<br />
 MOVT    R3, #0&#215;2009<br />
 /* b1011_0000_0000_0000_0000_0000_0000_0000 */<br />
 MOVW    R2, #0&#215;0000<br />
 MOVT    R2, #0xB000<br />
 STR     R2, [R3, #0]</p>
<p> /* P2.2, P2.3, P2.4, P2.5, and P2.6 function mode is located at<br />
 * FP2DIR (0&#215;2009_C040).<br />
 *<br />
 * P2.2  -> Bit[2] -> Output dir : 1<br />
 * P2.3  -> Bit[3] -> Output dir : 1<br />
 * P2.4  -> Bit[4] -> Output dir : 1<br />
 * P2.5  -> Bit[5] -> Output dir : 1<br />
 * P2.6  -> Bit[6] -> Output dir : 1<br />
 */<br />
 MOVW    R3, #0xC040<br />
 MOVT    R3, #0&#215;2009<br />
 /* b0000_0000_0000_0000_0000_0000_0111_1100 */<br />
 MOVW    R2, #0&#215;007C<br />
 MOVT    R2, #0&#215;0000<br />
 STR     R2, [R3, #0]</p>
<p> BX      LR</p>
<p> .thumb_func<br />
set_leds:<br />
 /* P1.28, P1.29, and P1.31 set is located at<br />
 * FP1SET (0&#215;2009_C038).<br />
 *<br />
 * P1.28 -> Bit[28]<br />
 * P1.29 -> Bit[29]<br />
 * P1.31 -> Bit[31]<br />
 */<br />
 MOVW    R3, #0xC038<br />
 MOVT    R3, #0&#215;2009<br />
 /* b1011_0000_0000_0000_0000_0000_0000_0000 */<br />
 MOVW    R2, #0&#215;0000<br />
 MOVT    R2, #0xB000<br />
 STR     R2, [R3, #0]</p>
<p> /* P2.2, P2.3, P2.4, P2.5, and P2.6 set is located at<br />
 * FP2SET (0&#215;2009_C058).<br />
 *<br />
 * P2.2  -> Bit[2]<br />
 * P2.3  -> Bit[3]<br />
 * P2.4  -> Bit[4]<br />
 * P2.5  -> Bit[5]<br />
 * P2.6  -> Bit[6]<br />
 */<br />
 MOVW    R3, #0xC058<br />
 MOVT    R3, #0&#215;2009<br />
 /* b0000_0000_0000_0000_0000_0000_0111_1100 */<br />
 MOVW    R2, #0&#215;007C<br />
 MOVT    R2, #0&#215;0000<br />
 STR     R2, [R3, #0]</p>
<p> BX      LR</p>
<p> .thumb_func<br />
clear_leds:<br />
 /* P1.28, P1.29, and P1.31 clear is located at<br />
 * FP1CLR (0&#215;2009_C03C).<br />
 *<br />
 * P1.28 -> Bit[28]<br />
 * P1.29 -> Bit[29]<br />
 * P1.31 -> Bit[31]<br />
 */<br />
 MOVW    R3, #0xC03C<br />
 MOVT    R3, #0&#215;2009<br />
 /* b1011_0000_0000_0000_0000_0000_0000_0000 */<br />
 MOVW    R2, #0&#215;0000<br />
 MOVT    R2, #0xB000<br />
 STR     R2, [R3, #0]</p>
<p> /* P2.2, P2.3, P2.4, P2.5, and P2.6 clear is located at<br />
 * FP2CLR (0&#215;2009_C05C).<br />
 *<br />
 * P2.2  -> Bit[2]<br />
 * P2.3  -> Bit[3]<br />
 * P2.4  -> Bit[4]<br />
 * P2.5  -> Bit[5]<br />
 * P2.6  -> Bit[6]<br />
 */<br />
 MOVW    R3, #0xC05C<br />
 MOVT    R3, #0&#215;2009<br />
 /* b0000_0000_0000_0000_0000_0000_0111_1100 */<br />
 MOVW    R2, #0&#215;007C<br />
 MOVT    R2, #0&#215;0000<br />
 STR     R2, [R3, #0]</p>
<p> BX      LR</p>
<p> .thumb_func<br />
delay:<br />
 MOVW    R3, #0xFFFF<br />
 MOVT    R3, #0&#215;0000<br />
__delay_loop:<br />
 CBZ     R3, __delay_exit<br />
 SUB     R3, R3, #1<br />
 B       __delay_loop<br />
__delay_exit:<br />
 BX      LR</p>
<p>You may find it not very optimized, but who cares =) It&#8217;s only a &#8216;hello world&#8217; after all. Anyway, save the file into &#8216;blinky.S&#8217;, and compile it with:</p>
<p># arm-none-eabi-as -mcpu=cortex-m3 -oblinky.o blinky.S</p>
<p>Now, to combine both object files (startup_lpc17xx.o and blinky.o), we will call the linker. The linker needs to know the device&#8217;s memory layout, which is put inside the linker file. Here&#8217;s the linker file:</p>
<p>/*<br />
 * Define the supported output formats &#8211; elf32-littlearm is the<br />
 *  default<br />
 */  <br />
OUTPUT_FORMAT(&#8220;elf32-littlearm&#8221;, &#8220;elf32-bigarm&#8221;, &#8220;elf32-littlearm&#8221;)   </p>
<p>/* Define the target architecture */  <br />
OUTPUT_ARCH(arm)   </p>
<p>/* Define the system entry point */  <br />
ENTRY(Reset_Handler)   </p>
<p>/* Define the memory layout for the board */<br />
SECTIONS  <br />
{  <br />
 /* LPC1766 flash is at 0&#215;0000_0000 */<br />
 .flash 0&#215;00000000 :<br />
 {<br />
 /* Tell the linker to collect any .cs3.interrupt_vector section to this<br />
 address */  <br />
 KEEP(*(.cs3.interrupt_vector));</p>
<p> /* Collect the executable parts here */<br />
 *(.text .text.*);<br />
 /* Collect the const variables here */<br />
 *(.rodata .rodata.*);</p>
<p> __sidata = .;<br />
 }</p>
<p> /*<br />
 * This section refers to variables with init value. The init values are<br />
 * stored on ROM, but addressed on RAM (otherwise the variables cannot be<br />
 * manipulated).<br />
 *<br />
 * This section will be loaded at 0&#215;10000000, the start of LPC1766 RAM<br />
 */<br />
 .data_at_ram 0&#215;10000000: AT (LOADADDR(.flash) + SIZEOF(.flash))<br />
 {<br />
 __sdata = .;<br />
 *(.data .data.*);<br />
 __edata = .;<br />
 }</p>
<p> .ram :<br />
 {<br />
 __sbss = .;<br />
 /* Collect the global / static variables, without init value here */<br />
 *(.bss .bss.*);<br />
 __ebss = .;<br />
 }</p>
<p> /* Set __cs3_stack to point the end of memory<br />
 * This one depends on your MCU. I&#8217;m using LPC1766 with 256kB flash and<br />
 * 32kB RAM<br />
 */  </p>
<p> __cs3_stack = 0&#215;10000000 + (32 * 1024);<br />
}</p>
<p>Save it into &#8216;linked.ld&#8217;. Now, you can combine all the object files into real ARM executable using this command:</p>
<p># arm-none-eabi-gcc -Wl,&#8211;gc-sections -Wl,-Map,LPC1766_Blinky_Assembly.map -Wl,-T/home/kunil/workspace/LPC1766_Blinky_Assembly/linker.ld -oLPC1766_Blinky_Assembly.elf startup_lpc17xx.o blinky.o</p>
<p>This will produce the ARM executable file (LPC1766_Blinky_Assembly.elf) and also the map of the symbols (global / static variables location, function locations, etc) in &#8216;LPC1766_Blinky_Assembly.map&#8217;. Unfortunately, the executable itself is pretty useless, unless you have OS that can load the executable part from it. To load the executable part from the executable file and put it into Intel HEX file, use:</p>
<p># arm-none-eabi-objcopy -j .flash -j .data_at_ram -O ihex LPC1766_Blinky_Assembly.elf LPC1766_Blinky_Assembly.hex</p>
<p>The thing that you&#8217;ll need to copy to your flash is: executable file (on .flash section) and init value for global/static variables (on .data_at_ram section).</p>
<p>The hex file contains the executable part that can be run by your MCU directly. Just download it using your favourite programmer (such as FlashMagic http://www.flashmagictool.com).</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/451/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=451&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2009/08/09/cortex-m3-blinky-in-assembly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>
	</item>
		<item>
		<title>Cortex-M3 Interrupt Vector Table</title>
		<link>http://embeddedfreak.wordpress.com/2009/08/07/cortex-m3-interrupt-vector-table/</link>
		<comments>http://embeddedfreak.wordpress.com/2009/08/07/cortex-m3-interrupt-vector-table/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 16:00:41 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[Cortex-M]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=443</guid>
		<description><![CDATA[In Cortex-M3, there are 255 interrupt vectors, and it&#8217;s relocateable. During the initial boot, the interrupt vector table are located at 0&#215;00, but, then, if you want to, you can move it to somewhere else.
The first ten interrupts are fixed by ARM (means you will always find them eventhough you&#8217;re using NXP, Atmel, STMicro, Luminary [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=443&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>In Cortex-M3, there are 255 interrupt vectors, and it&#8217;s relocateable. During the initial boot, the interrupt vector table are located at 0&#215;00, but, then, if you want to, you can move it to somewhere else.</p>
<p>The first ten interrupts are fixed by ARM (means you will always find them eventhough you&#8217;re using NXP, Atmel, STMicro, Luminary Micro, etc). The rest is up to the silicon vendors implementation.</p>
<p>The interrupt handler is a bit different from any other MCUs. You just need to write the interrupt handler address, without any additional code (such as: B &lt;interrupt_handler_addr&gt; to jump to specific address). Also, the first thing on your interrupt table is the stack address. The Cortex-M3 interrupt controller (NVIC) will need stack address before it can jump to the handler. Hence, it&#8217;s put as the first thing on the interrupt table.</p>
<p>Here&#8217;s how to implement it<span id="more-443"></span></p>
<p>/* Simple startup file for Cortex-M3 */</p>
<p> /* Thumb-2 instructions are only supported in unified syntax mode */<br />
 .syntax unified</p>
<p>/* Vector table definition */<br />
 .section &#8220;.cs3.interrupt_vector&#8221;<br />
 .long  __cs3_stack                 /* Top of Stack                 */<br />
 .long  Reset_Handler               /* Reset Handler                */<br />
 .long  NMI_Handler                 /* NMI Handler                  */<br />
 .long  HardFault_Handler           /* Hard Fault Handler           */<br />
 .long  MemManage_Handler           /* MPU Fault Handler            */<br />
 .long  BusFault_Handler            /* Bus Fault Handler            */<br />
 .long  UsageFault_Handler          /* Usage Fault Handler          */<br />
 .long  0                           /* Reserved                     */<br />
 .long  0                           /* Reserved                     */<br />
 .long  0                           /* Reserved                     */<br />
 .long  0                           /* Reserved                     */<br />
 .long  SVC_Handler                 /* SVCall Handler               */<br />
 .long  DebugMon_Handler            /* Debug Monitor Handler        */<br />
 .long  0                           /* Reserved                     */<br />
 .long  PendSV_Handler              /* PendSV Handler               */<br />
 .long  SysTick_Handler             /* SysTick Handler              */</p>
<p>/* Vendor specific interrupts<br />
 *  &#8212;- Not implemented<br />
 */</p>
<p> /* Program section */<br />
 .section &#8220;.text&#8221;</p>
<p> /* Declare as thumb function. Otherwise it will not be linked<br />
 * correctly<br />
 */<br />
 .thumb_func<br />
 /* Export the symbol so linker can see this */<br />
 .global Reset_Handler<br />
Reset_Handler:<br />
 /* Jump to main(), a thumb function */<br />
 LDR     R0, =main<br />
 BX      R0<br />
 /* If main() ever exit, this should hold MCU from running wild */<br />
 B       .</p>
<p>/* This is how the lazy guy doing it: by aliasing all the<br />
 * interrupts into single address<br />
 */<br />
.thumb_func<br />
NMI_Handler:<br />
.thumb_func<br />
HardFault_Handler:<br />
.thumb_func<br />
MemManage_Handler:<br />
.thumb_func<br />
BusFault_Handler:<br />
.thumb_func<br />
UsageFault_Handler:<br />
.thumb_func<br />
SVC_Handler:<br />
.thumb_func<br />
DebugMon_Handler:<br />
.thumb_func<br />
PendSV_Handler:<br />
.thumb_func<br />
SysTick_Handler:<br />
 B    . /* while(1); */</p>
<p>You don&#8217;t need to write your interrupt handler in assembly. Here&#8217;s simple way to write your Reset_Handler() in C:</p>
<p>/*<br />
 * Don&#8217;t forget to take out the Reset_Handler() on the startup file first<br />
 */<br />
extern int main(void);<br />
void Reset_Handler(void) {<br />
 main();</p>
<p> // If main() ever exit, this should hold MCU from running wild<br />
 while(1);<br />
}</p>
<p>But some people has found a way to code everything in C (instead of assembly and C) using callback function mechanism</p>
<p>/*<br />
 * startup_LPC1766.c &#8211; Startup file for LPC1766 Cortex-M3<br />
 *<br />
 * @author: kunilkuda_at_gmail_dot_com<br />
 */<br />
extern void __cs3_stack; ///< This is global variable from linker script</p>
<p>/* We gave &#8216;weak&#8217; attribute, so these functions can be aliased into a single<br />
 * function<br />
 */<br />
void __attribute__((weak)) Reset_Handler(void);     /* Reset Handler */<br />
void __attribute__((weak)) NMI_Handler(void);       /* NMI Handler */<br />
void __attribute__((weak)) HardFault_Handler(void); /* Hard Fault Handler */<br />
void __attribute__((weak)) MemManage_Handler(void); /* MPU Fault Handler */<br />
void __attribute__((weak)) BusFault_Handler(void);  /* Bus Fault Handler */<br />
void __attribute__((weak)) UsageFault_Handler(void);/* Usage Fault Handler */<br />
void __attribute__((weak)) SVC_Handler(void);       /* SVCall Handler */<br />
void __attribute__((weak)) DebugMon_Handler(void);  /* Debug Monitor Handler */<br />
void __attribute__((weak)) PendSV_Handler(void);    /* PendSV Handler */<br />
void __attribute__((weak)) SysTick_Handler(void);   /* SysTick Handler */</p>
<p>/*<br />
 * Ask GCC to put this array into .cs3.interrupt_vector section<br />
 */<br />
__attribute__ ((section(&#8220;.cs3.interrupt_vector&#8221;)))<br />
void (* const g_pfnVectors[])(void) = {<br />
 &#038;__cs3_stack,       // The initial stack pointer<br />
 Reset_Handler,      // The reset handler<br />
 NMI_Handler,        // The NMI handler<br />
 HardFault_Handler,  // The hard fault handler<br />
 MemManage_Handler,  // The MPU fault handler<br />
 BusFault_Handler,   // The bus fault handler<br />
 UsageFault_Handler, // The usage fault handler<br />
 0,                  // Reserved<br />
 0,                  // Reserved<br />
 0,                  // Reserved<br />
 0,                  // Reserved<br />
 SVC_Handler,        // SVCall handler<br />
 DebugMon_Handler,   // Debug monitor handler<br />
 0,                  // Reserved<br />
 PendSV_Handler,     // The PendSV handler<br />
 SysTick_Handler,    // The SysTick handler<br />
};</p>
<p>/* Here&#8217;s how to alias multiple functions into single function<br />
 */<br />
void Default_Handler(void);<br />
#pragma weak NMI_Handler        = Default_Handler /* NMI handler */<br />
#pragma weak HardFault_Handler  = Default_Handler /* Hard Fault handler */<br />
#pragma weak MemManage_Handler  = Default_Handler /* MPU Fault Handler */<br />
#pragma weak BusFault_Handler   = Default_Handler /* Bus Fault Handler */<br />
#pragma weak UsageFault_Handler = Default_Handler /* Usage Fault Handler */<br />
#pragma weak SVC_Handler        = Default_Handler /* SVCall Handler */<br />
#pragma weak DebugMon_Handler   = Default_Handler /* Debug Monitor Handler */<br />
#pragma weak PendSV_Handler     = Default_Handler /* PendSV Handler */<br />
#pragma weak SysTick_Handler    = Default_Handler /* SysTick Handler */</p>
<p>void Default_Handler(void) {<br />
 while (1);<br />
}</p>
<p>extern int main(void);<br />
void Reset_Handler(void) {<br />
 main();</p>
<p> // If main() ever exit, this should hold MCU from running wild<br />
 while(1);<br />
}</p>
<p>For any method that you use, the most important part is defining your linker script to layout the code into the correct place.</p>
<p>/*<br />
 * Define the supported output formats &#8211; elf32-littlearm is the<br />
 *  default<br />
 */  <br />
OUTPUT_FORMAT(&#8220;elf32-littlearm&#8221;, &#8220;elf32-bigarm&#8221;, &#8220;elf32-littlearm&#8221;)   </p>
<p>/* Define the target architecture */  <br />
OUTPUT_ARCH(arm)   </p>
<p>/* Define the system entry point */  <br />
ENTRY(Reset_Handler)   </p>
<p>/* Define the memory layout for the board */<br />
SECTIONS  <br />
{  <br />
 /* LPC1766 flash is at 0&#215;0000_0000 */<br />
 .flash 0&#215;00000000 :<br />
 {<br />
 /* Tell the linker to collect any .cs3.interrupt_vector section to this<br />
 address */  <br />
 KEEP(*(.cs3.interrupt_vector));</p>
<p> /* Collect the executable parts here */<br />
 *(.text .text.*);<br />
 /* Collect the const variables here */<br />
 *(.rodata .rodata.*);</p>
<p> __sidata = .;<br />
 }</p>
<p> /*<br />
 * This section refers to variables with init value. The init values are<br />
 * stored on ROM, but addressed on RAM (otherwise the variables cannot be<br />
 * manipulated).<br />
 *<br />
 * This section will be loaded at 0&#215;10000000, the start of LPC1766 RAM<br />
 */<br />
 .data_at_ram 0&#215;10000000: AT (LOADADDR(.flash) + SIZEOF(.flash))<br />
 {<br />
 __sdata = .;<br />
 *(.data .data.*);<br />
 __edata = .;<br />
 }</p>
<p> .ram :<br />
 {<br />
 __sbss = .;<br />
 /* Collect the global / static variables, without init value here */<br />
 *(.bss .bss.*);<br />
 __ebss = .;<br />
 }</p>
<p> /* Set __cs3_stack to point the end of memory<br />
 * This one depends on your MCU. I&#8217;m using LPC1766 with 256kB flash and<br />
 * 32kB RAM<br />
 */  </p>
<p> __cs3_stack = 0&#215;10000000 + (32 * 1024);<br />
}</p>
<p>This is just an example that does nothing. For more complete demo, you can download the original source code from CMSIS website (http://www.onarm.com/download/download389.asp).</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/443/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/443/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/443/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/443/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/443/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/443/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/443/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/443/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/443/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/443/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=443&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2009/08/07/cortex-m3-interrupt-vector-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>
	</item>
		<item>
		<title>Simple rectifier for MCU PSU</title>
		<link>http://embeddedfreak.wordpress.com/2009/07/10/simple-rectifier-for-mcu-psu/</link>
		<comments>http://embeddedfreak.wordpress.com/2009/07/10/simple-rectifier-for-mcu-psu/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 15:55:08 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[embedded-tips]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=436</guid>
		<description><![CDATA[A friend of mine told me about simple rectifier configuration that&#8217;s usually deployed in white goods (iron, washing machine, vacuum cleaner, etc). You can view the explanation here and here. It&#8217;s very interesting to know that you actually don&#8217;t need a big transformer or even switching PSU to supply your MCU from AC source.
Here&#8217;s my [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=436&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A friend of mine told me about simple rectifier configuration that&#8217;s usually deployed in white goods (iron, washing machine, vacuum cleaner, etc). You can view the explanation <a title="AN954, Transformerless Power Supplies: Resistive and Capacitive" href="http://ww1.microchip.com/downloads/en/AppNotes/00954A.pdf">here</a> and <a title="Transformerless Power Supply, TB008" href="http://ww1.microchip.com/downloads/en/AppNotes/91008b.pdf">here</a>. It&#8217;s very interesting to know that you actually don&#8217;t need a big transformer or even switching PSU to supply your MCU from AC source.</p>
<p>Here&#8217;s my schematic for my own board:</p>
<p><span id="more-436"></span><img class="aligncenter size-large wp-image-437" title="SimpleRectifier" src="http://embeddedfreak.files.wordpress.com/2009/07/simplerectifier.png?w=641&#038;h=188" alt="SimpleRectifier" width="641" height="188" />It will generate 5.1VDC at 21.12mA. Or at least that&#8217;s what I hope =)</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/436/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/436/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/436/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/436/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/436/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/436/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/436/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/436/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/436/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/436/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=436&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2009/07/10/simple-rectifier-for-mcu-psu/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>

		<media:content url="http://embeddedfreak.files.wordpress.com/2009/07/simplerectifier.png?w=1024" medium="image">
			<media:title type="html">SimpleRectifier</media:title>
		</media:content>
	</item>
		<item>
		<title>Linear PSU Efficiency Quiz</title>
		<link>http://embeddedfreak.wordpress.com/2009/05/09/linear-psu-efficiency-quiz/</link>
		<comments>http://embeddedfreak.wordpress.com/2009/05/09/linear-psu-efficiency-quiz/#comments</comments>
		<pubDate>Sat, 09 May 2009 10:15:25 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[embedded-tips]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=431</guid>
		<description><![CDATA[Moving out to Singapore, I&#8217;ve become nearly disabled. No solder, no board, no multimeter, etc. Actually, my employer provides me with the tools, but I&#8217;m kinda hesitated to use it for my own personal use. I guess I have to buy it for my own later.
Anyway, since I&#8217;ve got nothing to do this Sunday, I&#8217;ve [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=431&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Moving out to Singapore, I&#8217;ve become nearly disabled. No solder, no board, no multimeter, etc. Actually, my employer provides me with the tools, but I&#8217;m kinda hesitated to use it for my own personal use. I guess I have to buy it for my own later.</p>
<p>Anyway, since I&#8217;ve got nothing to do this Sunday, I&#8217;ve come up with quizzes, just to test my electronic insanity. What is the efficiency of these ugly linear PSUs ?</p>
<div id="attachment_432" class="wp-caption aligncenter" style="width: 310px"><img class="size-medium wp-image-432" title="Simple Zener Rectifier" src="http://embeddedfreak.files.wordpress.com/2009/05/linear.png?w=300&#038;h=217" alt="Simple Zener Rectifier" width="300" height="217" /><p class="wp-caption-text">Simple Zener Rectifier</p></div>
<p>I&#8217;d say its efficiency is 17.59%..Here&#8217;s another one:</p>
<div id="attachment_433" class="wp-caption aligncenter" style="width: 310px"><img class="size-medium wp-image-433" title="Current Feedback Linear Rectifier with PVR100AD" src="http://embeddedfreak.files.wordpress.com/2009/05/pvr100ad_circuit.png?w=300&#038;h=181" alt="Current Feedback Linear Rectifier" width="300" height="181" /><p class="wp-caption-text">Current Feedback Linear Rectifier</p></div>
<p>My insanity said that it&#8217;s efficiency is 17.64%.</p>
<p>Is there anyone who has different answer than me ? <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/431/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/431/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/431/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/431/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/431/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/431/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/431/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/431/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/431/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/431/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=431&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2009/05/09/linear-psu-efficiency-quiz/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>

		<media:content url="http://embeddedfreak.files.wordpress.com/2009/05/linear.png?w=300" medium="image">
			<media:title type="html">Simple Zener Rectifier</media:title>
		</media:content>

		<media:content url="http://embeddedfreak.files.wordpress.com/2009/05/pvr100ad_circuit.png?w=300" medium="image">
			<media:title type="html">Current Feedback Linear Rectifier with PVR100AD</media:title>
		</media:content>
	</item>
		<item>
		<title>Programming Timeout under FreeRTOS</title>
		<link>http://embeddedfreak.wordpress.com/2009/02/19/programming-timeout-under-freertos/</link>
		<comments>http://embeddedfreak.wordpress.com/2009/02/19/programming-timeout-under-freertos/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 03:23:01 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[programming-tips]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=422</guid>
		<description><![CDATA[It seems like the FreeRTOS has some support to program a timeout, but unpublished anywhere. I&#8217;ve checked the FreeRTOS source code, regarding the usage, and it seems like it&#8217;s quite safe to use it under user&#8217;s application.
Here&#8217;s what I&#8217;ve found:

xTimeOutType x_timeout; //< Var to hold the current time
portTickType openTimeout; //< Var to hold the timeout
// [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=422&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>It seems like the FreeRTOS has some support to program a timeout, but unpublished anywhere. I&#8217;ve checked the FreeRTOS source code, regarding the usage, and it seems like it&#8217;s quite safe to use it under user&#8217;s application.</p>
<p>Here&#8217;s what I&#8217;ve found:</p>
<p><span id="more-422"></span></p>
<p>xTimeOutType x_timeout; //< Var to hold the current time<br />
portTickType openTimeout; //< Var to hold the timeout</p>
<p>// Set the initial time, where timeout will be started<br />
vTaskSetTimeOutState(&#038;x_timeout);<br />
// Set the timeout value<br />
openTimeout = 512; //< In RTOS tick</p>
<p>// To check the timeout<br />
if(xTaskCheckForTimeOut(&#038;x_timeout, &#038;openTimeout) == pdTRUE)<br />
{<br />
  // It&#8217;s timeout<br />
} else {<br />
  // It&#8217;s not timeout yet.<br />
}</p>
<p>You can also combined the technique with do-while loop (<a href="http://embeddedfreak.wordpress.com/2007/09/24/programming-timeout-retry/">see my previous post</a>).</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/422/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/422/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/422/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/422/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/422/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/422/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/422/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/422/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/422/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/422/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=422&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2009/02/19/programming-timeout-under-freertos/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>
	</item>
		<item>
		<title>FreeRTOS for ATMega128</title>
		<link>http://embeddedfreak.wordpress.com/2009/02/10/freertos-for-atmega128/</link>
		<comments>http://embeddedfreak.wordpress.com/2009/02/10/freertos-for-atmega128/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 04:01:50 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[avr]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=416</guid>
		<description><![CDATA[FreeRTOS is free open source RTOS that supports several architectures. You can get its source code here. This post is my port for my ATMega128 board. It may run on your board (or may not).My version is not much different from the original ATMega323 port. The things that I&#8217;ve changed are:

Makefile: User can include it, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=416&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>FreeRTOS is free open source RTOS that supports several architectures. You can get its source code <a href="http://www.freertos.org/">here</a>. This post is my port for my ATMega128 board. It may run on your board (or may not).<span id="more-416"></span>My version is not much different from the original ATMega323 port. The things that I&#8217;ve changed are:</p>
<ul>
<li>Makefile: User can include it, rather than built their own Makefile.</li>
<li>GCC flags: I added new CFLAGS and LDFLAGS to remove dead codes/unused functions. It&#8217;ll make your binaries smaller (and still works, I hope)</li>
<li>Using RTC osc as RTOS&#8217; tick interrupt: I need precise resolution for timing. Therefore, I moved the interrupt from OCR1A to asynchronous OCR0.</li>
</ul>
<p>Here&#8217;s some explanation about the Makefile. I&#8217;ve added main Makefile in the FreeRTOS source code, so user that&#8217;s needed to build new code just need simple Makefile, like this:</p>
<p>SRC = leds.c<br />
include $(FREERTOS_DIR)/Makefile</p>
<p>You can compile, program, generate COFF, SREC, IHEX as usual (because I&#8217;m using the standard AVR Makefile from <a href="http://www.sax.de/~joerg/mfile/">mfile</a>) as long as you define the FREERTOS_DIR, like this:</p>
<p>$ make FREERTOS_DIR=../FreeRTOS_ATMega128 program</p>
<p>Take a look at the main Makefile to know which Makefile variables that you can override.</p>
<p>The RTOS&#8217; tick is changed to OCR0 interrupt. My board has RTC xtal (32.768kHz) attached on it. So, I&#8217;m expecting better timing precision than the original port. Anyway, this also means that you cannot use my FreeRTOS port without RTC xtal in your board.</p>
<p>The settings of the OCR0 enables the RTOS running with max 512Hz tick rate. Don&#8217;t try to use it more than 512Hz. It won&#8217;t tick (it will hang on OCR0 interrupt handler).</p>
<p>Anyway, for the curious minds, here&#8217;s <a href="http://www.4shared.com/file/86718790/d09330d1/FreeRTOS_ATMega128-090210_1129tar.html">my FreeRTOS port</a> and <a href="http://www.4shared.com/file/86718791/a7940047/FreeRTOS_Test-090209_1850tar.html">my sample hello world</a>.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/416/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/416/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/416/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/416/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/416/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/416/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/416/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/416/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/416/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/416/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=416&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2009/02/10/freertos-for-atmega128/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>
	</item>
	</channel>
</rss>