cancel
Showing results for 
Search instead for 
Did you mean: 

How to configure any GPIO pin individually using HAL ?

jefazo92
Associate III

Hi everyone,

I am trying to modify the demonstartion application in the NUCLEO-G47RE board. There is this comment in the main header file:

#define B1_Pin GPIO_PIN_13

I went to the Description of STM32G4 HAL and low-layer dirvers found in:

https://www.st.com/content/ccc/resource/technical/document/user_manual/group1/8c/dc/58/e5/4c/87/43/39/DM00610707/files/DM00610707.pdf/jcr:content/translations/en.DM00610707.pdf

In pages 354 and 355, you can see the GPIO_Pins which can be called with GPIO_PIN_x where is x is between 0 to 15. But I do not understand why the the first pin is Pin 0 when in the datasheet the pinout starts from Pin 1. Also why is the last pin to be configured Pin 15 when 52 pins are GPIOs. Any help would be really appreciated.

P.D. My MCU is using the LQFP64 package. That's the pinout I'm referring to.

4 REPLIES 4
jefazo92
Associate III

Ok . I found out that Pin0 to Pin15 refers to pins PA0-PA15 from a Youtube video (I don't know why the HAL guide doesn't explain this). But what about the PB and the PC pins ?

KnarfB
Principal III

GPIO pins are grouped in ports. See the RM0440 STM32G4 Series advanced Arm®-based 32-bit MCU reference manual. Ports are named GPIOA, GPIOB, ...

Pins within each port are numbered 0..15. Think of bits in a 16-bit word. These numbers are not related to the chip package.

If you use STm32CubeMX or STM32CubeIDE you can graphically choose and configure pins.

jefazo92
Associate III

Thanks for your reply @KnarfB​  but I am trying to understand how to configure any pin not belonging to the PA group, let's say PC9, using the HAL commands. Becasue it makes no sense that there is HAL code for the PA group but not for the PB and PC groups.

KnarfB
Principal III

I think the easiest way is to let STM32CubeIDE generate HAL code for you and studying the results.

// green LED on PB3
 
#define LEDgn_Pin GPIO_PIN_3
#define LEDgn_GPIO_Port GPIOB
 
// init
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = LEDgn_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LEDgn_GPIO_Port, &GPIO_InitStruct);
 
// LED on
HAL_GPIO_WritePin( LEDgn_GPIO_Port, LEDgn_Pin, GPIO_PIN_SET);