|
| 1 | +/* |
| 2 | + * Copyright 2025 NXP |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdbool.h> |
| 8 | +#include <zephyr/kernel.h> |
| 9 | +#include <zephyr/drivers/gpio.h> |
| 10 | +#include <zephyr/devicetree.h> |
| 11 | +#include <zephyr/drivers/i2s.h> |
| 12 | + |
| 13 | +#define SLEEP_TIME_MS 1000 |
| 14 | +#define LED_NODE DT_ALIAS(led1) |
| 15 | +#define BTN_NODE DT_ALIAS(sw0) |
| 16 | + |
| 17 | +static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED_NODE, gpios); |
| 18 | +static const struct gpio_dt_spec btn = GPIO_DT_SPEC_GET_OR(BTN_NODE, gpios, {0}); |
| 19 | +static struct gpio_callback button_cb_data; |
| 20 | + |
| 21 | +static bool blink = true; |
| 22 | + |
| 23 | +static struct k_thread th_blink; |
| 24 | +static K_THREAD_STACK_DEFINE(th_blink_stack, 4096); |
| 25 | + |
| 26 | +void btn_isr(const struct device *dev, struct gpio_callback *cb, uint32_t pins) |
| 27 | +{ |
| 28 | + printk("[DSP] Button pressed!\n"); |
| 29 | + blink = !blink; |
| 30 | +} |
| 31 | + |
| 32 | +void thread_blink(void *unused0, void *unused1, void *unused2) |
| 33 | +{ |
| 34 | + int ret = 0; |
| 35 | + |
| 36 | + while (1) { |
| 37 | + if (blink) { |
| 38 | + ret = gpio_pin_toggle_dt(&led); |
| 39 | + if (ret < 0) { |
| 40 | + return; |
| 41 | + } |
| 42 | + } else { |
| 43 | + ret = gpio_pin_set_dt(&led, 0); |
| 44 | + if (ret < 0) { |
| 45 | + return; |
| 46 | + } |
| 47 | + } |
| 48 | + k_msleep(SLEEP_TIME_MS); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +int main(void) |
| 53 | +{ |
| 54 | + int ret; |
| 55 | + |
| 56 | + printk("[DSP] Hello World! %s\n", CONFIG_BOARD_TARGET); |
| 57 | + |
| 58 | + if (!gpio_is_ready_dt(&btn)) { |
| 59 | + return 0; |
| 60 | + } |
| 61 | + |
| 62 | + if (gpio_pin_configure_dt(&btn, GPIO_INPUT) != 0) { |
| 63 | + return 0; |
| 64 | + } |
| 65 | + |
| 66 | + if (gpio_pin_interrupt_configure_dt(&btn, GPIO_INT_EDGE_TO_ACTIVE) != 0) { |
| 67 | + return 0; |
| 68 | + } |
| 69 | + |
| 70 | + gpio_init_callback(&button_cb_data, &btn_isr, BIT(btn.pin)); |
| 71 | + gpio_add_callback(btn.port, &button_cb_data); |
| 72 | + |
| 73 | + if (!gpio_is_ready_dt(&led)) { |
| 74 | + return 0; |
| 75 | + } |
| 76 | + |
| 77 | + ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE); |
| 78 | + if (ret < 0) { |
| 79 | + return 0; |
| 80 | + } |
| 81 | + |
| 82 | + k_thread_create(&th_blink, th_blink_stack, K_THREAD_STACK_SIZEOF(th_blink_stack), |
| 83 | + thread_blink, NULL, NULL, NULL, 5, 0, K_NO_WAIT); |
| 84 | + |
| 85 | + return 0; |
| 86 | +} |
0 commit comments