From cc4c47927b267d989d9bc4c6dd466c48e71441f4 Mon Sep 17 00:00:00 2001 From: rbaron Date: Sun, 27 Nov 2022 22:36:06 +0100 Subject: [PATCH] Moves soil_read_loop to samples/ --- .../samples/soil_read_loop/.gitignore | 3 + .../samples/soil_read_loop/CMakeLists.txt | 14 ++++ .../samples/soil_read_loop/README.md | 2 + .../samples/soil_read_loop/prj.conf | 10 +++ .../samples/soil_read_loop/src/main.c | 83 +++++++++++++++++++ 5 files changed, 112 insertions(+) create mode 100644 code/nrf-connect/samples/soil_read_loop/.gitignore create mode 100644 code/nrf-connect/samples/soil_read_loop/CMakeLists.txt create mode 100644 code/nrf-connect/samples/soil_read_loop/README.md create mode 100644 code/nrf-connect/samples/soil_read_loop/prj.conf create mode 100644 code/nrf-connect/samples/soil_read_loop/src/main.c diff --git a/code/nrf-connect/samples/soil_read_loop/.gitignore b/code/nrf-connect/samples/soil_read_loop/.gitignore new file mode 100644 index 0000000..5299748 --- /dev/null +++ b/code/nrf-connect/samples/soil_read_loop/.gitignore @@ -0,0 +1,3 @@ +build +build_* +*.code-workspace \ No newline at end of file diff --git a/code/nrf-connect/samples/soil_read_loop/CMakeLists.txt b/code/nrf-connect/samples/soil_read_loop/CMakeLists.txt new file mode 100644 index 0000000..18e0bc9 --- /dev/null +++ b/code/nrf-connect/samples/soil_read_loop/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.20.0) + +# Pull in the dts/ and boards/ from prstlib. +set(DTS_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../prstlib) +set(BOARD_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../prstlib) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(soil_read_loop) + +target_sources(app PRIVATE src/main.c) + +add_subdirectory(../../prstlib prstlib) +target_include_directories(app PRIVATE ../../prstlib/include) +target_link_libraries(app PUBLIC prstlib) diff --git a/code/nrf-connect/samples/soil_read_loop/README.md b/code/nrf-connect/samples/soil_read_loop/README.md new file mode 100644 index 0000000..ae802fa --- /dev/null +++ b/code/nrf-connect/samples/soil_read_loop/README.md @@ -0,0 +1,2 @@ +# Calibration +A sample for calibrating b-parasites soil moisture analog-to-digital conversion. \ No newline at end of file diff --git a/code/nrf-connect/samples/soil_read_loop/prj.conf b/code/nrf-connect/samples/soil_read_loop/prj.conf new file mode 100644 index 0000000..4dac114 --- /dev/null +++ b/code/nrf-connect/samples/soil_read_loop/prj.conf @@ -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 diff --git a/code/nrf-connect/samples/soil_read_loop/src/main.c b/code/nrf-connect/samples/soil_read_loop/src/main.c new file mode 100644 index 0000000..1ab8e37 --- /dev/null +++ b/code/nrf-connect/samples/soil_read_loop/src/main.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include + +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); + } +}