Sign in Join
Wiki copytemporary

ARM Embedded Tutorial - Entry FPC and STM32: what changed

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

Title: ARM Embedded Tutorial - Entry FPC and STM32

OldNewLine
1+# Introduction to STM32 and FPC
2+
3+This tutorial was tested using 64 bit Linux Mint.
4+
5+## Driver for ST-LINK V2
6+
7+### Linux
8+
9+Download the package as a zip with **clone or download** from <https://github.com/texane/stlink>
10+
11+Follow the instructions at <https://github.com/texane/stlink/blob/master/doc/compiling.md>
12+
13+The following packages must be installed:
14+
15+- CMake (minimum v2.8.7)
16+- C compiler (gcc, clang, mingw)
17+- Libusb 1.0 (minimum v1.0.9)
18+
19+``` bash
20+ sudo apt-get install cmake
21+ sudo apt-get install gcc
22+ sudo apt-get install libusb-1.0-0.dev
23+ // sudo apt-get install libusb-dev // ???
24+```
25+
26+Compile and install stlink:
27+
28+``` bash
29+ $ make release
30+ $ make debug
31+
32+ $ mkdir build # may already exist.
33+ $ cd build
34+ $ cmake -DCMAKE_BUILD_TYPE = Debug ..
35+ $ make
36+```
37+
38+To install in your home directory:
39+
40+``` bash
41+ $ cd build/release
42+ $ make install DESTDIR=$HOME
43+```
44+
45+To install for all users:
46+
47+``` bash
48+ $ cd build/release
49+ $ sudo make install
50+```
51+
52+Reboot or:
53+
54+``` bash
55+ $ sudo udevadm control --reload-rules
56+ $ sudo udevadm trigger
57+```
58+
59+Then there should be a device file /dev/stlinkv2_3 or similar.
60+
61+Run the following as a test:
62+
63+` st-util`
64+
65+This should produce several lines of information, including how much SRAM the controller has. You can then cancel it with Ctrl+C.
66+
67+If st-util aborts with the following error:
68+
69+` st-util: error while loading shared libraries: libstlink.so.1: cannot open shared object file: No such file or directory`
70+
71+This is probably the problem: <https://github.com/texane/stlink/issues/700> so run the following:
72+
73+``` bash
74+ $ sudo ldconfig
75+```
76+
77+### Windows/macOS
78+
79+` `[`https://github.com/texane/stlink/releases/tag/1.3.0`](https://github.com/texane/stlink/releases/tag/1.3.0)` (not tested) `
80+
81+## Hardware connections
82+
83+Connect the ST-LINK V2 to the STM32F103C as follows:
84+
85+| ST LINK V2 | -\> | STM32F103C |
86+|------------|-----|------------|
87+| SWDIO | -\> | SWO |
88+| GND | -\> | GND |
89+| SWCLK | -\> | SWCLK |
90+| 3.3V | -\> | 3.3V |
91+
92+## Set up Arduino IDE (optimal)
93+
94+This is not absolutely necessary, but it has the advantage that you can look up how problems have been solved there, eg I²C access.
95+
96+Note the following page (does not work with Sloeber (Eclipse for Arduino))
97+
98+` `[`https://github.com/stm32duino/Arduino_Core_STM32`](https://github.com/stm32duino/Arduino_Core_STM32)` `
99+
100+JSON link:
101+
102+` `[`https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json`](https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json)` `
103+
104+The following JSON also works with Sloeber. Insert this JSON link in the Arduino IDE under File \> Preferences:
105+
106+` `[`http://dan.drown.org/stm32duino/package_STM32duino_index.json`](http://dan.drown.org/stm32duino/package_STM32duino_index.json)` (Version 2018.4.2 has been tested.) `
107+
108+It is also best to activate the detailed output for compilation and upload. Then you can see any error uploading. Attention: Uploading is completed with the green bar which is displayed, even if an error occurred in the detailed report.
109+
110+Install the following in the board management:
111+
112+` stm32duino/STM32F1xx ... `
113+
114+Configure the board under Tools:
115+
116+- Board **Generic STM32F103 series**
117+- Serial Interface **No Serial**
118+- Upload method **STLINK**
119+
120+## First start
121+
122+Upload the Arduino blink sketch, then the LED PC13 should blink. Possibly. Set port to PC13.
123+
124+## Build Lazarus with FPCUPdeluxe
125+
126+### Build trunk
127+
128+Download FPCUPdeluxe-\[OS\] from <https://github.com/newpascal/fpcupdeluxe/releases> It is best to copy the download of FPCUPdeluxe into a new empty folder and then start the copied file in the file manager with a double click. Linux asks if the file should be made executable, answer yes. The FPCUPdeluxe GUI should then start.
129+
130+- Set the top left list boxes **FPC version** and **Lazarus version** to **trunk** or **embedded** which are both in the **Basic tab**.
131+- Build the Lazarus base trunk/embedded version with <span class="kbd">Install/update FPC+Laz</span>.
132+
133+If everything goes through without errors you get the current trunk from FPC and Lazarus. Normal desktop applications can now be created with this. There should be an icon for **Lazarus_fpcupdeluxe** on the Desktop.
134+
135+> **Note:** Note: SVN repositories may experience difficulties. If so, don't give up, just try again later ;-) What also helps, run FPCUPdeluxe twice in a row. An out of date FPCUPdeluxe can also cause difficulties. If something doesn't work, you can ask in the [Lazarus forum](https://forum.lazarus.freepascal.org/index.php/topic,34645.0.html), the author is very helpful.
136+
137+### Build ARM cross compiler
138+
139+Press the <span class="kbd">Setup+</span> button in the **Cross tab** to open a dialog with further options. Set the following there:
140+
141+- "Use jobs for GNU make": Remove hooks.
142+- CPU: **arm**
143+- OS: **embedded**
144+- Cross SubArch Overide: **armv7m**
145+- Close dialog with Ok .
146+
147+Select the following check boxes in the **Cross tab**:
148+
149+- CPU: **arm**
150+- OS: **embedded**
151+
152+Press the <span class="kbd">Install cross compiler</span> button, the cross compiler will be built. If everything worked correctly, exit FPCUPdeluxe.
153+
154+## Prepare Lazarus
155+
156+### Start with <span class="kbd">F9</span>
157+
158+If you try to start with <span class="kbd">F9</span>, you always get an error message. You can prevent this. Change the following in Lazarus:
159+
160+- "Tools \> Settings ... \> Keyboard layout \> Commands from the menu 'Start' \> Start program", set to Unknown.
161+- "Tools \> Settings ... \> Keyboard layout \> Commands from the menu 'Start' \> Start without debugger", remove the ticks from Shift and Ctrl.
162+
163+Now you can upload and start a program normally with <span class="kbd">F9</span>, but consider that the debugger is also disabled if you then write a normal PC program with Lazarus!
164+
165+### Code tools
166+
167+It may be that the code tools does not find the stm32f103xb unit. If so, then the following step is necessary:
168+
169+- "Tools \> Reread FPC source code directory"
170+
171+### Readable ASM code creation
172+
173+If you want to see the source as assembler code, the following step is necessary:
174+
175+- Enter "Project \> Project Settings ... \> User-defined settings \> User-defined settings" **-al**, this creates an *.s file that contains the program as ASM code.
176+
177+### Faulty optimization
178+
179+I have experienced a program not working properly because the compiler incorrectly optimised the code. So you should switch off code optimization with **{\$O-}** at the beginning of the program.
180+
181+## First project
182+
183+### Create a new project
184+
185+- "File -\> New ... \> Simple Program" - create a new project.
186+- "Project \> Project Settings ... \> Configuration and Goals" - set the following:
187+ - Target operating system (-T): **Embedded**
188+ - Target CPU family (-P): **arm**
189+ - Target processor (-Cp): **ARMV7M**
190+- "Project \> Project Settings ... \> Debug" : Remove the check mark "Generate debugger information for GDB" .
191+- "Project \> Project settings ... \> User-defined settings -\> User-defined settings still enter **-WpSTM32F103X8**.
192+- "Project \> Project Settings ... \> Compiler Commands -\> Execute After"
193+ - Remove the Compile and Recompile ticks.
194+ - Enter the following into the command: **st-flash write stm32Blink.bin 0x8000000**
195+
196+> **Note:** The paths still have to be adapted to your own installations.
197+
198+As a recommendation, I would take this project as a template and build all new projects on top of it. Setting the whole thing up for a new project each time is a bit tedious.
199+
200+### Start project
201+
202+Nothing should now be standing in the way of your first blink program (source below). Compile and upload the program with <span class="kbd">F9</span>. If everything worked, the LED at pin 13 should flash about every second.
203+
204+``` pascal
205+program stm32Blink;
206+
207+{$O-}
208+
209+procedure delay;
210+var
211+ i : uint32;
212+begin
213+ for i := 0 to 500000 do
214+ begin
215+ asm
216+ nop // empty instruction
217+ end;
218+ end;
219+end;
220+
221+begin
222+ // turn on Port C
223+ RCC.APB2ENR := RCC.APB2ENR or (%1 shl 4);
224+
225+ // Pin 13 from Port C to Output
226+ PortC.CRH := $00300000;
227+
228+ // Note: The LED lights up at LOW.
229+ while true do
230+ begin
231+ // Pin 13 - High
232+ PortC.BSRR := 1 shl 13;
233+ Delay;
234+
235+ // Pin 13 - Low
236+ PortC.BRR := 1 shl 13;
237+ Delay;
238+ end;
239+ end.
240+```
241+
242+## Manually with make
243+
244+Not tested.
245+
246+``` bash
247+ make clean all OS_TARGET=embedded CPU_TARGET=arm SUBARCH=armv7m
248+```
249+
250+## MSEide and MSEgui
251+
252+As an alternative to Lazarus you can also use the MSEide and MSEgui.
253+
254+The following tools are necessary:
255+
256+- <https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads> - GNU Arm Embedded Toolchain (gcc for ARM)
257+- <https://sourceforge.net/projects/mseide-msegui/files/fpccrossarmembedded/> - cross-compiler for ARM Embedded.
258+- <https://sourceforge.net/projects/mseide-msegui/files/mseide-msegui/4.6/> - mseGUI (download bin and sources)
259+- <https://gitlab.com/mseide-msegui/mseuniverse/tree/master/samples/embedded/arm/stm32l100c> - demo project
260+
261+Unzip these archives into a folder, eg /home/tux/STM3-FPC
262+
263+Start MSEide under /home/tux/STM32-FPC/msegui/bin.
264+
265+- Settings \> Configure MSEide
266+ - adjust the following path: \${MSEDIR} - /home/tux/STM32-FPC/msegui/
267+- Project \> open - Open the demo project in the extracted folder. (/home/tux/STM32-FPC/samples/embedded/arm/stm32l100c/stm32l100c.prj)
268+- Project \> Options \> Macros, adjust the following paths:
269+ - TOOLCHAINDIR - /home/tux/STM32-FPC/gcc-arm-none-eabi-7-2017-q4-major/
270+ - CROSSFPCDIR - / home/tux/STM32-FPC/crossfpc-x86_64_linux_eabi_embedded_3_1_1/
271+
272+If a red break point is set in the left column, deactivate it with a click. Start the program with <span class="kbd">F9</span>. If everything went well, this should work without errors.
273+
274+# See also
275+
276+- [[ARM Embedded Tutorials]] - Overview
277+
278+---
279+*From the Free Pascal wiki, page "ARM Embedded Tutorial - Entry FPC and STM32", as it stood on 2021-02-01 after a change by Trev, 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).*
280+Category: ARM
281+Category: Embedded
282+Category: Tutorials

Back to history