neingeist
/
arduinisten
Archived
1
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

134 lines
12 KiB
HTML

<!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.5.6 -->
<div class="contents">
<h1><a class="anchor" name="porting">Porting From IAR to AVR GCC </a></h1><h2><a class="anchor" name="iar_porting_intro">
Introduction</a></h2>
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>
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.<h2><a class="anchor" name="iar_porting_register">
Registers</a></h2>
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><p>
<dl class="note" compact><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>
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><p>
<dl class="note" compact><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>
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>
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.<h2><a class="anchor" name="iar_porting_isr">
Interrupt Service Routines (ISRs)</a></h2>
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>
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: <div class="fragment"><pre class="fragment"><a class="code" href="group__avr__interrupts.html#gd28590624d422cdf30d626e0a506255f">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" compact><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" name="iar_porting_intrinsic">
Intrinsic Routines</a></h2>
IAR has a number of intrinsic routine such as<p>
<code>__enable_interrupts()</code> <code>__disable_interrupts()</code> <code>__watchdog_reset()</code> <p>
These intrinsic functions compile to specific AVR opcodes (SEI, CLI, WDR).<p>
There are equivalent macros that are used in AVR GCC, however they are not located in a single include file.<p>
AVR GCC has <code><a class="el" href="group__avr__interrupts.html#gad5ebd34cb344c26ac87594f79b06b73">sei()</a></code> for <code>__enable_interrupts()</code>, and <code><a class="el" href="group__avr__interrupts.html#g68c330e94fe121eba993e5a5973c3162">cli()</a></code> for <code>__disable_interrupts()</code>. Both of these macros are located in <code>&lt;avr/interrupts.h&gt;</code>.<p>
AVR GCC has the macro <code><a class="el" href="group__avr__watchdog.html#g9e52c54d10b6a6a7ce04aaaa4abea51f">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>.<h2><a class="anchor" name="iar_porting_flash">
Flash Variables</a></h2>
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>
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><p>
<dl class="note" compact><dt><b>Note:</b></dt><dd>See the GCC User Manual for more information about Variable Attributes.</dd></dl>
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><p>
<dl class="note" compact><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>
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>
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" name="iar_porting_non_returning_main">
Non-Returning main()</a></h2>
To declare main() to be a non-returning function in IAR, it is done like this: <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><p>
<dl class="note" compact><dt><b>Note:</b></dt><dd>See the GCC User Manual for more information on Function Attributes.</dd></dl>
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>.<h2><a class="anchor" name="iar_porting_locking">
Locking Registers</a></h2>
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>
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">"r3"</span>);
</pre></div><p>
Typically, it should be possible to use r2 through r15 that way.<p>
<dl class="note" compact><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.<p>
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.5.6 on 4 Dec 2008.</center></p>
</body>
</html>