STM32-Peripheral’s-GPIO: Output, Input

STM32-Peripheral’s-GPIO: Output, Input

Closeup picture of some computer parts in blue light

GPIO Peripheral features


Subject to the specific hardware characteristics of each I/O port listed in the datasheet, each port bit of the
General Purpose IO (GPIO) Ports, can be individually configured by software in several modes:
• Input mode
• Analog mode
• Output mode
• Alternate function mode
• External interrupt/event lines
During and just after reset, the alternate functions and external interrupt lines are not active and the I/O ports are
configured in input floating mode.
All GPIO pins have weak internal pull-up and pull-down resistors, which can be activated or not.
In Output or Alternate mode, each IO can be configured on open-drain or push-pull type and the IO speed can be
selected depending on the VDD value.
All ports have external interrupt/event capability. To use external interrupt lines, the port must be configured in
input mode. All available GPIO pins are connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
The external interrupt/event controller consists of up to 23 edge detectors (16 lines are connected to GPIO) for
generating event/interrupt requests (each input line can be independently configured to select the type (interrupt
or event) and the corresponding trigger event (rising or falling or both). Each line can also be masked
independently

How to use this driver


1. Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
2. Configure the GPIO pin(s) using HAL_GPIO_Init().
– Configure the IO mode using “Mode” member from GPIO_InitTypeDef structure
– Activate Pull-up, Pull-down resistor using “Pull” member from GPIO_InitTypeDef structure.
– In case of Output or alternate function mode selection: the speed is configured through “Speed”
member from GPIO_InitTypeDef structure.
– In alternate mode is selection, the alternate function connected to the IO is configured through
“Alternate” member from GPIO_InitTypeDef structure.
– Analog mode is required when a pin is to be used as ADC channel or DAC output.
– In case of external interrupt/event selection the “Mode” member from GPIO_InitTypeDef structure
select the type (interrupt or event) and the corresponding trigger event (rising or falling or both).
3. In case of external interrupt/event mode selection, configure NVIC IRQ priority mapped to the EXTI line
using HAL_NVIC_SetPriority() and enable it using HAL_NVIC_EnableIRQ().
4. To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
5. To set/reset the level of a pin configured in output mode use HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
6. To lock pin configuration until next reset use HAL_GPIO_LockPin().
7. During and just after reset, the alternate functions are not active and the GPIO pins are configured in input
floating mode (except JTAG pins).
8. The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose (PC14 and PC15,
respectively) when the LSE oscillator is off. The LSE has priority over the GPIO function.
9. The HSE oscillator pins OSC_IN/OSC_OUT can be used as general purpose PH0 and PH1, respectively,
when the HSE oscillator is off. The HSE has priority over the GPIO function.

Initialization and de-initialization functions


This section provides functions allowing to initialize and de-initialize the GPIOs to be ready for use.
This section contains the following APIs:
• HAL_GPIO_Init
• HAL_GPIO_DeInit

IO operation functions


• HAL_GPIO_ReadPin
• HAL_GPIO_WritePin
• HAL_GPIO_TogglePin
• HAL_GPIO_LockPin
• HAL_GPIO_EXTI_IRQHandler
• HAL_GPIO_EXTI_Callback

Today we are going to discuss how to create general peripheral based project using cube ide, for this demonstration we will use STM32F429IDISCOVERY board.

Creating a new STM32 executable project

  1. The easiest way to create a new STM32 C/C++ project is to use the STM32 project wizard. It is selected through the menu [File]>[New STM32 Project].
  2. The MCU/MPU selector and Board Selector tabs can be selected at the top of the window. Use the first tab to create project for a specific device and the second if a project for a specific board is needed. today we will select specific device.
Figure 1

3. Select SMT32F429ZI in MCU Selector tab

4. According to the settings in Figure 3, the project is meant to be stored in the default location with the following

options set:

• C project

• Executable binary type

STM32CubeIDE targeted project type

Press [Next] to open the Firmware Library Package Setup page.

Figure 3 Project Name

5. In this page, it is possible to select the STM32Cube firmware package to use when creating the project. In this case, the default settings are used. Press [Finish] to create the project. (Fig 4)

Figure 4 Select Firmware Package

6. wait for project creation process

Figure 5 Project Creation process

7. Initial view of STM32CUBE ide after project generation

Figure 6

8. Go to System core > SYS > Enable Serial wire debug

Figure 8 Enable Serial wire debug

9. If require enable external clock

Figure 9 Enable external clock

10. System clock setting at 180MHz

Figure 10 clock settings

11. GPIO Selection : Go to desire pin location which user want to set either input or output

Figure 11: Input output selection

12. Example: go to pin pin 1 (PE1), Press left click on that particular pin, below options will appear for selection, select GPIO_Output for PE2 & GPIO_Input For PE3.

Figure 12: GPIO Pin Mode selections

13. GPIO_Output configuration settings:

GPIO output level : low or high

GPIO mode: output pushpull or output opendrain

GPIO Pull-up/Pull-down : pull up or pull down or no settings

Output speed: low, medium, high, very high

Figure 13: output Parameters settings

14. GPIO_Input configuration settings:

GPIO mode: Input Mode or no settings

GPIO Pull-up/Pull-down : pull up or pull down or no settings

Figure 14: Input Parameters settings

15. Press below icon for code generation

Figure 15: Code Generation

16. HAL_GPIO_WritePin

Function name
void HAL_GPIO_WritePin (GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)


Function description
Sets or clears the selected data port bit

Parameters
• GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F429X device or x can be (A..I) to
select the GPIO peripheral for STM32F40XX and STM32F427X devices.
• GPIO_Pin: specifies the port bit to be written. This parameter can be one of GPIO_PIN_x where x can be
(0..15).
• PinState: specifies the value to be written to the selected bit. This parameter can be one of the
GPIO_PinState enum values:
– GPIO_PIN_RESET: to clear the port pin
– GPIO_PIN_SET: to set the port pin


Return values
• None:

Line no.12 is used generate set or clears value on Port E pin 2,

Line no 13 is provide delay of 500ms

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
   	HAL_GPIO_WritePin(GPIOE,GPIO_PIN_2,GPIO_PIN_SET);
    HAL_Delay(500);	//500 ms delay
  }

17. HAL_GPIO_TogglePin

Function name
void HAL_GPIO_TogglePin (GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin)


Function description
Toggles the specified GPIO pins.


Parameters
• GPIOx: Where x can be (A..K) to select the GPIO peripheral for STM32F429X device or x can be (A..I) to
select the GPIO peripheral for STM32F40XX and STM32F427X devices.
• GPIO_Pin: Specifies the pins to be toggled.


Return values
• None:

Line no.12 is used generate toggle GPIO on Port E pin 2 ,

Line no 13 is provide delay of 500ms

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
   	HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_2);
	HAL_Delay(500);	//500 ms delay
  }
  /* USER CODE END 3 */

18. HAL_GPIO_ReadPin

Function name
GPIO_PinState HAL_GPIO_ReadPin (GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin)


Function description
Reads the specified input port pin.


Parameters
• GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F429X device or x can be (A..I) to
select the GPIO peripheral for STM32F40XX and STM32F427X devices.
• GPIO_Pin: specifies the port bit to read. This parameter can be GPIO_PIN_x where x can be (0..15).


Return values
• The: input port pin value.

  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_2);
	  HAL_Delay(500);	//500 ms delay
	  if (HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_3) == SET)
	  {
		  uart1_printf_debug("Read value is Set\n");
	  }
	  else
	  {
		  uart1_printf_debug("Read value is Clear\n");
	  }
  }
  /* USER CODE END 3 */

Output at serial terminal

Software Tools:

  1. STM32CubeIDE
  2. STM32CubeMx
  3. Teraterm

Hardware Setup;

  1. STM32F429IDISCOVERY board
  2. Mini USB Cable
  3. Jumper wire

Conclusion:

For beginners its very easy to start controller coding using STM32Cube IDE which has huge library base for device drivers with example codes, in given blog we learned how to generate code from STM32Cube IDE & use of GPIO read, write & toggle functions.

References:

  1. STM32 HAL Library
  2. STM32 UM1718 document

Similar topics:

  1. https://kalapiinfotech.in/stm32-cube-ide-freertos-code-generation-using-cubemx/
  2. https://kalapiinfotech.in/stm32-peripherals-dac/
  3. https://kalapiinfotech.in/stm32-peripherals-adc-polling-method/

Leave a Reply

Your email address will not be published. Required fields are marked *