Add 01_blinky_button
This commit is contained in:
1 parent
7e8ce94550
commit
cd2fda47ea
5 files changed
+140
No files matched your search
@@ -0,0 +1,10 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20.0)
|
||||||
|
|
||||||
|
# Locate the Zephyr build system as a CMake package
|
||||||
|
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||||
|
|
||||||
|
# Project name
|
||||||
|
project(blinky_button)
|
||||||
|
|
||||||
|
# Add source files belong to this application
|
||||||
|
target_sources(app PRIVATE src/main.c)
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
/ {
|
||||||
|
aliases {
|
||||||
|
led0 = &red_led;
|
||||||
|
led1 = &blue_led;
|
||||||
|
led2 = &green_led;
|
||||||
|
sw0 = &user_button0;
|
||||||
|
};
|
||||||
|
|
||||||
|
gpio_keys {
|
||||||
|
compatible = "gpio-keys";
|
||||||
|
|
||||||
|
user_button0: button {
|
||||||
|
label = "Button";
|
||||||
|
gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>;
|
||||||
|
zephyr,code = <INPUT_KEY_0>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
/ {
|
||||||
|
aliases {
|
||||||
|
led0 = &red_led_5;
|
||||||
|
led1 = &blue_led_6;
|
||||||
|
led2 = &green_led_4;
|
||||||
|
led3 = &orange_led_3;
|
||||||
|
sw0 = &user_button;
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
CONFIG_GPIO=y
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Include core zephyr kernel facilities e.g. k_sleep()
|
||||||
|
#include <zephyr/kernel.h>
|
||||||
|
|
||||||
|
// Inlcude Zephyr's device model types and helpers
|
||||||
|
#include <zephyr/device.h>
|
||||||
|
|
||||||
|
// Access the C macros for reading information from the devicetree at build time
|
||||||
|
#include <zephyr/devicetree.h>
|
||||||
|
|
||||||
|
// Include GPIO driver API and the typed GPIO helpers required
|
||||||
|
#include <zephyr/drivers/gpio.h>
|
||||||
|
|
||||||
|
|
||||||
|
#if !DT_NODE_HAS_STATUS(DT_ALIAS(led0), okay)
|
||||||
|
#error "Board does not define alias led0. Add a board overlay"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !DT_NODE_HAS_STATUS(DT_ALIAS(sw0), okay)
|
||||||
|
#error "Board does not define alias sw0. Add a board overlay"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if DT_HAS_ALIAS(led0)
|
||||||
|
static const struct gpio_dt_spec led_red =
|
||||||
|
GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if DT_HAS_ALIAS(led1)
|
||||||
|
static const struct gpio_dt_spec led_blue =
|
||||||
|
GPIO_DT_SPEC_GET(DT_ALIAS(led1), gpios);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if DT_HAS_ALIAS(led2)
|
||||||
|
static const struct gpio_dt_spec led_green =
|
||||||
|
GPIO_DT_SPEC_GET(DT_ALIAS(led2), gpios);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if DT_HAS_ALIAS(sw0)
|
||||||
|
static const struct gpio_dt_spec button =
|
||||||
|
GPIO_DT_SPEC_GET(DT_ALIAS(sw0), gpios);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
|
||||||
|
bool led_state = true;
|
||||||
|
|
||||||
|
// Make sure the GPIO is ready
|
||||||
|
#if DT_HAS_ALIAS(led0)
|
||||||
|
if (!gpio_is_ready_dt(&led_red)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
gpio_pin_configure_dt(&led_red, GPIO_OUTPUT_ACTIVE);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if DT_HAS_ALIAS(led1)
|
||||||
|
if (!gpio_is_ready_dt(&led_blue)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
gpio_pin_configure_dt(&led_blue, GPIO_OUTPUT_ACTIVE);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if DT_HAS_ALIAS(led2)
|
||||||
|
if (!gpio_is_ready_dt(&led_green)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
gpio_pin_configure_dt(&led_green, GPIO_OUTPUT_INACTIVE);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if DT_HAS_ALIAS(sw0)
|
||||||
|
if (!gpio_is_ready_dt(&button)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
gpio_pin_configure_dt(&button, GPIO_INPUT);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
|
||||||
|
#if DT_HAS_ALIAS(led0)
|
||||||
|
gpio_pin_toggle_dt(&led_red);
|
||||||
|
// gpio_pin_set_dt(&led_red, led_state);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if DT_HAS_ALIAS(led1)
|
||||||
|
gpio_pin_toggle_dt(&led_blue);
|
||||||
|
// gpio_pin_set_dt(&led_blue, led_state);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
uint8_t button_status = gpio_pin_get_dt(&button);
|
||||||
|
gpio_pin_set_dt(&led_green, button_status);
|
||||||
|
|
||||||
|
led_state = !led_state;
|
||||||
|
printf("LED state: %s\n", led_state ? "ON" : "OFF");
|
||||||
|
|
||||||
|
k_msleep(500);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user