From 0afe39c8fe001907b3600cc69abadd06a756c9cf Mon Sep 17 00:00:00 2001 From: hangpersonal Date: Mon, 21 Sep 2026 22:29:53 -0700 Subject: [PATCH] Update repo --- apps/05_button_exti_irq/CMakeLists.txt | 10 ++ .../boards/nucleo_f767zi.overlay | 33 ++++++ .../boards/stm32f4_disco.overlay | 24 ++++ apps/05_button_exti_irq/prj.conf | 7 ++ apps/05_button_exti_irq/src/main.c | 66 +++++++++++ apps/06_adc_pa1_irq/CMakeLists.txt | 10 ++ .../boards/nucleo_f767zi.overlay | 33 ++++++ .../boards/stm32f4_disco.overlay | 24 ++++ apps/06_adc_pa1_irq/prj.conf | 7 ++ apps/06_adc_pa1_irq/src/main.c | 110 ++++++++++++++++++ 10 files changed, 324 insertions(+) create mode 100644 apps/05_button_exti_irq/CMakeLists.txt create mode 100644 apps/05_button_exti_irq/boards/nucleo_f767zi.overlay create mode 100644 apps/05_button_exti_irq/boards/stm32f4_disco.overlay create mode 100644 apps/05_button_exti_irq/prj.conf create mode 100644 apps/05_button_exti_irq/src/main.c create mode 100644 apps/06_adc_pa1_irq/CMakeLists.txt create mode 100644 apps/06_adc_pa1_irq/boards/nucleo_f767zi.overlay create mode 100644 apps/06_adc_pa1_irq/boards/stm32f4_disco.overlay create mode 100644 apps/06_adc_pa1_irq/prj.conf create mode 100644 apps/06_adc_pa1_irq/src/main.c diff --git a/apps/05_button_exti_irq/CMakeLists.txt b/apps/05_button_exti_irq/CMakeLists.txt new file mode 100644 index 0000000..b687ee8 --- /dev/null +++ b/apps/05_button_exti_irq/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(button_exti_irq) + +# Add source files belong to this application +target_sources(app PRIVATE src/main.c) \ No newline at end of file diff --git a/apps/05_button_exti_irq/boards/nucleo_f767zi.overlay b/apps/05_button_exti_irq/boards/nucleo_f767zi.overlay new file mode 100644 index 0000000..5297d83 --- /dev/null +++ b/apps/05_button_exti_irq/boards/nucleo_f767zi.overlay @@ -0,0 +1,33 @@ +/ { + 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 = ; + }; + }; +}; + +&adc1{ + pinctrl-0 = <&adc1_in1_pa1 &adc1_in4_pa4 &adc1_in8_pb0>; + pinctrl-names = "default"; + st,adc-prescaler = <4>; + status = "okay"; +}; + +/*---Uncomment this block ONLY if your build complains the labels are undeclared--- +&pinctrl{ + adc1_in1_pa1: adc1_in1_pa1 { pinmux = ; } + adc1_in4_pa4: adc1_in4_pa4 { pinmux = ; } + adc1_in8_pb0: adc1_in8_pb0 { pinmux = ; } +} +*/ \ No newline at end of file diff --git a/apps/05_button_exti_irq/boards/stm32f4_disco.overlay b/apps/05_button_exti_irq/boards/stm32f4_disco.overlay new file mode 100644 index 0000000..0a9ee94 --- /dev/null +++ b/apps/05_button_exti_irq/boards/stm32f4_disco.overlay @@ -0,0 +1,24 @@ +/ { + aliases { + led0 = &red_led_5; + led1 = &blue_led_6; + led2 = &green_led_4; + led3 = &orange_led_3; + sw0 = &user_button; + }; +}; + +&adc1{ + pinctrl-0 = <&adc1_in1_pa1 &adc1_in4_pa4 &adc1_in8_pb0>; + pinctrl-names = "default"; + st,adc-prescaler = <4>; + status = "okay"; +}; + +/*---Uncomment this block ONLY if your build complains the labels are undeclared--- +&pinctrl{ + adc1_in1_pa1: adc1_in1_pa1 { pinmux = ; } + adc1_in4_pa4: adc1_in4_pa4 { pinmux = ; } + adc1_in8_pb0: adc1_in8_pb0 { pinmux = ; } +} +*/ \ No newline at end of file diff --git a/apps/05_button_exti_irq/prj.conf b/apps/05_button_exti_irq/prj.conf new file mode 100644 index 0000000..8a70ef3 --- /dev/null +++ b/apps/05_button_exti_irq/prj.conf @@ -0,0 +1,7 @@ +CONFIG_GPIO=y +CONFIG_CONSOLE=y +CONFIG_PRINTK=y +CONFIG_SERIAL=y +CONFIG_ADC=y +CONFIG_UART_INTERRUPT_DRIVEN=y + diff --git a/apps/05_button_exti_irq/src/main.c b/apps/05_button_exti_irq/src/main.c new file mode 100644 index 0000000..9cda78a --- /dev/null +++ b/apps/05_button_exti_irq/src/main.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include +#include + + +#if !DT_NODE_HAS_STATUS(DT_ALIAS(sw0), okay) +#error "Board does not define alias sw0. Add a boad overlay" +#endif + +static const struct gpio_dt_spec button = + GPIO_DT_SPEC_GET(DT_ALIAS(sw0), gpios); + +static struct gpio_callback button_cb; + + +static void button_isr(const struct device *port, + struct gpio_callback *cb, + gpio_port_pins_t pins) +{ + ARG_UNUSED(port); + ARG_UNUSED(pins); + ARG_UNUSED(cb); + + // Do something + int v = gpio_pin_get_dt(&button); + + if(v >= 0) { + printk("[IRQ] Button state: %s\r\n", v ? "ACTIVE" : "INVACTIVE"); + } + +} + + +int main(void) +{ + if(!device_is_ready(button.port)) { + return 0; + } + + if(gpio_pin_configure_dt(&button, GPIO_INPUT) != 0) { + return 0; + } + + // Set up callback and register it + gpio_init_callback(&button_cb, button_isr, BIT(button.pin)); + gpio_add_callback(button.port, &button_cb); + + + if(gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_BOTH) != 0) { + return 0; + } + + printk("\r\n[EXTI] Button pin interrupt is ready. Press button\r\n"); + + while (1) { + + k_sleep(K_FOREVER); + + } + + return 0; +} \ No newline at end of file diff --git a/apps/06_adc_pa1_irq/CMakeLists.txt b/apps/06_adc_pa1_irq/CMakeLists.txt new file mode 100644 index 0000000..ba6309b --- /dev/null +++ b/apps/06_adc_pa1_irq/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(adc_pa1_irq) + +# Add source files belong to this application +target_sources(app PRIVATE src/main.c) \ No newline at end of file diff --git a/apps/06_adc_pa1_irq/boards/nucleo_f767zi.overlay b/apps/06_adc_pa1_irq/boards/nucleo_f767zi.overlay new file mode 100644 index 0000000..72d53b4 --- /dev/null +++ b/apps/06_adc_pa1_irq/boards/nucleo_f767zi.overlay @@ -0,0 +1,33 @@ +/ { + 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 = ; + }; + }; +}; + +&adc1{ + pinctrl-0 = <&adc1_in1_pa1>; + pinctrl-names = "default"; + st,adc-prescaler = <4>; + status = "okay"; +}; + +/*---Uncomment this block ONLY if your build complains the labels are undeclared--- +&pinctrl{ + adc1_in1_pa1: adc1_in1_pa1 { pinmux = ; } + adc1_in4_pa4: adc1_in4_pa4 { pinmux = ; } + adc1_in8_pb0: adc1_in8_pb0 { pinmux = ; } +} +*/ \ No newline at end of file diff --git a/apps/06_adc_pa1_irq/boards/stm32f4_disco.overlay b/apps/06_adc_pa1_irq/boards/stm32f4_disco.overlay new file mode 100644 index 0000000..849045b --- /dev/null +++ b/apps/06_adc_pa1_irq/boards/stm32f4_disco.overlay @@ -0,0 +1,24 @@ +/ { + aliases { + led0 = &red_led_5; + led1 = &blue_led_6; + led2 = &green_led_4; + led3 = &orange_led_3; + sw0 = &user_button; + }; +}; + +&adc1{ + pinctrl-0 = <&adc1_in1_pa1>; + pinctrl-names = "default"; + st,adc-prescaler = <4>; + status = "okay"; +}; + +/*---Uncomment this block ONLY if your build complains the labels are undeclared--- +&pinctrl{ + adc1_in1_pa1: adc1_in1_pa1 { pinmux = ; } + adc1_in4_pa4: adc1_in4_pa4 { pinmux = ; } + adc1_in8_pb0: adc1_in8_pb0 { pinmux = ; } +} +*/ \ No newline at end of file diff --git a/apps/06_adc_pa1_irq/prj.conf b/apps/06_adc_pa1_irq/prj.conf new file mode 100644 index 0000000..8a70ef3 --- /dev/null +++ b/apps/06_adc_pa1_irq/prj.conf @@ -0,0 +1,7 @@ +CONFIG_GPIO=y +CONFIG_CONSOLE=y +CONFIG_PRINTK=y +CONFIG_SERIAL=y +CONFIG_ADC=y +CONFIG_UART_INTERRUPT_DRIVEN=y + diff --git a/apps/06_adc_pa1_irq/src/main.c b/apps/06_adc_pa1_irq/src/main.c new file mode 100644 index 0000000..3eb1576 --- /dev/null +++ b/apps/06_adc_pa1_irq/src/main.c @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +#include +#include + + +#define ADC_NODE DT_NODELABEL(adc1) +#define ADC_SAMPLE_PERIOD K_MSEC(50) + +#if !DT_NODE_HAS_STATUS(ADC_NODE, okay) +#error "ADC1 not available; enable it in a board overlay if needed." +#endif + +static const struct device *const adc_dev = + DEVICE_DT_GET(ADC_NODE); + +static void adc_sample_work_handler(struct k_work *work); + +static enum adc_action adc_cb(const struct device *dev, + const struct adc_sequence *sequence, + uint16_t sampling_index); + +static K_WORK_DELAYABLE_DEFINE(adc_sample_work, adc_sample_work_handler); + + +static uint16_t sample; + + +static struct adc_sequence_options seq_opts = { + .interval_us = 0, // Let driver decide + .extra_samplings = 0, // One-shot, resubmit work continuously + .user_data = NULL, + .callback = adc_cb + +}; + + +// Channel Configuration +static struct adc_channel_cfg ch1_cfg = { + .gain = ADC_GAIN_1, + .reference = ADC_REF_INTERNAL, + .acquisition_time = ADC_ACQ_TIME_DEFAULT, + .channel_id = 1, + .differential = 0, +}; + + +static struct adc_sequence seq = { + .channels = BIT(1), // Channel bitmap + .buffer = &sample, + .buffer_size = sizeof(sample), + .resolution = 12, + .options = &seq_opts +}; + + +static enum adc_action adc_cb(const struct device *dev, + const struct adc_sequence *sequence, + uint16_t sampling_index) +{ + ARG_UNUSED(dev); + ARG_UNUSED(sequence); + ARG_UNUSED(sampling_index); + + // This callback runs in interrupt context + return ADC_ACTION_FINISH; +} + + +static void adc_sample_work_handler(struct k_work *work) +{ + ARG_UNUSED(work); + + int rc = adc_read(adc_dev, &seq); + + if (rc == 0) { + printk("Sensor = %u\r\n", (unsigned int)sample); + } else { + printk("ADC read failed: %d\r\n", rc); + } + + // Schedule the next conversion without flooding the CPU/UART + (void)k_work_reschedule(&adc_sample_work, ADC_SAMPLE_PERIOD); +} + + +int main(void) +{ + if(!device_is_ready(adc_dev)) { + return 0; + } + + // Configure the channel + if(adc_channel_setup(adc_dev, &ch1_cfg) != 0) { + return 0; + } + + printk("\r\n[ADC IRQ] PA1(ADC1_IN1)\r\n"); + + (void)k_work_schedule(&adc_sample_work, K_NO_WAIT); + + while (1) { + k_sleep(K_FOREVER); + } + + return 0; +}