diff --git a/apps/01_blinky_button/CMakeLists.txt b/apps/01_blinky_button/CMakeLists.txt new file mode 100644 index 0000000..652f6a0 --- /dev/null +++ b/apps/01_blinky_button/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/apps/01_blinky_button/boards/nucleo_f767zi.overlay b/apps/01_blinky_button/boards/nucleo_f767zi.overlay new file mode 100644 index 0000000..8c9f503 --- /dev/null +++ b/apps/01_blinky_button/boards/nucleo_f767zi.overlay @@ -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 = ; + }; + }; +}; \ No newline at end of file diff --git a/apps/01_blinky_button/boards/stm32f4_disco.overlay b/apps/01_blinky_button/boards/stm32f4_disco.overlay new file mode 100644 index 0000000..e5b1739 --- /dev/null +++ b/apps/01_blinky_button/boards/stm32f4_disco.overlay @@ -0,0 +1,9 @@ +/ { + aliases { + led0 = &red_led_5; + led1 = &blue_led_6; + led2 = &green_led_4; + led3 = &orange_led_3; + sw0 = &user_button; + }; +}; \ No newline at end of file diff --git a/apps/01_blinky_button/prj.conf b/apps/01_blinky_button/prj.conf new file mode 100644 index 0000000..91c3c15 --- /dev/null +++ b/apps/01_blinky_button/prj.conf @@ -0,0 +1 @@ +CONFIG_GPIO=y diff --git a/apps/01_blinky_button/src/main.c b/apps/01_blinky_button/src/main.c new file mode 100644 index 0000000..2a21f46 --- /dev/null +++ b/apps/01_blinky_button/src/main.c @@ -0,0 +1,102 @@ +#include + +// Include core zephyr kernel facilities e.g. k_sleep() +#include + +// Inlcude Zephyr's device model types and helpers +#include + +// Access the C macros for reading information from the devicetree at build time +#include + +// Include GPIO driver API and the typed GPIO helpers required +#include + + +#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; + +} \ No newline at end of file