nrf9160 gpio interrupt callback handler crashes

Hi all,

I am trying to use an interrupt callback handler but I can't get it to work. I got the interrupt initialized but whenever the callback is triggered, the system crashes.

This is my code:

#include <zephyr.h>
#include <stdio.h>
#include <drivers/gpio.h>

int32_t counter = 0;

void gpio_callback_handler(const struct device *dev, struct gpio_callback *cb, uint32_t pins) {
	counter = 0;
}

void init_gpio(const struct device *gpio_dev) {
	struct gpio_callback gpio_cb;

	gpio_pin_configure(gpio_dev, 21, GPIO_INPUT);
	gpio_init_callback(&gpio_cb, gpio_callback_handler, BIT(21));
	gpio_add_callback(gpio_dev, &gpio_cb);
	gpio_pin_interrupt_configure(gpio_dev, 21, GPIO_INT_EDGE_FALLING);

}

int main(void) {
	const struct device *dev;

	uint32_t val;

	dev = device_get_binding("GPIO_0");

	init_gpio(dev);

	while(true) {
		val = gpio_pin_get(dev, 21);
		printf("Current value: %i | %i\n", val, counter);
		counter++;
		k_sleep(K_SECONDS(1));
	}

	return 0;
}

Instead of resetting the counter, it just reboots. And I can't understand why this is happening. From my understanding, this should work..

I already tried changing "counter = 0" inside the callback handler to one printf but this also crashes.

Can anybody explain to me why this is not working?

Thanks in advance!

Related