The way of making IO pin "floating" intentionally?

Suuuuuup,

I'm using STM32F103 5V tolerant pin and want to pull down 5V line or leave it be. The problem is if I set IO to HIGH that line gets pulled to 3.3v (3.3 acts as a GND for voltage different between 5V and 3.3V), I was suggested to use "floating" and GND states to do that, so here I'm...

Is the only way to make IO floating is to set it as "input" with no pull resistors? I've never had to make pin floating intentionally. Pin will be pulled up by 5K or 10K resistor (not decided yet). Goal is to control Pfet (please don't suggest using Nfet).

I'm guessing using this method switching between "floating" (Input) and GND (Output LOW) will be kinda slow, even using port manipulation...

Any input is welcome, Have great day.

DPM should be pretty quick. This is not tested but it should work (manipulating pin '2'), toggling between HiZ and active-low states (will need an external pull-up to see high level.)

#define MEGA    1   //comment out for Uno

void setup( void )
{
#ifdef MEGA
    //Mega2560
    //pin '2' is PE4
    DDRE &= 0b11101111;     //set as input
    PORTE &= 0b11101111;    //set output register LOW    
#else
    //Uno R3
    //pin '2' is PD2
    DDRD &= 0b11111011;     //set as input
    PORTD &= 0b11111011;    //set output register LOW    
#endif
    
}//setup

void loop( void )
{
#ifdef MEGA
    DDRE ^= 0b00010000;
#else
    DDRD ^= 0b00000100;
#endif
    delay(10);
    
}//loop

Thank you for your reply,

I will dig up from manual needed registers for my chip, but I just wondered, it that a good practice for a commercial product? As I said – I've never made pins "floating" intentionally...

I just noticed (sorry, I'm slow) you're using the STM32F103. Check out the reference manual; there are open-drain output options available that are similar to the AVR code I posted above.

Direct register manipulation removes portability in many cases but if speed/finesse trumps portability, there's nothing wrong with making that choice. You just need to understand how that choice will affect your options in the future.

I'm guessing using this method switching between "floating" (Input) and GND (Output LOW) will be kinda slow, even using port manipulation...

Why do you think that? On most chips, switching a pin between input and output is comparable in speed to changing the output state from 0 to 1...

Thank you again, I can figure out code easily, that's not a problem. And don't you dare to apologize for replying and helping :D

Datasheet info:

Subject to the specific hardware characteristics of each I/O port listed in the datasheet, each port bit of the General Purpose IO (GPIO) Ports, can be individually configured by software in several modes: • Input floating • Input pull-up • Input-pull-down • Analog • Output open-drain • Output push-pull • Alternate function push-pull • Alternate function open-drain

Output open-drain, so it's capable of doing what I need in "Output" mode. I'm playing with MCU for a while, but I never thought of using open drain as output mode or even knew this mode is as output mode, I think I learned something important today – :sunglasses:

westfw: Why do you think that?

Well, if Open drain wasn't available the only way would be to make IO as floating output and that's more than one register to modify, so it's naturally slower. But as open-drain is one of the output type it should be only one register.

It seems STM32duino core has "OUTPUT_OPEN_DRAIN" as output, so this should work:

pinMode(IOpin, OUTPUT_OPEN_DRAIN);

if Open drain wasn't available the only way would be to make IO as floating output and that's more than one register to modify, so it's naturally slower

On AVR, to simulate open drain you initialize the PORT registers to 0, and then to output bits you switch the DDR register between INPUT (outputs the pre-configured 0) and INPUT, so it's still just one register write.

I know, but I'm not using AVR :)

3Dgeo: I'm guessing using this method switching between "floating" (Input) and GND (Output LOW) will be kinda slow, even using port manipulation...

Why would it be slow? You guess wrong. Changing between INPUT and OUTPUT is no different to writing LOW or HIGH. Basically the code sets or clears one bit in a port control register to affect any such change to a pin driver.

INPUT actually means floating, since a GPIO pin is always an input (you can read its state whether its being driven as an output or not).

Often datasheets for microcontrollers show a schematic for the pin-driver logic, showing how the various port control register bits are combined to determine the output transistor drive states. There are two main transistors for high and low drive, perhaps some auxiliary ones for doing pull-up and/or pull down, and often some other bells and whistles.