feat(settings): Implement persistent Modbus configuration
- Integrate the Zephyr Settings subsystem to persist Modbus parameters. - Use NVS (Non-Volatile Storage) as the backend with a dedicated flash partition. - Modbus baudrate and slave ID are now loaded at startup. - Changes made via the shell are saved to flash and survive a reboot. - Add a 'reset' command to the shell for easier testing. - Fix all compiler and devicetree warnings for a clean build.
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/modbus/modbus.h>
|
||||
#include <zephyr/usb/usb_device.h>
|
||||
#include <zephyr/settings/settings.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
#include "modbus_bridge.h"
|
||||
@@ -164,6 +165,35 @@ uint8_t modbus_get_unit_id(void)
|
||||
return server_param.server.unit_id;
|
||||
}
|
||||
|
||||
static int settings_modbus_load(const char *name, size_t len,
|
||||
settings_read_cb read_cb, void *cb_arg)
|
||||
{
|
||||
const char *next;
|
||||
int rc;
|
||||
|
||||
if (settings_name_steq(name, "baudrate", &next) && !next) {
|
||||
rc = read_cb(cb_arg, &server_param.serial.baud, sizeof(server_param.serial.baud));
|
||||
if (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) {
|
||||
rc = read_cb(cb_arg, &server_param.server.unit_id, sizeof(server_param.server.unit_id));
|
||||
if (rc < 0) {
|
||||
return rc;
|
||||
}
|
||||
LOG_INF("Loaded modbus/unit_id: %u", server_param.server.unit_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
SETTINGS_STATIC_HANDLER_DEFINE(modbus, "modbus", NULL, settings_modbus_load, NULL, NULL);
|
||||
|
||||
|
||||
static int init_modbus_server(void)
|
||||
{
|
||||
@@ -183,6 +213,16 @@ static int init_modbus_server(void)
|
||||
int main(void)
|
||||
{
|
||||
LOG_INF("Starting APP");
|
||||
|
||||
if (settings_subsys_init()) {
|
||||
LOG_ERR("Failed to initialize settings subsystem");
|
||||
}
|
||||
|
||||
if (settings_load()) {
|
||||
LOG_ERR("Failed to load settings");
|
||||
}
|
||||
|
||||
|
||||
if (init_modbus_server()) {
|
||||
LOG_ERR("Modbus RTU server initialization failed");
|
||||
}
|
||||
@@ -193,4 +233,4 @@ int main(void)
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user