Sign in Join
Wiki copytemporary

ARM Embedded Tutorial - Entry FPC and STM32: what changed

Revision 1 to 2. 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
6363` st-util`
6464
6565This should produce several lines of information, including how much SRAM the controller has. You can then cancel it with Ctrl+C.
6666
6767If st-util aborts with the following error:
6868
6969` st-util: error while loading shared libraries: libstlink.so.1: cannot open shared object file: No such file or directory`
7070
7171This is probably the problem: <https://github.com/texane/stlink/issues/700> so run the following:
7272
7373``` bash
7474 $ sudo ldconfig
7575```
7676
7777### Windows/macOS
7878
7979` `[`https://github.com/texane/stlink/releases/tag/1.3.0`](https://github.com/texane/stlink/releases/tag/1.3.0)` (not tested) `
8080
8181## Hardware connections
8282
8383Connect the ST-LINK V2 to the STM32F103C as follows:
8484
8585| ST LINK V2 | -\> | STM32F103C |
8686|------------|-----|------------|
8787| SWDIO | -\> | SWO |
8888| GND | -\> | GND |
8989| SWCLK | -\> | SWCLK |
9090| 3.3V | -\> | 3.3V |
9191
9292## Set up Arduino IDE (optimal)
9393
9494This is not absolutely necessary, but it has the advantage that you can look up how problems have been solved there, eg I²C access.
9595
9696Note the following page (does not work with Sloeber (Eclipse for Arduino))
9797
9898` `[`https://github.com/stm32duino/Arduino_Core_STM32`](https://github.com/stm32duino/Arduino_Core_STM32)` `
9999
100100JSON link:
101101
102102` `[`https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json`](https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json)` `
103103
104104The following JSON also works with Sloeber. Insert this JSON link in the Arduino IDE under File \> Preferences:
105105
106106` `[`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.) `
107107
108108It 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.
109109
110110Install the following in the board management:
111111
112112` stm32duino/STM32F1xx ... `
113113
114114Configure the board under Tools:
115115
116116- Board **Generic STM32F103 series**
117117- Serial Interface **No Serial**
118118- Upload method **STLINK**
119119
120120## First start
121121
122122Upload the Arduino blink sketch, then the LED PC13 should blink. Possibly. Set port to PC13.
123123
124124## Build Lazarus with FPCUPdeluxe
125125
126126### Build trunk
127127
128128Download 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.
129129
130130- 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>.
131+- Build the Lazarus base trunk/embedded version with `Install/update FPC+Laz`.
132132
133133If 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.
134134
135135> **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.
136136
137137### Build ARM cross compiler
138138
139-Press the <span class="kbd">Setup+</span> button in the **Cross tab** to open a dialog with further options. Set the following there:
139+Press the `Setup+` button in the **Cross tab** to open a dialog with further options. Set the following there:
140140
141141- "Use jobs for GNU make": Remove hooks.
142142- CPU: **arm**
143143- OS: **embedded**
144144- Cross SubArch Overide: **armv7m**
145145- Close dialog with Ok .
146146
147147Select the following check boxes in the **Cross tab**:
148148
149149- CPU: **arm**
150150- OS: **embedded**
151151
152-Press the <span class="kbd">Install cross compiler</span> button, the cross compiler will be built. If everything worked correctly, exit FPCUPdeluxe.
152+Press the `Install cross compiler` button, the cross compiler will be built. If everything worked correctly, exit FPCUPdeluxe.
153153
154154## Prepare Lazarus
155155
156-### Start with <span class="kbd">F9</span>
156+### Start with `F9`
157157
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:
158+If you try to start with `F9`, you always get an error message. You can prevent this. Change the following in Lazarus:
159159
160160- "Tools \> Settings ... \> Keyboard layout \> Commands from the menu 'Start' \> Start program", set to Unknown.
161161- "Tools \> Settings ... \> Keyboard layout \> Commands from the menu 'Start' \> Start without debugger", remove the ticks from Shift and Ctrl.
162162
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!
163+Now 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!
164164
165165### Code tools
166166
167167It may be that the code tools does not find the stm32f103xb unit. If so, then the following step is necessary:
168168
169169- "Tools \> Reread FPC source code directory"
170170
171171### Readable ASM code creation
172172
173173If you want to see the source as assembler code, the following step is necessary:
174174
175175- Enter "Project \> Project Settings ... \> User-defined settings \> User-defined settings" **-al**, this creates an *.s file that contains the program as ASM code.
176176
177177### Faulty optimization
178178
179179I 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.
180180
181181## First project
182182
183183### Create a new project
184184
185185- "File -\> New ... \> Simple Program" - create a new project.
186186- "Project \> Project Settings ... \> Configuration and Goals" - set the following:
187187 - Target operating system (-T): **Embedded**
188188 - Target CPU family (-P): **arm**
189189 - Target processor (-Cp): **ARMV7M**
190190- "Project \> Project Settings ... \> Debug" : Remove the check mark "Generate debugger information for GDB" .
191191- "Project \> Project settings ... \> User-defined settings -\> User-defined settings still enter **-WpSTM32F103X8**.
192192- "Project \> Project Settings ... \> Compiler Commands -\> Execute After"
193193 - Remove the Compile and Recompile ticks.
194194 - Enter the following into the command: **st-flash write stm32Blink.bin 0x8000000**
195195
196196> **Note:** The paths still have to be adapted to your own installations.
197197
198198As 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.
199199
200200### Start project
201201
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.
202+Nothing 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.
203203
204204``` pascal
205205program stm32Blink;
206206
207207{$O-}
208208
209209procedure delay;
210210var
211211 i : uint32;
212212begin
213213 for i := 0 to 500000 do
214214 begin
215215 asm
216216 nop // empty instruction
217217 end;
218218 end;
219219end;
220220
221221begin
222222 // turn on Port C
223223 RCC.APB2ENR := RCC.APB2ENR or (%1 shl 4);
224224
225225 // Pin 13 from Port C to Output
226226 PortC.CRH := $00300000;
227227
228228 // Note: The LED lights up at LOW.
229229 while true do
230230 begin
231231 // Pin 13 - High
232232 PortC.BSRR := 1 shl 13;
233233 Delay;
234234
235235 // Pin 13 - Low
236236 PortC.BRR := 1 shl 13;
237237 Delay;
238238 end;
239239 end.
240240```
241241
242242## Manually with make
243243
244244Not tested.
245245
246246``` bash
247247 make clean all OS_TARGET=embedded CPU_TARGET=arm SUBARCH=armv7m
248248```
249249
250250## MSEide and MSEgui
251251
252252As an alternative to Lazarus you can also use the MSEide and MSEgui.
253253
254254The following tools are necessary:
255255
256256- <https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads> - GNU Arm Embedded Toolchain (gcc for ARM)
257257- <https://sourceforge.net/projects/mseide-msegui/files/fpccrossarmembedded/> - cross-compiler for ARM Embedded.
258258- <https://sourceforge.net/projects/mseide-msegui/files/mseide-msegui/4.6/> - mseGUI (download bin and sources)
259259- <https://gitlab.com/mseide-msegui/mseuniverse/tree/master/samples/embedded/arm/stm32l100c> - demo project
260260
261261Unzip these archives into a folder, eg /home/tux/STM3-FPC
262262
263263Start MSEide under /home/tux/STM32-FPC/msegui/bin.
264264
265265- Settings \> Configure MSEide
266266 - adjust the following path: \${MSEDIR} - /home/tux/STM32-FPC/msegui/
267267- Project \> open - Open the demo project in the extracted folder. (/home/tux/STM32-FPC/samples/embedded/arm/stm32l100c/stm32l100c.prj)
268268- Project \> Options \> Macros, adjust the following paths:
269269 - TOOLCHAINDIR - /home/tux/STM32-FPC/gcc-arm-none-eabi-7-2017-q4-major/
270270 - CROSSFPCDIR - / home/tux/STM32-FPC/crossfpc-x86_64_linux_eabi_embedded_3_1_1/
271271
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.
272+If 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.
273273
274274# See also
275275
276276- [[ARM Embedded Tutorials]] - Overview
277277
278278---
279279*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).*
280280Category: ARM
281281Category: Embedded
282282Category: Tutorials

Back to history