No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s2:aih:lab:nrf52gpio
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:edu:zmitac:s2:aih:lab:nrf52gpio [2023/11/26 23:24] – ktokarz | en:edu:zmitac:s2:aih:lab:nrf52gpio [2025/11/14 10:59] (current) – [Tasks for the laboratory on GPIO] ktokarz | ||
|---|---|---|---|
| Line 214: | Line 214: | ||
| ===== Tasks for the laboratory on GPIO ===== | ===== Tasks for the laboratory on GPIO ===== | ||
| - Import the Blinky (zephyr_blink) example in the VS Code, as shown in the introduction to PlatformIO document. | - Import the Blinky (zephyr_blink) example in the VS Code, as shown in the introduction to PlatformIO document. | ||
| - | - Find a zephyr-button example. It's not importable as the project, so the simplest solution is to replace files main.c and prj.conf in the already imported project (e.g. Blinky). Files can be found in the folder < | + | - Find a zephyr-button example. It's not importable as the project, so the simplest solution is to replace files main.c and prj.conf in the already imported project (e.g. Blinky). Files can be found in the folder < |
| - Merge two projects “blink”, | - Merge two projects “blink”, | ||
| + | ===== The code for the laboratory on GPIO ===== | ||
| + | ==== The Zephyr-button code example ==== | ||
| + | <code c> | ||
| + | /* | ||
| + | * Copyright (c) 2016 Open-RnD Sp. z o.o. | ||
| + | * Copyright (c) 2020 Nordic Semiconductor ASA | ||
| + | * | ||
| + | * SPDX-License-Identifier: | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #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, | ||
| + | #error " | ||
| + | #endif | ||
| + | static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET_OR(SW0_NODE, | ||
| + | {0}); | ||
| + | static struct gpio_callback button_cb_data; | ||
| + | |||
| + | /* | ||
| + | * 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), | ||
| + | {0}); | ||
| + | |||
| + | void button_pressed(const struct device *dev, struct gpio_callback *cb, | ||
| + | uint32_t pins) | ||
| + | { | ||
| + | printk(" | ||
| + | } | ||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | int ret; | ||
| + | |||
| + | if (!gpio_is_ready_dt(& | ||
| + | printk(" | ||
| + | | ||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | ret = gpio_pin_configure_dt(& | ||
| + | if (ret != 0) { | ||
| + | printk(" | ||
| + | ret, button.port-> | ||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | ret = gpio_pin_interrupt_configure_dt(& | ||
| + | GPIO_INT_EDGE_TO_ACTIVE); | ||
| + | if (ret != 0) { | ||
| + | printk(" | ||
| + | ret, button.port-> | ||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | gpio_init_callback(& | ||
| + | gpio_add_callback(button.port, | ||
| + | printk(" | ||
| + | |||
| + | if (led.port && !gpio_is_ready_dt(& | ||
| + | printk(" | ||
| + | ret, led.port-> | ||
| + | led.port = NULL; | ||
| + | } | ||
| + | if (led.port) { | ||
| + | ret = gpio_pin_configure_dt(& | ||
| + | if (ret != 0) { | ||
| + | printk(" | ||
| + | ret, led.port-> | ||
| + | led.port = NULL; | ||
| + | } else { | ||
| + | printk(" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | printk(" | ||
| + | if (led.port) { | ||
| + | while (1) { | ||
| + | /* If we have an LED, match its state to the button' | ||
| + | int val = gpio_pin_get_dt(& | ||
| + | |||
| + | if (val >= 0) { | ||
| + | gpio_pin_set_dt(& | ||
| + | } | ||
| + | k_msleep(SLEEP_TIME_MS); | ||
| + | } | ||
| + | } | ||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
en/edu/zmitac/s2/aih/lab/nrf52gpio.1701037474.txt.gz · Last modified: by ktokarz
