ADC works

This commit is contained in:
rbaron 2022-11-13 14:37:55 +01:00
parent 9c9492567f
commit 522452b465
3 changed files with 81 additions and 5 deletions

View file

@ -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 = <ADC_ACQ_TIME_DEFAULT>;
// P0.03.
zephyr,input-positive = <NRF_SAADC_AIN1>;
zephyr,resolution = <10>;
};
channel@1 {
reg = <1>;
zephyr,gain = "ADC_GAIN_1_6";
zephyr,reference = "ADC_REF_INTERNAL";
zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
zephyr,input-positive = <NRF_SAADC_AIN1>;
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>;

View file

@ -1,4 +1,5 @@
CONFIG_LOG=y
CONFIG_PWM=y
CONFIG_CBPRINTF_FP_SUPPORT=y
CONFIG_I2C=y
CONFIG_I2C=y
CONFIG_ADC=y

View file

@ -1,3 +1,5 @@
#include <drivers/adc.h>
#include <drivers/gpio.h>
#include <drivers/i2c.h>
#include <drivers/pwm.h>
#include <logging/log.h>
@ -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);
}
}