GPIO Output

1. CubeMX Configuration

Open STM32CubeMx. Generate basic code by assing osccillator pins, serial wire pins, configuring clock and project detail. Follow the generate_basic_code with:

  • microcontroller: stm32f407vgt6 or board: STM32F407VG-DISC1

  • project name: gpio_example

  • Toolchain/IDE: Makefile

STM32 Discovery board have leds on pins PD12, PD13, PD14 and PD15. Move to STM32CubeMX Pinout and Congiguration. Click PD12 and select GPIO-Output. For Bluepill, led pin is on PA13. Generate code.

2. Understanding the Project Structure

Well, you have generated the code. Now, let’s understand the project structure.

  • Core: Contains the startup main file, peripheral files and other system configuration files. - Inc: Contains the header files. - Src: Contains the source files.

  • Drivers: Contains the HAL library. - CMSIS: Contains the Cortex Microcontroller Software Interface Standard. - STM32F4xx_HAL_Driver: Contains the HAL library.

  • gpio_example.ioc: The project file. It conts configuration of the microcontroller used for STM32CubeMX.

  • Makefile: The makefile to build the project.

  • startup_stm32f407xx.s: The startup file. It contains the vector table and the reset handler.

  • STM32F407VGTx_FLASH.ld: The linker script file. It contains the memory layout of the microcontroller.

We mostly expend time on Core. Since you have generated the code for GPIO, you can see gpio.h and gpio.c.

4. Build and Flash

We need to add few line of code in the makefile to flash the code. Open Makefile and add the following lines at the bottom:

#######################################
# flash
#######################################
flash: $(BUILD_DIR)/$(TARGET).bin
     st-flash --reset write $(BUILD_DIR)/$(TARGET).bin 0x8000000

Now, build and flash the code.

make -j
make flash

This code only works with ST-Link. If you are using JLink, see build_setup and flash binary.

We will continue it for gpio input.

5. Observation

See LED blinking every second. Change delay time and enjoy.

References

References are from STM32 HAL Driver documentation.

HAL_GPIO_WritePin(GPIOx, GPIO_Pin, PinState)

Sets or clears the selected data port bit.

Note: This function uses the GPIOx_BSRR register to allow atomic read/modify accesses. There is no risk of an IRQ occurring between the read and modify access.

Parameters:
  • GPIOx (GPIO_TypeDef) – GPIO peripheral where x can be: - (A..K) for STM32F429X devices - (A..I) for STM32F40XX and STM32F427X devices

  • GPIO_Pin (uint16_t) – Specifies the port bit to be written. Can be one of the following: - GPIO_PIN_0 through GPIO_PIN_15

  • PinState (GPIO_PinState) – Specifies the value to be written to the selected bit. Can be one of the following: - GPIO_PIN_RESET: Clears the port pin - GPIO_PIN_SET: Sets the port pin

Returns:

None

HAL_GPIO_TogglePin(GPIOx, GPIO_Pin)

Toggles the state of the specified GPIO pins.

Parameters:
  • GPIOx (GPIO_TypeDef*) – GPIO peripheral where x can be: - (A..K) for STM32F429X devices - (A..I) for STM32F40XX and STM32F427X devices.

  • GPIO_Pin (uint16_t) – Specifies the pins to be toggled. This parameter can be a combination of GPIO_PIN_x values where x can range from 0 to 15.

Returns:

None

Return type:

None

HAL_Delay(Delay)

Provides a minimum delay (in milliseconds) based on a variable that is incremented regularly.

Note 1: In the default implementation, the SysTick timer is used as the time base. It generates interrupts at regular time intervals, where uwTick is incremented.

Note 2: This function is declared as __weak to allow overriding by user implementations in other files.

Parameters:

Delay (uint32_t) – Specifies the delay time length, in milliseconds.

Returns:

None

Return type:

None