diff --git a/apps/02_uart/src/main.c b/apps/02_uart/src/main.c index 43e9017..479ce11 100644 --- a/apps/02_uart/src/main.c +++ b/apps/02_uart/src/main.c @@ -78,10 +78,10 @@ int main(void) while (1) { while (k_msgq_get(&uart_rx_queue, &ch, K_NO_WAIT) == 0) { if (ch == 'O' || ch == 'o') { - (void)gpio_pin_set_dt(&led_red, 1); + gpio_pin_set_dt(&led_red, 1); printk("LED on\r\n"); } else if (ch == 'F' || ch == 'f') { - (void)gpio_pin_set_dt(&led_red, 0); + gpio_pin_set_dt(&led_red, 0); printk("LED off\r\n"); } } diff --git a/apps/03_adc_pa1_poll/CMakeLists.txt b/apps/03_adc_pa1_poll/CMakeLists.txt new file mode 100644 index 0000000..29640c5 --- /dev/null +++ b/apps/03_adc_pa1_poll/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_single_channel) + +# Add source files belong to this application +target_sources(app PRIVATE src/main.c) \ No newline at end of file diff --git a/apps/03_adc_pa1_poll/boards/nucleo_f767zi.overlay b/apps/03_adc_pa1_poll/boards/nucleo_f767zi.overlay new file mode 100644 index 0000000..cef8ca3 --- /dev/null +++ b/apps/03_adc_pa1_poll/boards/nucleo_f767zi.overlay @@ -0,0 +1,25 @@ +/ { + 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"; +}; \ No newline at end of file diff --git a/apps/03_adc_pa1_poll/boards/stm32f4_disco.overlay b/apps/03_adc_pa1_poll/boards/stm32f4_disco.overlay new file mode 100644 index 0000000..d3dc50d --- /dev/null +++ b/apps/03_adc_pa1_poll/boards/stm32f4_disco.overlay @@ -0,0 +1,16 @@ +/ { + 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"; +}; \ No newline at end of file diff --git a/apps/03_adc_pa1_poll/prj.conf b/apps/03_adc_pa1_poll/prj.conf new file mode 100644 index 0000000..8f673de --- /dev/null +++ b/apps/03_adc_pa1_poll/prj.conf @@ -0,0 +1,6 @@ +CONFIG_GPIO=y +CONFIG_SERIAL=y +CONFIG_ADC=y +CONFIG_UART_INTERRUPT_DRIVEN=y +CONFIG_CONSOLE=y +CONFIG_PRINTK=y diff --git a/apps/03_adc_pa1_poll/src/main.c b/apps/03_adc_pa1_poll/src/main.c new file mode 100644 index 0000000..0cd18f0 --- /dev/null +++ b/apps/03_adc_pa1_poll/src/main.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +#include + + +#define ADC_NODE DT_NODELABEL(adc1) + +#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 uint16_t sample; + +// 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, // 12-bit +}; + + +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] PA1(ADVC1_IN1) polling\r\n"); + + while (1) { + + int rc = adc_read(adc_dev, &seq); + if(rc == 0) { + uint16_t raw = sample; + printk("ADC raw value = %u\r\n", raw); + } + + k_sleep(K_MSEC(50)); + + } + + return 0; +} diff --git a/apps/04_adc_pa1_pa4_pb0_poll/CMakeLists.txt b/apps/04_adc_pa1_pa4_pb0_poll/CMakeLists.txt new file mode 100644 index 0000000..1dea049 --- /dev/null +++ b/apps/04_adc_pa1_pa4_pb0_poll/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_multichannel) + +# Add source files belong to this application +target_sources(app PRIVATE src/main.c) \ No newline at end of file diff --git a/apps/04_adc_pa1_pa4_pb0_poll/boards/nucleo_f767zi.overlay b/apps/04_adc_pa1_pa4_pb0_poll/boards/nucleo_f767zi.overlay new file mode 100644 index 0000000..5297d83 --- /dev/null +++ b/apps/04_adc_pa1_pa4_pb0_poll/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/04_adc_pa1_pa4_pb0_poll/boards/stm32f4_disco.overlay b/apps/04_adc_pa1_pa4_pb0_poll/boards/stm32f4_disco.overlay new file mode 100644 index 0000000..0a9ee94 --- /dev/null +++ b/apps/04_adc_pa1_pa4_pb0_poll/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/04_adc_pa1_pa4_pb0_poll/prj.conf b/apps/04_adc_pa1_pa4_pb0_poll/prj.conf new file mode 100644 index 0000000..8f673de --- /dev/null +++ b/apps/04_adc_pa1_pa4_pb0_poll/prj.conf @@ -0,0 +1,6 @@ +CONFIG_GPIO=y +CONFIG_SERIAL=y +CONFIG_ADC=y +CONFIG_UART_INTERRUPT_DRIVEN=y +CONFIG_CONSOLE=y +CONFIG_PRINTK=y diff --git a/apps/04_adc_pa1_pa4_pb0_poll/src/main.c b/apps/04_adc_pa1_pa4_pb0_poll/src/main.c new file mode 100644 index 0000000..7c5d322 --- /dev/null +++ b/apps/04_adc_pa1_pa4_pb0_poll/src/main.c @@ -0,0 +1,107 @@ +#include +#include +#include +#include +#include +#include +#include + + +#define ADC_NODE DT_NODELABEL(adc1) + +#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 uint16_t sample; + +// Channels Configuration: PA1(IN1), PA4(IN4), PB0(IN8) + +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_channel_cfg ch4_cfg = { + .gain = ADC_GAIN_1, + .reference = ADC_REF_INTERNAL, + .acquisition_time = ADC_ACQ_TIME_DEFAULT, + .channel_id = 4, + .differential = 0, +}; + +static struct adc_channel_cfg ch8_cfg = { + .gain = ADC_GAIN_1, + .reference = ADC_REF_INTERNAL, + .acquisition_time = ADC_ACQ_TIME_DEFAULT, + .channel_id = 8, + .differential = 0, +}; + +// A reusable sequence object for "one channel per read" +static struct adc_sequence seq = { + .channels = 0, // Zero for now, we will update before we read + .buffer = &sample, + .buffer_size = sizeof(sample), + .resolution = 12, // 12-bit +}; + +static int read_one(uint8_t ch, uint16_t *out) { + + // Set the channel to read + seq.channels = BIT(ch); + + // Read channel + int rc = adc_read(adc_dev, &seq); + + // If read is successful store value in 'out' + if(rc == 0) { + *out = sample; + } + + return rc; + +} + + +int main(void) +{ + if(!device_is_ready(adc_dev)) { + return 0; + } + + // Configure the channels + if(adc_channel_setup(adc_dev, &ch1_cfg) || + adc_channel_setup(adc_dev, &ch4_cfg) || + adc_channel_setup(adc_dev, &ch8_cfg)) { + return 0; + } + + printk("\r\n[ADC Multichannel] PA1, PA4, PB0\r\n"); + + while (1) { + + uint16_t v1 = 0, v4 = 0, v8 = 0; + int rc1 = read_one(1, &v1); + int rc4 = read_one(4, &v4); + int rc8 = read_one(8, &v8); + + if(rc1 == 0 && rc4 == 0 && rc8 == 0) { + printk("Sensor1 = %u, Sensor4 = %u, Sensor8 = %u \r\n", v1, v4, v8); + } + else { + printk("ADC Read: Ch1 = %d, Ch4 = %d, Ch8 = %d \r\n", rc1, rc4, rc8); + } + + k_sleep(K_MSEC(50)); + + } + + return 0; +} \ No newline at end of file