Forum > Embedded - AVR

FPC/Lazarus Arduino Tutorial

(1/10) > >>

ykot:
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: ---sudo apt-get install binutils-avr

--- End code ---

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: ---sudo apt-get install avrdude

--- End code ---

4. Compile trunk FreePascal for AVR target. On Windows, this is a sample batch script, which you may want to fine-tune yourself:

--- Code: ---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"

--- End code ---
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: ---#!/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

--- End code ---

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: ---#ifdef embedded
-FD$GNU_BIN_PATH$
-OoFastMath
#-XX
-CX
-Oonostackframe
#ifdef avr
-XPavr-embedded-
#endif
#endif

--- End code ---
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: ---#IFDEF embedded
-OoFastMath
-XX
-CX
-OoNoStackFrame
#IFDEF cpuavr
-XPavr-
#ENDIF
#ENDIF

--- End code ---

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: ---sudo avrdude -v -patmega328p -carduino -P/dev/ttyACM0 -b115200 -D -Uflash:w:Blinky.hex:i

--- End code ---
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: ---avrdude -v -patmega328p -carduino -P\\.\COM3 -b115200 -D -Uflash:w:Blinky.hex:i

--- End code ---

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).

Edson:
Interesting.

I will check when I get some time.

HatForCat:
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

Laksen:
Maybe if you told where you were stuck it would be easier to help :)

HatForCat:

--- Quote from: Laksen on January 30, 2016, 11:41:10 pm ---Maybe if you told where you were stuck it would be easier to help :)
--- End quote ---

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: ---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"

--- End code ---

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: ---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.

--- End code ---

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.

Navigation

[0] Message Index

[#] Next page

Go to full version