cancel
Showing results for 
Search instead for 
Did you mean: 

How to define bidirectional GPIO pins in STM32?

danesh
Associate II
Posted on July 09, 2015 at 11:57

Dear all,

I am using a high density STM32F103 MCU where I need to define GPIOC0-GPIOC7 as bidirectional since data on all these pins will either be sent or received to/from another chip. I have designed these pins as:

  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0

                           | GPIO_Pin_1

                           | GPIO_Pin_2

                           | GPIO_Pin_3

                           | GPIO_Pin_4

                           | GPIO_Pin_5

                           | GPIO_Pin_6

                           | GPIO_Pin_7;

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_Init(GPIOC, &GPIO_InitStruct);

but I am not sure if I have defined in a correct way, sine my code doesn't work as it should. So would you please let me know if I have (at least) defined the pins correctly, according to their functionality so I would make sure that the problem is not here?

Regards,

D.

4 REPLIES 4
Nickname4815_O
Associate II
Posted on July 10, 2015 at 09:11

Hi,

input and output for GPIO need two different port configurations.

If you want to drive the pin manually (i.e. setting or reading the voltage level to VDD or GND via SW) you need to reconfigure the port using the GPIO_Init fcn according to the mode you are going to use.

The mode should be GPIO_Mode_IN_FLOATING in case of input, GPIO_Mode_Out_PP in case of output pushpull).

Regards,

F.

danesh
Associate II
Posted on July 10, 2015 at 12:54

Hi Federico,

Great! Thanks! I am actually using an 8-bits bus which is shared between STMF103 as host and another module. The bus needs to act as an output (from the host) when the host is writing to it and then it should act as input (to the host) when the module is writing to it. At the moment I am using two different functions, to initialize pins as input and output as below:

void set_pins_as_input() {

  GPIO_InitTypeDef GPIO_InitStruct;

  // ---------- Setup ports for GPIOC ----------

  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0

                           | GPIO_Pin_1

                           | GPIO_Pin_2

                           | GPIO_Pin_3

                           | GPIO_Pin_4

                           | GPIO_Pin_5

                           | GPIO_Pin_6

                           | GPIO_Pin_7;

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_Init(GPIOC, &GPIO_InitStruct);

}

void set_pins_as_output() {

  GPIO_InitTypeDef GPIO_InitStruct;

  // ---------- Setup ports for GPIOC ----------

  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0

                           | GPIO_Pin_1

                           | GPIO_Pin_2

                           | GPIO_Pin_3

                           | GPIO_Pin_4

                           | GPIO_Pin_5

                           | GPIO_Pin_6

                           | GPIO_Pin_7;

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;

  GPIO_Init(GPIOC, &GPIO_InitStruct);

}

Than I call the routines in another functions as below:

uint8_t read_data(uint16_t addr) {

  set_pins_as_output();

  // Doing something by writing data to the pins.

  set_pins_as_input();

  // Doing something by reading data from the pins.

}

As far as I can check, when the pins are defined as output, the data is written correctly on the pins, but when the pins are set as input, no matter, what the module intends to write on them which will be read by the host, the pins are always in high state (they are set to 1). Am I doing something wrong?

Thanks for your help,

Dan.

Nickname4815_O
Associate II
Posted on July 15, 2015 at 09:21

Hi,

unfortunately this thread is off-topic for the current forum (it is about the RF low power products).

Since it is an issue related to the uC I would suggest to re-post it in

https://my.st.com/public/STe2ecommunities

>

Microcontrollers .

There someone will address Your request.

Regards,

F.

danesh
Associate II
Posted on July 15, 2015 at 14:06

Actually the module I am talking about is a NFC RF reader, so I thought this is the right forum. I will post my issue into the uc forum.

Thanks for the tip,

Dan.