Using nrf_gpio_cfg_sense_input to trigger an interrupt for light sensor.

Hello. 

I have a light sensor connected to 2 gpio pins on nrf5340, Enable Pin : P0.08 and Output Pin: P0.07
Trying to trigger an interrupt based on the light.  

With the button example, I am able to trigger the interrupt correctly based on button press, just used that as reference and trying to trigger interrupt based on pin sense mechanism. 

C code:

#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/irq.h>
#include <hal/nrf_gpio.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/device.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>

LOG_MODULE_REGISTER(test_sense_gpiote, LOG_LEVEL_DBG); // Register a module for your logs

static const struct gpio_dt_spec phswout = GPIO_DT_SPEC_GET_BY_IDX_OR(DT_NODELABEL(user_dbg_pin), gpios, 0, {0});
static const struct gpio_dt_spec phswen = GPIO_DT_SPEC_GET_BY_IDX_OR(DT_NODELABEL(user_gbd_pin), gpios, 0, {0});

void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
    LOG_DBG("Wake up\n");
    LOG_DBG("Set up LED at %s pin %d\n", phswout.port->name, phswout.pin);
}

static struct gpio_callback button_cb_data;

int light_sleep_example(void)
{
    int ret;
    static bool flag_x = true; 
    if(!device_is_ready(phswout.port))
    {
        LOG_DBG("PH SW OUT device not ready");
        return;
    }
    if(!device_is_ready(phswen.port))
    {
        LOG_DBG("PH SW EN device not ready");
        return;
    }
    LOG_DBG("Before interrrupt call");  
    LOG_DBG("State of phswout output %d", nrf_gpio_pin_read(phswout.pin));
	
    nrf_gpio_cfg_output(phswout.pin);
    nrf_gpio_pin_clear(phswout.pin); // this drives to 1
    
	LOG_DBG("State of phswout output %d", nrf_gpio_pin_read(phswout.pin));
	nrf_gpio_cfg_sense_input(phswout.pin, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_HIGH);
    
	LOG_DBG("Sense output state %d", nrf_gpio_pin_sense_get(phswout.pin)); // show pin sense as low. 
    
	ret = gpio_pin_interrupt_configure_dt(&phswout, GPIO_INT_EDGE_TO_ACTIVE);
    gpio_init_callback(&button_cb_data, button_pressed, BIT(phswout.pin));
    gpio_add_callback(phswout.port, &button_cb_data);  
    
    nrf_gpio_cfg_sense_input(phswout.pin, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_HIGH); // with this interrupt is getting triggered immediately
	
    LOG_DBG("Sense output state %d", nrf_gpio_pin_sense_get(phswout.pin)); // shows pin sense as low only. 
    LOG_DBG("After Interrupt call");
    
    if(flag_x)
    {
        LOG_DBG("WFI");
        k_sleep(K_SECONDS(1));
        __WFI();
		//Ideally I'd expect it to wait here until I remove the sticker from the light sensor. But the interrupt fires immediately.
        k_sleep(K_SECONDS(1));
        flag_x = false;
    }
    return 0;
}



Overlay File:

/ {
	user_dbg_pin: user-dbg-pin {
		compatible = "nordic,gpio-pins";
		gpios = <&gpio0 7 GPIO_ACTIVE_HIGH>;
		status = "okay";
	};
};

/ {
	user_gbd_pin: user-gbd-pin {
		compatible = "nordic,gpio-pins";
		gpios = <&gpio0 8 GPIO_ACTIVE_HIGH>;
		status = "okay";
	};
};



Misc Details:

NRF SDK 2.7.0
Zephyr 3.6.99
NRF5340dk

Any inputs or directions on where I am going wrong?
Related