Hey everyone,
im currently trying to print a message once a gpio interrupt is fired over usb to the serial console. Therefore i basicly merged the basic/button sample with the usb/console sample.
https://github.com/zephyrproject-rtos/zephyr/tree/main/samples/basic/button
https://github.com/zephyrproject-rtos/zephyr/tree/main/samples/subsys/usb/console
Unfortunately my board freezes once the callback function is called. Do you have any advice how i can solve or debug this?
best regards
/* * Copyright (c) 2016 Intel Corporation. * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr.h> #include <sys/printk.h> #include <sys/util.h> #include <string.h> #include <usb/usb_device.h> #include <drivers/uart.h> #include <drivers/gpio.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 button_cb_data; void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins) { printk("Button pressed at %" PRIu32 "\n", k_cycle_get_32()); } void main(void) { const struct device *dev = device_get_binding( CONFIG_UART_CONSOLE_ON_DEV_NAME); uint32_t dtr = 0; if (usb_enable(NULL)) { return; } /* Poll if the DTR flag was set, optional */ while (!dtr) { uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr); } if (strlen(CONFIG_UART_CONSOLE_ON_DEV_NAME) != strlen("CDC_ACM_0") || strncmp(CONFIG_UART_CONSOLE_ON_DEV_NAME, "CDC_ACM_0", strlen(CONFIG_UART_CONSOLE_ON_DEV_NAME))) { printk("Error: Console device name is not USB ACM\n"); return; } 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; } gpio_init_callback(&button_cb_data, button_pressed, BIT(button.pin)); gpio_add_callback(button.port, &button_cb_data); printk("Set up button at %s pin %d\n", button.port->name, button.pin); while (1) { printk("Hello World! %s\n", CONFIG_ARCH); k_sleep(K_SECONDS(1)); } }
CONFIG_USB=y CONFIG_USB_DEVICE_STACK=y CONFIG_USB_DEVICE_PRODUCT="Zephyr USB console sample" CONFIG_USB_UART_CONSOLE=y CONFIG_UART_INTERRUPT_DRIVEN=y CONFIG_UART_LINE_CTRL=y CONFIG_UART_CONSOLE_ON_DEV_NAME="CDC_ACM_0" CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC=y CONFIG_GPIO=y