Moves soil_read_loop to samples/
This commit is contained in:
parent
00a7114dd1
commit
cc4c47927b
5 changed files with 112 additions and 0 deletions
3
code/nrf-connect/samples/soil_read_loop/.gitignore
vendored
Normal file
3
code/nrf-connect/samples/soil_read_loop/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
build
|
||||||
|
build_*
|
||||||
|
*.code-workspace
|
||||||
14
code/nrf-connect/samples/soil_read_loop/CMakeLists.txt
Normal file
14
code/nrf-connect/samples/soil_read_loop/CMakeLists.txt
Normal file
|
|
@ -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)
|
||||||
2
code/nrf-connect/samples/soil_read_loop/README.md
Normal file
2
code/nrf-connect/samples/soil_read_loop/README.md
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
# Calibration
|
||||||
|
A sample for calibrating b-parasites soil moisture analog-to-digital conversion.
|
||||||
10
code/nrf-connect/samples/soil_read_loop/prj.conf
Normal file
10
code/nrf-connect/samples/soil_read_loop/prj.conf
Normal 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
|
||||||
83
code/nrf-connect/samples/soil_read_loop/src/main.c
Normal file
83
code/nrf-connect/samples/soil_read_loop/src/main.c
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue