Refactor VND7050AJ sensor configuration to eliminate redundancy
- Create centralized sensor multiplexer node (vnd7050aj_mux) with shared configuration - Consolidate ADC channel, GPIO pins, and reference voltage in single location - Update sensor bindings to reference centralized mux via sensor-mux property - Add channel-based sensor selection using mux-channel property (0-3) - Refactor ADC sensor library to use centralized GPIO and channel control - Update valve library to use new vnd7050aj_mux node reference - Eliminate duplicate ADC/GPIO definitions between voltage and current sensors - Ensure configuration consistency and prevent mismatched settings Benefits: - Single source of truth for VND7050AJ hardware configuration - Impossible to have inconsistent GPIO/ADC settings between sensors - Simplified maintenance and scalability for additional sensors - Clean channel-based multiplexer selection interface
This commit is contained in:
parent
a9a0626913
commit
222ffea568
|
|
@ -1,15 +1,23 @@
|
|||
/ {
|
||||
vnd7050aj: vnd7050aj {
|
||||
compatible = "vnd7050aj-valve-controller";
|
||||
/* VND7050AJ Sensor Multiplexer - Centralized configuration */
|
||||
vnd7050aj_mux: sensor-multiplexer {
|
||||
compatible = "vnd7050aj,sensor-mux";
|
||||
status = "okay";
|
||||
|
||||
// VND7050AJ GPIO pin definitions
|
||||
in0-gpios = <&gpiob 7 GPIO_ACTIVE_HIGH>; // IN0 (PB7) - Input 0 control signal
|
||||
in1-gpios = <&gpiob 9 GPIO_ACTIVE_HIGH>; // IN1 (PB9) - Input 1 control signal
|
||||
rst-gpios = <&gpiob 3 GPIO_ACTIVE_HIGH>; // RST (PB3) - Reset pin for VND7050AJ
|
||||
sen-gpios = <&gpiob 4 GPIO_ACTIVE_HIGH>; // SEN (PB4) - Sense Enable for current monitoring
|
||||
s0-gpios = <&gpiob 6 GPIO_ACTIVE_HIGH>; // S0 (PB6) - Status/Select 0 output from VND7050AJ
|
||||
s1-gpios = <&gpiob 5 GPIO_ACTIVE_HIGH>; // S1 (PB5) - Status/Select 1 output from VND7050AJ
|
||||
/* Shared ADC configuration */
|
||||
io-channels = <&adc1 1>; /* ADC1 channel 1 (PA0) */
|
||||
io-channel-names = "sensor-input";
|
||||
reference-mv = <3300>;
|
||||
|
||||
/* VND7050AJ GPIO pin definitions - shared by all sensors */
|
||||
sen-gpios = <&gpiob 4 GPIO_ACTIVE_HIGH>; /* SEN (PB4) - Sense Enable */
|
||||
s0-gpios = <&gpiob 6 GPIO_ACTIVE_HIGH>; /* S0 (PB6) - Mux select bit 0 */
|
||||
s1-gpios = <&gpiob 5 GPIO_ACTIVE_HIGH>; /* S1 (PB5) - Mux select bit 1 */
|
||||
|
||||
/* Valve control pins (separate from sensor mux) */
|
||||
in0-gpios = <&gpiob 7 GPIO_ACTIVE_HIGH>; /* IN0 (PB7) - Valve input 0 */
|
||||
in1-gpios = <&gpiob 9 GPIO_ACTIVE_HIGH>; /* IN1 (PB9) - Valve input 1 */
|
||||
rst-gpios = <&gpiob 3 GPIO_ACTIVE_HIGH>; /* RST (PB3) - Reset pin */
|
||||
};
|
||||
|
||||
adc_sensors {
|
||||
|
|
@ -17,32 +25,22 @@
|
|||
|
||||
supply_voltage: supply-voltage {
|
||||
compatible = "custom,supply-voltage";
|
||||
io-channels = <&adc1 1>; /* ADC1 channel 1 (PA0) */
|
||||
io-channel-names = "voltage";
|
||||
reference-mv = <3300>;
|
||||
sensor-mux = <&vnd7050aj_mux>; /* Reference to shared mux config */
|
||||
|
||||
/* Sensor-specific configuration */
|
||||
voltage-divider-ratio = <4>; /* Adjust based on your voltage divider */
|
||||
|
||||
/* GPIO control pins using VND7050AJ pins */
|
||||
sen-gpios = <&gpiob 4 GPIO_ACTIVE_HIGH>; /* SEN (PB4) - enable sensor */
|
||||
s0-gpios = <&gpiob 6 GPIO_ACTIVE_HIGH>; /* S0 (PB6) - mux select bit 0 */
|
||||
s1-gpios = <&gpiob 5 GPIO_ACTIVE_HIGH>; /* S1 (PB5) - mux select bit 1 */
|
||||
|
||||
measurement-delay-ms = <5>; /* 5ms delay after GPIO setup */
|
||||
mux-channel = <0>; /* Channel 0: s1=0, s0=0 */
|
||||
measurement-delay-ms = <5>; /* 5ms delay after GPIO setup */
|
||||
};
|
||||
|
||||
motor_current: motor-current {
|
||||
compatible = "custom,motor-current";
|
||||
io-channels = <&adc1 1>; /* Same ADC channel, different mux setting */
|
||||
io-channel-names = "current";
|
||||
reference-mv = <3300>;
|
||||
sensor-mux = <&vnd7050aj_mux>; /* Reference to shared mux config */
|
||||
|
||||
/* Sensor-specific configuration */
|
||||
current-sense-resistor-mohm = <100>; /* 100mΩ sense resistor */
|
||||
|
||||
/* GPIO control pins using VND7050AJ pins */
|
||||
sen-gpios = <&gpiob 4 GPIO_ACTIVE_HIGH>; /* SEN (PB4) - enable sensor */
|
||||
s0-gpios = <&gpiob 6 GPIO_ACTIVE_HIGH>; /* S0 (PB6) - mux select bit 0 */
|
||||
s1-gpios = <&gpiob 5 GPIO_ACTIVE_HIGH>; /* S1 (PB5) - mux select bit 1 */
|
||||
|
||||
measurement-delay-ms = <10>; /* 10ms delay for current settling */
|
||||
mux-channel = <1>; /* Channel 1: s1=0, s0=1 */
|
||||
measurement-delay-ms = <10>; /* 10ms delay for current settling */
|
||||
};
|
||||
};
|
||||
};
|
||||
|
|
@ -86,15 +84,6 @@
|
|||
st,adc-prescaler = <4>;
|
||||
#address-cells = <1>;
|
||||
#size-cells = <0>;
|
||||
|
||||
// Definition des ADC-Kanals für MULTISENSE (PA0)
|
||||
channel@1 { // ADC1_IN1 ist Kanal 1
|
||||
reg = <1>; // Kanalnummer
|
||||
zephyr,gain = "ADC_GAIN_1";
|
||||
zephyr,reference = "ADC_REF_INTERNAL";
|
||||
zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
|
||||
zephyr,resolution = <12>;
|
||||
};
|
||||
};
|
||||
|
||||
&pinctrl {
|
||||
|
|
|
|||
|
|
@ -1,16 +1,13 @@
|
|||
description: Custom motor current measurement with GPIO control
|
||||
# Custom motor current sensor binding
|
||||
description: Motor current sensor using VND7050AJ multiplexer
|
||||
|
||||
compatible: "custom,motor-current"
|
||||
|
||||
properties:
|
||||
io-channels:
|
||||
type: phandle-array
|
||||
sensor-mux:
|
||||
type: phandle
|
||||
required: true
|
||||
description: ADC channel for current measurement
|
||||
|
||||
io-channel-names:
|
||||
type: string-array
|
||||
description: Names for the ADC channels
|
||||
description: Reference to the VND7050AJ sensor multiplexer node
|
||||
|
||||
current-sense-resistor-mohm:
|
||||
type: int
|
||||
|
|
@ -19,30 +16,17 @@ properties:
|
|||
|
||||
amplifier-gain:
|
||||
type: int
|
||||
required: false
|
||||
default: 1
|
||||
description: Current sense amplifier gain
|
||||
|
||||
reference-mv:
|
||||
mux-channel:
|
||||
type: int
|
||||
default: 3300
|
||||
description: ADC reference voltage in millivolts
|
||||
|
||||
sen-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO to enable/disable the current measurement sensor
|
||||
|
||||
s0-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO for multiplexer control bit 0
|
||||
|
||||
s1-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO for multiplexer control bit 1
|
||||
description: Multiplexer channel number (0-3) for this sensor
|
||||
|
||||
measurement-delay-ms:
|
||||
type: int
|
||||
required: false
|
||||
default: 10
|
||||
description: Delay in milliseconds after setting GPIOs before ADC measurement
|
||||
description: Delay in milliseconds after GPIO setup before measurement
|
||||
|
|
|
|||
|
|
@ -1,63 +1,26 @@
|
|||
description: Custom supply voltage measurement with GPIO control
|
||||
# Custom supply voltage sensor binding
|
||||
description: Supply voltage sensor using VND7050AJ multiplexer
|
||||
|
||||
compatible: "custom,supply-voltage"
|
||||
|
||||
properties:
|
||||
io-channels:
|
||||
type: phandle-array
|
||||
sensor-mux:
|
||||
type: phandle
|
||||
required: true
|
||||
description: ADC channel for voltage measurement
|
||||
|
||||
io-channel-names:
|
||||
type: string-array
|
||||
description: Names for the ADC channels
|
||||
description: Reference to the VND7050AJ sensor multiplexer node
|
||||
|
||||
voltage-divider-ratio:
|
||||
type: int
|
||||
required: true
|
||||
description: Voltage divider ratio for scaling
|
||||
description: Voltage divider ratio for scaling measurements
|
||||
|
||||
reference-mv:
|
||||
mux-channel:
|
||||
type: int
|
||||
default: 3300
|
||||
description: ADC reference voltage in millivolts
|
||||
|
||||
sen-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO to enable/disable the voltage measurement sensor
|
||||
|
||||
s0-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO for multiplexer control bit 0
|
||||
|
||||
s1-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO for multiplexer control bit 1
|
||||
description: Multiplexer channel number (0-3) for this sensor
|
||||
|
||||
measurement-delay-ms:
|
||||
type: int
|
||||
default: 10
|
||||
description: Delay in milliseconds after setting GPIOs before ADC measurement
|
||||
|
||||
sen-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO for SEN (Sense Enable) pin
|
||||
|
||||
s0-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO for S0 (Select 0) pin
|
||||
|
||||
s1-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO for S1 (Select 1) pin
|
||||
|
||||
measurement-delay-ms:
|
||||
type: int
|
||||
default: 10
|
||||
description: Delay in milliseconds after setting control pins before ADC reading
|
||||
required: false
|
||||
default: 5
|
||||
description: Delay in milliseconds after GPIO setup before measurement
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
# VND7050AJ Sensor Multiplexer binding
|
||||
description: VND7050AJ sensor multiplexer for ADC channel selection
|
||||
|
||||
compatible: "vnd7050aj,sensor-mux"
|
||||
|
||||
properties:
|
||||
io-channels:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: ADC channel phandle and specifier for sensor input
|
||||
|
||||
io-channel-names:
|
||||
type: string-array
|
||||
required: true
|
||||
description: Names for the ADC channels
|
||||
|
||||
reference-mv:
|
||||
type: int
|
||||
required: true
|
||||
description: ADC reference voltage in millivolts
|
||||
|
||||
sen-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO for sensor enable (SEN pin)
|
||||
|
||||
s0-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO for multiplexer select bit 0 (S0 pin)
|
||||
|
||||
s1-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO for multiplexer select bit 1 (S1 pin)
|
||||
|
||||
in0-gpios:
|
||||
type: phandle-array
|
||||
required: false
|
||||
description: GPIO for valve input 0 control (IN0 pin)
|
||||
|
||||
in1-gpios:
|
||||
type: phandle-array
|
||||
required: false
|
||||
description: GPIO for valve input 1 control (IN1 pin)
|
||||
|
||||
rst-gpios:
|
||||
type: phandle-array
|
||||
required: false
|
||||
description: GPIO for reset control (RST pin)
|
||||
|
|
@ -23,16 +23,30 @@ LOG_MODULE_REGISTER(adc_sensor, LOG_LEVEL_INF);
|
|||
// Devicetree node checks
|
||||
#define VOLTAGE_SENSOR_NODE DT_NODELABEL(supply_voltage)
|
||||
#define CURRENT_SENSOR_NODE DT_NODELABEL(motor_current)
|
||||
#define SENSOR_MUX_NODE DT_NODELABEL(vnd7050aj_mux)
|
||||
|
||||
#ifndef CONFIG_ADC_SENSOR_SIMULATED
|
||||
// ADC device reference
|
||||
#if DT_NODE_EXISTS(VOLTAGE_SENSOR_NODE)
|
||||
#define ADC_NODE DT_PHANDLE(VOLTAGE_SENSOR_NODE, io_channels)
|
||||
#define ADC_CHANNEL DT_PHA(VOLTAGE_SENSOR_NODE, io_channels, input)
|
||||
// ADC device reference from centralized mux node
|
||||
#if DT_NODE_EXISTS(SENSOR_MUX_NODE)
|
||||
#define ADC_NODE DT_PHANDLE(SENSOR_MUX_NODE, io_channels)
|
||||
#define ADC_CHANNEL DT_PHA(SENSOR_MUX_NODE, io_channels, input)
|
||||
#define ADC_RESOLUTION 12
|
||||
#define ADC_REFERENCE_MV DT_PROP(VOLTAGE_SENSOR_NODE, reference_mv)
|
||||
#define ADC_REFERENCE_MV DT_PROP(SENSOR_MUX_NODE, reference_mv)
|
||||
|
||||
// Sensor-specific properties
|
||||
#if DT_NODE_EXISTS(VOLTAGE_SENSOR_NODE)
|
||||
#define VOLTAGE_DIVIDER_RATIO \
|
||||
DT_PROP(VOLTAGE_SENSOR_NODE, voltage_divider_ratio)
|
||||
#define VOLTAGE_MUX_CHANNEL DT_PROP(VOLTAGE_SENSOR_NODE, mux_channel)
|
||||
#define VOLTAGE_DELAY_MS DT_PROP(VOLTAGE_SENSOR_NODE, measurement_delay_ms)
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(CURRENT_SENSOR_NODE)
|
||||
#define CURRENT_SENSE_RESISTOR_MOHM \
|
||||
DT_PROP(CURRENT_SENSOR_NODE, current_sense_resistor_mohm)
|
||||
#define CURRENT_MUX_CHANNEL DT_PROP(CURRENT_SENSOR_NODE, mux_channel)
|
||||
#define CURRENT_DELAY_MS DT_PROP(CURRENT_SENSOR_NODE, measurement_delay_ms)
|
||||
#endif
|
||||
|
||||
static const struct device *adc_dev;
|
||||
static struct adc_channel_cfg adc_channel_cfg = {
|
||||
|
|
@ -55,81 +69,44 @@ static uint16_t adc_buffer;
|
|||
static bool initialized = false;
|
||||
|
||||
#ifndef CONFIG_ADC_SENSOR_SIMULATED
|
||||
// GPIO specs for voltage sensor (if devicetree nodes exist)
|
||||
#if DT_NODE_EXISTS(VOLTAGE_SENSOR_NODE)
|
||||
static const struct gpio_dt_spec voltage_sen_gpio =
|
||||
GPIO_DT_SPEC_GET(VOLTAGE_SENSOR_NODE, sen_gpios);
|
||||
static const struct gpio_dt_spec voltage_s0_gpio =
|
||||
GPIO_DT_SPEC_GET(VOLTAGE_SENSOR_NODE, s0_gpios);
|
||||
static const struct gpio_dt_spec voltage_s1_gpio =
|
||||
GPIO_DT_SPEC_GET(VOLTAGE_SENSOR_NODE, s1_gpios);
|
||||
#endif
|
||||
|
||||
// GPIO specs for current sensor (if devicetree nodes exist)
|
||||
#if DT_NODE_EXISTS(CURRENT_SENSOR_NODE)
|
||||
static const struct gpio_dt_spec current_sen_gpio =
|
||||
GPIO_DT_SPEC_GET(CURRENT_SENSOR_NODE, sen_gpios);
|
||||
static const struct gpio_dt_spec current_s0_gpio =
|
||||
GPIO_DT_SPEC_GET(CURRENT_SENSOR_NODE, s0_gpios);
|
||||
static const struct gpio_dt_spec current_s1_gpio =
|
||||
GPIO_DT_SPEC_GET(CURRENT_SENSOR_NODE, s1_gpios);
|
||||
// GPIO specs from centralized mux node
|
||||
#if DT_NODE_EXISTS(SENSOR_MUX_NODE)
|
||||
static const struct gpio_dt_spec sen_gpio =
|
||||
GPIO_DT_SPEC_GET(SENSOR_MUX_NODE, sen_gpios);
|
||||
static const struct gpio_dt_spec s0_gpio =
|
||||
GPIO_DT_SPEC_GET(SENSOR_MUX_NODE, s0_gpios);
|
||||
static const struct gpio_dt_spec s1_gpio =
|
||||
GPIO_DT_SPEC_GET(SENSOR_MUX_NODE, s1_gpios);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Configure GPIO pins for ADC sensor control
|
||||
* @brief Configure GPIO pins for ADC sensor multiplexer control
|
||||
*/
|
||||
static int configure_sensor_gpios(void) {
|
||||
int ret = 0;
|
||||
|
||||
#if DT_NODE_EXISTS(VOLTAGE_SENSOR_NODE)
|
||||
// Configure voltage sensor GPIOs
|
||||
if (gpio_is_ready_dt(&voltage_sen_gpio)) {
|
||||
ret = gpio_pin_configure_dt(&voltage_sen_gpio, GPIO_OUTPUT_INACTIVE);
|
||||
#if DT_NODE_EXISTS(SENSOR_MUX_NODE)
|
||||
// Configure sensor multiplexer GPIOs
|
||||
if (gpio_is_ready_dt(&sen_gpio)) {
|
||||
ret = gpio_pin_configure_dt(&sen_gpio, GPIO_OUTPUT_INACTIVE);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to configure voltage sen GPIO: %d", ret);
|
||||
LOG_ERR("Failed to configure SEN GPIO: %d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
if (gpio_is_ready_dt(&voltage_s0_gpio)) {
|
||||
ret = gpio_pin_configure_dt(&voltage_s0_gpio, GPIO_OUTPUT_INACTIVE);
|
||||
if (gpio_is_ready_dt(&s0_gpio)) {
|
||||
ret = gpio_pin_configure_dt(&s0_gpio, GPIO_OUTPUT_INACTIVE);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to configure voltage s0 GPIO: %d", ret);
|
||||
LOG_ERR("Failed to configure S0 GPIO: %d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
if (gpio_is_ready_dt(&voltage_s1_gpio)) {
|
||||
ret = gpio_pin_configure_dt(&voltage_s1_gpio, GPIO_OUTPUT_INACTIVE);
|
||||
if (gpio_is_ready_dt(&s1_gpio)) {
|
||||
ret = gpio_pin_configure_dt(&s1_gpio, GPIO_OUTPUT_INACTIVE);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to configure voltage s1 GPIO: %d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(CURRENT_SENSOR_NODE)
|
||||
// Configure current sensor GPIOs
|
||||
if (gpio_is_ready_dt(¤t_sen_gpio)) {
|
||||
ret = gpio_pin_configure_dt(¤t_sen_gpio, GPIO_OUTPUT_INACTIVE);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to configure current sen GPIO: %d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
if (gpio_is_ready_dt(¤t_s0_gpio)) {
|
||||
ret = gpio_pin_configure_dt(¤t_s0_gpio, GPIO_OUTPUT_INACTIVE);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to configure current s0 GPIO: %d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
if (gpio_is_ready_dt(¤t_s1_gpio)) {
|
||||
ret = gpio_pin_configure_dt(¤t_s1_gpio, GPIO_OUTPUT_INACTIVE);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to configure current s1 GPIO: %d", ret);
|
||||
LOG_ERR("Failed to configure S1 GPIO: %d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
|
@ -139,55 +116,27 @@ static int configure_sensor_gpios(void) {
|
|||
}
|
||||
|
||||
/**
|
||||
* @brief Set GPIO pins for voltage measurement
|
||||
* @param s0_state State for S0 pin (multiplexer bit 0)
|
||||
* @param s1_state State for S1 pin (multiplexer bit 1)
|
||||
* @brief Set multiplexer channel for sensor selection
|
||||
* @param enable Enable/disable the sensor
|
||||
* @param channel Multiplexer channel (0-3)
|
||||
* @param delay_ms Delay after setting GPIOs
|
||||
*/
|
||||
static int set_voltage_sensor_gpios(bool enable, bool s0_state, bool s1_state) {
|
||||
#if DT_NODE_EXISTS(VOLTAGE_SENSOR_NODE)
|
||||
if (gpio_is_ready_dt(&voltage_sen_gpio)) {
|
||||
gpio_pin_set_dt(&voltage_sen_gpio, enable ? 1 : 0);
|
||||
static int set_mux_channel(bool enable, uint8_t channel, uint32_t delay_ms) {
|
||||
#if DT_NODE_EXISTS(SENSOR_MUX_NODE)
|
||||
if (gpio_is_ready_dt(&sen_gpio)) {
|
||||
gpio_pin_set_dt(&sen_gpio, enable ? 1 : 0);
|
||||
}
|
||||
if (gpio_is_ready_dt(&voltage_s0_gpio)) {
|
||||
gpio_pin_set_dt(&voltage_s0_gpio, s0_state ? 1 : 0);
|
||||
if (gpio_is_ready_dt(&s0_gpio)) {
|
||||
gpio_pin_set_dt(&s0_gpio, (channel & 0x01) ? 1 : 0);
|
||||
}
|
||||
if (gpio_is_ready_dt(&voltage_s1_gpio)) {
|
||||
gpio_pin_set_dt(&voltage_s1_gpio, s1_state ? 1 : 0);
|
||||
if (gpio_is_ready_dt(&s1_gpio)) {
|
||||
gpio_pin_set_dt(&s1_gpio, (channel & 0x02) ? 1 : 0);
|
||||
}
|
||||
|
||||
// Delay for GPIO settling (from devicetree or default)
|
||||
#if DT_NODE_HAS_PROP(VOLTAGE_SENSOR_NODE, measurement_delay_ms)
|
||||
k_msleep(DT_PROP(VOLTAGE_SENSOR_NODE, measurement_delay_ms));
|
||||
#else
|
||||
k_msleep(5); // Default 5ms delay
|
||||
#endif
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set GPIO pins for current measurement
|
||||
* @param s0_state State for S0 pin (multiplexer bit 0)
|
||||
* @param s1_state State for S1 pin (multiplexer bit 1)
|
||||
*/
|
||||
static int set_current_sensor_gpios(bool enable, bool s0_state, bool s1_state) {
|
||||
#if DT_NODE_EXISTS(CURRENT_SENSOR_NODE)
|
||||
if (gpio_is_ready_dt(¤t_sen_gpio)) {
|
||||
gpio_pin_set_dt(¤t_sen_gpio, enable ? 1 : 0);
|
||||
// Delay for GPIO settling
|
||||
if (delay_ms > 0) {
|
||||
k_msleep(delay_ms);
|
||||
}
|
||||
if (gpio_is_ready_dt(¤t_s0_gpio)) {
|
||||
gpio_pin_set_dt(¤t_s0_gpio, s0_state ? 1 : 0);
|
||||
}
|
||||
if (gpio_is_ready_dt(¤t_s1_gpio)) {
|
||||
gpio_pin_set_dt(¤t_s1_gpio, s1_state ? 1 : 0);
|
||||
}
|
||||
|
||||
// Delay for GPIO settling (from devicetree or default)
|
||||
#if DT_NODE_HAS_PROP(CURRENT_SENSOR_NODE, measurement_delay_ms)
|
||||
k_msleep(DT_PROP(CURRENT_SENSOR_NODE, measurement_delay_ms));
|
||||
#else
|
||||
k_msleep(10); // Default 10ms delay
|
||||
#endif
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -195,11 +144,11 @@ static int set_current_sensor_gpios(bool enable, bool s0_state, bool s1_state) {
|
|||
|
||||
#ifndef CONFIG_ADC_SENSOR_SIMULATED
|
||||
/**
|
||||
* @brief Read ADC value and convert to millivolts
|
||||
* @brief Read ADC value and convert to millivolts (for voltage sensor)
|
||||
* @return ADC reading in millivolts, or 0 on error
|
||||
*/
|
||||
static uint16_t read_adc_voltage_mv(void) {
|
||||
#if DT_NODE_EXISTS(VOLTAGE_SENSOR_NODE)
|
||||
#if DT_NODE_EXISTS(SENSOR_MUX_NODE) && DT_NODE_EXISTS(VOLTAGE_SENSOR_NODE)
|
||||
int ret = adc_read(adc_dev, &adc_sequence);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("ADC read failed: %d", ret);
|
||||
|
|
@ -227,7 +176,7 @@ static uint16_t read_adc_voltage_mv(void) {
|
|||
* @return ADC reading in milliamps, or 0 on error
|
||||
*/
|
||||
static uint16_t read_adc_current_ma(void) {
|
||||
#if DT_NODE_EXISTS(CURRENT_SENSOR_NODE)
|
||||
#if DT_NODE_EXISTS(SENSOR_MUX_NODE) && DT_NODE_EXISTS(CURRENT_SENSOR_NODE)
|
||||
int ret = adc_read(adc_dev, &adc_sequence);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("ADC read failed: %d", ret);
|
||||
|
|
@ -238,10 +187,10 @@ static uint16_t read_adc_current_ma(void) {
|
|||
uint32_t adc_value = adc_buffer;
|
||||
uint32_t voltage_mv = (adc_value * ADC_REFERENCE_MV) / 4095;
|
||||
|
||||
// Convert voltage to current based on current sensor characteristics
|
||||
// Assuming a linear current sensor with specific mV/mA ratio
|
||||
// This will need to be calibrated for your specific current sensor
|
||||
uint32_t current_ma = voltage_mv / 10; // Example: 10mV per mA
|
||||
// Convert voltage to current based on sense resistor
|
||||
// I = V / R, where R is in milliohms and V is in millivolts
|
||||
// Result is in milliamps
|
||||
uint32_t current_ma = (voltage_mv * 1000) / CURRENT_SENSE_RESISTOR_MOHM;
|
||||
|
||||
LOG_DBG("ADC raw: %u, current: %u mA", adc_value, (uint16_t)current_ma);
|
||||
|
||||
|
|
@ -270,7 +219,7 @@ int adc_sensor_init(void) {
|
|||
}
|
||||
|
||||
// Initialize ADC hardware
|
||||
#if DT_NODE_EXISTS(VOLTAGE_SENSOR_NODE)
|
||||
#if DT_NODE_EXISTS(SENSOR_MUX_NODE)
|
||||
adc_dev = DEVICE_DT_GET(ADC_NODE);
|
||||
if (!device_is_ready(adc_dev)) {
|
||||
LOG_ERR("ADC device not ready");
|
||||
|
|
@ -288,13 +237,15 @@ int adc_sensor_init(void) {
|
|||
LOG_INF("ADC device ready: %s", adc_dev->name);
|
||||
#endif
|
||||
|
||||
LOG_INF("ADC sensor initialized (real ADC mode with GPIO control)");
|
||||
LOG_INF("ADC sensor initialized (real ADC mode with centralized mux)");
|
||||
|
||||
#if DT_NODE_EXISTS(VOLTAGE_SENSOR_NODE)
|
||||
LOG_INF("Voltage sensor found in devicetree");
|
||||
LOG_INF("Voltage sensor: channel %d, divider ratio %d", VOLTAGE_MUX_CHANNEL,
|
||||
VOLTAGE_DIVIDER_RATIO);
|
||||
#endif
|
||||
#if DT_NODE_EXISTS(CURRENT_SENSOR_NODE)
|
||||
LOG_INF("Current sensor found in devicetree");
|
||||
LOG_INF("Current sensor: channel %d, sense resistor %d mOhm",
|
||||
CURRENT_MUX_CHANNEL, CURRENT_SENSE_RESISTOR_MOHM);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
|
@ -311,16 +262,20 @@ uint16_t adc_sensor_get_voltage_mv(void) {
|
|||
#ifdef CONFIG_ADC_SENSOR_SIMULATED
|
||||
return SIMULATED_VOLTAGE_MV;
|
||||
#else
|
||||
// Set GPIOs for voltage measurement (example: s0=0, s1=0 for channel 0)
|
||||
set_voltage_sensor_gpios(true, false, false);
|
||||
// Set multiplexer to voltage channel
|
||||
#if DT_NODE_EXISTS(VOLTAGE_SENSOR_NODE)
|
||||
set_mux_channel(true, VOLTAGE_MUX_CHANNEL, VOLTAGE_DELAY_MS);
|
||||
|
||||
// Read real ADC value for voltage
|
||||
uint16_t voltage = read_adc_voltage_mv();
|
||||
|
||||
// Disable sensor after measurement to save power
|
||||
set_voltage_sensor_gpios(false, false, false);
|
||||
set_mux_channel(false, 0, 0);
|
||||
|
||||
return voltage;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
@ -333,15 +288,19 @@ uint16_t adc_sensor_get_current_ma(void) {
|
|||
#ifdef CONFIG_ADC_SENSOR_SIMULATED
|
||||
return SIMULATED_CURRENT_MA;
|
||||
#else
|
||||
// Set GPIOs for current measurement (example: s0=1, s1=0 for channel 1)
|
||||
set_current_sensor_gpios(true, true, false);
|
||||
// Set multiplexer to current channel
|
||||
#if DT_NODE_EXISTS(CURRENT_SENSOR_NODE)
|
||||
set_mux_channel(true, CURRENT_MUX_CHANNEL, CURRENT_DELAY_MS);
|
||||
|
||||
// Read real ADC value for current
|
||||
uint16_t current = read_adc_current_ma();
|
||||
|
||||
// Disable sensor after measurement to save power
|
||||
set_current_sensor_gpios(false, false, false);
|
||||
set_mux_channel(false, 0, 0);
|
||||
|
||||
return current;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,12 +17,12 @@
|
|||
LOG_MODULE_REGISTER(valve, LOG_LEVEL_INF);
|
||||
|
||||
static const struct valve_gpios valve_gpios = {
|
||||
.in0 = GPIO_DT_SPEC_GET(DT_NODELABEL(vnd7050aj), in0_gpios),
|
||||
.in1 = GPIO_DT_SPEC_GET(DT_NODELABEL(vnd7050aj), in1_gpios),
|
||||
.rst = GPIO_DT_SPEC_GET(DT_NODELABEL(vnd7050aj), rst_gpios),
|
||||
.sen = GPIO_DT_SPEC_GET(DT_NODELABEL(vnd7050aj), sen_gpios),
|
||||
.s0 = GPIO_DT_SPEC_GET(DT_NODELABEL(vnd7050aj), s0_gpios),
|
||||
.s1 = GPIO_DT_SPEC_GET(DT_NODELABEL(vnd7050aj), s1_gpios),
|
||||
.in0 = GPIO_DT_SPEC_GET(DT_NODELABEL(vnd7050aj_mux), in0_gpios),
|
||||
.in1 = GPIO_DT_SPEC_GET(DT_NODELABEL(vnd7050aj_mux), in1_gpios),
|
||||
.rst = GPIO_DT_SPEC_GET(DT_NODELABEL(vnd7050aj_mux), rst_gpios),
|
||||
.sen = GPIO_DT_SPEC_GET(DT_NODELABEL(vnd7050aj_mux), sen_gpios),
|
||||
.s0 = GPIO_DT_SPEC_GET(DT_NODELABEL(vnd7050aj_mux), s0_gpios),
|
||||
.s1 = GPIO_DT_SPEC_GET(DT_NODELABEL(vnd7050aj_mux), s1_gpios),
|
||||
};
|
||||
|
||||
static enum valve_state current_state = VALVE_STATE_CLOSED;
|
||||
|
|
|
|||
Loading…
Reference in New Issue