Sign in Join
Wiki copytemporary

AVR: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: AVR

OldNewLine
1+> **Warning:** The FPC-AVR port is experimental and might be broken from time to time. If this is the case, please fill a [bug report](http://bugs.freepascal.org)
2+
3+**FPC-AVR** is the [[FPC|Free Pascal Compiler]] port for the AVR microcontroller. It uses the GCC AVR tool chain and will be compatible with GCC regarding calling conventions etc.
4+
5+For instructions on how to program AVR's with Free Pascal, see the article [[AVR Programming]].
6+
7+## Implementation details
8+
9+FPC considers AVR to be a 16 bit CPU. So the type TRegister actually describes a register pair with the second register implicitly named.
10+
11+## Building cross-compiler
12+
13+There are some requirements, you should know about:
14+
15+- You should use the latest Free Pascal sources as there is the most recent support for more controllers.
16+- Binutils for avr target
17+ - Microchip (previously Atmel) provides bintuils binaries for Linux, macOS (Darwin) and Windows.\
18+ [Download](https://www.microchip.com/mplab/avr-support/avr-and-arm-toolchains-c-compilers) using the appropriate link for your operating system\]
19+ - On a Linux host, you may either install binutils for the avr target from your distribution or use also the binaries provided by Microchip (previously Atmel).
20+ - You may also use the binutils distributed with the Arduino IDE; on Windows they are installed to eg C:\Program Files (x86)\Arduino\hardware\tools\avr\avr\bin
21+ - Keep in mind that the Free Pascal Compiler will search for eg avr-embedded-as. So you must either rename the binary files, create symlinks (on Linux only) or adjust command line switch -XP\<prefix\> for FPC.
22+ - You may need to set FPC's command line switch -FD\<directory with binutils\>
23+- Latest *stable* Free Pascal Compiler.
24+
25+Then you can build your cross compiler. Please choose SUBARCH according to your Arduino board / AVR microcontroller.
26+
27+``` bash
28+make buildbase installbase CPU_TARGET=avr OS_TARGET=embedded SUBARCH=avr5 CROSSINSTALL=1 INSTALL_PREFIX=~/fpc NOGDB=1 PP=/usr/bin/fpc CROSSBINDIR=/usr/avr/bin
29+```
30+
31+You may also set the binutils prefix for this (equivalent to the **-XP** switch)
32+
33+``` bash
34+make buildbase installbase CPU_TARGET=avr OS_TARGET=embedded SUBARCH=avr5 CROSSINSTALL=1 INSTALL_PREFIX=~/fpc NOGDB=1 PP=/usr/bin/fpc CROSSBINDIR=/usr/avr/bin BINUTILSPREFIX=avr-
35+```
36+
37+The instructions above is good if only one subarchitecture is required. To install several subarchitectures, a unique unit install prefix needs to be specified for each subarchitecture using **INSTALL_UNITPREFIX**:
38+
39+``` bash
40+make buildbase installbase CPU_TARGET=avr OS_TARGET=embedded CROSSINSTALL=1 NOGDB=1 FPC=/usr/bin/fpc CROSSBINDIR=/usr/avr/bin BINUTILSPREFIX=avr- INSTALL_PREFIX=~/fpc INSTALL_UNITPREFIX=~/fpc/lib/fpc/3.2.2/units/avr-embedded/avr5 SUBARCH=avr5
41+```
42+
43+Remember to change both **SUBARCH** and **INSTALL_UNITPREFIX** when building a different subarchitecture.
44+
45+Together with this change, the fpc.cfg file paths needs to be updated to find units in this new structure. Below is an example of updating the unit specific search paths in fpc.cfg:
46+
47+```text
48+# searchpath for units and other system dependent things
49+#IFDEF EMBEDDED
50+-Fu~/fpc/lib/fpc/$fpcversion/units/$fpctarget/$fpcsubarch
51+-Fu~/fpc/lib/fpc/$fpcversion/units/$fpctarget/$fpcsubarch/*
52+-Fu~/fpc/lib/fpc/$fpcversion/units/$fpctarget/$fpcsubarch/rtl
53+#ELSE
54+-Fu~/fpc/lib/fpc/$fpcversion/units/$fpctarget/*
55+-Fu~/fpc/lib/fpc/$fpcversion/units/$fpctarget/rtl
56+#ENDIF
57+```
58+
59+Adapt paths as required.
60+
61+## Subarchitecture
62+
63+The operating system target *embedded* requires that you specify a subarchitectures when building a cross compiler. The subarchitectures are compatible with the ones used by GCC.
64+
65+## Resources
66+
67+- [Atmel Studio 7](https://www.microchip.com/mplab/avr-support/atmel-studio-7)
68+- [Downloads Archive for AVR tools](https://www.microchip.com/mplab/avr-support/avr-and-sam-downloads-archive)
69+- [General information about Atmel AVR in Wikipedia](http://en.wikipedia.org/wiki/Atmel_AVR)
70+- [Calling conventions](http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_reg_usage)
71+- [list of subarchitectures](http://www.nongnu.org/avr-libc/user-manual/using_tools.html)
72+- [Discussion on lazarusforum.de (German)](http://www.lazarusforum.de/viewtopic.php?f=9&t=5444&start=30)
73+- [[AVR Programming]]
74+- [[Arduino]]
75+- [[AVR Embedded Tutorial|AVR Embedded Tutorials]] - Tutorials with code examples
76+
77+---
78+*From the Free Pascal wiki, page "AVR", as it stood on 2021-12-26 after a change by Ccrause, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history