This blog is inspired by the question on Stack Exchange Arduino, Programming Arduino in Pascal?
There is any (stable) Pascal or Pascal-class (FPC, Delphi, mikroPascal) programming language for Arduino?
It could be also a Basic-based language.
It is all a bit pointless really, a pure academic exercise, simply because all of the Arduino libraries are written in C++, and so, unless you love re-inventing the wheel, or you are still in school learning how to code, you would have to rewrite all of the libraries (or, at least, the parts that you need) into Pascal. No mean feat, by any means, but not impossible.
There is an obvious compatibility between Pascal and C – one good example is that the original APIs, in the Macintosh Toolbox, were written in a mixture of 68000 assembly code and Pascal, as were the applications, prior to System 6, and then the switch was made to C, when writing Macintosh applications, circa System 7, yet all C code could still reference the Toolkit functions.
There is the misleadingly titled YouTube video, Arduino & Pascal programming language – HOW TO, which has NOTHING at all to do with coding in Pascal for the Arduino. It just goes on about the Lazarus IDE (which is a good IDE, just annoying to download and install).
Original Arduino IDE uses GCC to produce code for 8-bit Atmel AVR and some ARM processors. Lazarus IDE uses FreePascal to produce code for various targets. I guess that you are probably most interested in ARM embedded and ARM Linux targets. ARM embedded is possible but not very user friendly. ARM Linux is well supported and there are ready to use libraries for Raspberry Pi platform. More info can be found here: http://wiki.freepascal.org/Lazarus_on_Raspberry_Pi.
Arduino platform was microcontroller hardware oriented from the start – that’s why there are so many ready to use libraries to talk to various hardware chips (through GPIO, I2C, SPI, 1wire, serial…). For each library someone had to read sometimes hundreds of pages from datasheet of some chip and code a wrapper which is not easy and usually needs and engineer or an advanced user. Lazarus was pc oriented from the start and does not have such strong hardware background, and there are not many Lazarus people who know how to interprete datasheet data to make a usable library. Therefore you will not find many libraries for some specific chips, but nothings stops you from converting other’s people code (including Arduino) if you know what you are doing, or read datasheets yourself and use GPIO, I2C, SPI and other ways to talk to your device (step motor, servo motor, gyro, compass, temperature or any other type of sensor…).
If you want to use Pascal with a lot of included ready to use hardware drivers like you can find for Arduino, then take a look at E-Lab AvrCo Multitasking Pascal for Atmel AVR Tiny/Mega/XMega microcontrollers, or Mikroelektronika Pascal (also for Atmel, but supports PIC, ARM, 8051 and FT90x):
However, all is not lost, as there is an excellent tutorial here, FPC/Lazarus Arduino Tutorial:
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:
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:
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:
SET FPC_PATH=%CD%\3.0.0\bin\i386-win32In 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.
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"
On Linux, you can use the following script (assumes FPC is installed in “/usr/local”):
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:
#ifdef embeddedNote 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:
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:
sudo avrdude -v -patmega328p -carduino -P/dev/ttyACM0 -b115200 -D -Uflash:w:Blinky.hex:iIn 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:
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).