From c9b0f38576e01cc1262a2329637dd626ee1994d4 Mon Sep 17 00:00:00 2001 From: Eduard Iten Date: Tue, 8 Jul 2025 15:19:44 +0200 Subject: [PATCH] feat(lib): Introduce adc_sensor library Adds a new `adc_sensor` library to abstract reading analog values from ADC channels. The output of this library is currently simulated. This library is now used by the `modbus_server` to read the motor current and the main supply voltage, replacing the previous implementation. This change improves modularity by centralizing ADC-related code into a dedicated module. The build system has been updated to include the new library. --- software/include/lib/adc_sensor.h | 27 ++++++++++ software/lib/CMakeLists.txt | 1 + software/lib/Kconfig | 1 + software/lib/adc_sensor/CMakeLists.txt | 1 + software/lib/adc_sensor/Kconfig | 16 ++++++ software/lib/adc_sensor/adc_sensor.c | 61 ++++++++++++++++++++++ software/lib/modbus_server/modbus_server.c | 12 ++++- 7 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 software/include/lib/adc_sensor.h create mode 100644 software/lib/adc_sensor/CMakeLists.txt create mode 100644 software/lib/adc_sensor/Kconfig create mode 100644 software/lib/adc_sensor/adc_sensor.c diff --git a/software/include/lib/adc_sensor.h b/software/include/lib/adc_sensor.h new file mode 100644 index 0000000..21b7d74 --- /dev/null +++ b/software/include/lib/adc_sensor.h @@ -0,0 +1,27 @@ +#ifndef ADC_SENSOR_H +#define ADC_SENSOR_H + +#include + +/** + * @brief Initialize the ADC sensor system + * + * @return 0 on success, negative error code on failure + */ +int adc_sensor_init(void); + +/** + * @brief Get supply voltage reading in millivolts + * + * @return Voltage in millivolts (currently simulated: 12000mV) + */ +uint16_t adc_sensor_get_voltage_mv(void); + +/** + * @brief Get motor current reading in milliamps + * + * @return Current in milliamps (currently simulated: 45mA) + */ +uint16_t adc_sensor_get_current_ma(void); + +#endif /* ADC_SENSOR_H */ diff --git a/software/lib/CMakeLists.txt b/software/lib/CMakeLists.txt index 07303c6..ca2a6c0 100644 --- a/software/lib/CMakeLists.txt +++ b/software/lib/CMakeLists.txt @@ -1,3 +1,4 @@ +add_subdirectory_ifdef(CONFIG_ADC_SENSOR adc_sensor) add_subdirectory_ifdef(CONFIG_LIB_FWU fwu) add_subdirectory_ifdef(CONFIG_LIB_MODBUS_SERVER modbus_server) add_subdirectory_ifdef(CONFIG_LIB_VALVE valve) diff --git a/software/lib/Kconfig b/software/lib/Kconfig index 1e2390b..a279fef 100644 --- a/software/lib/Kconfig +++ b/software/lib/Kconfig @@ -1,5 +1,6 @@ menu "Irrigation system software libraries" +rsource "adc_sensor/Kconfig" rsource "fwu/Kconfig" rsource "modbus_server/Kconfig" rsource "valve/Kconfig" diff --git a/software/lib/adc_sensor/CMakeLists.txt b/software/lib/adc_sensor/CMakeLists.txt new file mode 100644 index 0000000..c45eb90 --- /dev/null +++ b/software/lib/adc_sensor/CMakeLists.txt @@ -0,0 +1 @@ +zephyr_library_sources(adc_sensor.c) diff --git a/software/lib/adc_sensor/Kconfig b/software/lib/adc_sensor/Kconfig new file mode 100644 index 0000000..13aca87 --- /dev/null +++ b/software/lib/adc_sensor/Kconfig @@ -0,0 +1,16 @@ +config ADC_SENSOR + bool "ADC sensor library" + default y + help + Enable ADC sensor library for voltage and current measurements. + +if ADC_SENSOR + +config ADC_SENSOR_SIMULATED + bool "Use simulated ADC readings" + default y + help + Use simulated values instead of real ADC readings. + Voltage: 12000mV, Current: 45mA + +endif # ADC_SENSOR diff --git a/software/lib/adc_sensor/adc_sensor.c b/software/lib/adc_sensor/adc_sensor.c new file mode 100644 index 0000000..c9a789e --- /dev/null +++ b/software/lib/adc_sensor/adc_sensor.c @@ -0,0 +1,61 @@ +#include +#include +#include + +LOG_MODULE_REGISTER(adc_sensor, LOG_LEVEL_INF); + +// Simulated values +#define SIMULATED_VOLTAGE_MV 12000 +#define SIMULATED_CURRENT_MA 45 + +static bool initialized = false; + +int adc_sensor_init(void) +{ + if (initialized) { + return 0; + } + +#ifdef CONFIG_ADC_SENSOR_SIMULATED + LOG_INF("ADC sensor initialized (simulated mode)"); + LOG_INF("Simulated values: %dmV, %dmA", SIMULATED_VOLTAGE_MV, SIMULATED_CURRENT_MA); +#else + // TODO: Initialize real ADC hardware + LOG_INF("ADC sensor initialized (real ADC mode - not yet implemented)"); +#endif + + initialized = true; + return 0; +} + +uint16_t adc_sensor_get_voltage_mv(void) +{ + if (!initialized) { + LOG_WRN("ADC sensor not initialized, calling adc_sensor_init()"); + adc_sensor_init(); + } + +#ifdef CONFIG_ADC_SENSOR_SIMULATED + return SIMULATED_VOLTAGE_MV; +#else + // TODO: Read real ADC value for voltage + // For now return simulated value + return SIMULATED_VOLTAGE_MV; +#endif +} + +uint16_t adc_sensor_get_current_ma(void) +{ + if (!initialized) { + LOG_WRN("ADC sensor not initialized, calling adc_sensor_init()"); + adc_sensor_init(); + } + +#ifdef CONFIG_ADC_SENSOR_SIMULATED + return SIMULATED_CURRENT_MA; +#else + // TODO: Read real ADC value for current + // For now return simulated value + return SIMULATED_CURRENT_MA; +#endif +} diff --git a/software/lib/modbus_server/modbus_server.c b/software/lib/modbus_server/modbus_server.c index 41ab8ad..9514ce8 100644 --- a/software/lib/modbus_server/modbus_server.c +++ b/software/lib/modbus_server/modbus_server.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -121,7 +122,7 @@ static int input_reg_rd(uint16_t addr, uint16_t *reg) *reg = (valve_get_movement() << 8) | (valve_get_state() & 0xFF); break; case REG_INPUT_MOTOR_CURRENT_MA: - *reg = valve_get_motor_current(); + *reg = adc_sensor_get_current_ma(); break; case REG_INPUT_UPTIME_SECONDS_LOW: *reg = (uint16_t)(uptime_s & 0xFFFF); @@ -130,7 +131,7 @@ static int input_reg_rd(uint16_t addr, uint16_t *reg) *reg = (uint16_t)(uptime_s >> 16); break; case REG_INPUT_SUPPLY_VOLTAGE_MV: - *reg = 12300; + *reg = adc_sensor_get_voltage_mv(); break; case REG_INPUT_FWU_LAST_CHUNK_CRC: *reg = fwu_get_last_chunk_crc(); @@ -160,6 +161,13 @@ int modbus_server_init(void) { k_timer_init(&watchdog_timer, watchdog_timer_handler, NULL); + // Initialize ADC sensor + int ret = adc_sensor_init(); + if (ret < 0) { + LOG_ERR("Failed to initialize ADC sensor: %d", ret); + return ret; + } + // Load saved settings uint32_t saved_baudrate = 19200; uint8_t saved_unit_id = 1;