O.Blue
O.Blue

Reputation: 137

How to create a function for configuring defined ports on a stm32f4xx platform?

I am using stm32f4xx with HAL library and I have configured some ports like this:

  #define Led1 GPIO_PIN_1
  #define Led1_Port GPIOC

  #define Led2 GPIO_PIN_2
  #define Led2_PoRT GPIOE

  GPIO_InitStruct.Pin = Led1;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(Led1_Port, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = Led2;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(Led2_Port, &GPIO_InitStruct);

I want to move the code above (not the defines of course) to a function which can be then called and setup the ports in the exact same way as above:

  #define Led1 GPIO_PIN_1
  #define Led1_Port GPIOC

  #define Led2 GPIO_PIN_2
  #define Led2_PoRT GPIOE

  void GPIOConfig(*Pin,GPIO_TypeDef *Port)
  {
    GPIO_InitStruct.Pin = &Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLDOWN;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(&Port, &GPIO_InitStruct);
  }

  // Calling the configuration function
  GPIOConfig(Led1,Led1_Port);
  GPIOConfig(Led2,Led2_Port);

For some reason it doesn't work.

Upvotes: 1

Views: 1802

Answers (1)

Realtime Rik
Realtime Rik

Reputation: 1714

I think your init function should be more like this as Pin is just a number and not a pointer. Also you are also passing Port in as a pointer which is what you want to pass to HAL_GPIO_Init (and not the address of the pointer):

void GPIOConfig(uint32_t Pin,GPIO_TypeDef *Port)
{
   GPIO_InitStruct.Pin = Pin;
   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
   GPIO_InitStruct.Pull = GPIO_PULLDOWN;
   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
   HAL_GPIO_Init(Port, &GPIO_InitStruct);
}

And then should called like:

GPIOConfig( Led1, Led1_Port );

Upvotes: 2

Related Questions