From e6f489f337b6876e24564ed521c9141f6eb58b3e Mon Sep 17 00:00:00 2001 From: TOKITA Hiroshi Date: Tue, 20 Sep 2022 20:36:23 +0900 Subject: [PATCH 1/2] zephyrCommon: Implement analogRead()/analogReference() The 'io-channels' node under the 'zephyr,user' defines available ADC channels. The 'io-channel-pins' defines the mapping of ADC channels to pin numbers. API looks up the 'io-channel-pins' when querying the ADC channel by digital pin number. So it should be in the same order as 'io-channels' node. For original Arduino API compatibility, adc resolution should configure as 10-bit. ADC that is on MCU supported by Zephyr exists only 16-bit resolution currently. So it supports resolutions up to 16-bit. Signed-off-by: TOKITA Hiroshi --- cores/arduino/Arduino.h | 1 + cores/arduino/zephyrCommon.cpp | 86 ++++++++++- .../arduino_mkrzero/arduino_mkrzero.overlay | 121 ++++++++++++++-- .../arduino_nano_33_ble.overlay | 88 ++++++++++++ .../arduino_nano_33_ble_sense.overlay | 88 ++++++++++++ .../arduino_nano_33_iot.overlay | 133 +++++++++++++++--- .../nrf52840dk_nrf52840.overlay | 86 +++++++++++ variants/variants.h | 8 ++ 8 files changed, 578 insertions(+), 33 deletions(-) diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h index d54908f3..383efea8 100644 --- a/cores/arduino/Arduino.h +++ b/cores/arduino/Arduino.h @@ -8,6 +8,7 @@ #include #include +#include #include #include diff --git a/cores/arduino/zephyrCommon.cpp b/cores/arduino/zephyrCommon.cpp index a8537db4..8b640a7b 100644 --- a/cores/arduino/zephyrCommon.cpp +++ b/cores/arduino/zephyrCommon.cpp @@ -6,13 +6,13 @@ #include -#define PWM_DT_SPEC(n,p,i) PWM_DT_SPEC_GET_BY_IDX(n, i), -#define PWM_PINS(n,p,i) DT_PROP_BY_IDX(n, p, i), - namespace { #ifdef CONFIG_PWM +#define PWM_DT_SPEC(n,p,i) PWM_DT_SPEC_GET_BY_IDX(n, i), +#define PWM_PINS(n,p,i) DT_PROP_BY_IDX(n, p, i), + const struct pwm_dt_spec arduino_pwm[] = { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), pwms, PWM_DT_SPEC) }; @@ -31,6 +31,33 @@ size_t pwm_pin_index(pin_size_t pinNumber) { #endif //CONFIG_PWM +#ifdef CONFIG_ADC + +#define ADC_DT_SPEC(n,p,i) ADC_DT_SPEC_GET_BY_IDX(n, i), +#define ADC_PINS(n,p,i) DT_PROP_BY_IDX(n, p, i), +#define ADC_CH_CFG(n,p,i) arduino_adc[i].channel_cfg, + +const struct adc_dt_spec arduino_adc[] = + { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, ADC_DT_SPEC) }; + +/* io-channel-pins node provides a mapping digital pin numbers to adc channels */ +const pin_size_t arduino_analog_pins[] = + { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channel_pins, ADC_PINS) }; + +struct adc_channel_cfg channel_cfg[ARRAY_SIZE(arduino_analog_pins)] = + { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, ADC_CH_CFG) }; + +size_t analog_pin_index(pin_size_t pinNumber) { + for(size_t i=0; i(mode); + } +} + +int analogRead(pin_size_t pinNumber) +{ + int err; + int16_t buf; + struct adc_sequence seq = { .buffer = &buf, .buffer_size = sizeof(buf) }; + size_t idx = analog_pin_index(pinNumber); + + if (idx >= ARRAY_SIZE(arduino_adc) ) { + return -EINVAL; + } + + /* + * ADC that is on MCU supported by Zephyr exists + * only 16bit resolution, currently. + */ + if (arduino_adc[idx].resolution > 16) { + return -ENOTSUP; + } + + err = adc_channel_setup(arduino_adc[idx].dev, &arduino_adc[idx].channel_cfg); + if (err < 0) { + return err; + } + + seq.channels = BIT(arduino_adc[idx].channel_id); + seq.resolution = arduino_adc[idx].resolution; + seq.oversampling = arduino_adc[idx].oversampling; + + err = adc_read(arduino_adc[idx].dev, &seq); + if (err < 0) { + return err; + } + + return buf; +} + +#endif diff --git a/variants/arduino_mkrzero/arduino_mkrzero.overlay b/variants/arduino_mkrzero/arduino_mkrzero.overlay index 4b8f2508..665a5da4 100644 --- a/variants/arduino_mkrzero/arduino_mkrzero.overlay +++ b/variants/arduino_mkrzero/arduino_mkrzero.overlay @@ -3,6 +3,9 @@ * * SPDX-License-Identifier: Apache-2.0 */ + +#include + / { zephyr,user { d0_gpios = <&arduino_mkr_header 0 0>; @@ -27,27 +30,121 @@ d19_gpios = <&arduino_mkr_header 19 0>; /* D19 / A5 / I2C-SCL */ d20_gpios = <&arduino_mkr_header 20 0>; d21_gpios = <&arduino_mkr_header 21 0>; + pwms = <&tcc0 2 255>, <&tcc0 3 255>; pwm-pins = <2 3>; + + io-channels = <&adc 0>, + <&adc 10>, + <&adc 11>, + <&adc 4>, + <&adc 5>, + <&adc 6>, + <&adc 7>; + io-channel-pins = <15 16 17 18 19 20 21>; }; }; &pinctrl { - pwm_default: pwm_default { - group1 { - pinmux = , - ; - }; - }; + pwm_default: pwm_default { + group1 { + pinmux = , + ; + }; + }; + + adc_default: adc_default { + group1 { + pinmux = , + , + , + , + , + , + ; + }; + }; }; &tcc0 { - status = "okay"; - compatible = "atmel,sam0-tcc-pwm"; - prescaler = <2>; - #pwm-cells = <2>; + status = "okay"; + compatible = "atmel,sam0-tcc-pwm"; + prescaler = <2>; + #pwm-cells = <2>; + + pinctrl-0 = <&pwm_default>; + pinctrl-names = "default"; +}; + +&adc { + status = "okay"; + pinctrl-0 = <&adc_default>; + pinctrl-names = "default"; + + #address-cells = <1>; + #size-cells = <0>; + + channel@0 { + reg = <0>; + zephyr,gain = "ADC_GAIN_1"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <10>; + zephyr,input-positive = <0>; + }; + + channel@4 { + reg = <4>; + zephyr,gain = "ADC_GAIN_1"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <10>; + zephyr,input-positive = <4>; + }; + + channel@5 { + reg = <5>; + zephyr,gain = "ADC_GAIN_1"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <10>; + zephyr,input-positive = <5>; + }; + + channel@6 { + reg = <6>; + zephyr,gain = "ADC_GAIN_1"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <10>; + zephyr,input-positive = <6>; + }; + + channel@7 { + reg = <7>; + zephyr,gain = "ADC_GAIN_1"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <10>; + zephyr,input-positive = <7>; + }; - pinctrl-0 = <&pwm_default>; - pinctrl-names = "default"; + channel@10 { + reg = <10>; + zephyr,gain = "ADC_GAIN_1"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <10>; + zephyr,input-positive = <10>; + }; + + channel@11 { + reg = <11>; + zephyr,gain = "ADC_GAIN_1"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <10>; + zephyr,input-positive = <11>; + }; }; diff --git a/variants/arduino_nano_33_ble/arduino_nano_33_ble.overlay b/variants/arduino_nano_33_ble/arduino_nano_33_ble.overlay index d943c28b..f26995d5 100644 --- a/variants/arduino_nano_33_ble/arduino_nano_33_ble.overlay +++ b/variants/arduino_nano_33_ble/arduino_nano_33_ble.overlay @@ -22,6 +22,7 @@ d19_gpios = <&arduino_nano_header 19 0>; /* D19 / A5 / I2C-SCL */ d20_gpios = <&arduino_nano_header 20 0>; d21_gpios = <&arduino_nano_header 21 0>; + pwms = <&pwm1 1 255 PWM_POLARITY_NORMAL>, <&pwm1 2 255 PWM_POLARITY_NORMAL>, <&pwm1 3 255 PWM_POLARITY_NORMAL>, @@ -30,6 +31,93 @@ <&pwm2 2 255 PWM_POLARITY_NORMAL>, <&pwm2 3 255 PWM_POLARITY_NORMAL>; pwm-pins = <3 5 6 13 9 10 11>; + + io-channels = <&adc 2>, + <&adc 3>, + <&adc 6>, + <&adc 5>, + <&adc 7>, + <&adc 0>, + <&adc 4>, + <&adc 1>; + io-channel-pins = <14 15 16 17 18 19 20 21>; + }; +}; + +&adc { + #address-cells = <1>; + #size-cells = <0>; + + channel@0 { + reg = <0>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.02 */ + zephyr,resolution = <10>; + }; + + channel@1 { + reg = <1>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.03 */ + zephyr,resolution = <10>; + }; + + channel@2 { + reg = <2>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.04 */ + zephyr,resolution = <10>; + }; + + channel@3 { + reg = <3>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.05 */ + zephyr,resolution = <10>; + }; + + channel@4 { + reg = <4>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.28 */ + zephyr,resolution = <10>; + }; + + channel@5 { + reg = <5>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.29 */ + zephyr,resolution = <10>; + }; + + channel@6 { + reg = <6>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.30 */ + zephyr,resolution = <10>; + }; + + channel@7 { + reg = <7>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.31 */ + zephyr,resolution = <10>; }; }; diff --git a/variants/arduino_nano_33_ble_sense/arduino_nano_33_ble_sense.overlay b/variants/arduino_nano_33_ble_sense/arduino_nano_33_ble_sense.overlay index d943c28b..f26995d5 100644 --- a/variants/arduino_nano_33_ble_sense/arduino_nano_33_ble_sense.overlay +++ b/variants/arduino_nano_33_ble_sense/arduino_nano_33_ble_sense.overlay @@ -22,6 +22,7 @@ d19_gpios = <&arduino_nano_header 19 0>; /* D19 / A5 / I2C-SCL */ d20_gpios = <&arduino_nano_header 20 0>; d21_gpios = <&arduino_nano_header 21 0>; + pwms = <&pwm1 1 255 PWM_POLARITY_NORMAL>, <&pwm1 2 255 PWM_POLARITY_NORMAL>, <&pwm1 3 255 PWM_POLARITY_NORMAL>, @@ -30,6 +31,93 @@ <&pwm2 2 255 PWM_POLARITY_NORMAL>, <&pwm2 3 255 PWM_POLARITY_NORMAL>; pwm-pins = <3 5 6 13 9 10 11>; + + io-channels = <&adc 2>, + <&adc 3>, + <&adc 6>, + <&adc 5>, + <&adc 7>, + <&adc 0>, + <&adc 4>, + <&adc 1>; + io-channel-pins = <14 15 16 17 18 19 20 21>; + }; +}; + +&adc { + #address-cells = <1>; + #size-cells = <0>; + + channel@0 { + reg = <0>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.02 */ + zephyr,resolution = <10>; + }; + + channel@1 { + reg = <1>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.03 */ + zephyr,resolution = <10>; + }; + + channel@2 { + reg = <2>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.04 */ + zephyr,resolution = <10>; + }; + + channel@3 { + reg = <3>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.05 */ + zephyr,resolution = <10>; + }; + + channel@4 { + reg = <4>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.28 */ + zephyr,resolution = <10>; + }; + + channel@5 { + reg = <5>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.29 */ + zephyr,resolution = <10>; + }; + + channel@6 { + reg = <6>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.30 */ + zephyr,resolution = <10>; + }; + + channel@7 { + reg = <7>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.31 */ + zephyr,resolution = <10>; }; }; diff --git a/variants/arduino_nano_33_iot/arduino_nano_33_iot.overlay b/variants/arduino_nano_33_iot/arduino_nano_33_iot.overlay index 80fa3f77..fb3ffb24 100644 --- a/variants/arduino_nano_33_iot/arduino_nano_33_iot.overlay +++ b/variants/arduino_nano_33_iot/arduino_nano_33_iot.overlay @@ -1,3 +1,5 @@ +#include + / { zephyr,user { d0_gpios = <&arduino_nano_header 0 0>; @@ -22,6 +24,7 @@ d19_gpios = <&arduino_nano_header 19 0>; /* D19 / A5 / I2C-SCL */ d20_gpios = <&arduino_nano_header 20 0>; d21_gpios = <&arduino_nano_header 21 0>; + pwms = <&tcc0 0 255>, <&tcc0 1 255>, <&tcc0 2 255>, @@ -31,30 +34,124 @@ <&tcc0 6 255>, <&tcc0 7 255>; pwm-pins = <6 5 17 12 2 3 9 10>; + + io-channels = <&adc 0>, + <&adc 10>, + <&adc 11>, + <&adc 4>, + <&adc 5>, + <&adc 6>, + <&adc 7>; + io-channel-pins = <14 15 16 17 18 19 20 21>; }; }; &pinctrl { - pwm_default: pwm_default { - group1 { - pinmux = , - , - , - , - , - , - , - ; - }; - }; + pwm_default: pwm_default { + group1 { + pinmux = , + , + , + , + , + , + , + ; + }; + }; + + adc_default: adc_default { + group1 { + pinmux = , + , + , + , + , + , + , + ; + }; + }; }; &tcc0 { - status = "okay"; - compatible = "atmel,sam0-tcc-pwm"; - prescaler = <2>; - #pwm-cells = <2>; + status = "okay"; + compatible = "atmel,sam0-tcc-pwm"; + prescaler = <2>; + #pwm-cells = <2>; + + pinctrl-0 = <&pwm_default>; + pinctrl-names = "default"; +}; + +&adc { + status = "okay"; + pinctrl-0 = <&adc_default>; + pinctrl-names = "default"; + + #address-cells = <1>; + #size-cells = <0>; + + channel@0 { + reg = <0>; + zephyr,gain = "ADC_GAIN_1"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <10>; + zephyr,input-positive = <0>; + }; - pinctrl-0 = <&pwm_default>; - pinctrl-names = "default"; + channel@2 { + reg = <2>; + zephyr,gain = "ADC_GAIN_1"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <10>; + zephyr,input-positive = <2>; + }; + + channel@3 { + reg = <3>; + zephyr,gain = "ADC_GAIN_1"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <10>; + zephyr,input-positive = <3>; + }; + + channel@10 { + reg = <10>; + zephyr,gain = "ADC_GAIN_1"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <10>; + zephyr,input-positive = <10>; + }; + + channel@11 { + reg = <11>; + zephyr,gain = "ADC_GAIN_1"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <10>; + zephyr,input-positive = <11>; + }; + + channel@18 { + reg = <18>; + zephyr,gain = "ADC_GAIN_1"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <10>; + zephyr,input-positive = <18>; + }; + + channel@19 { + reg = <19>; + zephyr,gain = "ADC_GAIN_1"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <10>; + zephyr,input-positive = <19>; + }; }; diff --git a/variants/nrf52840dk_nrf52840/nrf52840dk_nrf52840.overlay b/variants/nrf52840dk_nrf52840/nrf52840dk_nrf52840.overlay index 93fc03eb..907cc02f 100644 --- a/variants/nrf52840dk_nrf52840/nrf52840dk_nrf52840.overlay +++ b/variants/nrf52840dk_nrf52840/nrf52840dk_nrf52840.overlay @@ -23,6 +23,7 @@ d20_gpios = <&arduino_header 4 0>; d21_gpios = <&arduino_header 5 0>; d22_gpios = <&gpio0 13 GPIO_ACTIVE_LOW>; + pwms = <&pwm0 1 255 PWM_POLARITY_NORMAL>, <&pwm0 2 255 PWM_POLARITY_NORMAL>, <&pwm0 3 255 PWM_POLARITY_NORMAL>, @@ -30,6 +31,91 @@ <&pwm1 1 255 PWM_POLARITY_NORMAL>, <&pwm1 2 255 PWM_POLARITY_NORMAL>; pwm-pins = <22 3 5 6 9 10 11>; + + io-channels = <&arduino_adc 0>, + <&arduino_adc 1>, + <&arduino_adc 2>, + <&arduino_adc 3>, + <&arduino_adc 4>, + <&arduino_adc 5>; + io-channel-pins = <16 17 18 19 20 21>; + }; +}; + +&adc { + #address-cells = <1>; + #size-cells = <0>; + + channel@0 { + reg = <0>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.02 */ + zephyr,resolution = <10>; + }; + + channel@1 { + reg = <1>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.03 */ + zephyr,resolution = <10>; + }; + + channel@2 { + reg = <2>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.04 */ + zephyr,resolution = <10>; + }; + + channel@3 { + reg = <3>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.05 */ + zephyr,resolution = <10>; + }; + + channel@4 { + reg = <4>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.28 */ + zephyr,resolution = <10>; + }; + + channel@5 { + reg = <5>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.29 */ + zephyr,resolution = <10>; + }; + + channel@6 { + reg = <6>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.30 */ + zephyr,resolution = <10>; + }; + + channel@7 { + reg = <7>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; /* P0.31 */ + zephyr,resolution = <10>; }; }; diff --git a/variants/variants.h b/variants/variants.h index 25b0da61..282f1dab 100644 --- a/variants/variants.h +++ b/variants/variants.h @@ -44,3 +44,11 @@ static struct gpio_dt_spec arduino_pins[NUM_OF_DIGITAL_PINS] = { LISTIFY(MAX_DIGITAL_PINS, NUMBERED_GPIO_DT_SPEC, ()) DT_FOREACH_CHILD(DT_PATH(leds), LABELED_GPIO_DT_SPEC) }; + +#ifdef CONFIG_ADC + +#define AN_ENUMS(n, p, i) A ## i = DT_PROP_BY_IDX(n, p, i), +enum analogPins { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), + io_channel_pins, AN_ENUMS) }; + +#endif From f21e912ac1da5df68484c04d30e43fd5baddbb1b Mon Sep 17 00:00:00 2001 From: TOKITA Hiroshi Date: Wed, 21 Sep 2022 07:43:52 +0900 Subject: [PATCH 2/2] samples: Added analog_input sample Add a sample to demonstrate how to use analogRead API Signed-off-by: TOKITA Hiroshi --- samples/analog_input/CMakeLists.txt | 11 +++++++++++ samples/analog_input/README.rst | 24 ++++++++++++++++++++++++ samples/analog_input/prj.conf | 2 ++ samples/analog_input/src/main.cpp | 29 +++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 samples/analog_input/CMakeLists.txt create mode 100644 samples/analog_input/README.rst create mode 100644 samples/analog_input/prj.conf create mode 100644 samples/analog_input/src/main.cpp diff --git a/samples/analog_input/CMakeLists.txt b/samples/analog_input/CMakeLists.txt new file mode 100644 index 00000000..d18e0e2c --- /dev/null +++ b/samples/analog_input/CMakeLists.txt @@ -0,0 +1,11 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +set(DTC_OVERLAY_FILE $ENV{ZEPHYR_BASE}/../modules/lib/Arduino-Zephyr-API/variants/${BOARD}/${BOARD}.overlay) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(analog_input) + +target_sources(app PRIVATE src/main.cpp) +zephyr_compile_options(-Wno-unused-variable -Wno-comment) diff --git a/samples/analog_input/README.rst b/samples/analog_input/README.rst new file mode 100644 index 00000000..51349e48 --- /dev/null +++ b/samples/analog_input/README.rst @@ -0,0 +1,24 @@ +.. _analog_input: + +Analog Input +############ + +Overview +******** + +The analog_input sample blinks the LED with control of the period +by the voltage of the input pin. +Inputting high voltage to blink the LED slowly. +Blink the LED fast on input voltage is low. +When the input is 0V, LED light. + +Building and Running +******************** + +Build and flash analog_input sample as follows, + +```sh +$> west build -p -b arduino_nano_33_ble sample/analog_input/ + +$> west flash --bossac=/home/$USER/.arduino15/packages/arduino/tools/bossac/1.9.1-arduino2/bossac +``` diff --git a/samples/analog_input/prj.conf b/samples/analog_input/prj.conf new file mode 100644 index 00000000..7db48201 --- /dev/null +++ b/samples/analog_input/prj.conf @@ -0,0 +1,2 @@ +CONFIG_ADC=y +CONFIG_ARDUINO_API=y diff --git a/samples/analog_input/src/main.cpp b/samples/analog_input/src/main.cpp new file mode 100644 index 00000000..9cc23dae --- /dev/null +++ b/samples/analog_input/src/main.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 TOKITA Hiroshi + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +const int analog_input = A0; // select the input pin for the potentiometer +const int ledPin = LED_BUILTIN; // select the pin for the LED +const float wait_factor = 1.f; + +void setup() { + pinMode(ledPin, OUTPUT); +} + +void loop() { + int value = 0; + + value = analogRead(analog_input); + + /* Blinks slowly when the input voltage is high */ + + digitalWrite(ledPin, HIGH); + delay(value * wait_factor); + + digitalWrite(ledPin, LOW); + delay(value * wait_factor); +}