Adds a calibration sample

This commit is contained in:
rbaron 2022-11-25 23:20:03 +01:00
parent 5510fd963c
commit a9d551f33c
7 changed files with 105 additions and 0 deletions

2
code/calibration/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
build
build_*

View file

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(calibration)
target_sources(app PRIVATE src/main.c)

View file

@ -0,0 +1,2 @@
# Calibration
A sample for calibrating b-parasites soil moisture analog-to-digital conversion.

1
code/calibration/boards Symbolic link
View file

@ -0,0 +1 @@
../nrf-connect/boards

1
code/calibration/dts Symbolic link
View file

@ -0,0 +1 @@
../nrf-connect/dts

10
code/calibration/prj.conf Normal file
View file

@ -0,0 +1,10 @@
CONFIG_PINCTRL=y
CONFIG_LOG=y
CONFIG_PWM=y
CONFIG_PWM_LOG_LEVEL_DBG=y
CONFIG_CBPRINTF_FP_SUPPORT=y
CONFIG_ADC=y
CONFIG_GPIO=y
CONFIG_PM=y
CONFIG_PM_DEVICE=y
CONFIG_USE_SEGGER_RTT=y

View file

@ -0,0 +1,83 @@
#include <drivers/adc.h>
#include <drivers/gpio.h>
#include <drivers/pwm.h>
#include <logging/log.h>
#include <zephyr/zephyr.h>
LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG);
#define PRST_STRINGIFY(x) #x
#define PRST_TO_STRING(x) PRST_STRINGIFY(x)
#define PRST_LOCATION __FILE__ ":" PRST_TO_STRING(__LINE__)
#define RET_IF_ERR_MSG(expr, msg) \
{ \
int err = (expr); \
if (err) { \
LOG_ERR("Error: " msg " in " PRST_LOCATION); \
return err; \
} \
}
#define RET_IF_ERR(expr) RET_IF_ERR_MSG(expr, "")
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);
struct gpio_dt_spec fast_disch_dt =
GPIO_DT_SPEC_GET(DT_NODELABEL(fast_disch), gpios);
static const struct adc_dt_spec adc_soil_spec =
ADC_DT_SPEC_GET_BY_IDX(DT_PATH(zephyr_user), 0);
static const struct adc_dt_spec adc_batt_spec =
ADC_DT_SPEC_GET_BY_IDX(DT_PATH(zephyr_user), 2);
static int16_t soil_buf;
static struct adc_sequence soil_sequence = {
.buffer = &soil_buf,
.buffer_size = sizeof(soil_buf),
};
static int16_t batt_buf;
static struct adc_sequence batt_sequence = {
.buffer = &batt_buf,
.buffer_size = sizeof(batt_buf),
};
int main(void) {
RET_IF_ERR(!device_is_ready(fast_disch_dt.port));
RET_IF_ERR(!device_is_ready(soil_pwm_dt.dev));
// Configure analog-to-digital channels.
RET_IF_ERR(adc_channel_setup_dt(&adc_soil_spec));
RET_IF_ERR(adc_channel_setup_dt(&adc_batt_spec));
// Configure fast discharge enable pin.
RET_IF_ERR(gpio_pin_configure_dt(&fast_disch_dt, GPIO_OUTPUT));
// Enable fast discharge circuit.
RET_IF_ERR(gpio_pin_set_dt(&fast_disch_dt, 1));
// Start PWM.
RET_IF_ERR(pwm_set_dt(&soil_pwm_dt, soil_pwm_dt.period, pulse));
RET_IF_ERR(adc_sequence_init_dt(&adc_soil_spec, &soil_sequence));
RET_IF_ERR(adc_sequence_init_dt(&adc_batt_spec, &batt_sequence));
LOG_INF("input_voltage;soil_adc_output");
while (true) {
k_msleep(500);
RET_IF_ERR(adc_read(adc_batt_spec.dev, &batt_sequence));
int32_t batt_val_mv = batt_buf;
RET_IF_ERR(adc_raw_to_millivolts_dt(&adc_batt_spec, &batt_val_mv));
RET_IF_ERR(adc_read(adc_soil_spec.dev, &soil_sequence));
int32_t soil_val_mv = soil_buf;
RET_IF_ERR(adc_raw_to_millivolts_dt(&adc_soil_spec, &soil_val_mv));
LOG_INF("%.2f;%u", batt_val_mv / 1000.0f, soil_buf);
}
}