Docsity

Stm32 tutorial gpio hal, Apuntes de Microcontroladores

Toggle sidebar
bg1
STM32 Tutorial NUCLEO F103RB GPIO Pins
V1.0.1 – created on
20.05.2016
simon burkhardt page 1 /5
GPIO Operations on STM32 Microcontrollers using HAL
You can use the STM32CubeMX tool to create the necessary config. files to enable the GPIO Pins.
In this tutorial I’m going to explain how you can modify the generated GPIO configs and add additional
GPIOs.
This tutorial uses the following equipment:
- NUCLEO-F103RB Board
- Keil uVision 5 with the necessary packages for Nucleo boards installed
- STLink USB Driver
- STM32CubeMX
STM32CubeMX
Generating the config. files from STM32CubeMX.
1. Open STM32CubeMX and open a new project.
2. Select the Nucleo-F103RB from the Borards tab
3. Enable FreeRTOS
4. Set the RCC (HSE & LSE) to Crystal/Ceramic Resonator
5. Enable the USART2 port in Asynchronous mode
6. Set any GPIO to Output or Input (I am using PB13, PB14 and PC4)
7. Go to Project > Generate code
8. Enter a project name and select MDK-ARM V5
9. Generate the code and open the project in Keil uVision
Now let’s see what the code generator did
187
void
MX_GPIO_Init(
void
)
Announcement: check out the updated tutorials
https://github.com/mnemocron/STM32-Tutorial
pf3
pf4
pf5

Vista previa parcial del texto

¡Descarga Stm32 tutorial gpio hal y más Apuntes en PDF de Microcontroladores solo en Docsity!

GPIO Operations on STM32 Microcontrollers using HAL

You can use the STM32CubeMX tool to create the necessary config. files to enable the GPIO Pins.

In this tutorial I’m going to explain how you can modify the generated GPIO configs and add additional

GPIOs.

This tutorial uses the following equipment:

- NUCLEO-F103RB Board

- Keil uVision 5 with the necessary packages for Nucleo boards installed

- STLink USB Driver

- STM32CubeMX

STM32CubeMX

Generating the config. files from STM32CubeMX.

1. Open STM32CubeMX and open a new project.

2. Select the Nucleo-F103RB from the Borards tab

3. Enable FreeRTOS

4. Set the RCC (HSE & LSE) to Crystal/Ceramic Resonator

5. Enable the USART2 port in Asynchronous mode

6. Set any GPIO to Output or Input (I am using PB13, PB14 and PC4)

7. Go to Project > Generate code

8. Enter a project name and select MDK-ARM V

9. Generate the code and open the project in Keil uVision

Now let’s see what the code generator did

187 void MX_GPIO_Init(void)

The concept is simple, on line 190 an init struct is defined, this struct is filled with information.

This information will be processed by the HAL library at the function call HAL_GPIO_Init().

Lines 193 to 196 enable the clock for the GPIO ports.

The init struct consists of 4 values that can be set.

1. Pin

  • The Pin(s) that are about to be initialised
  • e.g. GPIO_PIN_3 (numbers reach from 0 to 15, or GPIO_PIN_ALL)

2. Mode

  • The mode of the selected pins (Input / Output / etc.)
  • e.g. GPIO_MODE_INPUT
  • Possible assignments are the following: GPIO_MODE_INPUT floating input GPIO_MODE_OUTPUT_PP output push-pull GPIO_MODE_OUTPUT_OD output open drain GPIO_MODE_AF_PP alternate function output push-pull GPIO_MODE_AF_OD alternate function output open drain

GPIO_InitTypeDef GPIO_InitStruct;

/* GPIO Ports Clock Enable */ __GPIOC_CLK_ENABLE(); __GPIOD_CLK_ENABLE(); __GPIOA_CLK_ENABLE(); __GPIOB_CLK_ENABLE();

/*Configure GPIO pin : B1_Pin */ GPIO_InitStruct.Pin = B1_Pin; GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);

/*Configure GPIO pin : LD2_Pin */ GPIO_InitStruct.Pin = LD2_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);

/*Configure GPIO pin : PC4 */ GPIO_InitStruct.Pin = GPIO_PIN_4; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // digital Input GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

/*Configure GPIO pin : PB13 */ GPIO_InitStruct.Pin = GPIO_PIN_13; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; // analog Input HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/*Configure GPIO pin : PB14 */ GPIO_InitStruct.Pin = GPIO_PIN_14; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; // digital Output GPIO_InitStruct.Speed = GPIO_SPEED_LOW; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

Using a GPIO output inside the program

Changes of the output state of an output pin are written to the GPIOx_ODR register (output data

register). This works best with masking.

Turning on an output pin

Turning off an output pin

Toggle an output pins state

An output pin can also be set using the integrated HAL library function

Reading a GPIO input inside the program

The current state of an input can be read from its GPIOx_IDR register (input data register)

Again, this works best using bit masking.

Reading an input pin

Or with the HAL library function

/* turn on PA0 */ GPIOA -> ODR |= GPIO_PIN_0;

/* turn off PA0 */ GPIOA -> ODR &= ~GPIO_PIN_0;

/* toggle PA0 */ GPIOA -> ODR ^= GPIO_PIN_0;

/* set PA0 */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET);

/* reset PA0 */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);

/* read PC13 / if(GPIOC -> IDR & GPIO_PIN_13) { / user code */ } 1 2 3 4 5

/* read PC13 / if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13)) { / user code */ }

Document Created by Simon Burkhardt

This tutorial is very basic and might not show the best way to use the STM32 environment.

It still might help you get into the whole HAL philosophy of STM if you are coming from another

platform. This document is free of copyright under the Creative Commons Zero attribution.

Simon Burkhardt

electronical engineering

simonmartin.ch

History:

V1.0 tested the code, created this document

V1.0.1 added contact info and cc0 notice