Skip to main content
Bogdan
Senior
June 6, 2018
Question

How to index diferent GPIO ports and pins

  • June 6, 2018
  • 3 replies
  • 1627 views
Posted on June 06, 2018 at 08:09

Hello,

i have a aplication where a SPI master from the stm32 chip is connected to 30 pieces of mux chips.

Each of the 30 spi channels has a enable signal connected to diferent gpio ports on the mcu side.

The enable signals are named EN1....EN30,   but for example en1 is on GPIOA PA3,    EN11 is on GPIOB PB7, en30 is on GPIOD PD9.

Is it posible to store these gpio ports and gpio pins like a array structure? 

Something like:

port_array[30] = { GPIOA.......GPIOD }

port_pins[30] = { GPIO_Pin_3....

GPIO_Pin_9 }

for( i=0; i<30; i++) {

port_array[i] -> BSRRH = port_pins[i]; // set port Lo  

between here i send the data over master spi

port_array[i] -> BSRRL = port_pins[i]; // set port Hi

}

    This topic has been closed for replies.

    3 replies

    Tilen MAJERLE
    ST Employee
    June 6, 2018
    Posted on June 06, 2018 at 08:23

    Hello,

    one option would be to create custom structure and then array of your pins:

    typedef struct {
     GPIO_TypeDef* gpio;
     uint16_t pin;
    } my_pin_t;
    const my_pin_t
    cs_pins[] = {
     { GPIOA, GPIO_PIN_0 },
     { GPIOB, GPIO_PIN_1 },
     ....,
    }
    #define E0 0
    #define E1 1
    ...
    //And call:
    cs_pins[E0].port->BSRR = cs_pins[E0].pin�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?;
    �?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

    This is more standard C question.

    Best regards,

    Tilen

    henry.dick
    Associate II
    June 6, 2018
    Posted on June 06, 2018 at 12:12

    generally you can define a datatype that has a pointer to the port and a mask for the pin. and then form an array of such datatype.

    like this:

    typedef struct {

      GPIO_TypeDef *port; //port

      uint32_t mask; //32-bit pin mask

    } PIN_TypeDef;

    const PIN_TypeDef pin_array[]={

      {GPIOB, 1<<2},  //PB2

      {GPIOA, 1<<6},  //PA6

    ...

    }

    an example can be found here: 

    https://dannyelectronics.wordpress.com/2017/02/18/driving-multiple-servos-off-a-pic-timer2-ranged-extended/

     
    AvaTar
    Senior III
    June 6, 2018
    Posted on June 06, 2018 at 12:19

    Another option would be a 4-bit multiplexer IC.

    Thirty pins as chip select is quite a lot, you may find out later you need them for other purposes.