Sophie

Sophie

distrib > Fedora > 13 > i386 > media > os > by-pkgid > 73443d16ffe49ffcb4131bf0d8d1b044 > files > 166

avr-libc-docs-1.6.7-2.fc13.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
    <title>avr-libc: Porting From IAR to AVR GCC</title>
    <link href="dox.css" rel="stylesheet" type="text/css">
  </head>
<body>
<center>
<table width="80%">
  <tr>
    <td align="left"><a href="http://www.nongnu.org/avr-libc/">AVR Libc Home Page</a></td>
    <td align="center" colspan=4><img src="avrs.png" alt="AVRs" align="middle" border="0"></td>
    <td align="right"><a href="https://savannah.nongnu.org/projects/avr-libc/">AVR Libc Development Pages</a></td>
  </tr>
  <tr>
    <td align="center" width="13%"><a href="index.html">Main Page</a></td>
    <td align="center" width="13%"><a href="pages.html">User Manual</a></td>
    <td align="center" width="13%"><a href="modules.html">Library Reference</a></td>
    <td align="center" width="13%"><a href="FAQ.html">FAQ</a></td>
    <td align="center" width="13%"><a href="globals.html">Alphabetical Index</a></td>
    <td align="center" width="13%"><a href="group__demos.html">Example Projects</a></td>
  </tr>
</table>
</center>
<hr width="80%">
<!-- Generated by Doxygen 1.6.1 -->
<div class="contents">


<h1><a class="anchor" id="porting">Porting From IAR to AVR GCC </a></h1><h2><a class="anchor" id="iar_porting_intro">
Introduction</a></h2>
<p>C language was designed to be a portable language. There two main types of porting activities: porting an application to a different platform (OS and/or processor), and porting to a different compiler. Porting to a different compiler can be exacerbated when the application is an embedded system. For example, the C language Standard, strangely, does not specify a standard for declaring and defining Interrupt Service Routines (ISRs). Different compilers have different ways of defining registers, some of which use non-standard language constructs.</p>
<p>This chapter describes some methods and pointers on porting an AVR application built with the IAR compiler to the GNU toolchain (AVR GCC). Note that this may not be an exhaustive list.</p>
<h2><a class="anchor" id="iar_porting_register">
Registers</a></h2>
<p>IO header files contain identifiers for all the register names and bit names for a particular processor. IAR has individual header files for each processor and they must be included when registers are being used in the code. For example:</p>
<div class="fragment"><pre class="fragment"><span class="preprocessor"> #include &lt;iom169.h&gt;</span> 
</pre></div><dl class="note"><dt><b>Note:</b></dt><dd>IAR does not always use the same register names or bit names that are used in the AVR datasheet.</dd></dl>
<p>AVR GCC also has individual IO header files for each processor. However, the actual processor type is specified as a command line flag to the compiler. (Using the <code>-mmcu=<em>processor</em></code> flag.) This is usually done in the Makefile. This allows you to specify only a single header file for any processor type:</p>
<div class="fragment"><pre class="fragment"><span class="preprocessor"> #include &lt;<a class="code" href="io_8h.html">avr/io.h</a>&gt;</span> 
</pre></div><dl class="note"><dt><b>Note:</b></dt><dd>The forward slash in the &lt;<a class="el" href="io_8h.html">avr/io.h</a>&gt; file name that is used to separate subdirectories can be used on Windows distributions of the toolchain and is the recommended method of including this file.</dd></dl>
<p>The compiler knows the processor type and through the single header file above, it can pull in and include the correct individual IO header file. This has the advantage that you only have to specify one generic header file, and you can easily port your application to another processor type without having to change every file to include the new IO header file.</p>
<p>The AVR toolchain tries to adhere to the exact names of the registers and names of the bits found in the AVR datasheet. There may be some descrepencies between the register names found in the IAR IO header files and the AVR GCC IO header files.</p>
<h2><a class="anchor" id="iar_porting_isr">
Interrupt Service Routines (ISRs)</a></h2>
<p>As mentioned above, the C language Standard, strangely, does not specify a standard way of declaring and defining an ISR. Hence, every compiler seems to have their own special way of doing so.</p>
<p>IAR declares an ISR like so:</p>
<div class="fragment"><pre class="fragment"><span class="preprocessor">#pragma vector=TIMER0_OVF_vect</span>
<span class="preprocessor"></span>__interrupt <span class="keywordtype">void</span> MotorPWMBottom()
{
    <span class="comment">// code</span>
}
</pre></div><p>In AVR GCC, you declare an ISR like so: </p>
<div class="fragment"><pre class="fragment"><a class="code" href="group__avr__interrupts.html#gad28590624d422cdf30d626e0a506255f">ISR</a>(PCINT1_vect)
{ 
    <span class="comment">//code</span>
}
</pre></div><p>AVR GCC uses the <code>ISR</code> macro to define an ISR. This macro requries the header file:</p>
<div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;<a class="code" href="interrupt_8h.html">avr/interrupt.h</a>&gt;</span>
</pre></div><p>The names of the various interrupt vectors are found in the individual processor IO header files that you must include with <code>&lt;<a class="el" href="io_8h.html">avr/io.h</a>&gt;</code>.</p>
<dl class="note"><dt><b>Note:</b></dt><dd>The names of the interrupt vectors in AVR GCC has been changed to match the names of the vectors in IAR. This significantly helps in porting applications from IAR to AVR GCC.</dd></dl>
<h2><a class="anchor" id="iar_porting_intrinsic">
Intrinsic Routines</a></h2>
<p>IAR has a number of intrinsic routine such as</p>
<p><code>__enable_interrupts()</code> <code>__disable_interrupts()</code> <code>__watchdog_reset()</code> </p>
<p>These intrinsic functions compile to specific AVR opcodes (SEI, CLI, WDR).</p>
<p>There are equivalent macros that are used in AVR GCC, however they are not located in a single include file.</p>
<p>AVR GCC has <code><a class="el" href="group__avr__interrupts.html#gaad5ebd34cb344c26ac87594f79b06b73">sei()</a></code> for <code>__enable_interrupts()</code>, and <code><a class="el" href="group__avr__interrupts.html#ga68c330e94fe121eba993e5a5973c3162">cli()</a></code> for <code>__disable_interrupts()</code>. Both of these macros are located in <code>&lt;avr/interrupts.h&gt;</code>.</p>
<p>AVR GCC has the macro <code><a class="el" href="group__avr__watchdog.html#ga9e52c54d10b6a6a7ce04aaaa4abea51f">wdt_reset()</a></code> in place of <code>__watchdog_reset()</code>. However, there is a whole Watchdog Timer API available in AVR GCC that can be found in <code>&lt;<a class="el" href="wdt_8h.html">avr/wdt.h</a>&gt;</code>.</p>
<h2><a class="anchor" id="iar_porting_flash">
Flash Variables</a></h2>
<p>The C language was not designed for Harvard architecture processors with separate memory spaces. This means that there are various non-standard ways to define a variable whose data resides in the Program Memory (Flash).</p>
<p>IAR uses a non-standard keyword to declare a variable in Program Memory:</p>
<div class="fragment"><pre class="fragment"> __flash <span class="keywordtype">int</span> mydata[] = .... 
</pre></div><p>AVR GCC uses Variable Attributes to achieve the same effect:</p>
<div class="fragment"><pre class="fragment"> <span class="keywordtype">int</span> mydata[] __attribute__((progmem)) 
</pre></div><dl class="note"><dt><b>Note:</b></dt><dd>See the GCC User Manual for more information about Variable Attributes.</dd></dl>
<p>avr-libc provides a convenience macro for the Variable Attribute:</p>
<div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;<a class="code" href="pgmspace_8h.html">avr/pgmspace.h</a>&gt;</span>
.
.
.
int mydata[] PROGMEM = ....
</pre></div><dl class="note"><dt><b>Note:</b></dt><dd>The PROGMEM macro expands to the Variable Attribute of <code>progmem</code>. This macro requires that you include <code>&lt;<a class="el" href="pgmspace_8h.html">avr/pgmspace.h</a>&gt;</code>. This is the canonical method for defining a variable in Program Space.</dd></dl>
<p>To read back flash data, use the <code>pgm_read_*</code>() macros defined in <code>&lt;<a class="el" href="pgmspace_8h.html">avr/pgmspace.h</a>&gt;</code>. All Program Memory handling macros are defined there.</p>
<p>There is also a way to create a method to define variables in Program Memory that is common between the two compilers (IAR and AVR GCC). Create a header file that has these definitions:</p>
<div class="fragment"><pre class="fragment"><span class="preprocessor">#if defined(__ICCAVR__) // IAR C Compiler</span>
<span class="preprocessor"></span><span class="preprocessor">#define FLASH_DECLARE(x) __flash x</span>
<span class="preprocessor"></span><span class="preprocessor">#endif</span>
<span class="preprocessor"></span><span class="preprocessor">#if defined(__GNUC__) // GNU Compiler</span>
<span class="preprocessor"></span><span class="preprocessor">#define FLASH_DECLARE(x) x __attribute__((__progmem__))</span>
<span class="preprocessor">#endif </span>
</pre></div><p>This code snippet checks for the IAR compiler or for the GCC compiler and defines a macro <code>FLASH_DECLARE(x)</code> that will declare a variable in Program Memory using the appropriate method based on the compiler that is being used. Then you would used it like so:</p>
<div class="fragment"><pre class="fragment"> FLASH_DECLARE(<span class="keywordtype">int</span> mydata[] = ...); 
</pre></div><h2><a class="anchor" id="iar_porting_non_returning_main">
Non-Returning main()</a></h2>
<p>To declare main() to be a non-returning function in IAR, it is done like this: </p>
<div class="fragment"><pre class="fragment">__C_task <span class="keywordtype">void</span> main(<span class="keywordtype">void</span>)
{
    <span class="comment">// code</span>
}
</pre></div><p>To do the equivalent in AVR GCC, do this:</p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> main(<span class="keywordtype">void</span>) __attribute__((noreturn));
        
<span class="keywordtype">void</span> main(<span class="keywordtype">void</span>)
{
    <span class="comment">//...</span>
}
</pre></div><dl class="note"><dt><b>Note:</b></dt><dd>See the GCC User Manual for more information on Function Attributes.</dd></dl>
<p>In AVR GCC, a prototype for main() is required so you can declare the function attribute to specify that the main() function is of type "noreturn". Then, define main() as normal. Note that the return type for main() is now <code>void</code>.</p>
<h2><a class="anchor" id="iar_porting_locking">
Locking Registers</a></h2>
<p>The IAR compiler allows a user to lock general registers from r15 and down by using compiler options and this keyword syntax:</p>
<div class="fragment"><pre class="fragment">__regvar __no_init <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> filteredTimeSinceCommutation @14;
</pre></div><p>This line locks r14 for use only when explicitly referenced in your code thorugh the var name "filteredTimeSinceCommutation". This means that the compiler cannot dispose of it at its own will.</p>
<p>To do this in AVR GCC, do this:</p>
<div class="fragment"><pre class="fragment"><span class="keyword">register</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> counter <span class="keyword">asm</span>(<span class="stringliteral">&quot;r3&quot;</span>);
</pre></div><p>Typically, it should be possible to use r2 through r15 that way.</p>
<dl class="note"><dt><b>Note:</b></dt><dd>Do not reserve r0 or r1 as these are used internally by the compiler for a temporary register and for a zero value.</dd>
<dd>
Locking registers is not recommended in AVR GCC as it removes this register from the control of the compiler, which may make code generation worse. Use at your own risk. </dd></dl>
</div>

<hr width="80%">
<p><center>Automatically generated by Doxygen 1.6.1 on 30 Nov 2009.</center></p>

</body>
</html>