sw3 needs to be pressed to trigger ISR set up for sw2

Hi,

I am using nrf5340 audio dk for development. I had no trouble with gpio_pin_get_dt() function when reading from sw2. However, once I switched to interrupt. I had to physically press sw3 to start the ISR I set up for sw2.

My code is as following:

struct gpio_callback button_cb_data;
const struct gpio_dt_spec button;

#define BUTTON_NODE DT_ALIAS(sw2)
const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(BUTTON_NODE, gpios);

static void button_pressed_cb(const struct device *dev, struct gpio_callback *cb, uint32_t pins) {

    if (!recording_toggle_locked) {
        recording_toggle_locked = true;
        recording = !recording;  // Toggle recording state
        printk("Recording toggled: %s\n", recording ? "ON" : "OFF");
        gpio_pin_set_dt(&led, recording);

        k_work_schedule(&unlock_work, K_SECONDS(1));
    }
    printk("GPIO Pin: %u triggered callback.\n", pins);
}

int button_init(void) {
    if (!gpio_is_ready_dt(&button)) {
		LOG_ERR("Button device not ready");
		return -1;
	}
    LOG_INF("Button device ready");

    k_work_init_delayable(&unlock_work, unlock_recording_toggle_locked);

    int ret = gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_TO_ACTIVE);
    if (ret < 0) {
        LOG_ERR("Failed to configure button GPIO: %d", ret);
        return ret;
    }

    gpio_init_callback(&button_cb_data, button_pressed_cb, BIT(button.pin));
    gpio_add_callback(button.port, &button_cb_data);
    LOG_INF("Button configured");
    LOG_INF("button pin: %d", button.pin);
    LOG_INF("button port: %p", button.port);
    return 0;
}

The console shows the following:

-- [00:00:00.385,803] <inf> button: Button device ready
-- [00:00:00.385,864] <inf> button: Button configured
-- [00:00:00.385,894] <inf> button: button pin: 4
-- [00:00:00.385,894] <inf> button: button port: 0x2bb5c

GPIO Pin: 16 triggered callback.

I did not change the device tree for the board and checked the zephyr.dts file from the build output folder. The relevant part is as following:

buttons {
		compatible = "gpio-keys";
		button_1_vol_dn: button_1_vol_dn {
			gpios = < &gpio0 0x2 0x11 >;
			label = "Push button 1";
			zephyr,code = < 0x72 >;
		};
		button_2_vol_up: button_2_vol_up {
			gpios = < &gpio0 0x3 0x11 >;
			label = "Push button 2";
			zephyr,code = < 0x73 >;
		};
		button3: button_3 {
			gpios = < &gpio0 0x4 0x11 >;
			label = "Push button 3";
			zephyr,code = < 0x4 >;
		};
		button4: button_4 {
			gpios = < &gpio0 0x6 0x11 >;
			label = "Push button 4";
			zephyr,code = < 0x5 >;
		};
		button5: button_5 {
			gpios = < &gpio0 0x5 0x11 >;
			label = "Push button 5";
			zephyr,code = < 0x6 >;
		};
	};
	
	aliases {
		pwm-led0 = &rgb1_red_pwm_led;
		pwm-led1 = &rgb1_green_pwm_led;
		pwm-led2 = &rgb1_blue_pwm_led;
		red-pwm-led = &rgb1_red_pwm_led;
		green-pwm-led = &rgb1_green_pwm_led;
		blue-pwm-led = &rgb1_blue_pwm_led;
		led0 = &rgb1_red;
		led1 = &rgb1_green;
		led2 = &rgb1_blue;
		led3 = &rgb2_red;
		led4 = &rgb2_green;
		led5 = &rgb2_blue;
		led6 = &led1_blue;
		led7 = &led2_green;
		led8 = &led3_green;
		sw0 = &button_1_vol_dn;
		sw1 = &button_2_vol_up;
		sw2 = &button3;
		sw3 = &button4;
		sw4 = &button5;
		mcuboot-led0 = &led1_blue;
		mcuboot-button0 = &button3;
		watchdog0 = &wdt0;
	};

Could anyone help me with this issue? Thank you very much!

  • Hi,

    It is not quite clear to me what goes wrong here, here are some tips and things to check:

    1. Are there any other pin interrupts that are configured?
    2. What is printed from printk("GPIO Pin: %u triggered callback.\n", pins);?
    3. I would advise against using printk in an interrupt. This takes time and will lock the system for an extended period. Use LOG_XXX and deferred mode instead.
    4. Note that the swX are zero-indexed whilst the silk screen on the board is 1-indexed

    Regards,
    Amanda H.

Related