Projekt

Obecné

Profil

STM32 UNO Lab 01 GPIO LEDs » Historie » Verze 1

Ján Hýbl, 2026-10-30 12:52

1 1 Ján Hýbl
h1. STM32-UNO - Lab 01: GPIO Configuration and LED Control
2
3
*Course:* F7ABBMTB
4
*Platform:* STM32-UNO
5
*Microcontroller:* STM32F446RET6 (STM32F4, Arm Cortex-M4)
6
*Tools:* STM32CubeMX 6.x, STM32CubeIDE 2.x, STM32 HAL, STLINK-V3MODS
7
*Language:* C
8
*Exercise type:* Introduction to embedded programming
9
*Revision:* 1.0 (30 October 2026)
10
11
[[STM32_UNO|Back to STM32-UNO documentation]]
12
13
{{toc}}
14
15
h2. 1. Learning objectives
16
17
This laboratory exercise introduces the STM32 development workflow. Students will configure two general-purpose digital outputs (GPIO), generate initialization code, write their first C program, compile it, and program the STM32-UNO board through the SWD interface using STLINK-V3MODS.
18
19
By the end of this exercise, students should be able to:
20
21
# Create a microcontroller project in STM32CubeMX.
22
# Configure GPIO pins as push-pull digital outputs.
23
# Generate source code using the STM32 HAL library.
24
# Edit and compile an application in STM32CubeIDE.
25
# Connect and use an STLINK-V3MODS programmer/debugger.
26
# Verify the operation of two digital outputs using the board's LED indicators.
27
28
h2. 2. Required equipment
29
30
* STM32-UNO development board with an STM32F446RET6 microcontroller.
31
* STLINK-V3MODS debugger/programmer and a suitable breakout/adapter for its 32 castellated contacts.
32
* SWD wiring or a verified programming adapter for STM32-UNO.
33
* USB cable for the programmer and the appropriate power cable for the STM32-UNO board.
34
* Windows or Linux computer with STM32CubeMX and STM32CubeIDE installed.
35
* Optional: multimeter to check logic levels and wiring.
36
37
*Important:* The STM32-UNO GPIO signals use 3.3 V logic. Do not connect an external 5 V output directly to a GPIO pin.
38
39
h2. 3. Software installation
40
41
Install the following software:
42
43
|_. Software |_. Purpose |_. Official download |
44
| STM32CubeMX (classic 6.x) | Microcontroller pinout, peripheral and clock configuration, generation of STM32CubeIDE projects | "STM32CubeMX":https://www.st.com/en/development-tools/stm32cubemx.html |
45
| STM32CubeIDE (Eclipse-based 2.x) | C editing, build, flashing and debugging | "STM32CubeIDE":https://www.st.com/en/development-tools/stm32cubeide.html |
46
| STM32CubeProgrammer (optional) | Independent flash programming and ST-LINK diagnostics | "STM32CubeProgrammer":https://www.st.com/en/development-tools/stm32cubeprog.html |
47
48
Since STM32CubeIDE 2.0, MCU configuration is carried out in the standalone STM32CubeMX application, not inside STM32CubeIDE. This guide uses the *classic STM32CubeMX 6.x* workflow with @Toolchain / IDE = STM32CubeIDE@. The separate *STM32CubeMX2* application uses a different project-generation/import workflow and is not covered by this exercise.
49
50
h2. 4. Select the two outputs
51
52
Use the two digital positions *D13* and *D12* on the Arduino-compatible STM32-UNO header. These are the intended LED control points for this introductory exercise.
53
54
|_. Board signal |_. MCU GPIO used in this draft |_. CubeMX user label |_. Mode |
55
| D13 / LED 1 | PA5 | LED_D13 | GPIO_Output |
56
| D12 / LED 2 | PA6 | LED_D12 | GPIO_Output |
57
58
*Instructor validation required before practical use:* D13 -> PA5 and D12 -> PA6 are the pin assignments of the reference NUCLEO-F446RE board. This mapping has *not yet been confirmed* against the STM32-UNO electrical schematic. Before the exercise, the instructor must check the actual STM32-UNO DIO-to-MCU connections and LED circuit. If they differ, replace the GPIO selections in this table and in the STM32CubeMX configuration. The user labels may remain @LED_D13@ and @LED_D12@, provided they match the physical board connectors.
59
60
The LED colors and their active logic levels also depend on the actual STM32-UNO circuit. A GPIO output set to HIGH is not necessarily the same as an illuminated LED.
61
62
h2. 5. Create the project in STM32CubeMX
63
64
h3. Step 1 - Launch STM32CubeMX
65
66
# Start @STM32CubeMX@.
67
# Select @File -> New Project@.
68
# Open the @MCU/MPU Selector@ tab.
69
70
h3. Step 2 - Select the MCU
71
72
# Enter @STM32F446RET6@ into the search field.
73
# Select the exact microcontroller model from the search results.
74
# Confirm the selection to open the device pinout configuration.
75
76
The target MCU is an STM32F446RET6 featuring an Arm Cortex-M4 processor, a maximum CPU clock of 180 MHz, 512 KB of Flash memory, and 128 KB of main SRAM.
77
78
h3. Step 3 - Enable SWD debugging
79
80
# Open @System Core -> SYS@.
81
# Set @Debug@ to @Serial Wire@.
82
# Keep @PA13 (SWDIO)@ and @PA14 (SWCLK)@ reserved for the debugger.
83
84
Do not reassign SWD pins to GPIO in this laboratory exercise.
85
86
h3. Step 4 - Configure the system clock
87
88
For the first exercise, keep a simple default clock configuration based on the MCU's internal HSI oscillator (16 MHz). There is no need to configure a high-speed external oscillator or the PLL for this task.
89
90
Do not enable HSE unless the actual board hardware and clock configuration have been verified. The STM32-UNO BOM indicates a 32.768 kHz low-speed crystal; that is not the same as an HSE crystal.
91
92
h2. 6. Configure GPIO outputs
93
94
# In @Pinout & Configuration@, select the first verified MCU GPIO for D13 and set it to @GPIO_Output@ (PA5 in the draft mapping).
95
# Select the second verified MCU GPIO for D12 and set it to @GPIO_Output@ (PA6 in the draft mapping).
96
# Open @System Core -> GPIO@.
97
# Configure the parameters shown below.
98
99
|_. Parameter |_. D13 output |_. D12 output |
100
| GPIO mode | Output Push Pull | Output Push Pull |
101
| Pull-up / Pull-down | No pull-up and no pull-down | No pull-up and no pull-down |
102
| Maximum output speed | Low | Low |
103
| GPIO output level | Low | Low |
104
| User label | LED_D13 | LED_D12 |
105
106
@Output Push Pull@ actively drives either LOW (approximately 0 V) or HIGH (approximately 3.3 V). The @Low@ speed setting is sufficient for LEDs and avoids unnecessarily fast transitions.
107
108
After code generation, the user labels result in symbols similar to the following, normally defined in @Core/Inc/main.h@:
109
110
<pre><code class="c">
111
LED_D13_Pin
112
LED_D13_GPIO_Port
113
LED_D12_Pin
114
LED_D12_GPIO_Port
115
</code></pre>
116
117
h2. 7. Generate the STM32CubeIDE project
118
119
# Open the @Project Manager@ tab in STM32CubeMX.
120
# Set @Project Name@ to @STM32_UNO_LED@.
121
# Choose a suitable project directory.
122
# Set @Toolchain / IDE@ to @STM32CubeIDE@.
123
# Use the @STM32Cube FW_F4@ firmware package and HAL drivers.
124
# Click @GENERATE CODE@.
125
# If prompted to download the STM32CubeF4 firmware package, approve the download.
126
127
h3. Import the generated project into STM32CubeIDE
128
129
# Start STM32CubeIDE.
130
# Select @File -> Import@.
131
# Choose @Import STM32 Project -> STM32CubeMX/STM32CubeIDE Project@ (the exact wording may vary slightly between versions).
132
# Select the directory containing the project generated by STM32CubeMX.
133
# Complete the import and wait for project indexing.
134
# Check that the project contains @Core/Src/main.c@, @Core/Inc/main.h@, and the STM32 HAL driver files.
135
136
If the @Build Project@ action is unavailable, recheck that the project was generated for @STM32CubeIDE@ and imported using the STM32-specific importer, rather than as an arbitrary directory.
137
138
h2. 8. Write the first program
139
140
Open @Core/Src/main.c@ and find the @while (1)@ loop in the @main()@ function. Paste only the *statements* below into the section bounded by @/* USER CODE BEGIN 3 */@ and @/* USER CODE END 3 */@ within the existing loop. Do not copy or duplicate the @while (1)@ loop itself.
141
142
h3. Task A - Set both outputs HIGH
143
144
<pre><code class="c">
145
HAL_GPIO_WritePin(LED_D13_GPIO_Port,
146
                  LED_D13_Pin,
147
                  GPIO_PIN_SET);
148
149
HAL_GPIO_WritePin(LED_D12_GPIO_Port,
150
                  LED_D12_Pin,
151
                  GPIO_PIN_SET);
152
</code></pre>
153
154
After the program starts, both selected outputs remain at logical HIGH. Depending on how the onboard LEDs are connected, HIGH may turn on one LED color, turn off another, or produce a different visible indication.
155
156
h3. Task B - Alternate the two outputs
157
158
Replace the statements from Task A with the following code inside the same user-code section:
159
160
<pre><code class="c">
161
/* State 1: D13 = HIGH, D12 = LOW */
162
HAL_GPIO_WritePin(LED_D13_GPIO_Port,
163
                  LED_D13_Pin,
164
                  GPIO_PIN_SET);
165
HAL_GPIO_WritePin(LED_D12_GPIO_Port,
166
                  LED_D12_Pin,
167
                  GPIO_PIN_RESET);
168
HAL_Delay(500);
169
170
/* State 2: D13 = LOW, D12 = HIGH */
171
HAL_GPIO_WritePin(LED_D13_GPIO_Port,
172
                  LED_D13_Pin,
173
                  GPIO_PIN_RESET);
174
HAL_GPIO_WritePin(LED_D12_GPIO_Port,
175
                  LED_D12_Pin,
176
                  GPIO_PIN_SET);
177
HAL_Delay(500);
178
</code></pre>
179
180
The program sets opposite states on the two outputs, then reverses them every 500 ms. One complete cycle takes 1000 ms (1 second).
181
182
*Why use USER CODE sections?* STM32CubeMX regenerates initialization code when the hardware configuration changes. Custom application code stored outside the designated @USER CODE BEGIN@ / @USER CODE END@ sections may be overwritten.
183
184
h2. 9. Connect the STLINK-V3MODS programmer
185
186
The STLINK-V3MODS communicates with the STM32 microcontroller over SWD. Unlike a typical ST-LINK dongle with a cable header, STLINK-V3MODS has a 32-contact castellated edge connection intended for an adapter or host PCB. Use a properly designed breakout; do not assume it has the STDC14 pinout of STLINK-V3MINI.
187
188
h3. SWD signal mapping
189
190
|_. STLINK-V3MODS contact |_. Signal |_. STM32F446RET6 target |
191
| 4 | T_SWDIO | PA13 (SWDIO) |
192
| 13 | T_SWCLK | PA14 (SWCLK) |
193
| 8 | GND | GND |
194
| 30 | T_VCC (input) | 3.3 V target voltage reference |
195
| 31 | T_NRST | NRST (recommended) |
196
| 5 | GNDDetect | Optional connection detection; consult adapter design |
197
198
*Power and wiring precautions:*
199
200
* Power the STM32-UNO via its approved USB power input or another verified board power source.
201
* Connect the target GND and programmer GND together.
202
* STLINK-V3MODS contact 30 (@T_VCC@) is a *voltage-sense input*, not a 3.3 V output.
203
* Contact 22 of STLINK-V3MODS is a separate 5 V output. *Do not connect it to the board's 3.3 V rail* in this exercise.
204
* For @Connect Under Reset@ to work, connect the NRST signal.
205
* Confirm all connections against the STM32-UNO electrical schematic and the STLINK-V3MODS user manual (UM2502) *before powering the assembly*.
206
207
*Instructor validation required:* The physical STM32-UNO SWD connection point and its contact numbering have not been established from the available PCB renders, Gerber files, and BOM. The instructor must document the board-side SWD connector or test pads and provide a verified adapter before students perform this step.
208
209
h2. 10. Build and flash the firmware
210
211
# Connect the powered STM32-UNO to the STLINK-V3MODS through the verified SWD adapter.
212
# Connect STLINK-V3MODS to the computer using USB.
213
# In STM32CubeIDE, save @main.c@ (@Ctrl+S@).
214
# Choose @Project -> Build Project@ or click the hammer icon.
215
# Check the console: the build should complete with *0 errors*.
216
# Open @Run -> Debug Configurations@.
217
# Select or create the STM32 debug configuration for @STM32_UNO_LED@.
218
# Select the @ST-LINK@ debugger and the @SWD@ interface.
219
# Use a conservative SWD clock (for example, around 1 MHz) if the adapter or connection is new.
220
# Click @Debug@. The IDE should program the target Flash memory and enter the debug session.
221
# If the CPU is paused at @main()@, click @Resume@ (typically @F8@) to execute the firmware.
222
223
Subsequent firmware changes can be built and downloaded through the same debug configuration.
224
225
h2. 11. Verify the result
226
227
For Task B, verify the following sequence:
228
229
|_. Time in the cycle |_. D13 |_. D12 |
230
| 0-500 ms | HIGH (3.3 V) | LOW (0 V) |
231
| 500-1000 ms | LOW (0 V) | HIGH (3.3 V) |
232
| Repeats every 1000 ms | HIGH / LOW alternating | LOW / HIGH alternating |
233
234
Observe the corresponding onboard LEDs. Their *colors and on/off polarity are board-dependent*; the electrical output states in the table are the primary acceptance criterion. If necessary, verify the output voltage using a multimeter connected between a DIO pin and GND.
235
236
h2. 12. Troubleshooting
237
238
|_. Problem |_. Suggested checks |
239
| @No ST-LINK detected@ | USB data cable, operating-system drivers, STLINK-V3MODS firmware and USB connection. |
240
| @No target detected@ | Board power, common GND, SWDIO, SWCLK, T_VCC voltage reference, verified adapter wiring. |
241
| SWD connection fails | Reduce SWD frequency; verify SYS/Serial Wire configuration; try Connect Under Reset if NRST is wired. |
242
| Flash programming succeeds, but the program does not run | Resume execution from a breakpoint; confirm the firmware was built for STM32F446RET6. |
243
| Output pins do not change | Verify MCU-to-DIO mapping and that GPIO initialization is generated. |
244
| LEDs behave opposite to expectations | Inspect the LED driver circuit; the indicator may be active-low or may contain two LED colors. |
245
| Application enters @Error_Handler()@ | Verify the clock configuration; avoid enabling an HSE oscillator that is not fitted. |
246
247
h2. 13. Independent assignments
248
249
After completing Task B:
250
251
# Change the delay from 500 ms to 100 ms and verify the new period.
252
# Modify the program so that both outputs are always in the same logic state and switch together.
253
# Create two independent blinking periods: D13 changes state every 200 ms and D12 every 1000 ms. Solve this extension without relying on a single blocking @HAL_Delay()@ sequence; consider @HAL_GetTick()@.
254
255
h2. 14. Completion checklist
256
257
* [ ] The STM32CubeMX project targets the correct MCU.
258
* [ ] Two verified STM32-UNO DIO signals are configured as GPIO outputs.
259
* [ ] STM32CubeIDE builds the project with no errors.
260
* [ ] STLINK-V3MODS successfully programs the STM32-UNO over SWD.
261
* [ ] Both outputs switch with the expected timing.
262
* [ ] The student can explain @GPIO_PIN_SET@, @GPIO_PIN_RESET@ and @HAL_Delay()@.
263
264
h2. 15. References
265
266
* "STM32CubeMX - STMicroelectronics":https://www.st.com/en/development-tools/stm32cubemx.html
267
* "STM32CubeIDE - STMicroelectronics":https://www.st.com/en/development-tools/stm32cubeide.html
268
* "STM32CubeProgrammer - STMicroelectronics":https://www.st.com/en/development-tools/stm32cubeprog.html
269
* "STM32F446xC/E datasheet - STMicroelectronics":https://www.st.com/resource/en/datasheet/stm32f446re.pdf
270
* "UM2502 - STLINK-V3MODS / STLINK-V3MINI user manual":https://www.st.com/resource/en/user_manual/dm00555046-stlink-v3mods-and-stlink-v3mini-mini-debuggers-programmers-for-stm32-stmicroelectronics.pdf
271
* "STM32CubeIDE 2.0 - separate STM32CubeMX workflow":https://community.st.com/stm32cubeide-mcus-28/stm32cubeide-2-0-0-released-159542
272
273
h2. Instructor notes (not part of the assessed student task)
274
275
Before releasing this lesson as an executable wiring exercise, verify and update: (1) the actual D13/D12 to MCU GPIO mapping; (2) the onboard LED electrical polarity and any required LED jumpers; (3) the physical STM32-UNO SWD pad/header location and cable pin numbering. Keep this note visible until all three items have been validated on real hardware.