Goodday
I thought this could be good learning curve to get started with Zephyr. Im having some problems with the GPIO. If I rewrite this code for 3 buttons with PORT1 (pin 1.10 pin 1.13 and pin 1.15) the code works. But it does not work on PORT 0 with 10 buttons. I presume it may be something with the DTS file that may be configuring the ports for other purposes (SPI/UART/I2C) etc.
1. How do I use PORT 0 in zephyr instead of port 1.
2. is there anything I need to reconfigure in the DTS file to make it work.
Any help appreciated as always
/* * Copyright (c) 2016 Open-RnD Sp. z o.o. * Copyright (c) 2020 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr/kernel.h> #include <zephyr/device.h> #include <zephyr/drivers/gpio.h> #include <zephyr/sys/util.h> #include <zephyr/sys/printk.h> #include <inttypes.h> #define SLEEP_TIME_MS 1 /* * Get button configuration from the devicetree sw0 alias. This is mandatory. */ #define SW0_NODE DT_ALIAS(sw0) #if !DT_NODE_HAS_STATUS(SW0_NODE, okay) #error "Unsupported board: sw0 devicetree alias is not defined" #endif static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET_OR(SW0_NODE, gpios, {0}); static struct gpio_callback button0_cb_data; static struct gpio_callback button1_cb_data; static struct gpio_callback button2_cb_data; static struct gpio_callback button3_cb_data; static struct gpio_callback button4_cb_data; static struct gpio_callback button5_cb_data; static struct gpio_callback button6_cb_data; static struct gpio_callback button7_cb_data; static struct gpio_callback button8_cb_data; static struct gpio_callback button9_cb_data; static struct gpio_callback button10_cb_data; #define BUT_PIN1 13 //port 0 #define BUT_PIN2 15 //port 0 #define BUT_PIN3 17 //port 0 #define BUT_PIN4 20 //port 0 #define BUT_PIN5 24 //port 0 #define BUT_PIN6 02 //port 0 #define BUT_PIN7 9 //port 0 #define BUT_PIN8 10 //port 0 #define BUT_PIN9 29 //port 0 #define BUT_PIN10 31 //port 0 /* * The led0 devicetree alias is optional. If present, we'll use it * to turn on the LED whenever the button is pressed. */ static struct gpio_dt_spec led = GPIO_DT_SPEC_GET_OR(DT_ALIAS(led0), gpios, {0}); void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins) { uint32_t bit_to_pin = 0; for (int i = 0; i < 32; i++) { if ((pins >> i) == 1) { bit_to_pin = i; break; } } printk("Button %d pressed at %" PRIu32 "\n", bit_to_pin, k_cycle_get_32()); static bool val; val = !val; gpio_pin_set_dt(&led, val); } void main(void) { int ret; if (!device_is_ready(button.port)) { printk("Error: button device %s is not ready\n", button.port->name); return; } ret = gpio_pin_configure_dt(&button, GPIO_INPUT); if (ret != 0) { printk("Error %d: failed to configure %s pin %d\n", ret, button.port->name, button.pin); return; } ret = gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_TO_ACTIVE); if (ret != 0) { printk("Error %d: failed to configure interrupt on %s pin %d\n", ret, button.port->name, button.pin); return; } ret = gpio_pin_configure(button.port, BUT_PIN1, GPIO_INPUT | GPIO_PULL_UP); printk("ret %d\n", ret); ret = gpio_pin_configure(button.port, BUT_PIN2, GPIO_INPUT | GPIO_PULL_UP); printk("ret %d\n", ret); ret = gpio_pin_configure(button.port, BUT_PIN3, GPIO_INPUT | GPIO_PULL_UP); printk("ret %d\n", ret); ret = gpio_pin_configure(button.port, BUT_PIN4, GPIO_INPUT | GPIO_PULL_UP); printk("ret %d\n", ret); ret = gpio_pin_configure(button.port, BUT_PIN5, GPIO_INPUT | GPIO_PULL_UP); printk("ret %d\n", ret); ret = gpio_pin_configure(button.port, BUT_PIN6, GPIO_INPUT | GPIO_PULL_UP); printk("ret %d\n", ret); ret = gpio_pin_configure(button.port, BUT_PIN7, GPIO_INPUT | GPIO_PULL_UP); printk("ret %d\n", ret); ret = gpio_pin_configure(button.port, BUT_PIN8, GPIO_INPUT | GPIO_PULL_UP); printk("ret %d\n", ret); ret = gpio_pin_configure(button.port, BUT_PIN9, GPIO_INPUT | GPIO_PULL_UP); printk("ret %d\n", ret); ret = gpio_pin_configure(button.port, BUT_PIN10, GPIO_INPUT | GPIO_PULL_UP); printk("ret %d\n", ret); ret = gpio_pin_interrupt_configure(button.port, BUT_PIN1, GPIO_INT_EDGE_BOTH); printk("ret %d\n", ret); ret = gpio_pin_interrupt_configure(button.port, BUT_PIN2, GPIO_INT_EDGE_BOTH); printk("ret %d\n", ret); ret = gpio_pin_interrupt_configure(button.port, BUT_PIN3, GPIO_INT_EDGE_BOTH); printk("ret %d\n", ret); ret = gpio_pin_interrupt_configure(button.port, BUT_PIN4, GPIO_INT_EDGE_BOTH); printk("ret %d\n", ret); ret = gpio_pin_interrupt_configure(button.port, BUT_PIN5, GPIO_INT_EDGE_BOTH); printk("ret %d\n", ret); ret = gpio_pin_interrupt_configure(button.port, BUT_PIN6, GPIO_INT_EDGE_BOTH); printk("ret %d\n", ret); ret = gpio_pin_interrupt_configure(button.port, BUT_PIN7, GPIO_INT_EDGE_BOTH); printk("ret %d\n", ret); ret = gpio_pin_interrupt_configure(button.port, BUT_PIN8, GPIO_INT_EDGE_BOTH); printk("ret %d\n", ret); ret = gpio_pin_interrupt_configure(button.port, BUT_PIN9, GPIO_INT_EDGE_BOTH); printk("ret %d\n", ret); ret = gpio_pin_interrupt_configure(button.port, BUT_PIN10, GPIO_INT_EDGE_BOTH); printk("ret %d\n", ret); gpio_init_callback(&button1_cb_data, button_pressed, BIT(BUT_PIN1)); ret = gpio_add_callback(button.port, &button1_cb_data); printk("ret %d\n", ret); gpio_init_callback(&button2_cb_data, button_pressed, BIT(BUT_PIN2)); ret = gpio_add_callback(button.port, &button2_cb_data); printk("ret %d\n", ret); gpio_init_callback(&button3_cb_data, button_pressed, BIT(BUT_PIN3)); ret = gpio_add_callback(button.port, &button3_cb_data); printk("ret %d\n", ret); gpio_init_callback(&button4_cb_data, button_pressed, BIT(BUT_PIN4)); ret = gpio_add_callback(button.port, &button4_cb_data); printk("ret %d\n", ret); gpio_init_callback(&button5_cb_data, button_pressed, BIT(BUT_PIN5)); ret = gpio_add_callback(button.port, &button5_cb_data); printk("ret %d\n", ret); gpio_init_callback(&button6_cb_data, button_pressed, BIT(BUT_PIN6)); ret = gpio_add_callback(button.port, &button6_cb_data); printk("ret %d\n", ret); gpio_init_callback(&button7_cb_data, button_pressed, BIT(BUT_PIN7)); ret = gpio_add_callback(button.port, &button7_cb_data); printk("ret %d\n", ret); gpio_init_callback(&button8_cb_data, button_pressed, BIT(BUT_PIN8)); ret = gpio_add_callback(button.port, &button8_cb_data); printk("ret %d\n", ret); gpio_init_callback(&button9_cb_data, button_pressed, BIT(BUT_PIN9)); ret = gpio_add_callback(button.port, &button9_cb_data); printk("ret %d\n", ret); gpio_init_callback(&button10_cb_data, button_pressed, BIT(BUT_PIN10)); ret = gpio_add_callback(button.port, &button10_cb_data); printk("ret %d\n", ret); gpio_init_callback(&button0_cb_data, button_pressed, BIT(button.pin)); gpio_add_callback(button.port, &button0_cb_data); printk("Set up button at %s pin %d\n", button.port->name, button.pin); if (led.port && !device_is_ready(led.port)) { printk("Error %d: LED device %s is not ready; ignoring it\n", ret, led.port->name); led.port = NULL; } if (led.port) { ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT); if (ret != 0) { printk("Error %d: failed to configure LED device %s pin %d\n", ret, led.port->name, led.pin); led.port = NULL; } else { printk("Set up LED at %s pin %d\n", led.port->name, led.pin); } } printk("Press the button\n"); }