Projekt

Obecné

Profil

Akce

STM32 UNO Lab 01 GPIO LEDs » Historie » Revize 4

« Předchozí | Revize 4/7 (rozdíl) | Další »
Ján Hýbl, 2026-10-30 15:13


STM32-UNO - Lab 01: GPIO Configuration and LED Control

Course: F7ABBMTB
Platform: STM32-UNO
Microcontroller: STM32F446RET6 (STM32F4, Arm Cortex-M4)
Tools: STM32CubeMX 6.x, STM32CubeIDE 2.x, STM32 HAL, STLINK-V3MODS
Language: C
Exercise type: Introduction to embedded programming
Revision: 1.0 (30 October 2026)

Back to STM32-UNO documentation

1. Learning objectives

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.

By the end of this exercise, students should be able to:

  1. Create a microcontroller project in STM32CubeMX.
  2. Configure GPIO pins as push-pull digital outputs.
  3. Generate source code using the STM32 HAL library.
  4. Edit and compile an application in STM32CubeIDE.
  5. Connect and use an STLINK-V3MODS programmer/debugger.
  6. Verify the operation of two digital outputs using the board's LED indicators.

2. Required equipment

  • STM32-UNO development board with an STM32F446RET6 microcontroller.
  • STLINK-V3MODS debugger/programmer and a suitable breakout/adapter for its 32 castellated contacts.
  • SWD wiring or a verified programming adapter for STM32-UNO.
  • USB cable for the programmer and the appropriate power cable for the STM32-UNO board.
  • Windows or Linux computer with STM32CubeMX and STM32CubeIDE installed.
  • Optional: multimeter to check logic levels and wiring.

Important: The STM32-UNO GPIO signals use 3.3 V logic. Do not connect an external 5 V output directly to a GPIO pin.

3. Software installation

Install the following software:

Software Purpose Official download
STM32CubeMX (classic 6.x) Microcontroller pinout, peripheral and clock configuration, generation of STM32CubeIDE projects STM32CubeMX
STM32CubeIDE (Eclipse-based 2.x) C editing, build, flashing and debugging STM32CubeIDE
STM32CubeProgrammer (optional) Independent flash programming and ST-LINK diagnostics STM32CubeProgrammer

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.

4. Select the two outputs

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.

Board signal MCU GPIO used in this draft CubeMX user label Mode
D13 / LED 1 PC10 LED_D13 GPIO_Output
D12 / LED 2 PC12 LED_D12 GPIO_Output

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.

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.

5. Create the project in STM32CubeMX

Step 1 - Launch STM32CubeMX

  1. Start STM32CubeMX.
  2. Select File -> New Project.
  3. Open the MCU/MPU Selector tab.

Step 2 - Select the MCU

  1. Enter STM32F446RET6 into the search field.
  2. Select the exact microcontroller model from the search results.
  3. Confirm the selection to open the device pinout configuration.

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.

Step 3 - Enable SWD debugging

  1. Open System Core -> SYS.
  2. Set Debug to Serial Wire.
  3. Keep PA13 (SWDIO) and PA14 (SWCLK) reserved for the debugger.

Do not reassign SWD pins to GPIO in this laboratory exercise.

Step 4 - Configure the system clock

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.

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.

6. Configure GPIO outputs

  1. In Pinout & Configuration, select the first verified MCU GPIO for D13 and set it to GPIO_Output (PA5 in the draft mapping).
  2. Select the second verified MCU GPIO for D12 and set it to GPIO_Output (PA6 in the draft mapping).
  3. Open System Core -> GPIO.
  4. Configure the parameters shown below.
Parameter D13 output D12 output
GPIO mode Output Push Pull Output Push Pull
Pull-up / Pull-down No pull-up and no pull-down No pull-up and no pull-down
Maximum output speed Low Low
GPIO output level Low Low
User label LED_D13 LED_D12

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.

After code generation, the user labels result in symbols similar to the following, normally defined in Core/Inc/main.h:

LED_D13_Pin
LED_D13_GPIO_Port
LED_D12_Pin
LED_D12_GPIO_Port

7. Generate the STM32CubeIDE project

  1. Open the Project Manager tab in STM32CubeMX.
  2. Set Project Name to STM32_UNO_LED.
  3. Choose a suitable project directory.
  4. Set Toolchain / IDE to STM32CubeIDE.
  5. Use the STM32Cube FW_F4 firmware package and HAL drivers.
  6. Click GENERATE CODE.
  7. If prompted to download the STM32CubeF4 firmware package, approve the download.

Import the generated project into STM32CubeIDE

  1. Start STM32CubeIDE.
  2. Select File -> Import.
  3. Choose Import STM32 Project -> STM32CubeMX/STM32CubeIDE Project (the exact wording may vary slightly between versions).
  4. Select the directory containing the project generated by STM32CubeMX.
  5. Complete the import and wait for project indexing.
  6. Check that the project contains Core/Src/main.c, Core/Inc/main.h, and the STM32 HAL driver files.

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.

8. Write the first program

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.

Task A - Set both outputs HIGH

HAL_GPIO_WritePin(LED_D13_GPIO_Port,
                  LED_D13_Pin,
                  GPIO_PIN_SET);

HAL_GPIO_WritePin(LED_D12_GPIO_Port,
                  LED_D12_Pin,
                  GPIO_PIN_SET);

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.

Task B - Alternate the two outputs

Replace the statements from Task A with the following code inside the same user-code section:

/* State 1: D13 = HIGH, D12 = LOW */
HAL_GPIO_WritePin(LED_D13_GPIO_Port,
                  LED_D13_Pin,
                  GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_D12_GPIO_Port,
                  LED_D12_Pin,
                  GPIO_PIN_RESET);
HAL_Delay(500);

/* State 2: D13 = LOW, D12 = HIGH */
HAL_GPIO_WritePin(LED_D13_GPIO_Port,
                  LED_D13_Pin,
                  GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_D12_GPIO_Port,
                  LED_D12_Pin,
                  GPIO_PIN_SET);
HAL_Delay(500);

The program sets opposite states on the two outputs, then reverses them every 500 ms. One complete cycle takes 1000 ms (1 second).

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.

9. Connect the STLINK-V3MODS programmer

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.

SWD signal mapping

STLINK-V3MODS contact Signal STM32F446RET6 target
4 T_SWDIO PA13 (SWDIO)
13 T_SWCLK PA14 (SWCLK)
8 GND GND
30 T_VCC (input) 3.3 V target voltage reference
31 T_NRST NRST (recommended)
5 GNDDetect Optional connection detection; consult adapter design

Power and wiring precautions:

  • Power the STM32-UNO via its approved USB power input or another verified board power source.
  • Connect the target GND and programmer GND together.
  • STLINK-V3MODS contact 30 (T_VCC) is a voltage-sense input, not a 3.3 V output.
  • 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.
  • For Connect Under Reset to work, connect the NRST signal.
  • Confirm all connections against the STM32-UNO electrical schematic and the STLINK-V3MODS user manual (UM2502) before powering the assembly.

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.

10. Build and flash the firmware

  1. Connect the powered STM32-UNO to the STLINK-V3MODS through the verified SWD adapter.
  2. Connect STLINK-V3MODS to the computer using USB.
  3. In STM32CubeIDE, save main.c (Ctrl+S).
  4. Choose Project -> Build Project or click the hammer icon.
  5. Check the console: the build should complete with 0 errors.
  6. Open Run -> Debug Configurations.
  7. Select or create the STM32 debug configuration for STM32_UNO_LED.
  8. Select the ST-LINK debugger and the SWD interface.
  9. Use a conservative SWD clock (for example, around 1 MHz) if the adapter or connection is new.
  10. Click Debug. The IDE should program the target Flash memory and enter the debug session.
  11. If the CPU is paused at main(), click Resume (typically F8) to execute the firmware.

Subsequent firmware changes can be built and downloaded through the same debug configuration.

11. Verify the result

For Task B, verify the following sequence:

Time in the cycle D13 D12
0-500 ms HIGH (3.3 V) LOW (0 V)
500-1000 ms LOW (0 V) HIGH (3.3 V)
Repeats every 1000 ms HIGH / LOW alternating LOW / HIGH alternating

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.

12. Troubleshooting

Problem Suggested checks
No ST-LINK detected USB data cable, operating-system drivers, STLINK-V3MODS firmware and USB connection.
No target detected Board power, common GND, SWDIO, SWCLK, T_VCC voltage reference, verified adapter wiring.
SWD connection fails Reduce SWD frequency; verify SYS/Serial Wire configuration; try Connect Under Reset if NRST is wired.
Flash programming succeeds, but the program does not run Resume execution from a breakpoint; confirm the firmware was built for STM32F446RET6.
Output pins do not change Verify MCU-to-DIO mapping and that GPIO initialization is generated.
LEDs behave opposite to expectations Inspect the LED driver circuit; the indicator may be active-low or may contain two LED colors.
Application enters Error_Handler() Verify the clock configuration; avoid enabling an HSE oscillator that is not fitted.

13. Independent assignments

After completing Task B:

  1. Change the delay from 500 ms to 100 ms and verify the new period.
  2. Modify the program so that both outputs are always in the same logic state and switch together.
  3. 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().

14. Completion checklist

  • [ ] The STM32CubeMX project targets the correct MCU.
  • [ ] Two verified STM32-UNO DIO signals are configured as GPIO outputs.
  • [ ] STM32CubeIDE builds the project with no errors.
  • [ ] STLINK-V3MODS successfully programs the STM32-UNO over SWD.
  • [ ] Both outputs switch with the expected timing.
  • [ ] The student can explain GPIO_PIN_SET, GPIO_PIN_RESET and HAL_Delay().

15. References

Instructor notes (not part of the assessed student task)

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.

PINOUT configuration:
DIO:
PC10 connector num13
PC12 connector num12
PD2
PB3
PC13
PA1
PB2
PB1
PB4
PB0
PB5

Aktualizováno uživatelem Ján Hýbl před 4 dny(ů) · 7 revizí