Recent

Author Topic: Cross compiling installation for AVR  (Read 7008 times)

process_1

  • Guest
Cross compiling installation for AVR
« on: July 23, 2020, 11:35:06 am »
https://wiki.freepascal.org/AVR

This guide is quite a bit obsolete now as the Atmel link which provides bintuils binaries is broken. I believe we all know that Atmel is sold to Microchip several years ago...

- What would be exact steps to install cross-compiler for AVRs ?
- Is it possible to precisely debug the code, regarding to shosen AVR model and crystal frequency in order to get precise cpu cycles count and variable values?
« Last Edit: July 23, 2020, 12:17:15 pm by process_1 »

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: Cross compiling installation for AVR
« Reply #1 on: July 23, 2020, 01:11:23 pm »
I replaced the Atmel links with Microchip links.

d.ioannidis

  • Full Member
  • ***
  • Posts: 221
    • Nephelae
Re: Cross compiling installation for AVR
« Reply #2 on: July 23, 2020, 01:46:26 pm »
Hi,

< snip >
- Is it possible to precisely debug the code, regarding to shosen AVR model and crystal frequency in order to get precise cpu cycles count and variable values?

  Yes it is.

  I'm able to debug physical AVR MCU's with Lazarus . I can set breakpoints, step in, step out,  see variables.

  The only limitation currently, is that this is possible only with the use of Atmel Studio backend tools. No Linux or MacOS support ( for the tools ) until someone implement a gdb proxy for the mplab debugger which support Windows, Linux and MacOS. One workaround is to use a VM to host those tools and run Lazarus from MacOS and/or Linux which works. 

  As for debuggers, unfortunately the low price non HV programmer / debugger SNAP ( ~ 20€ ) is not supported yet in the Atmel Studio backend. AFAIK, it's  scheduled for addition at Q3 / Q4 this year . But you can use an xplained board ( look for those with an AVR mcu ), which have a debugger mcu on board specific for that board mcu.

  Personal, I'm using a "custom" mEDBG debugger derived from the Xplained Yourself project. BTW, if you're only using mcu's with UPDI debug interface you could use a pro micro with some modifications as per the mcudude's microUPDI.

PS: Maybe I should create a tutorial on how to setup such enviroment.
 
regards,

ccrause

  • Hero Member
  • *****
  • Posts: 845
Re: Cross compiling installation for AVR
« Reply #3 on: July 23, 2020, 02:11:09 pm »
- Is it possible to precisely debug the code, regarding to shosen AVR model and crystal frequency in order to get precise cpu cycles count and variable values?
You can use avr-gdb and debug in Lazarus - for this you either need debugging hardware as per Dimitrios's post (for debugwire there are a couple of projects to easily hook up via a usb-serial dongle, search for debugwire on Github), or connect to a simulator/emulator (simavr, fp-avrsim, or qemu).  This will allow you to step through code, stop and inspect variables, memory, registers etc.

The easy way to get cycle counts is to use either Atmel Studio or MPLAB X.  I've once tested an FPC compiled project in MPLAB X and it worked OK, but I stopped using it because it was frustratingly slow on my computer. The simulators listed above should also have cycle counters, but I haven't explored it myself.

process_1

  • Guest
Re: Cross compiling installation for AVR
« Reply #4 on: July 23, 2020, 02:50:59 pm »
First of all, thank you all.

Hopefully, I have account on Microchip, thus had no issue to download required file avr8-gnu-toolchain-3.6.2.1759-linux.any.x86_64.tar.gz for toolchain.

In ~/.bashrc I have added this line:

export PATH=/usr/avr8-gnu-toolchain-linux_x86_64/bin:$PATH

Then restarted the system and executed this script I have made previously:

FPC_CrossLink_AVR
Code: [Select]
#!/bin/sh
sudo cp avr8-gnu-toolchain-3.6.2.1759-linux.any.x86_64.tar.gz /usr

cd /usr
sudo tar zvxf avr8-gnu-toolchain-3.6.2.1759-linux.any.x86_64.tar.gz
sudo rm  avr8-gnu-toolchain-3.6.2.1759-linux.any.x86_64.tar.gz
sudo apt install avrdude

cd /usr/share/fpcsrc/3.2.0
sudo make clean all OS_TARGET=embedded CPU_TARGET=avr
sudo make crossinstall OS_TARGET=embedded CPU_TARGET=avr INSTALL_PREFIX=/usr SUBARCH=avr5
sudo ln -sf /usr/lib/fpc/3.2.0/ppcrossavr /usr/bin/ppcrossavr

All passed well, but at end this error is shown:

system.pp(325) Error: Assembler avr-embedded-as not found, switching to external assembling

Indeed there is no avr-embedded-as inside /usr/avr8-gnu-toolchain-linux_x86_64/bin, just avr-as which is required file or not?

If it is, then symbolic link will do the rest, I believe...
« Last Edit: July 23, 2020, 02:56:19 pm by process_1 »

d.ioannidis

  • Full Member
  • ***
  • Posts: 221
    • Nephelae
Re: Cross compiling installation for AVR
« Reply #5 on: July 23, 2020, 02:57:44 pm »
Hi,


< snip >

sudo make crossinstall OS_TARGET=embedded CPU_TARGET=avr INSTALL_PREFIX=/usr SUBARCH=avr5

< snip >

Indeed there is no avr-embedded-as inside /usr/avr8-gnu-toolchain-linux_x86_64

fpc is searching for the fpc binutils starting with avr-embedded-* which AFAIU is only for windows .

As you're using the official binutils binaries from Microhip, you should change the binutilsprefix from the avr-embedded- to avr-. Meaning you should add the BINUTILSPREFIX=avr- to the command line :

Code: Bash  [Select][+][-]
  1. sudo make crossinstall OS_TARGET=embedded CPU_TARGET=avr INSTALL_PREFIX=/usr SUBARCH=avr5 BINUTILSPREFIX=avr-

regards,
« Last Edit: July 23, 2020, 03:20:56 pm by Dimitrios Chr. Ioannidis »

d.ioannidis

  • Full Member
  • ***
  • Posts: 221
    • Nephelae
Re: Cross compiling installation for AVR
« Reply #6 on: July 23, 2020, 03:04:53 pm »
Hi,

  FYI, an avr-embedded free pascal cross linux archive exists at ftp://ftp.hu.freepascal.org/pub/fpc/dist/3.2.0/avr-embedded/ .

regards,

process_1

  • Guest
Re: Cross compiling installation for AVR
« Reply #7 on: July 23, 2020, 05:09:30 pm »
With suggestion regardin BINUTILSPREFIX it makes cross-compiler fine (attached script).

Attached Blinky sample found somewhere at this forum is also compiled fine from commandline by:

Code: [Select]
ppcrossavr -Tembedded -Pavr -WpATMEGA328P -XPavr- Blinky.lpr

However when tried to compile from IDE, it returns error regarding avr-embedded-as:
Code: [Select]
Project checks, Hints: 1
Note: passing compiler option -Cp twice with different values
Compile Project, OS: embedded, CPU: avr, Target: Blinky: Exit code 1, Errors: 1
Blinky.lpr(7,1) Error: Assembler avr-embedded-as not found, switching to external assembling

This is strange...

Also "-Cp twice with different values". I have specifically set AVR5 group in project settings, but it is already set by defailt. Obvious conflict in parameters, or a bug.

Another problem I can see is that ATmega2560 (which I'm using regularly) is in AVR6 group, while this compiler compiles only code for models from AVR5 group. Would this compiler can be configured to compile code for any AVR MCUs group, or it is currently limited only to AVR5 group?
« Last Edit: July 23, 2020, 05:46:09 pm by process_1 »

ccrause

  • Hero Member
  • *****
  • Posts: 845
Re: Cross compiling installation for AVR
« Reply #8 on: July 23, 2020, 05:52:25 pm »
<snip>
Code: [Select]
ppcrossavr -Tembedded -Pavr -WpATMEGA328P -XPavr- Blinky.lpr

Howere, when tried to compile from IDE, it returns error regarding avr-embedded-as:
Code: [Select]
Project checks, Hints: 1
Note: passing compiler option -Cp twice with different values
Compile Project, OS: embedded, CPU: avr, Target: Blinky: Exit code 1, Errors: 1
Blinky.lpr(7,1) Error: Assembler avr-embedded-as not found, switching to external assembling
In Lazarus you need to add the -XPavr- option to the project custom options (Project | Project Options | Custom Options).

Quote
Another problem I can see is that ATmega2560 (which I'm using regularly) is in avr6 group, while this co,piler compiles only models from avr5 group. Is this compiler can be configured to compile code for any AVR MCUs group, or it is currently limited only to avr6 group?
The problem is that the compiler generates different units depending on the subarchitecture specified.  Unfortunately these units are all stored in a folder with the generic name avr-embedded by default.  This means that you can recompile the RTL and specify a different subarch, which will then overwrite the previous units.  To work around this, one has to build the RTL for a single subarch, then copy/move/install this into a differently named folder with a unique name, say avr-embedded-avr5 (some people prefer a subfolder with the subarch name e.g. avr-embedded/avr6).  The next step is to update the fpc.cfg file for the AVR target and set the unit paths for AVR according to this structure.  Somewhere in fpc.cfg look for a -Fu entry that points to the RTL and packages units and change them more or less as follows:
Code: Text  [Select][+][-]
  1. # searchpath for units and other system dependent things
  2. #ifdef cpuavr
  3.   -Fu~/fpc/$fpcversion/rtl/units/$fpctarget-$fpcsubarch
  4.   -Fu~/fpc/$fpcversion/packages/*/units/$fpctarget-$fpcsubarch/*
  5. #else
  6.   -Fu~/fpc/$fpcversion/rtl/units/$fpctarget
  7.   -Fu~/fpc/$fpcversion/packages/*/units/$fpctarget
  8. #endif

Obviously you have to adapt the folder template above to reflect your setup.

If you are using make install then you can specify the destination folder using the following option:
Code: Pascal  [Select][+][-]
  1. INSTALL_UNITDIR=/usr/local/lib/fpc/3.3.1/units/avr-embedded-avr5
Again adapt to your specific path requirements (I don't install FPC so I'm not familiar with the typical paths used).

d.ioannidis

  • Full Member
  • ***
  • Posts: 221
    • Nephelae
Re: Cross compiling installation for AVR
« Reply #9 on: July 23, 2020, 06:15:14 pm »
Hi,


< snip >

Also "-Cp twice with different values". I have specifically set AVR5 group in project settings, but it is already set by defailt. Obvious conflict in parameters, or a bug.

< snip >


It's not a bug.

In Lazarus change the Target processor ( Project | Project Options | Compiler Options | Config and Target | Target processor ) to (default) and use the -Cp<mcu> option in ( Project | Project Options | Compiler Options | Custom Options ) i.e. -CpAVR6 or -CpAVR25.

Don't forget to enable the macro ( -Sm ) for the mcu frequency .

For example this is what my custom options looks like from a avr project :

Code: Pascal  [Select][+][-]
  1. -a
  2. -al
  3. -Xe
  4. -Xm
  5. -Sm
  6. -CpAVR25
  7. -WpATTINY2313A
  8. -XPavr-
  9. -FD"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin"
  10. -godwarfsets
  11. -dF_CPU:=8000000

You can see what those options do if you click at the "All options.." button in the project custom options window.

EDIT: Because the "All Options" window has a lot of options, you can check the "Show only modified" checkbox to filter out those that are not enabled.

regards,
« Last Edit: July 23, 2020, 06:23:55 pm by Dimitrios Chr. Ioannidis »

process_1

  • Guest
Re: Cross compiling installation for AVR
« Reply #10 on: July 23, 2020, 07:36:45 pm »
The problem is that the compiler generates different units depending on the subarchitecture specified.  Unfortunately these units are all stored in a folder with the generic name avr-embedded by default.
...

For now, it is fine for me to recompile all whenever is necessary to change of MCU from another group, as recompilation is much faster now on new hardware.

Anyway, the whole process definitely require to be suggested to be fixed "officially" for some next realease of FPC.


process_1

  • Guest
Re: Cross compiling installation for AVR
« Reply #11 on: July 23, 2020, 07:51:25 pm »
Dimitrios,

Thank you once again for all info, it worked now as should be.

One thing only require to be clarified:


It's not a bug.

In Lazarus change the Target processor ( Project | Project Options | Compiler Options | Config and Target | Target processor ) to (default) and use the -Cp<mcu> option in ( Project | Project Options | Compiler Options | Custom Options ) i.e. -CpAVR6 or -CpAVR25.


If Target processor is chaged, thus -Cp in Custom Options have to be changed accordingly, I suppose.

With MCUs, additional combobox with filtered MCU list is as well desired to be shown instead to manually change -CP. As well additional edit field for frequency in -dF_CPU is welcome.

Similar as used in MPLABX and many other compilers in order to simplify and clear all. Perhaps also posibility to use some compiler directives in the main file for some settings, as F_CPU and specific MCU.

BTW, is it F_CPU allowed to be used in the code, for delays, USART or similar code modifications in order to calculate some constants without need to change it whenever change frequency or MCU?

« Last Edit: July 23, 2020, 08:27:00 pm by process_1 »

ccrause

  • Hero Member
  • *****
  • Posts: 845
Re: Cross compiling installation for AVR
« Reply #12 on: July 23, 2020, 08:13:38 pm »
<snip>
Anyway, the whole process definitely require to be suggested to be fixed "officially" for some next realease of FPC.
This issue has been around for quite a while (e.g. this discussion) but no fix has been included in trunk yet. It also affects other targets such as ARM and xtensa.

d.ioannidis

  • Full Member
  • ***
  • Posts: 221
    • Nephelae
Re: Cross compiling installation for AVR
« Reply #13 on: July 23, 2020, 08:30:13 pm »
Hi,


< snip >

One thing only require to be clarified:


It's not a bug.

In Lazarus change the Target processor ( Project | Project Options | Compiler Options | Config and Target | Target processor ) to (default) and use the -Cp<mcu> option in ( Project | Project Options | Compiler Options | Custom Options ) i.e. -CpAVR6 or -CpAVR25.


If Target processor is chaged, thus -Cp in Custom Options have to be changed accordingly, I suppose.

< snip >



 Those are mutually exclusive. If you set the Target processor then the IDE sets the -Cp option. Thats why I thought that you had the error. You had set the Target processor to AVR5 and also in custom options you added a -CpAVR5 option.

  You can always check the fpc command line call with the "Show Options" button in the Project Option window.



< snip >

With MCUs, additional combobox with filtered MCU list is as well deisered to be shown instead to manually change -CP. As well additional edit field for frequency in -dF_CPU is welcome.

Similar as used in MPLABX and many other compilers in order to simplify and clear all. Perhaos also posibility to use compiler directives in the main file for some settings, as F_CPU and specific MCU.

< snip >



  A package which do what you suggest exist ( didn't test it my self ) already. Look at AVR / Arduino GUI Package for Lazarus.



< snip >

BTW, is it F_CPU allowed to be used in the code, for delays, USART or similar code modifications in order to calculate some constants without need to change it whenever change frequency or MCU?



  Yes this is exactly the purpose.  You can read about macro's and embedded targets here.

regards,
« Last Edit: July 23, 2020, 08:34:54 pm by Dimitrios Chr. Ioannidis »

process_1

  • Guest
Re: Cross compiling installation for AVR
« Reply #14 on: July 23, 2020, 08:47:49 pm »
You had set the Target processor to AVR5 and also in custom options you added a -CpAVR5 option.

I didn't change a thing in "Custom options" memo. The -CpAVR5 where by default, even I didn't set it in IDE. When I choose AVR5 in IDE, this warning was shown. As well, -Wpatmega328p is there by default.


All that makes me to believe this is a bug.




 

TinyPortal © 2005-2018