Sign in Join
Wiki copytemporary

ARM Embedded Tutorial - Entry FPC and STM32: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11# Introduction to STM32 and FPC
22
33This tutorial was tested using 64 bit Linux Mint.
44
55## Driver for ST-LINK V2
66
77### Linux
88
99Download the package as a zip with **clone or download** from <https://github.com/texane/stlink>
1010
1111Follow the instructions at <https://github.com/texane/stlink/blob/master/doc/compiling.md>
1212
1313The following packages must be installed:
1414
1515- CMake (minimum v2.8.7)
1616- C compiler (gcc, clang, mingw)
1717- Libusb 1.0 (minimum v1.0.9)
1818
1919``` bash
2020 sudo apt-get install cmake
2121 sudo apt-get install gcc
2222 sudo apt-get install libusb-1.0-0.dev
2323 // sudo apt-get install libusb-dev // ???
2424```
2525
2626Compile and install stlink:
2727
2828``` bash
2929 $ make release
3030 $ make debug
3131
3232 $ mkdir build # may already exist.
3333 $ cd build
3434 $ cmake -DCMAKE_BUILD_TYPE = Debug ..
3535 $ make
3636```
3737
3838To install in your home directory:
3939
4040``` bash
4141 $ cd build/release
4242 $ make install DESTDIR=$HOME
4343```
4444
4545To install for all users:
4646
4747``` bash
4848 $ cd build/release
4949 $ sudo make install
5050```
5151
5252Reboot or:
5353
5454``` bash
5555 $ sudo udevadm control --reload-rules
5656 $ sudo udevadm trigger
5757```
5858
5959Then there should be a device file /dev/stlinkv2_3 or similar.
6060
6161Run the following as a test:
6262
63-` st-util`
63+```
64+ st-util
65+```
6466
6567This should produce several lines of information, including how much SRAM the controller has. You can then cancel it with Ctrl+C.
6668
6769If st-util aborts with the following error:
6870
69-` st-util: error while loading shared libraries: libstlink.so.1: cannot open shared object file: No such file or directory`
71+``` pascal
72+ st-util: error while loading shared libraries: libstlink.so.1: cannot open shared object file: No such file or directory
73+```
7074
7175This is probably the problem: <https://github.com/texane/stlink/issues/700> so run the following:
7276
7377``` bash
7478 $ sudo ldconfig
7579```
7680
7781### Windows/macOS
7882
79-` `[`https://github.com/texane/stlink/releases/tag/1.3.0`](https://github.com/texane/stlink/releases/tag/1.3.0)` (not tested) `
83+```
84+ https://github.com/texane/stlink/releases/tag/1.3.0 (not tested)
85+```
8086
8187## Hardware connections
8288
8389Connect the ST-LINK V2 to the STM32F103C as follows:
8490
8591| ST LINK V2 | -\> | STM32F103C |
8692|------------|-----|------------|
8793| SWDIO | -\> | SWO |
8894| GND | -\> | GND |
8995| SWCLK | -\> | SWCLK |
9096| 3.3V | -\> | 3.3V |
9197
9298## Set up Arduino IDE (optimal)
9399
94100This is not absolutely necessary, but it has the advantage that you can look up how problems have been solved there, eg I²C access.
95101
96102Note the following page (does not work with Sloeber (Eclipse for Arduino))
97103
98-` `[`https://github.com/stm32duino/Arduino_Core_STM32`](https://github.com/stm32duino/Arduino_Core_STM32)` `
104+```
105+ https://github.com/stm32duino/Arduino_Core_STM32
106+```
99107
100108JSON link:
101109
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)` `
110+```
111+ https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json
112+```
103113
104114The following JSON also works with Sloeber. Insert this JSON link in the Arduino IDE under File \> Preferences:
105115
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.) `
116+```
117+ http://dan.drown.org/stm32duino/package_STM32duino_index.json (Version 2018.4.2 has been tested.)
118+```
107119
108120It 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.
109121
110122Install the following in the board management:
111123
112-` stm32duino/STM32F1xx ... `
124+```
125+ stm32duino/STM32F1xx ...
126+```
113127
114128Configure the board under Tools:
115129
116130- Board **Generic STM32F103 series**
117131- Serial Interface **No Serial**
118132- Upload method **STLINK**
119133
120134## First start
121135
122136Upload the Arduino blink sketch, then the LED PC13 should blink. Possibly. Set port to PC13.
123137
124138## Build Lazarus with FPCUPdeluxe
125139
126140### Build trunk
127141
128142Download 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.
129143
130144- Set the top left list boxes **FPC version** and **Lazarus version** to **trunk** or **embedded** which are both in the **Basic tab**.
131145- Build the Lazarus base trunk/embedded version with `Install/update FPC+Laz`.
132146
133147If 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.
134148
135149> **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.
136150
137151### Build ARM cross compiler
138152
139153Press the `Setup+` button in the **Cross tab** to open a dialog with further options. Set the following there:
140154
141155- "Use jobs for GNU make": Remove hooks.
142156- CPU: **arm**
143157- OS: **embedded**
144158- Cross SubArch Overide: **armv7m**
145159- Close dialog with Ok .
146160
147161Select the following check boxes in the **Cross tab**:
148162
149163- CPU: **arm**
150164- OS: **embedded**
151165
152166Press the `Install cross compiler` button, the cross compiler will be built. If everything worked correctly, exit FPCUPdeluxe.
153167
154168## Prepare Lazarus
155169
156170### Start with `F9`
157171
158172If you try to start with `F9`, you always get an error message. You can prevent this. Change the following in Lazarus:
159173
160174- "Tools \> Settings ... \> Keyboard layout \> Commands from the menu 'Start' \> Start program", set to Unknown.
161175- "Tools \> Settings ... \> Keyboard layout \> Commands from the menu 'Start' \> Start without debugger", remove the ticks from Shift and Ctrl.
162176
163177Now you can upload and start a program normally with `F9`, but consider that the debugger is also disabled if you then write a normal PC program with Lazarus!
164178
165179### Code tools
166180
167181It may be that the code tools does not find the stm32f103xb unit. If so, then the following step is necessary:
168182
169183- "Tools \> Reread FPC source code directory"
170184
171185### Readable ASM code creation
172186
173187If you want to see the source as assembler code, the following step is necessary:
174188
175189- Enter "Project \> Project Settings ... \> User-defined settings \> User-defined settings" **-al**, this creates an *.s file that contains the program as ASM code.
176190
177191### Faulty optimization
178192
179193I 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.
180194
181195## First project
182196
183197### Create a new project
184198
185199- "File -\> New ... \> Simple Program" - create a new project.
186200- "Project \> Project Settings ... \> Configuration and Goals" - set the following:
187201 - Target operating system (-T): **Embedded**
188202 - Target CPU family (-P): **arm**
189203 - Target processor (-Cp): **ARMV7M**
190204- "Project \> Project Settings ... \> Debug" : Remove the check mark "Generate debugger information for GDB" .
191205- "Project \> Project settings ... \> User-defined settings -\> User-defined settings still enter **-WpSTM32F103X8**.
192206- "Project \> Project Settings ... \> Compiler Commands -\> Execute After"
193207 - Remove the Compile and Recompile ticks.
194208 - Enter the following into the command: **st-flash write stm32Blink.bin 0x8000000**
195209
196210> **Note:** The paths still have to be adapted to your own installations.
197211
198212As 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.
199213
200214### Start project
201215
202216Nothing should now be standing in the way of your first blink program (source below). Compile and upload the program with `F9`. If everything worked, the LED at pin 13 should flash about every second.
203217
204218``` pascal
205219program stm32Blink;
206220
207221{$O-}
208222
209223procedure delay;
210224var
211225 i : uint32;
212226begin
213227 for i := 0 to 500000 do
214228 begin
215229 asm
216230 nop // empty instruction
217231 end;
218232 end;
219233end;
220234
221235begin
222236 // turn on Port C
223237 RCC.APB2ENR := RCC.APB2ENR or (%1 shl 4);
224238
225239 // Pin 13 from Port C to Output
226240 PortC.CRH := $00300000;
227241
228242 // Note: The LED lights up at LOW.
229243 while true do
230244 begin
231245 // Pin 13 - High
232246 PortC.BSRR := 1 shl 13;
233247 Delay;
234248
235249 // Pin 13 - Low
236250 PortC.BRR := 1 shl 13;
237251 Delay;
238252 end;
239253 end.
240254```
241255
242256## Manually with make
243257
244258Not tested.
245259
246260``` bash
247261 make clean all OS_TARGET=embedded CPU_TARGET=arm SUBARCH=armv7m
248262```
249263
250264## MSEide and MSEgui
251265
252266As an alternative to Lazarus you can also use the MSEide and MSEgui.
253267
254268The following tools are necessary:
255269
256270- <https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads> - GNU Arm Embedded Toolchain (gcc for ARM)
257271- <https://sourceforge.net/projects/mseide-msegui/files/fpccrossarmembedded/> - cross-compiler for ARM Embedded.
258272- <https://sourceforge.net/projects/mseide-msegui/files/mseide-msegui/4.6/> - mseGUI (download bin and sources)
259273- <https://gitlab.com/mseide-msegui/mseuniverse/tree/master/samples/embedded/arm/stm32l100c> - demo project
260274
261275Unzip these archives into a folder, eg /home/tux/STM3-FPC
262276
263277Start MSEide under /home/tux/STM32-FPC/msegui/bin.
264278
265279- Settings \> Configure MSEide
266280 - adjust the following path: \${MSEDIR} - /home/tux/STM32-FPC/msegui/
267281- Project \> open - Open the demo project in the extracted folder. (/home/tux/STM32-FPC/samples/embedded/arm/stm32l100c/stm32l100c.prj)
268282- Project \> Options \> Macros, adjust the following paths:
269283 - TOOLCHAINDIR - /home/tux/STM32-FPC/gcc-arm-none-eabi-7-2017-q4-major/
270284 - CROSSFPCDIR - / home/tux/STM32-FPC/crossfpc-x86_64_linux_eabi_embedded_3_1_1/
271285
272286If a red break point is set in the left column, deactivate it with a click. Start the program with `F9`. If everything went well, this should work without errors.
273287
274288# See also
275289
276290- [[ARM Embedded Tutorials]] - Overview
277291
278292---
279293*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).*
280294Category: ARM
281295Category: Embedded
282296Category: Tutorials

Back to history