In addition to targeting all of the mainstream desktop OS environments, Free Pascal is also capable of generating code for bare metal architectures. This is supported by cross compiler builds with the target operating system parameter (-T) set to ’embedded’, in combination with the target CPU (-P) and architecture instruction set (-Cp) parameters identifying the required processor.
I want to be able create embedded programs for the Nordic Semiconductor ARM SoC’s the nRF52832 and nRF51822. Fortunately, Free Pascal already contains support for the nRF52832 in the current trunk version (3.1.1) and that is what I will be using here. The nRF51822 will have to be the subject of a future post!
The process of creating a cross-compiler I have already looked at in my post ‘Cross-compiling to Pi Zero’. The procedure is exactly the same for an embedded target, except for the embedded parameter and the fact that only a minimal RTL is required. This minimal RTL creation is controlled by the make file options ‘buildbase’ and ‘installbase’. So the actual building and installing commands for a nRF52832 cross compiler are as follows (all parameters on a single line):
make buildbase CROSSINSTALL=1 OS_TARGET=embedded CPU_TARGET=arm SUBARCH=armv7em CROSSOPT="-O2 -gw2" CROSSBINDIR=/usr/bin/ BINUTILSPREFIX=arm-none-eabi- sudo make installbase CROSSINSTALL=1 OS_TARGET=embedded CPU_TARGET=arm SUBARCH=armv7em CROSSOPT="-O2 -gw2" BINUTILSPREFIX=arm-none-eabi- PREFIX=/usr
They can replace the commands given in the script for the Pi Zero as it is identical in all other respects. Once the cross compiler is installed, a program may be compiled for the nRF52832 with the following command line:
fpc -Tembedded -Parm -Wpnrf52832 -CpARMV7EM -XParm-none-eabi- -Oonostackframe Test.lpr
Note the key parameter ‘-Wpnrf52832’ which identifies the exact embedded target to build for. There is a very useful command line parameter for a Free Pascal cross compiler:
Which lists all of the supported embedded targets for that particular compiler. This is a good check that the cross compiler was created with the correct command line options.
To transfer the program to the target board I use nrfjprog which can be installed from the RF5x-Command-Line-Tools-Linux64. The actual commands to perform the transfer are:
nrfjprog --recover -f NRF52 nrfjprog --program Test.hex --verify --chiperase -f NRF52 nrfjprog --rbp ALL -f NRF52
On the nRF52-DK, which is the test platform I am using, the program starts running as soon as the board resets at the end of the transfer. Obviously the build and transfer commands can be contained in a single make files so the day-to-day workflow feels very similar to using GCC, just with much faster build times.
However, this being Free Pascal, it is possible to forget about explicitly executing make from the command-line and instead we can use the various Lazarus project options to perform the same operations when the Lazarus build command is executed (Shift F9). Even the nrfjprog steps may be invoked using the ‘Execute After’ compiler settings, so that we never have to leave the IDE! For anyone interested, my ‘proof of concept’ test program (simple nRF5x SDK ‘Blinky’ translation) is available here.