diff --git a/code/nrf-connect/nrf52840dk_nrf52840.overlay b/code/nrf-connect/nrf52840dk_nrf52840.overlay index 4193d47..b17fb73 100644 --- a/code/nrf-connect/nrf52840dk_nrf52840.overlay +++ b/code/nrf-connect/nrf52840dk_nrf52840.overlay @@ -39,7 +39,35 @@ }; }; +&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 = ; + // P0.03. + zephyr,input-positive = ; + zephyr,resolution = <10>; + + }; + + channel@1 { + reg = <1>; + zephyr,gain = "ADC_GAIN_1_6"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,input-positive = ; + zephyr,resolution = <10>; + }; +}; + / { + zephyr,user { + io-channels = <&adc 0>, <&adc 1>; + }; + soil_pwm: soil_pwm { compatible = "pwm-fixed"; pwms = <&pwm0 0 PWM_MSEC(100) PWM_POLARITY_INVERTED>; diff --git a/code/nrf-connect/prj.conf b/code/nrf-connect/prj.conf index a7d8b01..be6e3b8 100644 --- a/code/nrf-connect/prj.conf +++ b/code/nrf-connect/prj.conf @@ -1,4 +1,5 @@ CONFIG_LOG=y CONFIG_PWM=y CONFIG_CBPRINTF_FP_SUPPORT=y -CONFIG_I2C=y \ No newline at end of file +CONFIG_I2C=y +CONFIG_ADC=y \ No newline at end of file diff --git a/code/nrf-connect/src/main.c b/code/nrf-connect/src/main.c index 8e00ace..fe544d4 100644 --- a/code/nrf-connect/src/main.c +++ b/code/nrf-connect/src/main.c @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -8,15 +10,60 @@ LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG); // PWM_DT_SPEC_GET(DT_LABEL(pwm0)); -static const struct pwm_dt_spec soil_pwm_dt = - PWM_DT_SPEC_GET(DT_NODELABEL(soil_pwm)); -static const uint32_t pulse = DT_PROP(DT_NODELABEL(soil_pwm), pulse); +// static const struct pwm_dt_spec soil_pwm_dt = +// PWM_DT_SPEC_GET(DT_NODELABEL(soil_pwm)); +// static const uint32_t pulse = DT_PROP(DT_NODELABEL(soil_pwm), pulse); + +// static const struct adc_channel_cfg soil_adc_config = +// // ADC_CHANNEL_CFG_DT(DT_CHILD(DT_NODELABEL(adc), channel_0)); +// ADC_CHANNEL_CFG_DT(DT_NODELABEL(soil_adc_channel)); +// static const struct adc_dt_spec soil_adc_spec = +// ADC_CHANNEL_CFG_DT(DT_CHILD(DT_NODELABEL(adc), channel_0)); +// ADC_DT_SPEC_GET(DT_NODELABEL(soil_adc_channel)); +static const struct adc_dt_spec adc_soil_spec = + ADC_DT_SPEC_GET_BY_IDX(DT_PATH(zephyr_user), 0); + +// #define ERR_CHECK(expr) \ +// { \ +// err = expr; \ +// if (err != 0) {\ +// LOG_ERR("Error " __lineno__); +// }\ +// } void main(void) { // prst_shtc3_read_t shtc3_read = prst_shtc3_read(); - pwm_set_pulse_dt(&soil_pwm_dt, pulse); + // pwm_set_pulse_dt(&soil_pwm_dt, pulse); + int err; + int16_t buf; + int32_t val_mv; + struct adc_sequence sequence = { + .buffer = &buf, + /* buffer size in bytes, not number of samples */ + .buffer_size = sizeof(buf), + }; + err = adc_channel_setup_dt(&adc_soil_spec); + if (err) { + LOG_ERR("Error in adc_channel_setup_dt"); + } + + err = adc_sequence_init_dt(&adc_soil_spec, &sequence); + if (err) { + LOG_ERR("Error in adc_sequence_init_dt"); + } while (true) { + err = adc_read(adc_soil_spec.dev, &sequence); + if (err) { + LOG_ERR("Error in adc_read"); + } + val_mv = buf; + err = adc_raw_to_millivolts_dt(&adc_soil_spec, &val_mv); + if (err < 0) { + printk(" (value in mV not available)\n"); + } else { + printk(" = %u mV\n", val_mv); + } k_msleep(500); } }