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.
This commit is contained in:
parent
edf0fb2563
commit
c9b0f38576
|
|
@ -0,0 +1,27 @@
|
|||
#ifndef ADC_SENSOR_H
|
||||
#define ADC_SENSOR_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
menu "Irrigation system software libraries"
|
||||
|
||||
rsource "adc_sensor/Kconfig"
|
||||
rsource "fwu/Kconfig"
|
||||
rsource "modbus_server/Kconfig"
|
||||
rsource "valve/Kconfig"
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
zephyr_library_sources(adc_sensor.c)
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <lib/adc_sensor.h>
|
||||
|
||||
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
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@
|
|||
#include <lib/modbus_server.h>
|
||||
#include <lib/valve.h>
|
||||
#include <lib/fwu.h>
|
||||
#include <lib/adc_sensor.h>
|
||||
#include <app_version.h>
|
||||
|
||||
#include <zephyr/usb/usb_device.h>
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in New Issue