Pin as DigitalOut and InterruptIn

Hi!
I’m using a P-Nucle-WB55RG board for a project with an I2C sensor, for the sensor to activate I2C, it has to have the interrupt pin set at 0V at startup, which I can do with a DigitalOut.
However, this same pin is the one used for the interrupts from the sensor, so I need to configure it as an InterruptIn.
I have been searching, but I can’t find a way to “release” the DigitalOut, in order to receive the interrupts.

Thanks for your help!

Hi Samuel,
This might be a bit tricky, you may need to ‘hold off’ the power to sensor whilst the mcu is booting up for a couple of milliseconds. The state of the GPIO pins can be unpredictable during this time. So even getting this to work reliably the way you are trying may be problematic.

I would suggest to power the sensor via a GPIO pin providing its current requirements are below the pin rated current, or add a switching transistor. The transistor is the recommend method and preferable, however I power all my i2c devices via GPIO pins on many platforms without problems.

This is how I normally do this, but others may do it differently.

DigitalOut   sensor_Vcc(D1,0);  //initialize GPIO pin D1 to low that to keep the sensor power 'off'
InterruptIn  sensor_control_pin(D2);

int main()
{
    sensor_control_pin.mode(PullDown); // keep the control pin low before powering up sensor
    sensor_Vcc = 1; // now power up the sensor
    // check for initialize time and add a delay here 
    sensor_control_pin.mode(PullUp);  // might need to use pull up after start up?


// rest of you code here...

Hello,

I have no practical experience like Paul but this maybe also works.

#include "mbed.h" //MbedOS 5.15 bare metal - STM32F303K8

#define INPIN A2 // your pin place here

DigitalOut led1(LED1);
InterruptIn in(INPIN, PullDefault);

// that will hold the pin down
DigitalInOut iop(INPIN,PIN_OUTPUT,PullDefault,0);

void key(){
    led1 = !led1;
}

int main()
{
    thread_sleep_for(5000);  
    iop.input();              // that will release it for interupts
    //in.mode(PullUp);        // if a manipulation with pullups is needed
    in.rise(&key);
    
    while (true)
    {
        thread_sleep_for(100);
    }
}

BR, Jan