Sign in Join
Wiki copytemporary

TARGET Embedded: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: TARGET Embedded

OldNewLine
1+This is an overview of using FPC on embedded systems without an operating system. The embedded target is still under development and only a few microcontroller families are supported. These are ARM (several architectures), AVR, PIC32MX (MIPSel), Xtensa (ESP32) and Z80. The embedded target targets systems which are *without any operating system* and typically only have several kBs of RAM and several dozens of kB of flash. A typical target is the LPC family of NXP with popular members like the LPC2124 with 16 kB RAM and 256 kB flash using the ARM7 instruction set.
2+
3+## Embedded Port Status
4+
5+- Best support is available in the Git "main" branch.
6+- Compilers are working, register definitions are available, but libs for peripheral access are rare to non-existent. Hardware can be accessed via hardware registers.
7+- Not all microcontrollers of each family are supported. If you're interested in support for further microcontrollers, please subscribe to the [fpc-devel mailing list](http://lists.freepascal.org/mailman/listinfo/fpc-devel) and write a mail so adding the support can be discussed.
8+
9+## How to use heap
10+
11+By default, heap is not initialized on embedded targets, so any app trying to allocate on the heap, will crash. To fix that, add the **heapmgr** unit to the `uses` clause.
12+
13+The size of the heap is controlled with the [{\$Memory \<StackSize\>, \<HeapSize\>}](https://www.freepascal.org/docs-html/current/prog/progsu102.html#x110-1110001.3.19) directive or the -Ch option. Additional regions beside the initial one can be registered using `HeapMgr.RegisterHeapBlock`.
14+
15+## ARM Embedded
16+
17+### Build
18+
19+Get latest FPC from git:
20+
21+``` bash
22+git clone https://gitlab.com/freepascal.org/fpc/source.git fpc
23+```
24+
25+Get arm-embedded binutils.
26+
27+For windows, they are available at <http://svn.freepascal.org/svn/fpcbuild/binaries/i386-win32>. You can either checkout the whole directory with or download only arm-embedded-ar.exe, arm-embedded-as.exe, arm-embedded-ld.exe, arm-embedded-strip.exe, arm-embedded-objdump.exe and arm-embedded-objcopy.exe manually. Put these utils in a directory included in your path.
28+
29+To build binutils from sources <http://wiki.lazarus.freepascal.org/Binutils> has instructions, for example, ./configure --target=arm-linux --prefix=/opt/embarm/ --program-prefix=arm-embedded- --disable-werror, has been known to work on binutils 2.21.1 to build the binaries in the form that the fpc target embedded build desires.
30+
31+Build FPC for arm-embedded (OABI):
32+
33+``` bash
34+cd fpc
35+make clean buildbase installbase OS_TARGET=embedded CPU_TARGET=arm SUBARCH=armv4
36+```
37+
38+This builds only the compiler and the RTL instead of the whole FPC. Because of the limited capabilities of the embedded systems supposed to be used, building all packages is not useful. However, one must be careful to avoid overwriting an existing arm compiler on the system. If this is the case, INSTALL_PREFIX should be used to install into a different directory and switch between those.
39+
40+SUBARCH is required and should match your target processor, e.g.:
41+
42+- for the LPC 21xx parts which are ARM7TDMI (not to be confused with armv7!) based use SUBARCH=armv4
43+- for cortex-m3 parts (stellaris, stm32, etc) use SUBARCH=armv7m
44+- for cortex-m devices that support DSP extensions use SUBARCH=armv7em. The compiler does not yet support the cortex-m* FPU
45+- for cortex-m0 devices use -Cparmv6m
46+
47+### NXP LPC2124 example
48+
49+Below you find a simple example program for the LPC2124 microcontroller, save it as tled1.pp to follow the description. The program is made for development boards like the LPC-WEB from OLIMEX (http://www.olimex.com/dev/lpc-e2124.html). When you press button 1, led 1 lights for a certain time, same for button 2 and led 2.
50+
51+``` pascal
52+procedure Wait(d : dword);
53+begin
54+ while d<>0 do
55+ dec(d);
56+end;
57+
58+begin
59+ { initialize PLL }
60+ InitPLL(2,1);
61+
62+ { initialize LEDs }
63+ { port 0.8: output }
64+ TBitvector32(GPIO0_IODIR)[8]:=1;
65+ { port 0.10: output }
66+ TBitvector32(GPIO0_IODIR)[10]:=1;
67+
68+ { turn off both LEDs }
69+ TBitvector32(GPIO0_IOSET)[8]:=1;
70+ TBitvector32(GPIO0_IOSET)[10]:=1;
71+
72+ { initialize button inputs }
73+ { port 0.9: input }
74+ TBitvector32(GPIO0_IODIR)[9]:=0;
75+ { port 0.15: input }
76+ TBitvector32(GPIO0_IODIR)[15]:=0;
77+
78+ { endless loop}
79+ while true do
80+ begin
81+
82+ { button 1 pressed }
83+ if TBitvector32(GPIO0_IOPIN)[15]=0 then
84+ begin
85+ { turn on LED, inverse logic }
86+ TBitvector32(GPIO0_IOCLR)[8]:=1;
87+ { wait }
88+ Wait(500000);
89+ { turn off LED, inverse logic }
90+ TBitvector32(GPIO0_IOSET)[8]:=1;
91+ end;
92+
93+ { button 2 pressed }
94+ if TBitvector32(GPIO0_IOPIN)[9]=0 then
95+ begin
96+ { turn on LED, inverse logic }
97+ TBitvector32(GPIO0_IOCLR)[10]:=1;
98+ { wait }
99+ Wait(500000);
100+ { turn off LED, inverse logic }
101+ TBitvector32(GPIO0_IOSET)[10]:=1;
102+ end;
103+ end;
104+end.
105+```
106+
107+If FPC for arm-embedded is installed as described above, the program can be compiled by
108+
109+``` bash
110+fpc -Parm -Tembedded -Wplpc2124 tled1.pp
111+```
112+
113+``` bash
114+-Parm
115+```
116+
117+tells the compiler to compile for arm.
118+
119+``` bash
120+-Tembedded
121+```
122+
123+tells the compiler to compile for the embedded target.
124+
125+``` bash
126+-Wplpc2124
127+```
128+
129+tells the compiler to compile for the NXP LPC 2124. This has two effects: first, a unit (lpc21x4 in this case) containing the startup code and the port etc. definitions for this controller is loaded. Further, the compiler uses a linker script which fits the needs of this controller.
130+
131+The result of the compiler is a .hex file which can be programmed by the NXP flash programming utility.
132+
133+When using a board like the mbed (http://mbed.org) which is cortex-m3 based. Another ARM core but uses only thumb/thumb2 instructions not ARM/thumb instructions. A few subtle differences, first building fpc
134+
135+``` bash
136+make clean buildbase installbase CROSSINSTALL=1 OS_TARGET=embedded CPU_TARGET=arm SUBARCH=armv7m
137+```
138+
139+Second when compiling your program
140+
141+``` bash
142+fpc -Parm -Tembedded -Wplpc1768 -Cparmv7m myprogram.pp
143+```
144+
145+The -Cparmv7m option causes thumb2 code to be generated instead of the default (ARM based), probably not a bad idea to use the -Cp for processors using ARM instructions as well. -Wp as with the example above specifies the specific target, in this case microcontroller, to build for.
146+
147+### Nordic nRF51822 example
148+
149+See the [[micro:bit]] page.
150+
151+## Adding new microcontrollers
152+
153+Adding a new microcontroller type requires basically three steps: first, extend the compiler so it knows about the name of the controller then add a linker script and finally create an RTL unit with the register definitions and the startup code.
154+
155+#### Add the microcontroller type to the compiler
156+
157+This is done in the file `compiler/arm/cpuinfo.pas`. Here you need to add an identifier for the controller in the tcontrollertype enum At the same time you must update the embedded_controllers array. The array holds information about the amount of flash and sram in the controller, and what rtl unit should be included by default when compiling for that controller.
158+
159+*Note: Make sure the string of the two arrays are long enough to contain the strings you write*
160+
161+#### Add the linker script to the compiler
162+
163+Adding a linker script consists of adding the controllertype enum identifier to the case statement in compiler/systems/t_embed.pas If this is not done the compiler will generate an internalerror
164+
165+#### Create an RTL unit with startup code and register definitions
166+
167+Creating an RTL unit means creating a unit that'll automatically be used in a project compiled for the given controller type. This unit should supply an entry point and basic initialization code. Enough to get the controller running
168+
169+This unit, which must be named the same as the string written in the controllerunitstr field in the embedded_controllers array, goes into the directory rtl/embedded/arm/ where there are some examples already
170+
171+After that the rtl/embedded/Makefile.fpc should be modified, and translated with [[Fpcmake]] so the newly added rtl unit is compiled when building the cross compiler. The commandline for fpcmake (to be executed in rtl/embedded) is "fpcmake.exe -w -Tall".
172+
173+## AVR
174+
175+See the [[AVR]] wiki page.
176+
177+## MIPSel (PIC32)
178+
179+See the [[TARGET Embedded Mipsel|MIPSel]] wiki page.
180+
181+## Xtensa
182+
183+See the [[Xtensa]] wiki page.
184+
185+## Z80
186+
187+See the [[Z80]] wiki page.
188+
189+## Useful links
190+
191+- <http://de.wikipedia.org/wiki/Intel_HEX>
192+- <http://www.nxp.com/products/microcontrollers/support/software_download/lpc2000/>
193+- <http://www.nxp.com/acrobat_download/usermanuals/UM10114_3.pdf>
194+- Jim Lynch: Using Open Source Tools for AT91SAM7 Cross Development - Revision B <http://www.atmel.com/dyn/resources/prod_documents/atmel_tutorial_source.zip>
195+
196+## See Also
197+
198+- [[Small Virtual Machines|Small Virtual Machines#WinXp_virtual_machine]] as have **simple** example for Teensy. **Files released here:** [[http://turbocontrol.com/simpleteensy.htm]]
199+
200+---
201+*From the Free Pascal wiki, page "TARGET Embedded", as it stood on 2023-04-06 after a change by Ccrause, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
202+Category: Embedded
203+Category: FPC
204+Category: Microcontrollers

Back to history