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