led Part Drivers

March 13, 2026 ยท View on GitHub

tag release note

Overview

The LED part drivers provide APIs to drive LEDs through a GPIO or a timer (PWM) for advanced blinking/fading features.

Description and Usage

GPIO flavor

This part driver is a simple wrapper around the HAL GPIO. It relies on macros to link a LED to the HAL GPIO. Example for a LED named LED0:

#define LED0_GPIO_PORT        HAL_GPIOE
#define LED0_PIN              HAL_GPIO_PIN_13
#define LED0_ACTIVE_STATE     HAL_GPIO_PIN_SET
#define LED0_INACTIVE_STATE   HAL_GPIO_PIN_RESET

The required macros may be generated by STM32CubeMX2, or written manually in the software project.

The usage sequence for an application is the following:

  1. GPIO initialization (HAL_Init(), HAL_GPIO_Init(...), ...)
  2. LED usage (led_on(LED0), led_off(LED0), ...)

As opposed to other part drivers (including the TIM LED driver, cf. below), there is no led_init() function. The reasoning is that the HAL GPIO initialization already covers all the required settings; cf. init_state in the hal_gpio_config_t structure.

PWM flavor

This driver assumes that the initialization of all needed peripherals (DMA, TIM) is done by the main application. After the MCU IPs have been initialized, the selected resources can be assigned to LED PWM objects. This is done in led_pwm_io_init(), which the user application must override. That function is called by led_pwm_init(); the .id field of the led_pwm_t structure must be used to discriminate the resources to be assigned.

To use the LED PWM part API, the application must define the led_pwm_t object and the led_pwm_io_init() function to assign TIM resources to it. This code may be generated by STM32CubeMX2, or written manually in the software project. The link between the two is made by led_pwm_init(), which must be called after all the MCU peripherals have been initialized.

Once this is successfully done, all the other part APIs can be used freely.