feat: Integrate VND7050AJ driver and enhance gateway settings
This commit introduces the VND7050AJ driver as a new submodule and integrates it into the project. Key changes include: - Added as a git submodule. - Enhanced the gateway application () with LittleFS and the settings subsystem. - Implemented new shell commands (, , ) for managing custom settings. - Added functionality to compact the settings file. - Updated to include new library dependencies and log return code. - Adjusted include paths for in relevant files. Signed-off-by: Eduard Iten <eduard@iten.pro>
This commit is contained in:
0
software/apps/bl_test/sysbuild/flash.dtsi
Normal file
0
software/apps/bl_test/sysbuild/flash.dtsi
Normal file
@@ -1,28 +1,47 @@
|
||||
# Enable logging
|
||||
# -------------------
|
||||
# Logging and Console
|
||||
# -------------------
|
||||
CONFIG_LOG=y
|
||||
CONFIG_UART_CONSOLE=y
|
||||
|
||||
# Enable shell
|
||||
# -------------
|
||||
# Zephyr Shell
|
||||
# -------------
|
||||
CONFIG_SHELL=y
|
||||
CONFIG_KERNEL_SHELL=y
|
||||
CONFIG_REBOOT=y
|
||||
|
||||
# Enable MCUMGR
|
||||
# -------------------
|
||||
# MCUmgr OS Management
|
||||
# -------------------
|
||||
CONFIG_MCUMGR=y
|
||||
|
||||
# Enable MCUMGR OS management group only
|
||||
CONFIG_MCUMGR_GRP_OS=y
|
||||
|
||||
# Configure MCUMGR transport to UART
|
||||
CONFIG_MCUMGR_TRANSPORT_UART=y
|
||||
|
||||
# -------------------
|
||||
# MCUmgr Filesystem Group
|
||||
# -------------------
|
||||
CONFIG_MCUMGR_GRP_FS=y
|
||||
|
||||
# -------------------
|
||||
# LittleFS and Flash
|
||||
# -------------------
|
||||
CONFIG_FILE_SYSTEM=y
|
||||
CONFIG_FILE_SYSTEM_LITTLEFS=y
|
||||
CONFIG_FLASH=y
|
||||
CONFIG_FLASH_MAP=y
|
||||
|
||||
# -------------------
|
||||
# Settings Subsystem
|
||||
# -------------------
|
||||
CONFIG_SETTINGS=y
|
||||
CONFIG_SETTINGS_FILE=y
|
||||
CONFIG_SETTINGS_FILE_PATH="/lfs/settings.bin"
|
||||
|
||||
# -------------------
|
||||
# Dependencies
|
||||
# -------------------
|
||||
CONFIG_NET_BUF=y
|
||||
CONFIG_ZCBOR=y
|
||||
CONFIG_CRC=y
|
||||
CONFIG_BASE64=y
|
||||
|
||||
CONFIG_MCUMGR_GRP_IMG=y
|
||||
CONFIG_IMG_MANAGER=y
|
||||
CONFIG_MCUBOOT_IMG_MANAGER=y
|
||||
CONFIG_STREAM_FLASH=y
|
||||
@@ -1,11 +1,136 @@
|
||||
#include <zephyr/fs/fs.h>
|
||||
#include <zephyr/fs/littlefs.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/settings/settings.h>
|
||||
#include <zephyr/shell/shell.h>
|
||||
#include <app_version.h>
|
||||
#include <string.h>
|
||||
|
||||
LOG_MODULE_REGISTER(hello_world);
|
||||
|
||||
/* LittleFS mount configuration for 'storage_partition' partition */
|
||||
FS_LITTLEFS_DECLARE_DEFAULT_CONFIG(storage_partition);
|
||||
static struct fs_mount_t littlefs_mnt = {
|
||||
.type = FS_LITTLEFS,
|
||||
.mnt_point = "/lfs",
|
||||
.fs_data = &storage_partition, // default config macro
|
||||
.storage_dev = (void *)FIXED_PARTITION_ID(storage_partition),
|
||||
};
|
||||
|
||||
static char my_setting[32] = "default";
|
||||
|
||||
static int my_settings_set(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg)
|
||||
{
|
||||
if (strcmp(name, "value") == 0) {
|
||||
if (len > sizeof(my_setting) - 1) {
|
||||
len = sizeof(my_setting) - 1;
|
||||
}
|
||||
if (read_cb(cb_arg, my_setting, len) == len) {
|
||||
my_setting[len] = '\0';
|
||||
return 0;
|
||||
}
|
||||
return -EINVAL;
|
||||
}
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
static int my_settings_export(int (*export_func)(const char *, const void *, size_t))
|
||||
{
|
||||
return export_func("my/setting/value", my_setting, strlen(my_setting));
|
||||
}
|
||||
|
||||
SETTINGS_STATIC_HANDLER_DEFINE(my, "my/setting", NULL, my_settings_set, NULL, my_settings_export);
|
||||
|
||||
static int cmd_my_get(const struct shell *shell, size_t argc, char **argv)
|
||||
{
|
||||
shell_print(shell, "my_setting = '%s'", my_setting);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cmd_my_reset(const struct shell *shell, size_t argc, char **argv)
|
||||
{
|
||||
strcpy(my_setting, "default");
|
||||
settings_save();
|
||||
shell_print(shell, "my_setting reset to default");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Improved set command: join all arguments for whitespace support
|
||||
static int cmd_my_set(const struct shell *shell, size_t argc, char **argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
shell_error(shell, "Usage: my set <value>");
|
||||
return -EINVAL;
|
||||
}
|
||||
// Join all argv[1..] with spaces
|
||||
size_t i, pos = 0;
|
||||
my_setting[0] = '\0';
|
||||
for (i = 1; i < argc; ++i) {
|
||||
size_t left = sizeof(my_setting) - 1 - pos;
|
||||
if (left == 0)
|
||||
break;
|
||||
strncat(my_setting, argv[i], left);
|
||||
pos = strlen(my_setting);
|
||||
if (i < argc - 1 && left > 1) {
|
||||
strncat(my_setting, " ", left - 1);
|
||||
pos = strlen(my_setting);
|
||||
}
|
||||
}
|
||||
my_setting[sizeof(my_setting) - 1] = '\0';
|
||||
settings_save();
|
||||
shell_print(shell, "my_setting set to '%s'", my_setting);
|
||||
return 0;
|
||||
}
|
||||
|
||||
SHELL_STATIC_SUBCMD_SET_CREATE(my_subcmds,
|
||||
SHELL_CMD(get, NULL, "Get my_setting", cmd_my_get),
|
||||
SHELL_CMD(set, NULL, "Set my_setting (supports spaces)", cmd_my_set),
|
||||
SHELL_CMD(reset, NULL, "Reset my_setting to default and compact settings file", cmd_my_reset),
|
||||
SHELL_SUBCMD_SET_END);
|
||||
|
||||
SHELL_CMD_REGISTER(my, &my_subcmds, "My settings commands", NULL);
|
||||
|
||||
static void compact_settings_file(void)
|
||||
{
|
||||
struct fs_file_t file;
|
||||
fs_file_t_init(&file);
|
||||
int rc = fs_open(&file, "/lfs/settings.bin", FS_O_WRITE | FS_O_CREATE | FS_O_TRUNC);
|
||||
if (rc == 0) {
|
||||
fs_close(&file);
|
||||
LOG_INF("Settings file compacted (truncated and recreated)");
|
||||
} else if (rc == -ENOENT) {
|
||||
LOG_INF("Settings file did not exist, created new");
|
||||
} else {
|
||||
LOG_ERR("Failed to compact settings file (%d)", rc);
|
||||
}
|
||||
settings_save();
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int rc = fs_mount(&littlefs_mnt);
|
||||
if (rc < 0) {
|
||||
LOG_ERR("Error mounting LittleFS [%d]", rc);
|
||||
} else {
|
||||
LOG_INF("LittleFS mounted at /lfs");
|
||||
}
|
||||
|
||||
/* Initialize settings subsystem */
|
||||
settings_subsys_init();
|
||||
LOG_INF("Settings subsystem initialized");
|
||||
|
||||
/* Load settings from storage */
|
||||
rc = settings_load();
|
||||
if (rc == 0) {
|
||||
LOG_INF("Settings loaded: my_setting='%s'", my_setting);
|
||||
} else {
|
||||
LOG_ERR("Failed to load settings (%d)", rc);
|
||||
}
|
||||
|
||||
/* Compact settings file on each start */
|
||||
compact_settings_file();
|
||||
|
||||
LOG_INF("Hello World! Version: %s", APP_VERSION_EXTENDED_STRING);
|
||||
return 0;
|
||||
}
|
||||
@@ -6,7 +6,3 @@
|
||||
zephyr,code-partition = &slot0_partition;
|
||||
};
|
||||
};
|
||||
|
||||
&usb_serial {
|
||||
status = "okay";
|
||||
};
|
||||
@@ -3,6 +3,8 @@ cmake_minimum_required(VERSION 3.20)
|
||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
|
||||
project(slave_node LANGUAGES C)
|
||||
|
||||
zephyr_include_directories(../../include)
|
||||
add_subdirectory(../../lib lib)
|
||||
|
||||
target_sources(app PRIVATE src/main.c)
|
||||
|
||||
47
software/apps/slave_node/boards/native_sim.overlay
Normal file
47
software/apps/slave_node/boards/native_sim.overlay
Normal file
@@ -0,0 +1,47 @@
|
||||
/ {
|
||||
aliases {
|
||||
vnd7050aj = &vnd7050aj;
|
||||
};
|
||||
|
||||
vnd7050aj: vnd7050aj {
|
||||
compatible = "st,vnd7050aj";
|
||||
status = "okay";
|
||||
|
||||
input0-gpios = <&gpio0 1 GPIO_ACTIVE_HIGH>;
|
||||
input1-gpios = <&gpio0 2 GPIO_ACTIVE_HIGH>;
|
||||
select0-gpios = <&gpio0 3 GPIO_ACTIVE_HIGH>;
|
||||
select1-gpios = <&gpio0 4 GPIO_ACTIVE_HIGH>;
|
||||
sense-enable-gpios = <&gpio0 5 GPIO_ACTIVE_HIGH>;
|
||||
fault-reset-gpios = <&gpio0 6 GPIO_ACTIVE_LOW>;
|
||||
io-channels = <&adc0 0>;
|
||||
r-sense-ohms = <1500>;
|
||||
k-vcc = <4000>;
|
||||
};
|
||||
|
||||
modbus_uart: uart_2 {
|
||||
compatible = "zephyr,native-pty-uart";
|
||||
status = "okay";
|
||||
current-speed = <19200>;
|
||||
|
||||
modbus0: modbus0 {
|
||||
compatible = "zephyr,modbus-serial";
|
||||
status = "okay";
|
||||
};
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
&adc0 {
|
||||
#address-cells = <1>;
|
||||
#size-cells = <0>;
|
||||
ref-internal-mv = <3300>;
|
||||
ref-external1-mv = <5000>;
|
||||
|
||||
channel@0 {
|
||||
reg = <0>;
|
||||
zephyr,gain = "ADC_GAIN_1";
|
||||
zephyr,reference = "ADC_REF_INTERNAL";
|
||||
zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
|
||||
zephyr,resolution = <12>;
|
||||
};
|
||||
};
|
||||
88
software/apps/slave_node/dts/bindings/st,vnd7050aj.yaml
Normal file
88
software/apps/slave_node/dts/bindings/st,vnd7050aj.yaml
Normal file
@@ -0,0 +1,88 @@
|
||||
# Copyright (c) 2024, Eduard Iten
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
description: |
|
||||
STMicroelectronics VND7050AJ dual-channel high-side driver.
|
||||
This is a GPIO and ADC controlled device.
|
||||
|
||||
compatible: "st,vnd7050aj"
|
||||
|
||||
include: base.yaml
|
||||
|
||||
properties:
|
||||
input0-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO to control output channel 0.
|
||||
|
||||
input1-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO to control output channel 1.
|
||||
|
||||
select0-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO for MultiSense selection bit 0.
|
||||
|
||||
select1-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO for MultiSense selection bit 1.
|
||||
|
||||
sense-enable-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO to enable the MultiSense output.
|
||||
|
||||
fault-reset-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO to reset a latched fault (active-low).
|
||||
|
||||
io-channels:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: |
|
||||
ADC channel connected to the MultiSense pin. This should be an
|
||||
io-channels property pointing to the ADC controller and channel number.
|
||||
|
||||
r-sense-ohms:
|
||||
type: int
|
||||
required: true
|
||||
description: |
|
||||
Value of the external sense resistor connected from the MultiSense
|
||||
pin to GND, specified in Ohms. This is critical for correct
|
||||
conversion of the analog readings.
|
||||
|
||||
k-factor:
|
||||
type: int
|
||||
default: 1500
|
||||
description: |
|
||||
Factor between PowerMOS and SenseMOS.
|
||||
|
||||
k-vcc:
|
||||
type: int
|
||||
default: 8000
|
||||
description: |
|
||||
VCC sense ratio multiplied by 1000. Used for supply voltage calculation.
|
||||
|
||||
t-sense-0:
|
||||
type: int
|
||||
default: 25
|
||||
description: |
|
||||
Temperature sense reference temperature in degrees Celsius.
|
||||
|
||||
v-sense-0:
|
||||
type: int
|
||||
default: 2070
|
||||
description: |
|
||||
Temperature sense reference voltage in millivolts.
|
||||
|
||||
k-tchip:
|
||||
type: int
|
||||
default: -5500
|
||||
description: |
|
||||
Temperature sense gain coefficient multiplied by 1000.
|
||||
Used for chip temperature calculation.
|
||||
|
||||
@@ -22,7 +22,7 @@ CONFIG_SETTINGS_LOG_LEVEL_DBG=y
|
||||
CONFIG_UART_INTERRUPT_DRIVEN=y
|
||||
CONFIG_MODBUS=y
|
||||
CONFIG_MODBUS_ROLE_SERVER=y
|
||||
CONFIG_MODBUS_BUFFER_SIZE=256
|
||||
CONFIG_MODBUS_LOG_LEVEL_DBG=y
|
||||
|
||||
# Enable VND7050AJ
|
||||
CONFIG_VND7050AJ=y
|
||||
|
||||
@@ -9,6 +9,7 @@ LOG_MODULE_REGISTER(main, LOG_LEVEL_INF);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int rc;
|
||||
LOG_INF("Starting Irrigation System Slave Node");
|
||||
|
||||
if (settings_subsys_init() || settings_load()) {
|
||||
@@ -18,9 +19,10 @@ int main(void)
|
||||
valve_init();
|
||||
fwu_init();
|
||||
|
||||
if (modbus_server_init()) {
|
||||
LOG_ERR("Modbus RTU server initialization failed");
|
||||
return 0;
|
||||
rc = modbus_server_init();
|
||||
if (rc) {
|
||||
LOG_ERR("Modbus server initialization failed: %d", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
LOG_INF("Irrigation System Slave Node started successfully");
|
||||
|
||||
5
software/apps/slave_node/sysbuild.conf
Normal file
5
software/apps/slave_node/sysbuild.conf
Normal file
@@ -0,0 +1,5 @@
|
||||
SB_CONFIG_BOOTLOADER_MCUBOOT=y
|
||||
SB_CONFIG_MCUBOOT_MODE_SINGLE_APP=y
|
||||
|
||||
CONFIG_LOG=y
|
||||
CONFIG_MCUBOOT_LOG_LEVEL_INF=y
|
||||
Reference in New Issue
Block a user