Controlling GPIO in FreeRTOS Print

 

This application note explains how to control GPIO in FreeRTOS.

The FreeRTOS demo application illustrates the corresponding APIs through controlling of the LEDs and Push-Buttons available on the carrier board.


Understanding FreeRTOS GPIO Interfaces


FreeRTOS GPIO Implementation

The FreeRTOS BSP makes use of the STM32CubeH7 software component to provide a device driver for the STM32H7 GPIO. The driver is configured (enabled / disabled) at the BSP build time, using the HAL_GPIO_MODULE_ENABLED configuration option, defined in the stm32h7xx_hal_conf.h file.


FreeRTOS GPIO C-Binding API

The GPIO driver implements the following C-binding API:

Function Description Comments
void HAL_GPIO_Init(GPIO_TypeDef * GPIOx, GPIO_InitTypeDef * GPIO_Init) Initialise GPIO pins in a specified GPIO port GPIOx is a pointer to the GPIO port registers; CPIO_Init is a pointer to a data structure describing configuration of a specific pin of the given GPIO port (see below)
void HAL_GPIO_DeInit(GPIO_TypeDef * GPIOx, uint32_t GPIO_Pin) Reset a GPIO pin to default settings GPIO_Pin is GPIO_PIN_x, х is one of values from 0 to 15
GPIO_PinState HAL_GPIO_ReadPin (GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin) Read state of a GPIO pin GPIO_Pin is GPIO_PIN_x, х is one of values from 0 to 15; returns one of {GPIO_PIN_RESET = 0, GPIO_PIN_SET}
void HAL_GPIO_WritePin(GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState) Set or clear a GPIO pin GPIO_Pin is GPIO_PIN_x, х is one of values from 0 to 15; PinState is either GPIO_PIN_RESET or GPIO_PIN_SET
void HAL_GPIO_TogglePin(GPIO_TypeDef * GPIOx, uint16_t GPIO_Pins) Toggle specified GPIO pins


The GPIO_InitTypeDef data structure used in the HAL_GPIO_Init interface has the following definition:

typedef struct { uint32_t Pin; /* GPIO pins to be configured */ uint32_t Mode; /* Operating mode for the selected pins */ uint32_t Pull; /* Pull-up or Pull-Down activation for the selected pins */ uint32_t Speed; /* Speed for the selected pins */ uint32_t Alternate;/* Peripheral to be connected to the selected pin */ } GPIO_InitTypeDef;


FreeRTOS LED and Push-Button Tasks

The FreeRTOS demos application provides two threads illustrating use of the GPIO API from a FreeRTOS application. Specifically:

  • The LED Thread implements a blink pattern on the LED residing on the STM32H750 system-on-module (blink LED at 0.5Hz rate).
  • The Push-Buttons Thread waits for one of the two user push-buttons residing on the STM32H7-BSB carrier board (BTN1 and BTN2) to change its state. As soon as a push-button has been pressed, the thread lilts the corresponding LED residing on the STM32H7-BSB carrier board (LED1 and LED2, respectively). As soon as a push-button has been released, the thread unlits the corresponding LED. Implementation of this thread is interrupt-driven.