Recent

Author Topic: FPC/Lazarus Arduino Tutorial  (Read 38641 times)

ykot

  • Full Member
  • ***
  • Posts: 141
FPC/Lazarus Arduino Tutorial
« on: January 02, 2016, 09:27:06 pm »
As it has been asked in another thread, I'm writing this as a quick/short tutorial for people who want to try programming their Arduino Uno using FreePascal and Lazarus. The steps should work on Windows and Linux.

1. Make sure you are running up-to-date Trunk versions of both FreePascal and Lazarus. There are several posts on this forum as well as Internet blogs about it, so make sure to check those. You should be having a fully working environment before proceeding and be comfortable configuring and rebuilding the trunk yourself.

2. On Windows, instead of AVR Toolchain, you may want to just use FPCBuild Binaries, which include AVR-related tools with prefix "avr-embedded-" (e.g. "avr-embedded-as.exe"). Make sure you have all these tools in some folder that you can easily remember.

On Linux, there is official AVR Toolchain. If you happen to use Debian-based distribution, just use:
Code: [Select]
sudo apt-get install binutils-avr

3. Make sure to use latest version of avrdude. On Windows, you can download it from here. For Linux, you can pick the appropriate version from this list. On Debian-based distribution, use:
Code: [Select]
sudo apt-get install avrdude

4. Compile trunk FreePascal for AVR target. On Windows, this is a sample batch script, which you may want to fine-tune yourself:
Code: [Select]
SET FPC_PATH=%CD%\3.0.0\bin\i386-win32
SET PPCBIN=%FPC_PATH%\fpc.exe
SET INSTALL_PATH=%CD%\Win32
SET PATH=%FPC_PATH%;%NDK_BIN%;%PATH%
make clean crossall crossinstall FPC=%PPCBIN% OS_TARGET=embedded CPU_TARGET=avr SUBARCH=avr5 INSTALL_PREFIX=%INSTALL_PATH% CROSSBINDIR=%GNU_BIN_PATH% BINUTILSPREFIX=avr-embedded- CROSSOPT="-O3 -XX -CX"
In above code, adjust the paths as needed. Pay special attention to "GNU_BIN_PATH", which should point to the path, where you downloaded FPCBuild binaries for AVR.

On Linux, you can use the following script (assumes FPC is installed in "/usr/local"):
Code: [Select]
#!/bin/sh
set -e
make clean crossall OS_TARGET=embedded CPU_TARGET=avr SUBARCH=avr5 BINUTILSPREFIX=avr- CROSSOPT="-O3 -XX -CX"
sudo make crossinstall OS_TARGET=embedded CPU_TARGET=avr SUBARCH=avr5
sudo ln -s -f /usr/local/lib/fpc/3.1.1/ppcrossavr /usr/local/bin/ppcrossavr

In above scripts, notice "avr5" subarch flag. This may be different for other Atmel chips, in FreePascal sources open to "/rtl/embedded/Makefile" to see what chip corresponds to each subarch. For Arduino Uno and ATMega328P, this is "avr5".

5. Edit "fpc.cfg" and on Windows add the following:
Code: [Select]
#ifdef embedded
-FD$GNU_BIN_PATH$
-OoFastMath
#-XX
-CX
-Oonostackframe
#ifdef avr
-XPavr-embedded-
#endif
#endif
Note that "GNU_BIN_PATH" should be set as a global environment variable pointing to AVR tools that you've installed in step 2.

On Linux, add something like this:
Code: [Select]
#IFDEF embedded
-OoFastMath
-XX
-CX
-OoNoStackFrame
#IFDEF cpuavr
-XPavr-
#ENDIF
#ENDIF

6. Open the attached example "Blinky" and try compiling it. If you have configured toolchains and built FreePascal for AVR target correctly, it should succeed. You should obtain files such as "Blinky.elf", "Blinky.hex" and "Blinky.bin". Either one of these files can be used for uploading to Arduino.

7. Upload the resulting executable to Arduino using its own bootloader and avrdude. The command would be something like:
Code: [Select]
sudo avrdude -v -patmega328p -carduino -P/dev/ttyACM0 -b115200 -D -Uflash:w:Blinky.hex:i
In above command, on Linux, "/dev/ttyACM0" is the path to mapped serial port after connecting Arduino, make sure to adjust it as needed. On Windows, it would probably be something like:
Code: [Select]
avrdude -v -patmega328p -carduino -P\\.\COM3 -b115200 -D -Uflash:w:Blinky.hex:i

If the operation is successful, the application should execute immediately and you should see internal LED blinking. If you have any problems with this, feel free to ask here.

Note that attached example accesses port bits directly, similar to C/C++ AVR samples. Currently, there is no higher-level framework, but PXL on GitHub is being ported. Meanwhile, you can just use code similar to AVR C tutorials.

The following are some limitations on AVR target:
  • In RTL, bit flags like "PA0", "PB3" are not defined. However, since these are just friendly names for bits, you can use something like "1 shl 2" (for PA2, PB2, PC2, etc..)
  • Floating-point emulation doesn't seem to work yet, so you should stick to integers and/or fixed-point math.
  • Classes don't seem to work either, currently compiler complains about something related to VMT. You can just stick to procedural approach with advanced records.
To figure out what Arduino pin corresponds to what port, you can use pinout diagram from this post. Internal LED appears to be connected to pin 13 (PB5).
« Last Edit: January 02, 2016, 09:44:21 pm by ykot »

Edson

  • Hero Member
  • *****
  • Posts: 1301
Re: FPC/Lazarus Arduino Tutorial
« Reply #1 on: January 03, 2016, 07:15:50 pm »
Interesting.

I will check when I get some time.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: FPC/Lazarus Arduino Tutorial
« Reply #2 on: January 30, 2016, 10:26:45 pm »
Has anyone been able to get this working for windows 7 or 8? I have just spent 4 hours and I am getting no closer.

The instructions on this page are are not complete or ambiguous at best.

I can see the OP, ykot, has been active up to yesterday so I am hoping he/she can shed some light as I am getting nowhere.

I am at the point with the Arduino stuff, where I need to spend a month or so to learn C/C++ OR try and get something that will compile and upload to to the Arduino.

I have been programming in Pascal for 35+ years and the mental move to C/C++ is too much for this tired old brain. :)

Thanks
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

Laksen

  • Hero Member
  • *****
  • Posts: 724
    • J-Software
Re: FPC/Lazarus Arduino Tutorial
« Reply #3 on: January 30, 2016, 11:41:10 pm »
Maybe if you told where you were stuck it would be easier to help :)

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: FPC/Lazarus Arduino Tutorial
« Reply #4 on: January 31, 2016, 12:28:49 am »
Maybe if you told where you were stuck it would be easier to help :)

OK, good point. I downloaded the latest Lazarus and did a clean install to D:\Lazarus on a win8.1 i5 8MB that had never seen Lazarus before.
I downloaded six "avr-embedded...exe files and put them in a Folder within Lazarus "d:\Lazarus\AVREmbedded"
Saved the .bat file as "install01.bat" and here is the contents of it.
Code: [Select]
SET FPC_PATH=d:\lazarus\fpc
SET PPCBIN=%FPC_PATH%\fpc.exe
SET INSTALL_PATH=%CD%\Win32
SET PATH=%FPC_PATH%;%NDK_BIN%;%PATH%
make clean crossall crossinstall FPC=%PPCBIN% OS_TARGET=embedded CPU_TARGET=avr SUBARCH=avr5 INSTALL_PREFIX=%INSTALL_PATH% CROSSBINDIR=d:\lazarus\AVREmbedded BINUTILSPREFIX=avr-embedded- CROSSOPT="-O3 -XX -CX"

When I ran that it complained about "no make" so I found an earlier version of Arduino that had make.exe and copied it to the Lazarus folder.

I opened an elevated command prompt and navigated to d:\Lazarus and ran the .bat file.

...and got this...
Code: [Select]
D:\Lazarus>install01

D:\Lazarus>SET FPC_PATH=d:\lazarus\fpc

D:\Lazarus>SET PPCBIN=d:\lazarus\fpc\fpc.exe

D:\Lazarus>SET INSTALL_PATH=D:\Lazarus\Win32

D:\Lazarus>SET PATH=d:\lazarus\fpc;;d:\lazarus\fpc;;d:\lazarus\fpc  d:\lazarus\f
pc;;d:\lazarus\fpc;;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\W
indows\System32\WindowsPowerShell\v1.0\

D:\Lazarus>make clean crossall crossinstall FPC=d:\lazarus\fpc\fpc.exe OS_TARGET
=embedded CPU_TARGET=avr SUBARCH=avr5 INSTALL_PREFIX=D:\Lazarus\Win32 CROSSBINDI
R=d:\lazarus\AVREmbedded BINUTILSPREFIX=avr-embedded- CROSSOPT="-O3 -XX -CX"
makefile:29: *** You need the GNU utils package to use this Makefile.  Stop.

It seems the instructions for the .bat file are incorrect, incomplete and/or ambiguous.

It would make more sense if the instructions were in two separate parts. One for windows and one for linux instead of being interleaved.

The .bat file needs to use actual install folders using the default Lazarus installation. I changed only the drive letter during the default  installation.
« Last Edit: January 31, 2016, 12:32:39 am by HatForCat »
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: FPC/Lazarus Arduino Tutorial
« Reply #5 on: January 31, 2016, 12:39:43 am »
Hi HatForCat,

my FPC binaries are inside: Q:\WinDev\Lazarus\FPC\2.6.4\bin\i386-win32 and that seems a bit different when comparing that with yours.

That would probably cause the batch file to not be able to find the required executables.

you can check by opening a command shell and typing fpc and see what that returns.

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: FPC/Lazarus Arduino Tutorial
« Reply #6 on: January 31, 2016, 12:49:08 am »
Thanks Molly,

OK, changed to that and got some more errors so copied the six avr files to the Lazarus folder and then it did a lot of stuff.

I loaded the Blinky file and got
d:\lazarus\FPC\2.6.4\bin\i386-win32\fpc.exe does not support target avr-embedded.

Compile Project, OS: embedded, CPU: avr, Target: Blinky: Exit code 1, Errors: 1
Error: Illegal processor type "avr"

Shutting down for the night, "duty" calls. :)

Thanks so far, I feel we are getting closer...
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: FPC/Lazarus Arduino Tutorial
« Reply #7 on: January 31, 2016, 01:55:05 pm »
OK, so I have uninstalled everything and did another clean full install of Lazarus.

I think the problems are coming from the use of Path-tokens in the Batch file as I don't have these predefined

%CD%
%NDK_BIN%
%INSTALL_PATH%\Win32  <-- what is that ?Install for what?

Could someone please just give me a copy of that batch file with all their actual things defined and I can figure it out from there.
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: FPC/Lazarus Arduino Tutorial
« Reply #8 on: January 31, 2016, 02:37:49 pm »
HiHatForCat,

I can't help you with the actual values (as they are specific for your setup), but i can attempt to tell you what they do:

%CD% is representing teh current directory (automatically filled by dos shell -> do a echo %CD% in a shell to see for yourself)

%NDK_BIN% should be the path to your SDK. use SET NDK_BIN=Path_to_your_NDK_Directory

%INSTALL_PATH% is used for the INSTALL_PREFIX option and is used as a location where to store the final results

"Error: Illegal processor type "avr"" seems to indicate that your compiler does not support this cpu, hence why you need to create one that does (read: create cross-compiler).

PS: Sometimes, the makefiles get confused by residue that was left behind by a previous attempt. in that case do a "make clean" before you start the batch again.
« Last Edit: January 31, 2016, 02:41:23 pm by molly »

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: FPC/Lazarus Arduino Tutorial
« Reply #9 on: January 31, 2016, 04:30:16 pm »
Hi Molly,

Thank you so much for taking the time to explain this. VERY much appreciated.

%NDK_BIN% should be the path to your SDK. use SET NDK_BIN=Path_to_your_NDK_Directory

Thanks, but what SDK? A Software Developers Kit for what? Do I have to download something else? What? Where?

PS: Sometimes, the makefiles get confused by residue that was left behind.

Thanks, I am already doing that. I use Revo to uninstall Lazarus, then roll back with a System Restore. Been caught too many times in the past dealing with hidden remnants. :)
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: FPC/Lazarus Arduino Tutorial
« Reply #10 on: January 31, 2016, 04:45:08 pm »
Thanks, but what SDK? A Software Developers Kit for what? Do I have to download something else? What? Where?
I am unfamiliar with this particular target, so you would need to ask the person that wrote the instructions  :D

Generally speaking, some targets require extra files to be able to compile working executables for the target with FPC or requires extra utilities to aid development cycle.

I can only assume that the person writing the instructions meant the avrdude utils. (but please note that there might be more to it).

Well, at least i figured out that you can't create a cross-compiler from 3.0.0 release source files:
Quote
Clean of package fpc-all completed
MAKE[3]: Leaving directory `D:/Temp/Build-FPC-from-source/fpc-3.0.0/packages'
MAKE[2]: Leaving directory `D:/Temp/Build-FPC-from-source/fpc-3.0.0'
MAKE rtl_all FPC=D:/Temp/Build-FPC-from-source/fpc-3.0.0/compiler/ppcrossavr.exe FPCFPMAKE=D:/Temp/Build-FPC-from-source/fpc-3.0.0/compiler/ppc.exe RELEASE=1 'OPT='
MAKE[2]: Entering directory `D:/Temp/Build-FPC-from-source/fpc-3.0.0'
MAKE -C rtl all
MAKE[3]: Entering directory `D:/Temp/Build-FPC-from-source/fpc-3.0.0/rtl'
MAKE -C embedded all
MAKE[4]: Entering directory `D:/Temp/Build-FPC-from-source/fpc-3.0.0/rtl/embedded'
D:/Temp/Build-FPC-from-source/fpc-3.0.0/compiler/ppcrossavr.exe @rtl.cfg -Ur -Tembedded -Pavr -XPavr- -Xr -Ur -Xs -O2 -n -Fi../inc -Fi../avr -FDD:\WinDev\BinUtils -FE. -FUD:/Temp/Build-FPC-from-source/fpc-3.0.0/rtl/units/avr-embedded -davr -dRELEASE -Us -Sg system.pp
sstrings.inc(1004,1) Fatal: Internal error 200309041
Fatal: Compilation aborted
MAKE[4]: Leaving directory `D:/Temp/Build-FPC-from-source/fpc-3.0.0/rtl/embedded'
MAKE[3]: Leaving directory `D:/Temp/Build-FPC-from-source/fpc-3.0.0/rtl'
MAKE[2]: Leaving directory `D:/Temp/Build-FPC-from-source/fpc-3.0.0'
MAKE[1]: Leaving directory `D:/Temp/Build-FPC-from-source/fpc-3.0.0'
 
Log stopped zo 31-01-2016, 16:36:42,50

So afaik that means you would have to install 3.0.0, and attempt to build the cross-compiler from trunk sources (but user ykot did wrote that)
« Last Edit: January 31, 2016, 04:51:30 pm by molly »

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: FPC/Lazarus Arduino Tutorial
« Reply #11 on: January 31, 2016, 05:39:01 pm »
OK, thanks Molly. I quit.

The OP's instructions are too vague and as you have found out, just plain wrong in some places. There is great skill required in writing effective instructions and these were not it.

It is far better to have no instructions rather than poor ones that only frustrate after wasting many hours. :)

BUT, thank you very much for trying to help. I have been programming in Pascal for 35+ years and the learning curve for C/C++ for the Arduino was too great a stretch so I was hoping for this to work. Ahhh well, uninstall Lazarus (I used Delphi) and back to the C/C++ tutorials.
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: FPC/Lazarus Arduino Tutorial
« Reply #12 on: January 31, 2016, 07:30:26 pm »
@HatForCat
Quitter  :P  ;)

ykot's instructions do mention needing to create the cross-compiler from FPC trunk sources, it could be that you missed that part ?

I can't judge ykot's instructions fully, as i didn't created the cross-compiler from trunk but from fpc 3.0 source-tree (i had that sitting on my disk, so that was easy to test).

Basically, these instructions from ykot seems correct to me (although it does leave room for interpretation, especially if not understanding fully what each and every parameter/option does).

For my situation, things worked as advertised (except for the error i ran into creating the FPC RTL)

The only thing that doesn't make sense to me is having lazarus compiled from trunk (afaik there is no need for that).

Please realize that afaik, the compiler for avr is a work in progress (and therefor experimental -> bad things might happen). The target seems not suited for end-users (yet).

In that regards, i disagree a little with your opinion to have no instructions at all. Every error you run into will help improve things, as developers do read these kind of things (might be that a bug-report needs to be created though -> i haven't looked yet if it was already reported or perhaps already fixed in trunk).

edit:
... and a little search led me to this thread (which reports the same error as i ran into).

Looking at the commits, then i can see a related commit here
« Last Edit: January 31, 2016, 07:54:33 pm by molly »

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: FPC/Lazarus Arduino Tutorial
« Reply #13 on: January 31, 2016, 08:09:37 pm »
Quitter

:) It does not happen often, but I look at it as a positive thing making me go learn C++. I have been putting it off for years. But, given that I am 76 and retired there seemed no real point. Now that I am messing with Arduino, the point has become obvious.

Unless I can find out what the %NDK_BIN% means it is pointless.

I saw the mention of "trunks" and did some google searching and could not find anything so assumed it just meant the latest version of Lazarus so that's what I downloaded.

I stand by the "no instructions" as it is useless trying to do something if a large chunk of the instructions assumes I am an expert on Lazarus and the things needed to create a cross-compiler with it. By definition a Tutorial should be for the lowest common denominator. Many years back, I wrote quite a few award winning (by peers) technical manuals and they all covered things taking into consideration the barest novice to the trained expert. They all had sidebars on every page indicating the level of prior knowledge needed to read or skip.

Now, I am not by any stretch expecting that degree of detail on a free website, but I do expect tutorials to be just that. If a "trunk" version is need then a link to that download is mandatory.

Quitting rather than massive esophageal reflux after many hours of frustration guessing what the tutorial meant, was/is the better option. I would have been happy to write a complete tutorial on the subject for this site, but if I can't get it working what could I write. <-- Yes, rhetorical.
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: FPC/Lazarus Arduino Tutorial
« Reply #14 on: January 31, 2016, 08:10:54 pm »
For Windows:

1-Install latest Lazarus ( I installed Lazarus 1.6 RC2 mentioned here ) later on I'll assume that  you installed it in c:\Laz1.6RC2 folder.
2-Get FPC trunk ( you can get the archived version from ftp://ftp.freepascal.org/pub/fpc/snapshot/trunk/source/fpc.zip )
3-Decompress that archive somewhere (if you did that in c:\avrtest then you should have a file named Makefile in c:\avrtest\fpc\ folder )
4-Get the avr toolchain (avr-embedded-* from here )
5-I put the avr-embedded-* files in c:\avrtest folder
6-Make a batch file in c:\avrtest\fpc\ , where the Makefile file located, and name the batch file avrbuild.bat that contains:

Code: Text  [Select][+][-]
  1. REM if you installed Lazarus 1.6 RC2 in c:\Laz1.6RC2 it comes with FPC.exe in c:\Laz1.6RC2\fpc\3.0.0\bin\i386-win32 folder
  2. REM That is the folder we need to add here.
  3. SET FPC_PATH=c:\Laz1.6RC2\fpc\3.0.0\bin\i386-win32
  4.  
  5. REM The compiler itself ( Which its location is saved in FPC_PATH )
  6. SET PPCBIN=%FPC_PATH%\fpc.exe
  7.  
  8. REM A folder where the generated cross compiler and run-time libraries are going to be saved
  9. SET INSTALL_PATH=c:\avrtest\Win32
  10.  
  11. REM We need the path to avr-embedded-* files
  12. SET NDK_BIN=c:\avrtest\
  13.  
  14. REM Make all the binary files that we need accessible
  15. SET PATH=%FPC_PATH%;%NDK_BIN%;%PATH%
  16.  
  17. REM Use the correct SUBARCH here
  18. REM for Uno (ATMega328P) use avr5
  19. REM for Mega (ATMega2560) use avr6
  20. REM To find the correct SUBARCH value check http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/rtl/embedded/Makefile?view=co
  21. REM Start the process
  22. make clean crossall crossinstall FPC=%PPCBIN% OS_TARGET=embedded CPU_TARGET=avr SUBARCH=avr5 INSTALL_PREFIX=%INSTALL_PATH% CROSSBINDIR=%GNU_BIN_PATH% BINUTILSPREFIX=avr-embedded- CROSSOPT="-O3 -XX -CX"

Make sure to use the correct SUBARCH, for Uno (ATMega328P) use avr5, for Mega (ATMega2560) use avr6. Check /rtl/embedded/Makefile to find the correct one.

7-Start a command window ( Run - Type cmd  )
8-Move to the directory where you saved the previous batch file ( CD c:\avrtest\fpc )
9-Run the batch file ( type avrbuild.bat and hit Enter )
10-Wait and be patient ( patience is a virtue! )

If it succeeded then you should have:
1-a cross compiler named ppcrossavr.exe in c:\avrtest\win32\bin\i386-win32
2-compiled rtl units in c:\avrtest\units\avr-embedded\

How to use them is a different story.

Corrections:
1-Thanks to jmpessoa, the folder name I chose is avrtest, not armtest.
2-Thanks to HatForCat: corrected the link to FPC trunk archive.
3-Thanks to HatForCat: corrected the name of the Makefile (not make).
4-Thanks to HatForCat: corrected the path for fpc binaries.
5-Thanks to HatForCat: avr5 for Uno, avr6 for Mega.
« Last Edit: February 03, 2016, 01:03:03 am by engkin »

 

TinyPortal © 2005-2018