Difference between revisions of "AVR"

From Free Pascal wiki
Jump to navigationJump to search
m (Move to template Translate)
(Add example of how to place subarch units in separate folders.)
 
(7 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{Translate}}
+
{{AVR(pg)}}
  
 
{{Warning|The FPC-AVR port is experimental and might be broken from time to time. If this is the case, please fill a [http://bugs.freepascal.org bug report]}}  
 
{{Warning|The FPC-AVR port is experimental and might be broken from time to time. If this is the case, please fill a [http://bugs.freepascal.org bug report]}}  
  
'''FPC-AVR''' is the [[FPC|Free Pascal Compiler]] port for the Atmel-AVR processor.  
+
'''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.
It uses the GCC AVR tool chain and will be compatible with GCC regarding calling conventions etc.
 
  
For instructions on how to program AVR's with FreePascal see the article [[AVR Programming]].
+
For instructions on how to program AVR's with Free Pascal, see the article [[AVR Programming]].
  
 
== Implementation details ==
 
== Implementation details ==
 +
 
FPC considers AVR to be a 16 bit CPU. So the type TRegister actually describes a register pair with the second register implicitly named.
 
FPC considers AVR to be a 16 bit CPU. So the type TRegister actually describes a register pair with the second register implicitly named.
  
 
== Building cross-compiler ==
 
== Building cross-compiler ==
There are some requirements, you should know about
+
 
 +
There are some requirements, you should know about:
 
* You should use the latest Free Pascal sources as there is the most recent support for more controllers.
 
* You should use the latest Free Pascal sources as there is the most recent support for more controllers.
 
* Binutils for avr target
 
* Binutils for avr target
** Atmel provides bintuils binaries for Linux, Mac&nbsp;OS&nbsp;X and Windows.<br />[http://distribute.atmel.no/tools/opensource/Atmel-AVR-GNU-Toolchain/3.5.0/ atmel.no (registration required) follow the appropriate link for your operating system]
+
** Microchip (previously Atmel) provides bintuils binaries for Linux, macOS (Darwin) and Windows.<br />[https://www.microchip.com/mplab/avr-support/avr-and-arm-toolchains-c-compilers Download] using the appropriate link for your operating system]
** On Linux host, you may either install bintuils for avr target from your distribution or use also the binaries provided by Atmel.
+
** 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).
** You may also use the bintuils distributed with the Arduino IDE; on Windows they are installed to e.g. C:\Program Files (x86)\Arduino\hardware\tools\avr\avr\bin
+
** 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
** Keep in mind, the Free Pacal Compiler will search for e.g. 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.
+
** 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.
 
** You may need to set FPC's command line switch -FD<directory with binutils>
 
** You may need to set FPC's command line switch -FD<directory with binutils>
* Latest ''stable'' Free Pascal Compiler
+
* Latest ''stable'' Free Pascal Compiler.
  
Then you can build your cross compiler. Please choose SUBARCH according to your Arduino board / AVR microprocessor.
+
Then you can build your cross compiler. Please choose SUBARCH according to your Arduino board / AVR microcontroller.
  
<code lang="shell">
+
<syntaxhighlight lang="bash">
 
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
 
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
</code>
+
</syntaxhighlight>
  
You may also set the bintuils prefix for this (equivialent to -XP switch)
+
You may also set the binutils prefix for this (equivalent to the '''-XP''' switch)
  
<code lang="shell">
+
<syntaxhighlight lang="bash">
 
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-
 
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-
</code>
+
</syntaxhighlight>
 +
 
 +
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''':
 +
<syntaxhighlight lang="bash">
 +
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
 +
</syntaxhighlight>
 +
Remember to change both '''SUBARCH''' and '''INSTALL_UNITPREFIX''' when building a different subarchitecture.
 +
 
 +
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:
 +
<syntaxhighlight lang='text'>
 +
# searchpath for units and other system dependent things
 +
#IFDEF EMBEDDED
 +
-Fu~/fpc/lib/fpc/$fpcversion/units/$fpctarget/$fpcsubarch
 +
-Fu~/fpc/lib/fpc/$fpcversion/units/$fpctarget/$fpcsubarch/*
 +
-Fu~/fpc/lib/fpc/$fpcversion/units/$fpctarget/$fpcsubarch/rtl
 +
#ELSE
 +
-Fu~/fpc/lib/fpc/$fpcversion/units/$fpctarget/*
 +
-Fu~/fpc/lib/fpc/$fpcversion/units/$fpctarget/rtl
 +
#ENDIF
 +
</syntaxhighlight>
 +
Adapt paths as required.
  
 
== Subarchitecture ==
 
== Subarchitecture ==
The operating system target ''embedded'' requires you to specify a aubarchicture when building a cross compiler. The subarchitectures are compatible to the ones used by gcc.
+
 
 +
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.
  
 
== Resources ==
 
== Resources ==
* [http://en.wikipedia.org/wiki/Atmel_AVR General information about Atmel AVR in wikipedia]  
+
 
* [http://www.atmel.com/products/microcontrollers/avr/default.aspx Atmel site]
+
* [https://www.microchip.com/mplab/avr-support/atmel-studio-7 Atmel Studio 7]
 +
* [https://www.microchip.com/mplab/avr-support/avr-and-sam-downloads-archive Downloads Archive for AVR tools]
 +
* [http://en.wikipedia.org/wiki/Atmel_AVR General information about Atmel AVR in Wikipedia]  
 
* [http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_reg_usage Calling conventions]
 
* [http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_reg_usage Calling conventions]
 
* [http://www.nongnu.org/avr-libc/user-manual/using_tools.html list of subarchitectures]  
 
* [http://www.nongnu.org/avr-libc/user-manual/using_tools.html list of subarchitectures]  
Line 45: Line 69:
 
* [[AVR Programming]]
 
* [[AVR Programming]]
 
* [[Arduino]]
 
* [[Arduino]]
* [[AVR Embedded Tutorial|AVR Embedded Tutorial]], Tutorial with code examples (English)
+
* [[AVR Embedded Tutorial|AVR Embedded Tutorials]] - Tutorials with code examples
* [[AVR Embedded Tutorial/de|AVR Embedded Tutorial]], Tutorial with code examples (German)
 

Latest revision as of 20:36, 26 December 2021

English (en)

Warning-icon.png

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

FPC-AVR is the 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.

For instructions on how to program AVR's with Free Pascal, see the article AVR Programming.

Implementation details

FPC considers AVR to be a 16 bit CPU. So the type TRegister actually describes a register pair with the second register implicitly named.

Building cross-compiler

There are some requirements, you should know about:

  • You should use the latest Free Pascal sources as there is the most recent support for more controllers.
  • Binutils for avr target
    • Microchip (previously Atmel) provides bintuils binaries for Linux, macOS (Darwin) and Windows.
      Download using the appropriate link for your operating system]
    • 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).
    • 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
    • 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.
    • You may need to set FPC's command line switch -FD<directory with binutils>
  • Latest stable Free Pascal Compiler.

Then you can build your cross compiler. Please choose SUBARCH according to your Arduino board / AVR microcontroller.

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

You may also set the binutils prefix for this (equivalent to the -XP switch)

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-

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:

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

Remember to change both SUBARCH and INSTALL_UNITPREFIX when building a different subarchitecture.

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:

# searchpath for units and other system dependent things
#IFDEF EMBEDDED
-Fu~/fpc/lib/fpc/$fpcversion/units/$fpctarget/$fpcsubarch
-Fu~/fpc/lib/fpc/$fpcversion/units/$fpctarget/$fpcsubarch/*
-Fu~/fpc/lib/fpc/$fpcversion/units/$fpctarget/$fpcsubarch/rtl
#ELSE
-Fu~/fpc/lib/fpc/$fpcversion/units/$fpctarget/*
-Fu~/fpc/lib/fpc/$fpcversion/units/$fpctarget/rtl
#ENDIF

Adapt paths as required.

Subarchitecture

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.

Resources