cancel
Showing results for 
Search instead for 
Did you mean: 

How to work with GPIO

Filipx.87
Associate II

Please advise where I'm doing wrong, I need to create variables with GPIO output with variable assignment and I don't know how to make it work correctly. 'my_gpio_port' reports as undeclared, but the declaration is without errors. Thanx

 

GPIO_TypeDef* my_gpio_port;
uint16_t  my_gpio_pin;

  if (Device_Config == Device_A)
	{
		my_gpio_port = GPIOA;
		my_gpio_pin = GPIO_PIN_7;
	}
  else
	{
		my_gpio_port = GPIOG;
		my_gpio_pin = GPIO_PIN_4;
	}

HAL_GPIO_Write(my_gpio_port, my_gpio_pin, 1);

 

 

6 REPLIES 6
SofLit
ST Employee

,Hello,

First could you please provide the MCU part number? You created this thread on STM32 wireless.

Second, where did you define that typedef?:

GPIO_TypeDef

Third, did you enable the Port RCC clock? did you configure the GPIO pins?

Why are you using such code to access the pins:

  if (Device_Config == Device_A)
	{
		my_gpio_port = GPIOA;
		my_gpio_pin = GPIO_PIN_7;
	}
  else
	{
		my_gpio_port = GPIOG;
		my_gpio_pin = GPIO_PIN_4;
	}

Then you use the HAL API to access another pin!

Need to clarify what you need to do.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

First could you please provide the MCU part number?

STM32F429ZIT6

You created this thread on STM32 wireless.

I'm sorry, I know I made a mistake and I can't edit it anymore.

where did you define that typedef?

In the int main(void)

Why are you using that kind of access in the first part

  if (Device_Config == Device_A)
	{
		my_gpio_port = GPIOA;
		my_gpio_pin = GPIO_PIN_7;
	}
  else
	{
		my_gpio_port = GPIOG;
		my_gpio_pin = GPIO_PIN_4;
	}

then you are using HAL for this part to set the GPIO pin?

HAL_GPIO_Write(my_gpio_port, my_gpio_pin, 1);
To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

Meanwhile to do a direct access to the register: 

You can access it over BRR and BSRR or ODR registers:

GPIOx->BSRR to set the pin and  GPIOx->BRR to reset the pin.

You need to look at HAL_GPIO_WritePin() implementation.

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

@Filipx.87 wrote:

Please advise where I'm doing wrong


Not enough context to be sure.

 


@Filipx.87 wrote:

'my_gpio_port' reports as undeclared


Please post a minimum but complete example which illustrates this.

Also post the complete build output. You haven't said what tools you're using but, for STM32CubeIDE, select-all & copy from the 'Console' window:

https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/tac-p/725146/highlight/true#M54


@SofLit wrote:

Why are you using that kind of access in the first part

Seems to be just to support 2 different configurations; equivalent to

 

if (Device_Config == Device_A)
{
	HAL_GPIO_Write( GPIOA, GPIO_PIN_7 );
}
else
{
	HAL_GPIO_Write( GPIOG, GPIO_PIN_4 );
}

 

 

@Filipx.87  - do you really need to do this at run time, or could it be done with conditional compilation?