0
\$\begingroup\$

I am using an STM32F4 discovery board and I have the following array v

typedef struct pin_map_dummy {
     GPIO_TypeDef* GPIO_Port;
     uint16_t GPIO_Pin;
} pin_map_t;
...
vector_pin_t v={{GPIOD,GPIO_Pin_0},{GPIOD,GPIO_Pin_1},{GPIOD,GPIO_Pin_2},
{GPIOD,GPIO_Pin_3} , {GPIOD,GPIO_Pin_4}, {GPIOD,GPIO_Pin_5}, {GPIOD,GPIO_Pin_6}, {GPIOD,GPIO_Pin_7}};

I'm passing the array to the initialization function but I'm trying to figure out if it's possible to set each Pin to a different GPIO port, in the array v all pins are located in PORTD but what if one pin happens to be in PORTA

This is the GPIO init function:

 void init_library(vector_pin_t pines)
    {

        GPIO_InitTypeDef puerto;
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); 


        puerto.GPIO_Pin   = pines[0].GPIO_Pin | pines[1].GPIO_Pin | pines[2].GPIO_Pin | pines[3].GPIO_Pin
                          | pines[4].GPIO_Pin | pines[5].GPIO_Pin | pines[6].GPIO_Pin | pines[7].GPIO_Pin ; //All pins
        puerto.GPIO_Mode  = GPIO_Mode_OUT;
        puerto.GPIO_OType = GPIO_OType_PP; 
        puerto.GPIO_Speed = GPIO_Speed_100MHz;  
        puerto.GPIO_PuPd  = GPIO_PuPd_NOPULL;


    GPIO_Init(pines[0].GPIO_Port, &puerto);


}
\$\endgroup\$
1
  • \$\begingroup\$ You should probably contemplate the fact that the pins are bitmap constants rather than ordinal numbers and then rethink either the function interface, or the value of abstracting it as a function at all. Putting things in an unrelated form of expression to pass them through and interface and then convert back is a bit inefficient (of code size at least) on a small system. \$\endgroup\$ Commented Apr 24, 2020 at 15:31

2 Answers 2

1
\$\begingroup\$

You cannot initialize different ports with just one write of the init function.

This is because they are located at different peripheral registers and thus in different memory locations. So in the end, there will always be multiple accesses to the GPIO peripherals to set different ports.

If all you want is a function, which is intelligent enough to handle an array with multiple ports and pins, that is definitely doable. There are different ways to go about that.

Simplest approach would be to do one initialization for each element in the array.

As pins are organized in ports, you could also use an approach which loops over the array and gathers all the pins for one port into one structure and then only call the init for one port structure.

\$\endgroup\$
0
\$\begingroup\$

If each entry in the array can point to different port, the simplest way is to initialize the pins in a for loop one at a time. Take a port and pin, init it. Take next port and pin, init it. There are faster ways too, but since the IO ports are each dynamically set in run-time by the user anyway, it makes little difference how it is done.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.