added formatting
This commit is contained in:
@@ -2,14 +2,14 @@
|
||||
* @file adc_sensor.c
|
||||
* @brief Implementation of the ADC sensor library.
|
||||
*
|
||||
* This file contains the implementation for initializing and reading from ADC sensors.
|
||||
* It currently provides simulated values for voltage and current, with placeholders
|
||||
* for real hardware ADC implementation.
|
||||
* This file contains the implementation for initializing and reading from ADC
|
||||
* sensors. It currently provides simulated values for voltage and current, with
|
||||
* placeholders for real hardware ADC implementation.
|
||||
*/
|
||||
|
||||
#include <lib/adc_sensor.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <lib/adc_sensor.h>
|
||||
|
||||
LOG_MODULE_REGISTER(adc_sensor, LOG_LEVEL_INF);
|
||||
|
||||
@@ -17,54 +17,53 @@ LOG_MODULE_REGISTER(adc_sensor, LOG_LEVEL_INF);
|
||||
#define SIMULATED_VOLTAGE_MV 12000
|
||||
#define SIMULATED_CURRENT_MA 45
|
||||
|
||||
static bool initialized = false; // Flag to indicate if the ADC sensor is initialized
|
||||
static bool initialized =
|
||||
false; // Flag to indicate if the ADC sensor is initialized
|
||||
|
||||
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;
|
||||
int adc_sensor_init(void) {
|
||||
if (initialized) {
|
||||
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;
|
||||
LOG_INF("ADC sensor initialized (simulated mode)");
|
||||
LOG_INF("Simulated values: %dmV, %dmA", SIMULATED_VOLTAGE_MV,
|
||||
SIMULATED_CURRENT_MA);
|
||||
#else
|
||||
// TODO: Read real ADC value for voltage
|
||||
// For now return simulated value
|
||||
return SIMULATED_VOLTAGE_MV;
|
||||
// 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();
|
||||
}
|
||||
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;
|
||||
return SIMULATED_CURRENT_MA;
|
||||
#else
|
||||
// TODO: Read real ADC value for current
|
||||
// For now return simulated value
|
||||
return SIMULATED_CURRENT_MA;
|
||||
// TODO: Read real ADC value for current
|
||||
// For now return simulated value
|
||||
return SIMULATED_CURRENT_MA;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -8,48 +8,51 @@
|
||||
* the update process. The actual writing to flash is simulated.
|
||||
*/
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/sys/crc.h>
|
||||
#include <zephyr/sys/byteorder.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <lib/fwu.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/sys/byteorder.h>
|
||||
#include <zephyr/sys/crc.h>
|
||||
|
||||
LOG_MODULE_REGISTER(fwu, LOG_LEVEL_INF);
|
||||
|
||||
#define FWU_BUFFER_SIZE 256
|
||||
static uint8_t fwu_buffer[FWU_BUFFER_SIZE]; // Buffer to store incoming firmware data chunks
|
||||
static uint32_t fwu_chunk_offset = 0; // Offset for the current firmware chunk in the overall image
|
||||
static uint16_t fwu_chunk_size = 0; // Size of the current firmware chunk
|
||||
static uint16_t fwu_last_chunk_crc = 0; // CRC16 of the last received firmware chunk
|
||||
static uint8_t fwu_buffer[FWU_BUFFER_SIZE]; // Buffer to store incoming
|
||||
// firmware data chunks
|
||||
static uint32_t fwu_chunk_offset =
|
||||
0; // Offset for the current firmware chunk in the overall image
|
||||
static uint16_t fwu_chunk_size = 0; // Size of the current firmware chunk
|
||||
static uint16_t fwu_last_chunk_crc =
|
||||
0; // CRC16 of the last received firmware chunk
|
||||
|
||||
void fwu_init(void) {}
|
||||
|
||||
void fwu_handler(uint16_t addr, uint16_t reg)
|
||||
{
|
||||
// This is a simplified handler. In a real scenario, you would have a proper mapping
|
||||
// between register addresses and actions.
|
||||
if (addr == 0x0100) { // FWU_COMMAND
|
||||
if (reg == 1) { LOG_INF("FWU: Chunk at offset %u (size %u) verified.", fwu_chunk_offset, fwu_chunk_size); }
|
||||
else if (reg == 2) { LOG_INF("FWU: Finalize command received. Rebooting (simulated)."); }
|
||||
} else if (addr == 0x0101) { // FWU_CHUNK_OFFSET_LOW
|
||||
fwu_chunk_offset = (fwu_chunk_offset & 0xFFFF0000) | reg;
|
||||
} else if (addr == 0x0102) { // FWU_CHUNK_OFFSET_HIGH
|
||||
fwu_chunk_offset = (fwu_chunk_offset & 0x0000FFFF) | ((uint32_t)reg << 16);
|
||||
} else if (addr == 0x0103) { // FWU_CHUNK_SIZE
|
||||
fwu_chunk_size = (reg > FWU_BUFFER_SIZE) ? FWU_BUFFER_SIZE : reg;
|
||||
} else if (addr >= 0x0180 && addr < (0x0180 + (FWU_BUFFER_SIZE / 2))) {
|
||||
uint16_t index = (addr - 0x0180) * 2;
|
||||
if (index < sizeof(fwu_buffer)) {
|
||||
sys_put_be16(reg, &fwu_buffer[index]);
|
||||
if (index + 2 >= fwu_chunk_size) {
|
||||
fwu_last_chunk_crc = crc16_ccitt(0xffff, fwu_buffer, fwu_chunk_size);
|
||||
LOG_INF("FWU: Chunk received, CRC is 0x%04X", fwu_last_chunk_crc);
|
||||
}
|
||||
}
|
||||
void fwu_handler(uint16_t addr, uint16_t reg) {
|
||||
// This is a simplified handler. In a real scenario, you would have a proper
|
||||
// mapping between register addresses and actions.
|
||||
if (addr == 0x0100) { // FWU_COMMAND
|
||||
if (reg == 1) {
|
||||
LOG_INF("FWU: Chunk at offset %u (size %u) verified.", fwu_chunk_offset,
|
||||
fwu_chunk_size);
|
||||
} else if (reg == 2) {
|
||||
LOG_INF("FWU: Finalize command received. Rebooting (simulated).");
|
||||
}
|
||||
} else if (addr == 0x0101) { // FWU_CHUNK_OFFSET_LOW
|
||||
fwu_chunk_offset = (fwu_chunk_offset & 0xFFFF0000) | reg;
|
||||
} else if (addr == 0x0102) { // FWU_CHUNK_OFFSET_HIGH
|
||||
fwu_chunk_offset = (fwu_chunk_offset & 0x0000FFFF) | ((uint32_t)reg << 16);
|
||||
} else if (addr == 0x0103) { // FWU_CHUNK_SIZE
|
||||
fwu_chunk_size = (reg > FWU_BUFFER_SIZE) ? FWU_BUFFER_SIZE : reg;
|
||||
} else if (addr >= 0x0180 && addr < (0x0180 + (FWU_BUFFER_SIZE / 2))) {
|
||||
uint16_t index = (addr - 0x0180) * 2;
|
||||
if (index < sizeof(fwu_buffer)) {
|
||||
sys_put_be16(reg, &fwu_buffer[index]);
|
||||
if (index + 2 >= fwu_chunk_size) {
|
||||
fwu_last_chunk_crc = crc16_ccitt(0xffff, fwu_buffer, fwu_chunk_size);
|
||||
LOG_INF("FWU: Chunk received, CRC is 0x%04X", fwu_last_chunk_crc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t fwu_get_last_chunk_crc(void)
|
||||
{
|
||||
return fwu_last_chunk_crc;
|
||||
}
|
||||
uint16_t fwu_get_last_chunk_crc(void) { return fwu_last_chunk_crc; }
|
||||
|
||||
@@ -7,28 +7,27 @@
|
||||
* libraries like valve control, ADC sensors, and firmware updates.
|
||||
*/
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/drivers/uart.h>
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/modbus/modbus.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/settings/settings.h>
|
||||
#include <zephyr/sys/reboot.h>
|
||||
#include <app_version.h>
|
||||
#include <lib/adc_sensor.h>
|
||||
#include <lib/fwu.h>
|
||||
#include <lib/modbus_server.h>
|
||||
#include <lib/valve.h>
|
||||
#include <lib/fwu.h>
|
||||
#include <lib/adc_sensor.h>
|
||||
#include <app_version.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/uart.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/modbus/modbus.h>
|
||||
#include <zephyr/settings/settings.h>
|
||||
#include <zephyr/sys/reboot.h>
|
||||
#include <zephyr/usb/usb_device.h>
|
||||
|
||||
LOG_MODULE_REGISTER(modbus_server, LOG_LEVEL_INF);
|
||||
|
||||
static int modbus_iface;
|
||||
static struct modbus_iface_param server_param = {
|
||||
.mode = MODBUS_MODE_RTU,
|
||||
.server = {.user_cb = NULL, .unit_id = 1},
|
||||
.serial = {.baud = 19200, .parity = UART_CFG_PARITY_NONE},
|
||||
.mode = MODBUS_MODE_RTU,
|
||||
.server = {.user_cb = NULL, .unit_id = 1},
|
||||
.serial = {.baud = 19200, .parity = UART_CFG_PARITY_NONE},
|
||||
};
|
||||
|
||||
static uint16_t watchdog_timeout_s = 0;
|
||||
@@ -43,10 +42,9 @@ static struct k_timer watchdog_timer;
|
||||
*
|
||||
* @param timer_id Pointer to the timer instance.
|
||||
*/
|
||||
static void watchdog_timer_handler(struct k_timer *timer_id)
|
||||
{
|
||||
LOG_WRN("Modbus watchdog expired! Closing valve as a fail-safe.");
|
||||
valve_close();
|
||||
static void watchdog_timer_handler(struct k_timer *timer_id) {
|
||||
LOG_WRN("Modbus watchdog expired! Closing valve as a fail-safe.");
|
||||
valve_close();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,12 +53,10 @@ static void watchdog_timer_handler(struct k_timer *timer_id)
|
||||
* This function should be called upon receiving any valid Modbus request
|
||||
* to prevent the watchdog from expiring.
|
||||
*/
|
||||
static inline void reset_watchdog(void)
|
||||
{
|
||||
if (watchdog_timeout_s > 0)
|
||||
{
|
||||
k_timer_start(&watchdog_timer, K_SECONDS(watchdog_timeout_s), K_NO_WAIT);
|
||||
}
|
||||
static inline void reset_watchdog(void) {
|
||||
if (watchdog_timeout_s > 0) {
|
||||
k_timer_start(&watchdog_timer, K_SECONDS(watchdog_timeout_s), K_NO_WAIT);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,25 +66,23 @@ static inline void reset_watchdog(void)
|
||||
* @param reg Pointer to store the read value.
|
||||
* @return 0 on success.
|
||||
*/
|
||||
static int holding_reg_rd(uint16_t addr, uint16_t *reg)
|
||||
{
|
||||
reset_watchdog();
|
||||
switch (addr)
|
||||
{
|
||||
case REG_HOLDING_MAX_OPENING_TIME_S:
|
||||
*reg = valve_get_max_open_time();
|
||||
break;
|
||||
case REG_HOLDING_MAX_CLOSING_TIME_S:
|
||||
*reg = valve_get_max_close_time();
|
||||
break;
|
||||
case REG_HOLDING_WATCHDOG_TIMEOUT_S:
|
||||
*reg = watchdog_timeout_s;
|
||||
break;
|
||||
default:
|
||||
*reg = 0;
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
static int holding_reg_rd(uint16_t addr, uint16_t *reg) {
|
||||
reset_watchdog();
|
||||
switch (addr) {
|
||||
case REG_HOLDING_MAX_OPENING_TIME_S:
|
||||
*reg = valve_get_max_open_time();
|
||||
break;
|
||||
case REG_HOLDING_MAX_CLOSING_TIME_S:
|
||||
*reg = valve_get_max_close_time();
|
||||
break;
|
||||
case REG_HOLDING_WATCHDOG_TIMEOUT_S:
|
||||
*reg = watchdog_timeout_s;
|
||||
break;
|
||||
default:
|
||||
*reg = 0;
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,56 +92,45 @@ static int holding_reg_rd(uint16_t addr, uint16_t *reg)
|
||||
* @param reg Value to write.
|
||||
* @return 0 on success.
|
||||
*/
|
||||
static int holding_reg_wr(uint16_t addr, uint16_t reg)
|
||||
{
|
||||
reset_watchdog();
|
||||
switch (addr)
|
||||
{
|
||||
case REG_HOLDING_VALVE_COMMAND:
|
||||
if (reg == 1)
|
||||
{
|
||||
valve_open();
|
||||
}
|
||||
else if (reg == 2)
|
||||
{
|
||||
valve_close();
|
||||
}
|
||||
else if (reg == 0)
|
||||
{
|
||||
valve_stop();
|
||||
}
|
||||
break;
|
||||
case REG_HOLDING_MAX_OPENING_TIME_S:
|
||||
valve_set_max_open_time(reg);
|
||||
break;
|
||||
case REG_HOLDING_MAX_CLOSING_TIME_S:
|
||||
valve_set_max_close_time(reg);
|
||||
break;
|
||||
case REG_HOLDING_WATCHDOG_TIMEOUT_S:
|
||||
watchdog_timeout_s = reg;
|
||||
if (watchdog_timeout_s > 0)
|
||||
{
|
||||
LOG_INF("Watchdog enabled with %u s timeout.", watchdog_timeout_s);
|
||||
reset_watchdog();
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_INF("Watchdog disabled.");
|
||||
k_timer_stop(&watchdog_timer);
|
||||
}
|
||||
break;
|
||||
case REG_HOLDING_DEVICE_RESET:
|
||||
if (reg == 1)
|
||||
{
|
||||
LOG_WRN("Modbus reset command received. Rebooting...");
|
||||
sys_reboot(SYS_REBOOT_WARM);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
fwu_handler(addr, reg);
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
static int holding_reg_wr(uint16_t addr, uint16_t reg) {
|
||||
reset_watchdog();
|
||||
switch (addr) {
|
||||
case REG_HOLDING_VALVE_COMMAND:
|
||||
if (reg == 1) {
|
||||
valve_open();
|
||||
} else if (reg == 2) {
|
||||
valve_close();
|
||||
} else if (reg == 0) {
|
||||
valve_stop();
|
||||
}
|
||||
break;
|
||||
case REG_HOLDING_MAX_OPENING_TIME_S:
|
||||
valve_set_max_open_time(reg);
|
||||
break;
|
||||
case REG_HOLDING_MAX_CLOSING_TIME_S:
|
||||
valve_set_max_close_time(reg);
|
||||
break;
|
||||
case REG_HOLDING_WATCHDOG_TIMEOUT_S:
|
||||
watchdog_timeout_s = reg;
|
||||
if (watchdog_timeout_s > 0) {
|
||||
LOG_INF("Watchdog enabled with %u s timeout.", watchdog_timeout_s);
|
||||
reset_watchdog();
|
||||
} else {
|
||||
LOG_INF("Watchdog disabled.");
|
||||
k_timer_stop(&watchdog_timer);
|
||||
}
|
||||
break;
|
||||
case REG_HOLDING_DEVICE_RESET:
|
||||
if (reg == 1) {
|
||||
LOG_WRN("Modbus reset command received. Rebooting...");
|
||||
sys_reboot(SYS_REBOOT_WARM);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
fwu_handler(addr, reg);
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,130 +140,124 @@ static int holding_reg_wr(uint16_t addr, uint16_t reg)
|
||||
* @param reg Pointer to store the read value.
|
||||
* @return 0 on success.
|
||||
*/
|
||||
static int input_reg_rd(uint16_t addr, uint16_t *reg)
|
||||
{
|
||||
reset_watchdog();
|
||||
uint32_t uptime_s = k_uptime_get_32() / 1000;
|
||||
switch (addr)
|
||||
{
|
||||
case REG_INPUT_VALVE_STATE_MOVEMENT:
|
||||
*reg = (valve_get_movement() << 8) | (valve_get_state() & 0xFF);
|
||||
break;
|
||||
case REG_INPUT_MOTOR_CURRENT_MA:
|
||||
*reg = adc_sensor_get_current_ma();
|
||||
break;
|
||||
case REG_INPUT_UPTIME_SECONDS_LOW:
|
||||
*reg = (uint16_t)(uptime_s & 0xFFFF);
|
||||
break;
|
||||
case REG_INPUT_UPTIME_SECONDS_HIGH:
|
||||
*reg = (uint16_t)(uptime_s >> 16);
|
||||
break;
|
||||
case REG_INPUT_SUPPLY_VOLTAGE_MV:
|
||||
*reg = adc_sensor_get_voltage_mv();
|
||||
break;
|
||||
case REG_INPUT_FWU_LAST_CHUNK_CRC:
|
||||
*reg = fwu_get_last_chunk_crc();
|
||||
break;
|
||||
case REG_INPUT_FIRMWARE_VERSION_MAJOR_MINOR:
|
||||
*reg = (APP_VERSION_MAJOR << 8) | APP_VERSION_MINOR;
|
||||
break;
|
||||
case REG_INPUT_FIRMWARE_VERSION_PATCH:
|
||||
*reg = APP_PATCHLEVEL;
|
||||
break;
|
||||
default:
|
||||
*reg = 0;
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
static int input_reg_rd(uint16_t addr, uint16_t *reg) {
|
||||
reset_watchdog();
|
||||
uint32_t uptime_s = k_uptime_get_32() / 1000;
|
||||
switch (addr) {
|
||||
case REG_INPUT_VALVE_STATE_MOVEMENT:
|
||||
*reg = (valve_get_movement() << 8) | (valve_get_state() & 0xFF);
|
||||
break;
|
||||
case REG_INPUT_MOTOR_CURRENT_MA:
|
||||
*reg = adc_sensor_get_current_ma();
|
||||
break;
|
||||
case REG_INPUT_UPTIME_SECONDS_LOW:
|
||||
*reg = (uint16_t)(uptime_s & 0xFFFF);
|
||||
break;
|
||||
case REG_INPUT_UPTIME_SECONDS_HIGH:
|
||||
*reg = (uint16_t)(uptime_s >> 16);
|
||||
break;
|
||||
case REG_INPUT_SUPPLY_VOLTAGE_MV:
|
||||
*reg = adc_sensor_get_voltage_mv();
|
||||
break;
|
||||
case REG_INPUT_FWU_LAST_CHUNK_CRC:
|
||||
*reg = fwu_get_last_chunk_crc();
|
||||
break;
|
||||
case REG_INPUT_FIRMWARE_VERSION_MAJOR_MINOR:
|
||||
*reg = (APP_VERSION_MAJOR << 8) | APP_VERSION_MINOR;
|
||||
break;
|
||||
case REG_INPUT_FIRMWARE_VERSION_PATCH:
|
||||
*reg = APP_PATCHLEVEL;
|
||||
break;
|
||||
default:
|
||||
*reg = 0;
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct modbus_user_callbacks mbs_cbs = { // Modbus server callback functions
|
||||
.holding_reg_rd = holding_reg_rd,
|
||||
.holding_reg_wr = holding_reg_wr,
|
||||
.input_reg_rd = input_reg_rd,
|
||||
static struct modbus_user_callbacks mbs_cbs = {
|
||||
// Modbus server callback functions
|
||||
.holding_reg_rd = holding_reg_rd,
|
||||
.holding_reg_wr = holding_reg_wr,
|
||||
.input_reg_rd = input_reg_rd,
|
||||
};
|
||||
|
||||
#define MODBUS_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(zephyr_modbus_serial)
|
||||
|
||||
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;
|
||||
settings_load_one("modbus/baudrate", &saved_baudrate, sizeof(saved_baudrate));
|
||||
settings_load_one("modbus/unit_id", &saved_unit_id, sizeof(saved_unit_id));
|
||||
|
||||
// Apply loaded settings
|
||||
server_param.serial.baud = saved_baudrate;
|
||||
server_param.server.unit_id = saved_unit_id;
|
||||
|
||||
const char iface_name[] = {DEVICE_DT_NAME(MODBUS_NODE)};
|
||||
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;
|
||||
settings_load_one("modbus/baudrate", &saved_baudrate, sizeof(saved_baudrate));
|
||||
settings_load_one("modbus/unit_id", &saved_unit_id, sizeof(saved_unit_id));
|
||||
|
||||
// Apply loaded settings
|
||||
server_param.serial.baud = saved_baudrate;
|
||||
server_param.server.unit_id = saved_unit_id;
|
||||
|
||||
const char iface_name[] = {DEVICE_DT_NAME(MODBUS_NODE)};
|
||||
#if DT_NODE_HAS_COMPAT(DT_PARENT(MODBUS_NODE), zephyr_cdc_acm_uart)
|
||||
const struct device *const dev = DEVICE_DT_GET(DT_PARENT(MODBUS_NODE));
|
||||
uint32_t dtr = 0;
|
||||
const struct device *const dev = DEVICE_DT_GET(DT_PARENT(MODBUS_NODE));
|
||||
uint32_t dtr = 0;
|
||||
|
||||
if (!device_is_ready(dev) || usb_enable(NULL))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (!device_is_ready(dev) || usb_enable(NULL)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (!dtr)
|
||||
{
|
||||
uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr);
|
||||
k_sleep(K_MSEC(100));
|
||||
}
|
||||
while (!dtr) {
|
||||
uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr);
|
||||
k_sleep(K_MSEC(100));
|
||||
}
|
||||
|
||||
LOG_INF("Client connected to server on %s", dev->name);
|
||||
LOG_INF("Client connected to server on %s", dev->name);
|
||||
#endif
|
||||
modbus_iface = modbus_iface_get_by_name(iface_name);
|
||||
if (modbus_iface < 0)
|
||||
{
|
||||
return modbus_iface;
|
||||
}
|
||||
server_param.server.user_cb = &mbs_cbs;
|
||||
|
||||
LOG_INF("Starting Modbus server: baudrate=%u, unit_id=%u", saved_baudrate, saved_unit_id);
|
||||
return modbus_init_server(modbus_iface, server_param);
|
||||
modbus_iface = modbus_iface_get_by_name(iface_name);
|
||||
if (modbus_iface < 0) {
|
||||
return modbus_iface;
|
||||
}
|
||||
server_param.server.user_cb = &mbs_cbs;
|
||||
|
||||
LOG_INF("Starting Modbus server: baudrate=%u, unit_id=%u", saved_baudrate,
|
||||
saved_unit_id);
|
||||
return modbus_init_server(modbus_iface, server_param);
|
||||
}
|
||||
|
||||
int modbus_reconfigure(uint32_t baudrate, uint8_t unit_id)
|
||||
{
|
||||
// Update parameters
|
||||
server_param.serial.baud = baudrate;
|
||||
server_param.server.unit_id = unit_id;
|
||||
int modbus_reconfigure(uint32_t baudrate, uint8_t unit_id) {
|
||||
// Update parameters
|
||||
server_param.serial.baud = baudrate;
|
||||
server_param.server.unit_id = unit_id;
|
||||
|
||||
// Try to reinitialize - this should work for most cases
|
||||
int ret = modbus_init_server(modbus_iface, server_param);
|
||||
// Try to reinitialize - this should work for most cases
|
||||
int ret = modbus_init_server(modbus_iface, server_param);
|
||||
|
||||
if (ret == 0)
|
||||
{
|
||||
settings_save_one("modbus/baudrate", &baudrate, sizeof(baudrate));
|
||||
settings_save_one("modbus/unit_id", &unit_id, sizeof(unit_id));
|
||||
LOG_INF("Modbus reconfigured: baudrate=%u, unit_id=%u", baudrate, unit_id);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERR("Failed to reconfigure Modbus: %d", ret);
|
||||
LOG_INF("Modbus reconfiguration requires restart to take effect");
|
||||
|
||||
// Save settings for next boot
|
||||
settings_save_one("modbus/baudrate", &baudrate, sizeof(baudrate));
|
||||
settings_save_one("modbus/unit_id", &unit_id, sizeof(unit_id));
|
||||
|
||||
LOG_INF("Settings saved. Type 'reset' to restart the device and apply the change.");
|
||||
return 0; // Return success since settings are saved
|
||||
}
|
||||
if (ret == 0) {
|
||||
settings_save_one("modbus/baudrate", &baudrate, sizeof(baudrate));
|
||||
settings_save_one("modbus/unit_id", &unit_id, sizeof(unit_id));
|
||||
LOG_INF("Modbus reconfigured: baudrate=%u, unit_id=%u", baudrate, unit_id);
|
||||
} else {
|
||||
LOG_ERR("Failed to reconfigure Modbus: %d", ret);
|
||||
LOG_INF("Modbus reconfiguration requires restart to take effect");
|
||||
|
||||
return ret;
|
||||
// Save settings for next boot
|
||||
settings_save_one("modbus/baudrate", &baudrate, sizeof(baudrate));
|
||||
settings_save_one("modbus/unit_id", &unit_id, sizeof(unit_id));
|
||||
|
||||
LOG_INF(
|
||||
"Settings saved. Type 'reset' to restart the device and apply the "
|
||||
"change.");
|
||||
return 0; // Return success since settings are saved
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint32_t modbus_get_baudrate(void) { return server_param.serial.baud; }
|
||||
|
||||
@@ -8,10 +8,10 @@
|
||||
* storage.
|
||||
*/
|
||||
|
||||
#include <zephyr/shell/shell.h>
|
||||
#include <stdlib.h>
|
||||
#include <lib/modbus_server.h>
|
||||
#include <lib/valve.h>
|
||||
#include <stdlib.h>
|
||||
#include <zephyr/shell/shell.h>
|
||||
|
||||
/**
|
||||
* @brief Shell command to set the Modbus baudrate.
|
||||
@@ -21,41 +21,44 @@
|
||||
* @param argv Argument values.
|
||||
* @return 0 on success, -EINVAL on error.
|
||||
*/
|
||||
static int cmd_modbus_set_baud(const struct shell *sh, size_t argc, char **argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
shell_error(sh, "Usage: set_baud <baudrate>");
|
||||
return -EINVAL;
|
||||
}
|
||||
static int cmd_modbus_set_baud(const struct shell *sh, size_t argc,
|
||||
char **argv) {
|
||||
if (argc != 2) {
|
||||
shell_error(sh, "Usage: set_baud <baudrate>");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
uint32_t new_baud = (uint32_t)strtoul(argv[1], NULL, 10);
|
||||
const uint32_t valid_baud_rates[] = {1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
|
||||
bool is_valid = false;
|
||||
uint32_t new_baud = (uint32_t)strtoul(argv[1], NULL, 10);
|
||||
const uint32_t valid_baud_rates[] = {1200, 2400, 4800, 9600,
|
||||
19200, 38400, 57600, 115200};
|
||||
bool is_valid = false;
|
||||
|
||||
for (int i = 0; i < ARRAY_SIZE(valid_baud_rates); i++) {
|
||||
if (new_baud == valid_baud_rates[i]) {
|
||||
is_valid = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < ARRAY_SIZE(valid_baud_rates); i++) {
|
||||
if (new_baud == valid_baud_rates[i]) {
|
||||
is_valid = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_valid) {
|
||||
char error_msg[128];
|
||||
int offset = snprintf(error_msg, sizeof(error_msg), "Invalid baudrate. Valid rates are: ");
|
||||
for (int i = 0; i < ARRAY_SIZE(valid_baud_rates); i++) {
|
||||
offset += snprintf(error_msg + offset, sizeof(error_msg) - offset, "%u ", valid_baud_rates[i]);
|
||||
}
|
||||
shell_error(sh, "%s", error_msg);
|
||||
return -EINVAL;
|
||||
}
|
||||
if (!is_valid) {
|
||||
char error_msg[128];
|
||||
int offset = snprintf(error_msg, sizeof(error_msg),
|
||||
"Invalid baudrate. Valid rates are: ");
|
||||
for (int i = 0; i < ARRAY_SIZE(valid_baud_rates); i++) {
|
||||
offset += snprintf(error_msg + offset, sizeof(error_msg) - offset, "%u ",
|
||||
valid_baud_rates[i]);
|
||||
}
|
||||
shell_error(sh, "%s", error_msg);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (modbus_reconfigure(new_baud, modbus_get_unit_id()) != 0) {
|
||||
shell_error(sh, "Failed to apply new baudrate");
|
||||
} else {
|
||||
shell_print(sh, "Modbus baudrate set to: %u (and saved)", new_baud);
|
||||
}
|
||||
if (modbus_reconfigure(new_baud, modbus_get_unit_id()) != 0) {
|
||||
shell_error(sh, "Failed to apply new baudrate");
|
||||
} else {
|
||||
shell_print(sh, "Modbus baudrate set to: %u (and saved)", new_baud);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,27 +69,27 @@ static int cmd_modbus_set_baud(const struct shell *sh, size_t argc, char **argv)
|
||||
* @param argv Argument values.
|
||||
* @return 0 on success, -EINVAL on error.
|
||||
*/
|
||||
static int cmd_modbus_set_id(const struct shell *sh, size_t argc, char **argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
shell_error(sh, "Usage: set_id <slave_id>");
|
||||
return -EINVAL;
|
||||
}
|
||||
static int cmd_modbus_set_id(const struct shell *sh, size_t argc, char **argv) {
|
||||
if (argc != 2) {
|
||||
shell_error(sh, "Usage: set_id <slave_id>");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
uint32_t new_id_u32 = (uint32_t)strtoul(argv[1], NULL, 10);
|
||||
if (new_id_u32 == 0 || new_id_u32 > 247) {
|
||||
shell_error(sh, "Invalid slave ID: %s. Must be between 1 and 247.", argv[1]);
|
||||
return -EINVAL;
|
||||
}
|
||||
uint8_t new_id = (uint8_t)new_id_u32;
|
||||
uint32_t new_id_u32 = (uint32_t)strtoul(argv[1], NULL, 10);
|
||||
if (new_id_u32 == 0 || new_id_u32 > 247) {
|
||||
shell_error(sh, "Invalid slave ID: %s. Must be between 1 and 247.",
|
||||
argv[1]);
|
||||
return -EINVAL;
|
||||
}
|
||||
uint8_t new_id = (uint8_t)new_id_u32;
|
||||
|
||||
if (modbus_reconfigure(modbus_get_baudrate(), new_id) != 0) {
|
||||
shell_error(sh, "Failed to apply new slave ID");
|
||||
} else {
|
||||
shell_print(sh, "Modbus slave ID set to: %u (and saved)", new_id);
|
||||
}
|
||||
if (modbus_reconfigure(modbus_get_baudrate(), new_id) != 0) {
|
||||
shell_error(sh, "Failed to apply new slave ID");
|
||||
} else {
|
||||
shell_print(sh, "Modbus slave ID set to: %u (and saved)", new_id);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,18 +100,18 @@ static int cmd_modbus_set_id(const struct shell *sh, size_t argc, char **argv)
|
||||
* @param argv Argument values.
|
||||
* @return 0 on success, -EINVAL on error.
|
||||
*/
|
||||
static int cmd_valve_set_open_time(const struct shell *sh, size_t argc, char **argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
shell_error(sh, "Usage: set_open_time <seconds>");
|
||||
return -EINVAL;
|
||||
}
|
||||
static int cmd_valve_set_open_time(const struct shell *sh, size_t argc,
|
||||
char **argv) {
|
||||
if (argc != 2) {
|
||||
shell_error(sh, "Usage: set_open_time <seconds>");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
uint16_t seconds = (uint16_t)strtoul(argv[1], NULL, 10);
|
||||
valve_set_max_open_time(seconds);
|
||||
shell_print(sh, "Max opening time set to: %u seconds (and saved)", seconds);
|
||||
uint16_t seconds = (uint16_t)strtoul(argv[1], NULL, 10);
|
||||
valve_set_max_open_time(seconds);
|
||||
shell_print(sh, "Max opening time set to: %u seconds (and saved)", seconds);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,18 +122,18 @@ static int cmd_valve_set_open_time(const struct shell *sh, size_t argc, char **a
|
||||
* @param argv Argument values.
|
||||
* @return 0 on success, -EINVAL on error.
|
||||
*/
|
||||
static int cmd_valve_set_close_time(const struct shell *sh, size_t argc, char **argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
shell_error(sh, "Usage: set_close_time <seconds>");
|
||||
return -EINVAL;
|
||||
}
|
||||
static int cmd_valve_set_close_time(const struct shell *sh, size_t argc,
|
||||
char **argv) {
|
||||
if (argc != 2) {
|
||||
shell_error(sh, "Usage: set_close_time <seconds>");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
uint16_t seconds = (uint16_t)strtoul(argv[1], NULL, 10);
|
||||
valve_set_max_close_time(seconds);
|
||||
shell_print(sh, "Max closing time set to: %u seconds (and saved)", seconds);
|
||||
uint16_t seconds = (uint16_t)strtoul(argv[1], NULL, 10);
|
||||
valve_set_max_close_time(seconds);
|
||||
shell_print(sh, "Max closing time set to: %u seconds (and saved)", seconds);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -141,29 +144,33 @@ static int cmd_valve_set_close_time(const struct shell *sh, size_t argc, char **
|
||||
* @param argv Argument values.
|
||||
* @return 0 on success.
|
||||
*/
|
||||
static int cmd_config_show(const struct shell *sh, size_t argc, char **argv)
|
||||
{
|
||||
shell_print(sh, "Current Modbus Configuration:");
|
||||
shell_print(sh, " Baudrate: %u", modbus_get_baudrate());
|
||||
shell_print(sh, " Slave ID: %u", modbus_get_unit_id());
|
||||
shell_print(sh, "Current Valve Configuration:");
|
||||
shell_print(sh, " Max Opening Time: %u s", valve_get_max_open_time());
|
||||
shell_print(sh, " Max Closing Time: %u s", valve_get_max_close_time());
|
||||
return 0;
|
||||
static int cmd_config_show(const struct shell *sh, size_t argc, char **argv) {
|
||||
shell_print(sh, "Current Modbus Configuration:");
|
||||
shell_print(sh, " Baudrate: %u", modbus_get_baudrate());
|
||||
shell_print(sh, " Slave ID: %u", modbus_get_unit_id());
|
||||
shell_print(sh, "Current Valve Configuration:");
|
||||
shell_print(sh, " Max Opening Time: %u s", valve_get_max_open_time());
|
||||
shell_print(sh, " Max Closing Time: %u s", valve_get_max_close_time());
|
||||
return 0;
|
||||
}
|
||||
|
||||
SHELL_STATIC_SUBCMD_SET_CREATE(sub_modbus_cmds,
|
||||
SHELL_CMD(set_baud, NULL, "Set Modbus baudrate", cmd_modbus_set_baud),
|
||||
SHELL_CMD(set_id, NULL, "Set Modbus slave ID", cmd_modbus_set_id),
|
||||
SHELL_SUBCMD_SET_END
|
||||
);
|
||||
SHELL_CMD(set_baud, NULL, "Set Modbus baudrate",
|
||||
cmd_modbus_set_baud),
|
||||
SHELL_CMD(set_id, NULL, "Set Modbus slave ID",
|
||||
cmd_modbus_set_id),
|
||||
SHELL_SUBCMD_SET_END);
|
||||
|
||||
SHELL_STATIC_SUBCMD_SET_CREATE(sub_valve_cmds,
|
||||
SHELL_CMD(set_open_time, NULL, "Set max valve opening time", cmd_valve_set_open_time),
|
||||
SHELL_CMD(set_close_time, NULL, "Set max valve closing time", cmd_valve_set_close_time),
|
||||
SHELL_SUBCMD_SET_END
|
||||
);
|
||||
SHELL_CMD(set_open_time, NULL,
|
||||
"Set max valve opening time",
|
||||
cmd_valve_set_open_time),
|
||||
SHELL_CMD(set_close_time, NULL,
|
||||
"Set max valve closing time",
|
||||
cmd_valve_set_close_time),
|
||||
SHELL_SUBCMD_SET_END);
|
||||
|
||||
SHELL_CMD_REGISTER(modbus, &sub_modbus_cmds, "Modbus configuration", NULL);
|
||||
SHELL_CMD_REGISTER(valve, &sub_valve_cmds, "Valve configuration", NULL);
|
||||
SHELL_CMD_REGISTER(show_config, NULL, "Show all configurations", cmd_config_show);
|
||||
SHELL_CMD_REGISTER(show_config, NULL, "Show all configurations",
|
||||
cmd_config_show);
|
||||
@@ -20,12 +20,11 @@
|
||||
* @param argv Argument values.
|
||||
* @return 0 on success.
|
||||
*/
|
||||
static int cmd_reset(const struct shell *sh, size_t argc, char **argv)
|
||||
{
|
||||
shell_print(sh, "Rebooting system...");
|
||||
k_sleep(K_MSEC(100)); // Allow the shell to print the message
|
||||
sys_reboot(SYS_REBOOT_WARM);
|
||||
return 0;
|
||||
static int cmd_reset(const struct shell *sh, size_t argc, char **argv) {
|
||||
shell_print(sh, "Rebooting system...");
|
||||
k_sleep(K_MSEC(100)); // Allow the shell to print the message
|
||||
sys_reboot(SYS_REBOOT_WARM);
|
||||
return 0;
|
||||
}
|
||||
|
||||
SHELL_CMD_REGISTER(reset, NULL, "Reboot the system", cmd_reset);
|
||||
|
||||
@@ -7,12 +7,12 @@
|
||||
* safety timeouts for opening and closing operations.
|
||||
*/
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/settings/settings.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <lib/valve.h>
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <lib/valve.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/settings/settings.h>
|
||||
|
||||
LOG_MODULE_REGISTER(valve, LOG_LEVEL_INF);
|
||||
|
||||
@@ -29,81 +29,93 @@ static enum valve_state current_state = VALVE_STATE_CLOSED;
|
||||
static enum valve_movement current_movement = VALVE_MOVEMENT_IDLE;
|
||||
static uint16_t max_opening_time_s = 60;
|
||||
static uint16_t max_closing_time_s = 60;
|
||||
static struct k_work_delayable valve_work; // Work item for scheduling valve movement timeouts
|
||||
static struct k_work_delayable
|
||||
valve_work; // Work item for scheduling valve movement timeouts
|
||||
|
||||
/**
|
||||
* @brief Work handler for valve movement timeouts.
|
||||
*
|
||||
* This function is executed when the valve's movement timer expires.
|
||||
* It stops the motor to prevent damage and updates the valve's state.
|
||||
*
|
||||
*
|
||||
* @param work Pointer to the k_work item.
|
||||
*/
|
||||
static void valve_work_handler(struct k_work *work)
|
||||
{
|
||||
gpio_pin_set_dt(&valve_gpios.in0, 0);
|
||||
static void valve_work_handler(struct k_work *work) {
|
||||
gpio_pin_set_dt(&valve_gpios.in0, 0);
|
||||
gpio_pin_set_dt(&valve_gpios.in1, 0);
|
||||
gpio_pin_set_dt(&valve_gpios.rst, 0);
|
||||
|
||||
if (current_movement == VALVE_MOVEMENT_OPENING) {
|
||||
LOG_INF("Valve finished opening");
|
||||
} else if (current_movement == VALVE_MOVEMENT_CLOSING) {
|
||||
current_state = VALVE_STATE_CLOSED;
|
||||
LOG_INF("Valve finished closing");
|
||||
}
|
||||
current_movement = VALVE_MOVEMENT_IDLE;
|
||||
}
|
||||
|
||||
void valve_init(void) {
|
||||
k_work_init_delayable(&valve_work, valve_work_handler);
|
||||
settings_load_one("valve/max_open_time", &max_opening_time_s,
|
||||
sizeof(max_opening_time_s));
|
||||
settings_load_one("valve/max_close_time", &max_closing_time_s,
|
||||
sizeof(max_closing_time_s));
|
||||
|
||||
gpio_pin_configure_dt(&valve_gpios.in0, GPIO_OUTPUT_INACTIVE);
|
||||
gpio_pin_configure_dt(&valve_gpios.in1, GPIO_OUTPUT_INACTIVE);
|
||||
gpio_pin_configure_dt(&valve_gpios.rst,
|
||||
GPIO_OUTPUT_ACTIVE); // Keep VND7050AJ out of reset
|
||||
gpio_pin_configure_dt(&valve_gpios.sen, GPIO_OUTPUT_INACTIVE);
|
||||
gpio_pin_configure_dt(&valve_gpios.s0,
|
||||
GPIO_OUTPUT_INACTIVE); // S0 select pin - output
|
||||
gpio_pin_configure_dt(&valve_gpios.s1,
|
||||
GPIO_OUTPUT_INACTIVE); // S1 select pin - output
|
||||
|
||||
LOG_INF("Valve initialized: max_open=%us, max_close=%us", max_opening_time_s,
|
||||
max_closing_time_s);
|
||||
}
|
||||
|
||||
void valve_open(void) {
|
||||
if (current_state == VALVE_STATE_CLOSED) {
|
||||
gpio_pin_set_dt(&valve_gpios.rst, 1);
|
||||
gpio_pin_set_dt(&valve_gpios.in1, 0);
|
||||
gpio_pin_set_dt(&valve_gpios.rst, 0);
|
||||
|
||||
if (current_movement == VALVE_MOVEMENT_OPENING) {
|
||||
LOG_INF("Valve finished opening");
|
||||
} else if (current_movement == VALVE_MOVEMENT_CLOSING) {
|
||||
current_state = VALVE_STATE_CLOSED;
|
||||
LOG_INF("Valve finished closing");
|
||||
}
|
||||
current_movement = VALVE_MOVEMENT_IDLE;
|
||||
gpio_pin_set_dt(&valve_gpios.in0, 1);
|
||||
current_state = VALVE_STATE_OPEN;
|
||||
current_movement = VALVE_MOVEMENT_OPENING;
|
||||
k_work_schedule(&valve_work, K_MSEC(max_opening_time_s * 1000 * 0.9));
|
||||
}
|
||||
}
|
||||
|
||||
void valve_init(void)
|
||||
{
|
||||
k_work_init_delayable(&valve_work, valve_work_handler);
|
||||
settings_load_one("valve/max_open_time", &max_opening_time_s, sizeof(max_opening_time_s));
|
||||
settings_load_one("valve/max_close_time", &max_closing_time_s, sizeof(max_closing_time_s));
|
||||
|
||||
gpio_pin_configure_dt(&valve_gpios.in0, GPIO_OUTPUT_INACTIVE);
|
||||
gpio_pin_configure_dt(&valve_gpios.in1, GPIO_OUTPUT_INACTIVE);
|
||||
gpio_pin_configure_dt(&valve_gpios.rst, GPIO_OUTPUT_ACTIVE); // Keep VND7050AJ out of reset
|
||||
gpio_pin_configure_dt(&valve_gpios.sen, GPIO_OUTPUT_INACTIVE);
|
||||
gpio_pin_configure_dt(&valve_gpios.s0, GPIO_OUTPUT_INACTIVE); // S0 select pin - output
|
||||
gpio_pin_configure_dt(&valve_gpios.s1, GPIO_OUTPUT_INACTIVE); // S1 select pin - output
|
||||
|
||||
LOG_INF("Valve initialized: max_open=%us, max_close=%us", max_opening_time_s, max_closing_time_s);
|
||||
void valve_close(void) {
|
||||
if (current_state == VALVE_STATE_OPEN) {
|
||||
gpio_pin_set_dt(&valve_gpios.rst, 1);
|
||||
gpio_pin_set_dt(&valve_gpios.in0, 0);
|
||||
gpio_pin_set_dt(&valve_gpios.in1, 1);
|
||||
current_movement = VALVE_MOVEMENT_CLOSING;
|
||||
k_work_schedule(&valve_work, K_MSEC(max_closing_time_s * 1000 * 0.9));
|
||||
}
|
||||
}
|
||||
|
||||
void valve_open(void)
|
||||
{
|
||||
if (current_state == VALVE_STATE_CLOSED) {
|
||||
gpio_pin_set_dt(&valve_gpios.rst, 1);
|
||||
gpio_pin_set_dt(&valve_gpios.in1, 0);
|
||||
gpio_pin_set_dt(&valve_gpios.in0, 1);
|
||||
current_state = VALVE_STATE_OPEN;
|
||||
current_movement = VALVE_MOVEMENT_OPENING;
|
||||
k_work_schedule(&valve_work, K_MSEC(max_opening_time_s * 1000 * 0.9));
|
||||
}
|
||||
}
|
||||
|
||||
void valve_close(void)
|
||||
{
|
||||
if (current_state == VALVE_STATE_OPEN) {
|
||||
gpio_pin_set_dt(&valve_gpios.rst, 1);
|
||||
gpio_pin_set_dt(&valve_gpios.in0, 0);
|
||||
gpio_pin_set_dt(&valve_gpios.in1, 1);
|
||||
current_movement = VALVE_MOVEMENT_CLOSING;
|
||||
k_work_schedule(&valve_work, K_MSEC(max_closing_time_s * 1000 * 0.9));
|
||||
}
|
||||
}
|
||||
|
||||
void valve_stop(void)
|
||||
{
|
||||
k_work_cancel_delayable(&valve_work);
|
||||
current_movement = VALVE_MOVEMENT_IDLE;
|
||||
void valve_stop(void) {
|
||||
k_work_cancel_delayable(&valve_work);
|
||||
current_movement = VALVE_MOVEMENT_IDLE;
|
||||
}
|
||||
|
||||
enum valve_state valve_get_state(void) { return current_state; }
|
||||
enum valve_movement valve_get_movement(void) { return current_movement; }
|
||||
uint16_t valve_get_motor_current(void) { return (current_movement != VALVE_MOVEMENT_IDLE) ? 150 : 10; }
|
||||
uint16_t valve_get_motor_current(void) {
|
||||
return (current_movement != VALVE_MOVEMENT_IDLE) ? 150 : 10;
|
||||
}
|
||||
|
||||
void valve_set_max_open_time(uint16_t seconds) { max_opening_time_s = seconds; settings_save_one("valve/max_open_time", &max_opening_time_s, sizeof(max_opening_time_s)); }
|
||||
void valve_set_max_close_time(uint16_t seconds) { max_closing_time_s = seconds; settings_save_one("valve/max_close_time", &max_closing_time_s, sizeof(max_closing_time_s)); }
|
||||
void valve_set_max_open_time(uint16_t seconds) {
|
||||
max_opening_time_s = seconds;
|
||||
settings_save_one("valve/max_open_time", &max_opening_time_s,
|
||||
sizeof(max_opening_time_s));
|
||||
}
|
||||
void valve_set_max_close_time(uint16_t seconds) {
|
||||
max_closing_time_s = seconds;
|
||||
settings_save_one("valve/max_close_time", &max_closing_time_s,
|
||||
sizeof(max_closing_time_s));
|
||||
}
|
||||
uint16_t valve_get_max_open_time(void) { return max_opening_time_s; }
|
||||
uint16_t valve_get_max_close_time(void) { return max_closing_time_s; }
|
||||
|
||||
Reference in New Issue
Block a user