Sign in Join
Wiki copytemporary

ARM Embedded Tutorial - Entry FPC and STM32: what changed

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

Back to history