feat(slave_node): Implement full Modbus register map

- Implement all remaining Modbus registers as defined in the documentation v1.0.
- Add support for digital I/O, system status, and a simulated watchdog.
- Implement a placeholder for the firmware update mechanism, including CRC calculation for received data chunks.
- Remove the input simulation timer; digital inputs are now static and ready for real hardware.
This commit is contained in:
Eduard Iten 2025-07-01 21:36:10 +02:00
parent 6dcb11ae0c
commit 21797d8507
1 changed files with 113 additions and 179 deletions

View File

@ -11,6 +11,8 @@
#include <zephyr/modbus/modbus.h> #include <zephyr/modbus/modbus.h>
#include <zephyr/usb/usb_device.h> #include <zephyr/usb/usb_device.h>
#include <zephyr/settings/settings.h> #include <zephyr/settings/settings.h>
#include <zephyr/sys/crc.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/logging/log.h> #include <zephyr/logging/log.h>
#include "modbus_bridge.h" #include "modbus_bridge.h"
@ -21,54 +23,72 @@ LOG_MODULE_REGISTER(mbs_sample, LOG_LEVEL_INF);
#define APP_VERSION_MINOR 0 #define APP_VERSION_MINOR 0
#define APP_VERSION_PATCH 0 #define APP_VERSION_PATCH 0
/* Register Definitions from Documentation */
enum { enum {
/* Valve Control & Status */
REG_INPUT_VALVE_STATE_MOVEMENT = 0x0000, REG_INPUT_VALVE_STATE_MOVEMENT = 0x0000,
REG_INPUT_MOTOR_CURRENT_MA = 0x0001, REG_INPUT_MOTOR_CURRENT_MA = 0x0001,
/* Digital Inputs */
REG_INPUT_DIGITAL_INPUTS_STATE = 0x0020,
REG_INPUT_BUTTON_EVENTS = 0x0021,
/* System Config & Status */
REG_INPUT_FIRMWARE_VERSION_MAJOR_MINOR = 0x00F0, REG_INPUT_FIRMWARE_VERSION_MAJOR_MINOR = 0x00F0,
REG_INPUT_FIRMWARE_VERSION_PATCH = 0x00F1, REG_INPUT_FIRMWARE_VERSION_PATCH = 0x00F1,
REG_INPUT_DEVICE_STATUS = 0x00F2, REG_INPUT_DEVICE_STATUS = 0x00F2,
REG_INPUT_UPTIME_SECONDS_LOW = 0x00F3, REG_INPUT_UPTIME_SECONDS_LOW = 0x00F3,
REG_INPUT_UPTIME_SECONDS_HIGH = 0x00F4, REG_INPUT_UPTIME_SECONDS_HIGH = 0x00F4,
/* Firmware Update */
REG_INPUT_FWU_LAST_CHUNK_CRC = 0x0100,
}; };
enum { enum {
/* Valve Control */
REG_HOLDING_VALVE_COMMAND = 0x0000, REG_HOLDING_VALVE_COMMAND = 0x0000,
REG_HOLDING_MAX_OPENING_TIME_S = 0x0001, REG_HOLDING_MAX_OPENING_TIME_S = 0x0001,
REG_HOLDING_MAX_CLOSING_TIME_S = 0x0002, REG_HOLDING_MAX_CLOSING_TIME_S = 0x0002,
/* Digital Outputs */
REG_HOLDING_DIGITAL_OUTPUTS_STATE = 0x0010,
/* System Config */
REG_HOLDING_WATCHDOG_TIMEOUT_S = 0x00F0, REG_HOLDING_WATCHDOG_TIMEOUT_S = 0x00F0,
/* Firmware Update */
REG_HOLDING_FWU_COMMAND = 0x0100,
REG_HOLDING_FWU_CHUNK_OFFSET_LOW = 0x0101,
REG_HOLDING_FWU_CHUNK_OFFSET_HIGH = 0x0102,
REG_HOLDING_FWU_CHUNK_SIZE = 0x0103,
REG_HOLDING_FWU_DATA_BUFFER = 0x0180,
}; };
enum valve_state { /* Valve Simulation */
VALVE_STATE_CLOSED, enum valve_state { VALVE_STATE_CLOSED, VALVE_STATE_OPEN };
VALVE_STATE_OPEN, enum valve_movement { VALVE_MOVEMENT_IDLE, VALVE_MOVEMENT_OPENING, VALVE_MOVEMENT_CLOSING, VALVE_MOVEMENT_ERROR };
};
enum valve_movement {
VALVE_MOVEMENT_IDLE,
VALVE_MOVEMENT_OPENING,
VALVE_MOVEMENT_CLOSING,
VALVE_MOVEMENT_ERROR,
};
static enum valve_state current_state = VALVE_STATE_CLOSED; static enum valve_state current_state = VALVE_STATE_CLOSED;
static enum valve_movement current_movement = VALVE_MOVEMENT_IDLE; static enum valve_movement current_movement = VALVE_MOVEMENT_IDLE;
static uint16_t max_opening_time_s = 60; static uint16_t max_opening_time_s = 60;
static uint16_t max_closing_time_s = 60; static uint16_t max_closing_time_s = 60;
static uint16_t watchdog_timeout_s;
static int modbus_iface;
static struct k_work_delayable valve_work; static struct k_work_delayable valve_work;
/* Digital I/O State */
static uint16_t digital_outputs_state = 0;
static uint16_t digital_inputs_state = 0; // Will be controlled by real hardware
static uint16_t button_events = 0; // Clear-on-read
/* System State */
static uint16_t device_status = 0; // 0 = OK
static uint16_t watchdog_timeout_s = 0;
/* Firmware Update State */
#define FWU_BUFFER_SIZE 256
static uint8_t fwu_buffer[FWU_BUFFER_SIZE];
static uint32_t fwu_chunk_offset = 0;
static uint16_t fwu_chunk_size = 0;
static uint16_t fwu_last_chunk_crc = 0;
/* Modbus Configuration */
static int modbus_iface;
static struct modbus_iface_param server_param = { static struct modbus_iface_param server_param = {
.mode = MODBUS_MODE_RTU, .mode = MODBUS_MODE_RTU,
.server = { .server = { .user_cb = NULL, .unit_id = 1 },
.user_cb = NULL, // Will be set later .serial = { .baud = 19200, .parity = UART_CFG_PARITY_NONE },
.unit_id = 1,
},
.serial = {
.baud = 19200,
.parity = UART_CFG_PARITY_NONE,
},
}; };
static void valve_work_handler(struct k_work *work) static void valve_work_handler(struct k_work *work)
@ -82,38 +102,15 @@ static void valve_work_handler(struct k_work *work)
current_movement = VALVE_MOVEMENT_IDLE; current_movement = VALVE_MOVEMENT_IDLE;
} }
static int coil_rd(uint16_t addr, bool *state)
{
*state = true;
LOG_INF("Coil read, addr %u, %d", addr, (int)*state);
return 0;
}
static int coil_wr(uint16_t addr, bool state)
{
LOG_INF("Coil write, addr %u, %d", addr, (int)state);
return 0;
}
static int holding_reg_rd(uint16_t addr, uint16_t *reg) static int holding_reg_rd(uint16_t addr, uint16_t *reg)
{ {
switch (addr) { switch (addr) {
case REG_HOLDING_MAX_OPENING_TIME_S: case REG_HOLDING_MAX_OPENING_TIME_S: *reg = max_opening_time_s; break;
*reg = max_opening_time_s; case REG_HOLDING_MAX_CLOSING_TIME_S: *reg = max_closing_time_s; break;
break; case REG_HOLDING_DIGITAL_OUTPUTS_STATE: *reg = digital_outputs_state; break;
case REG_HOLDING_MAX_CLOSING_TIME_S: case REG_HOLDING_WATCHDOG_TIMEOUT_S: *reg = watchdog_timeout_s; break;
*reg = max_closing_time_s; default: *reg = 0; break;
break;
case REG_HOLDING_WATCHDOG_TIMEOUT_S:
*reg = watchdog_timeout_s;
break;
default:
*reg = 0;
break;
} }
LOG_INF("Holding register read, addr %u, value %u", addr, *reg);
return 0; return 0;
} }
@ -125,19 +122,16 @@ static int holding_reg_wr(uint16_t addr, uint16_t reg)
if (current_state == VALVE_STATE_CLOSED) { if (current_state == VALVE_STATE_CLOSED) {
current_state = VALVE_STATE_OPEN; current_state = VALVE_STATE_OPEN;
current_movement = VALVE_MOVEMENT_OPENING; current_movement = VALVE_MOVEMENT_OPENING;
LOG_INF("Virtual valve opening..."); k_work_schedule(&valve_work, K_SECONDS(max_opening_time_s));
k_work_schedule(&valve_work, K_MSEC(max_opening_time_s * 1000 * 0.9));
} }
} else if (reg == 2) { /* Close */ } else if (reg == 2) { /* Close */
if (current_state == VALVE_STATE_OPEN) { if (current_state == VALVE_STATE_OPEN) {
current_movement = VALVE_MOVEMENT_CLOSING; current_movement = VALVE_MOVEMENT_CLOSING;
LOG_INF("Virtual valve closing..."); k_work_schedule(&valve_work, K_SECONDS(max_closing_time_s));
k_work_schedule(&valve_work, K_MSEC(max_closing_time_s * 1000 * 0.9));
} }
} else if (reg == 0) { /* Stop */ } else if (reg == 0) { /* Stop */
k_work_cancel_delayable(&valve_work); k_work_cancel_delayable(&valve_work);
current_movement = VALVE_MOVEMENT_IDLE; current_movement = VALVE_MOVEMENT_IDLE;
LOG_INF("Virtual valve movement stopped");
} }
break; break;
case REG_HOLDING_MAX_OPENING_TIME_S: case REG_HOLDING_MAX_OPENING_TIME_S:
@ -148,14 +142,43 @@ static int holding_reg_wr(uint16_t addr, uint16_t reg)
max_closing_time_s = reg; max_closing_time_s = reg;
settings_save_one("valve/max_close_time", &max_closing_time_s, sizeof(max_closing_time_s)); settings_save_one("valve/max_close_time", &max_closing_time_s, sizeof(max_closing_time_s));
break; break;
case REG_HOLDING_DIGITAL_OUTPUTS_STATE:
digital_outputs_state = reg;
LOG_INF("Digital outputs set to 0x%04X", digital_outputs_state);
// Here you would typically write to GPIOs
break;
case REG_HOLDING_WATCHDOG_TIMEOUT_S: case REG_HOLDING_WATCHDOG_TIMEOUT_S:
watchdog_timeout_s = reg; watchdog_timeout_s = reg;
LOG_INF("Watchdog timeout set to %u s", watchdog_timeout_s);
break;
case REG_HOLDING_FWU_COMMAND:
if (reg == 1) { // Verify Chunk
LOG_INF("FWU: Chunk at offset %u (size %u) verified by client, writing to flash (simulated).", fwu_chunk_offset, fwu_chunk_size);
} else if (reg == 2) { // Finalize Update
LOG_INF("FWU: Finalize command received. Rebooting (simulated).");
// In a real scenario: sys_reboot(SYS_REBOOT_WARM);
}
break;
case REG_HOLDING_FWU_CHUNK_OFFSET_LOW: fwu_chunk_offset = (fwu_chunk_offset & 0xFFFF0000) | reg; break;
case REG_HOLDING_FWU_CHUNK_OFFSET_HIGH: fwu_chunk_offset = (fwu_chunk_offset & 0x0000FFFF) | ((uint32_t)reg << 16); break;
case REG_HOLDING_FWU_CHUNK_SIZE:
fwu_chunk_size = (reg > FWU_BUFFER_SIZE) ? FWU_BUFFER_SIZE : reg;
break; break;
default: default:
// Handle FWU_DATA_BUFFER writes
if (addr >= REG_HOLDING_FWU_DATA_BUFFER && addr < (REG_HOLDING_FWU_DATA_BUFFER + (FWU_BUFFER_SIZE / 2))) {
uint16_t index = (addr - REG_HOLDING_FWU_DATA_BUFFER) * 2;
if (index < sizeof(fwu_buffer)) {
sys_put_be16(reg, &fwu_buffer[index]);
// After the last register of a chunk is written, calculate CRC
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);
}
}
}
break; break;
} }
LOG_INF("Holding register write, addr %u, value %u", addr, reg);
return 0; return 0;
} }
@ -164,39 +187,25 @@ static int input_reg_rd(uint16_t addr, uint16_t *reg)
uint32_t uptime_s = k_uptime_get_32() / 1000; uint32_t uptime_s = k_uptime_get_32() / 1000;
switch (addr) { switch (addr) {
case REG_INPUT_VALVE_STATE_MOVEMENT: case REG_INPUT_VALVE_STATE_MOVEMENT: *reg = (current_movement << 8) | (current_state & 0xFF); break;
*reg = (current_movement << 8) | (current_state & 0xFF); case REG_INPUT_MOTOR_CURRENT_MA: *reg = (current_movement != VALVE_MOVEMENT_IDLE) ? 150 : 10; break; // Simulated
break; case REG_INPUT_DIGITAL_INPUTS_STATE: *reg = digital_inputs_state; break;
case REG_INPUT_MOTOR_CURRENT_MA: case REG_INPUT_BUTTON_EVENTS:
*reg = 50; /* Dummy value */ *reg = button_events;
break; button_events = 0; // Clear-on-read
case REG_INPUT_FIRMWARE_VERSION_MAJOR_MINOR:
*reg = (APP_VERSION_MAJOR << 8) | (APP_VERSION_MINOR & 0xFF);
break;
case REG_INPUT_FIRMWARE_VERSION_PATCH:
*reg = APP_VERSION_PATCH;
break;
case REG_INPUT_DEVICE_STATUS:
*reg = 0; /* 0 = OK */
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;
default:
*reg = 0;
break; break;
case REG_INPUT_FIRMWARE_VERSION_MAJOR_MINOR: *reg = (APP_VERSION_MAJOR << 8) | (APP_VERSION_MINOR & 0xFF); break;
case REG_INPUT_FIRMWARE_VERSION_PATCH: *reg = APP_VERSION_PATCH; break;
case REG_INPUT_DEVICE_STATUS: *reg = device_status; 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_FWU_LAST_CHUNK_CRC: *reg = fwu_last_chunk_crc; break;
default: *reg = 0; break;
} }
LOG_INF("Input register read, addr %u, value %u", addr, *reg);
return 0; return 0;
} }
static struct modbus_user_callbacks mbs_cbs = { static struct modbus_user_callbacks mbs_cbs = {
.coil_rd = coil_rd,
.coil_wr = coil_wr,
.holding_reg_rd = holding_reg_rd, .holding_reg_rd = holding_reg_rd,
.holding_reg_wr = holding_reg_wr, .holding_reg_wr = holding_reg_wr,
.input_reg_rd = input_reg_rd, .input_reg_rd = input_reg_rd,
@ -207,146 +216,71 @@ static struct modbus_user_callbacks mbs_cbs = {
int modbus_reconfigure(uint32_t baudrate, uint8_t unit_id) int modbus_reconfigure(uint32_t baudrate, uint8_t unit_id)
{ {
int err; int err;
LOG_INF("Reconfiguring Modbus: baudrate=%u, id=%u", baudrate, unit_id);
err = modbus_disable(modbus_iface); err = modbus_disable(modbus_iface);
if (err) { if (err) { return err; }
LOG_ERR("Failed to disable Modbus: %d", err);
return err;
}
server_param.serial.baud = baudrate; server_param.serial.baud = baudrate;
server_param.server.unit_id = unit_id; server_param.server.unit_id = unit_id;
err = modbus_init_server(modbus_iface, server_param); err = modbus_init_server(modbus_iface, server_param);
if (err) { return err;
LOG_ERR("Failed to re-init Modbus server: %d", err);
return err;
}
return 0;
} }
uint32_t modbus_get_baudrate(void) uint32_t modbus_get_baudrate(void) { return server_param.serial.baud; }
{ uint8_t modbus_get_unit_id(void) { return server_param.server.unit_id; }
return server_param.serial.baud; 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; }
uint8_t modbus_get_unit_id(void) static int settings_load_cb(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg)
{
return server_param.server.unit_id;
}
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;
}
static int settings_load_cb(const char *name, size_t len,
settings_read_cb read_cb, void *cb_arg)
{ {
const char *next; const char *next;
int rc; int rc;
if (settings_name_steq(name, "baudrate", &next) && !next) { if (settings_name_steq(name, "baudrate", &next) && !next) {
rc = read_cb(cb_arg, &server_param.serial.baud, sizeof(server_param.serial.baud)); rc = read_cb(cb_arg, &server_param.serial.baud, sizeof(server_param.serial.baud));
if (rc < 0) { return (rc < 0) ? rc : 0;
return rc;
}
LOG_INF("Loaded modbus/baudrate: %u", server_param.serial.baud);
return 0;
} }
if (settings_name_steq(name, "unit_id", &next) && !next) { if (settings_name_steq(name, "unit_id", &next) && !next) {
rc = read_cb(cb_arg, &server_param.server.unit_id, sizeof(server_param.server.unit_id)); rc = read_cb(cb_arg, &server_param.server.unit_id, sizeof(server_param.server.unit_id));
if (rc < 0) { return (rc < 0) ? rc : 0;
return rc;
}
LOG_INF("Loaded modbus/unit_id: %u", server_param.server.unit_id);
return 0;
} }
if (settings_name_steq(name, "max_open_time", &next) && !next) { if (settings_name_steq(name, "max_open_time", &next) && !next) {
rc = read_cb(cb_arg, &max_opening_time_s, sizeof(max_opening_time_s)); rc = read_cb(cb_arg, &max_opening_time_s, sizeof(max_opening_time_s));
if (rc < 0) { return (rc < 0) ? rc : 0;
return rc;
}
LOG_INF("Loaded valve/max_open_time: %u", max_opening_time_s);
return 0;
} }
if (settings_name_steq(name, "max_close_time", &next) && !next) { if (settings_name_steq(name, "max_close_time", &next) && !next) {
rc = read_cb(cb_arg, &max_closing_time_s, sizeof(max_closing_time_s)); rc = read_cb(cb_arg, &max_closing_time_s, sizeof(max_closing_time_s));
if (rc < 0) { return (rc < 0) ? rc : 0;
return rc;
}
LOG_INF("Loaded valve/max_close_time: %u", max_closing_time_s);
return 0;
} }
return -ENOENT; return -ENOENT;
} }
SETTINGS_STATIC_HANDLER_DEFINE(modbus, "modbus", NULL, settings_load_cb, NULL, NULL); SETTINGS_STATIC_HANDLER_DEFINE(modbus, "modbus", NULL, settings_load_cb, NULL, NULL);
SETTINGS_STATIC_HANDLER_DEFINE(valve, "valve", NULL, settings_load_cb, NULL, NULL); SETTINGS_STATIC_HANDLER_DEFINE(valve, "valve", NULL, settings_load_cb, NULL, NULL);
static int init_modbus_server(void) static int init_modbus_server(void)
{ {
const char iface_name[] = {DEVICE_DT_NAME(MODBUS_NODE)}; const char iface_name[] = {DEVICE_DT_NAME(MODBUS_NODE)};
modbus_iface = modbus_iface_get_by_name(iface_name); modbus_iface = modbus_iface_get_by_name(iface_name);
if (modbus_iface < 0) { if (modbus_iface < 0) { return modbus_iface; }
LOG_ERR("Failed to get iface index for %s", iface_name);
return modbus_iface;
}
server_param.server.user_cb = &mbs_cbs; server_param.server.user_cb = &mbs_cbs;
return modbus_init_server(modbus_iface, server_param); return modbus_init_server(modbus_iface, server_param);
} }
int main(void) int main(void)
{ {
LOG_INF("Starting APP"); LOG_INF("Starting Irrigation System Slave Node");
k_work_init_delayable(&valve_work, valve_work_handler); k_work_init_delayable(&valve_work, valve_work_handler);
if (settings_subsys_init()) { if (settings_subsys_init() || settings_load()) {
LOG_ERR("Failed to initialize settings subsystem"); LOG_ERR("Settings initialization or loading failed");
} }
if (settings_load()) {
LOG_ERR("Failed to load settings");
}
if (init_modbus_server()) { if (init_modbus_server()) {
LOG_ERR("Modbus RTU server initialization failed"); LOG_ERR("Modbus RTU server initialization failed");
} return 0;
LOG_INF("APP started");
while (1) {
k_sleep(K_MSEC(1000));
} }
LOG_INF("Irrigation System Slave Node started successfully");
return 0; return 0;
} }