Compare commits

8 Commits

Author SHA1 Message Date
6c1ff0c4df feat(refactor): Restructure project for improved modularity and clarity
This commit introduces a major refactoring of the project structure to align
with Zephyr's recommended multi-application and library organization.

Key changes include:
- Relocation of custom modules from 'software/modules/' to 'software/lib/'.
- Introduction of a central 'software/CMakeLists.txt' to manage application
  and library subdirectories.
- Creation of new Kconfig files for 'software/' and 'software/apps/slave_node/'
  to define project-wide and application-specific configurations.
- Removal of the 'gateway' and 'stm32g431_tests' applications.
- Removal of 'shell_modbus.c' and 'shell_system.c' from 'slave_node' application's
  direct source files, indicating a shift towards library-based shell commands.
- Updates to 'software/apps/slave_node/CMakeLists.txt', 'prj.conf', and
  'boards/bluepill_f103rb.conf' to reflect the new structure and dependencies.
2025-07-03 16:58:43 +02:00
3f0d5a76c6 feat(cdc_acm): Add CDC-ACM support and remove old test applications
- Implemented CDC-ACM (USB Virtual COM Port) support for the slave_node application.
- Removed the now obsolete 'hello_world' and 'stm32g431_tests' applications.
2025-07-03 14:31:17 +02:00
10a770de59 fix(modbus_server): Implement hardcoded firmware version 0.0.1
Set firmware version to 0.0.1 in modbus_server.c for Modbus tool display.
This is a temporary solution until MCUboot integration is complete.
2025-07-03 13:43:15 +02:00
1b0519aadf Resolve merge conflict in modbus_server.c and add hardcoded firmware version. 2025-07-03 13:34:59 +02:00
e429a0874d Revert "feat(slave_node): Refine Modbus UART and add CDC-ACM support"
This reverts commit 3a05c80b25.
2025-07-03 13:34:01 +02:00
3a05c80b25 feat(slave_node): Refine Modbus UART and add CDC-ACM support
- Adjusted Device Tree Overlays for bluepill_f103rb and weact_stm32g431_core
  to correctly define Modbus UART via 'modbus0' subnode with 'zephyr,modbus-serial'
  compatibility, aligning with rtu_server sample.
- Prepared modbus_server.c to use the correct Device Tree node for Modbus UART.
2025-07-03 13:18:47 +02:00
5208f1370d feat(slave_node): Support multi-board build for bluepill_f103rb and weact_stm32g431_core
Refactor slave_node application to support building for both bluepill_f103rb and
weact_stm32g431_core boards.

- Moved RTT-specific console and shell backend configurations from prj.conf
  into board-specific .conf files (bluepill_f103rb.conf).
- Configured USART2 as console/shell for weact_stm32g431_core.
- Added Device Tree Overlay for weact_stm32g431_core to enable USART1 for Modbus
  communication (PA9/PA10).
2025-07-03 10:53:21 +02:00
a59e8518cc Rename hello_world app to stm32g431_tests 2025-07-03 10:02:53 +02:00
41 changed files with 206 additions and 129 deletions

5
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,5 @@
{
"files.associations": {
"fwu.h": "c"
}
}

6
lib/fwu/CMakeLists.txt Normal file
View File

@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.20)
project(fwu)
target_sources(fwu PRIVATE src/fwu.c)
target_include_directories(fwu PUBLIC include)

View File

@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.20)
project(modbus_server)
target_sources(modbus_server PRIVATE src/modbus_server.c)
target_include_directories(modbus_server PUBLIC include)

6
lib/valve/CMakeLists.txt Normal file
View File

@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.20)
project(valve)
target_sources(valve PRIVATE src/valve.c)
target_include_directories(valve PUBLIC include)

View File

@@ -1,8 +0,0 @@
cmake_minimum_required(VERSION 3.13.1)
project(software)
add_subdirectory(modules/modbus_server)
add_subdirectory(modules/valve)
add_subdirectory(modules/fwu)
add_subdirectory(apps/hello_world)

1
software/Kconfig Normal file
View File

@@ -0,0 +1 @@
rsource "lib/Kconfig"

View File

@@ -1,6 +0,0 @@
cmake_minimum_required(VERSION 3.20)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(hello_world)
target_sources(app PRIVATE src/main.c)

View File

@@ -1,4 +0,0 @@
CONFIG_CONSOLE=y
CONFIG_LOG=y
CONFIG_UART_CONSOLE=y

View File

@@ -1,9 +0,0 @@
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
int main(void)
{
printk("Hello World! %s\n", CONFIG_BOARD);
return 0;
}

View File

@@ -1,27 +1,8 @@
cmake_minimum_required(VERSION 3.20) cmake_minimum_required(VERSION 3.20)
# Point BOARD_ROOT and DTS_ROOT to the 'software' directory, which contains 'boards'.
list(APPEND BOARD_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(slave_node)
list(APPEND KCONFIG_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/../../modules/modbus_server/Kconfig) project(slave_node LANGUAGES C)
list(APPEND KCONFIG_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/../../modules/valve/Kconfig) zephyr_include_directories(../../include)
list(APPEND KCONFIG_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/../../modules/fwu/Kconfig) add_subdirectory(../../lib lib)
target_sources(app PRIVATE src/main.c)
target_include_directories(app PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/../../modules/valve/include
${CMAKE_CURRENT_SOURCE_DIR}/../../modules/modbus_server/include
${CMAKE_CURRENT_SOURCE_DIR}/../../modules/fwu/include
)
# Add the source files from the app and the libraries
target_sources(app PRIVATE
src/main.c
src/shell_modbus.c
src/shell_system.c
${CMAKE_CURRENT_SOURCE_DIR}/../../modules/valve/src/valve.c
${CMAKE_CURRENT_SOURCE_DIR}/../../modules/modbus_server/src/modbus_server.c
${CMAKE_CURRENT_SOURCE_DIR}/../../modules/fwu/src/fwu.c
)

View File

@@ -0,0 +1,2 @@
rsource "../../lib/Kconfig"
source "Kconfig.zephyr"

View File

@@ -0,0 +1,7 @@
# Disable UART console
CONFIG_UART_CONSOLE=n
# Enable RTT console
CONFIG_RTT_CONSOLE=y
CONFIG_USE_SEGGER_RTT=y
CONFIG_SHELL_BACKEND_RTT=y

View File

@@ -0,0 +1,9 @@
&usart1 {
modbus0 {
compatible = "zephyr,modbus-serial";
status = "okay";
};
status = "okay";
pinctrl-0 = <&usart1_tx_pa9 &usart1_rx_pa10>;
pinctrl-names = "default";
};

View File

@@ -0,0 +1,10 @@
&zephyr_udc0 {
cdc_acm_uart0: cdc_acm_uart0 {
compatible = "zephyr,cdc-acm-uart";
modbus0 {
compatible = "zephyr,modbus-serial";
status = "okay";
};
};
};

View File

@@ -0,0 +1,4 @@
CONFIG_USB_DEVICE_STACK=y
CONFIG_USB_DEVICE_PRODUCT="Modbus slave node"
CONFIG_UART_LINE_CTRL=y
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n

View File

@@ -2,16 +2,8 @@
CONFIG_CONSOLE=y CONFIG_CONSOLE=y
CONFIG_LOG=y CONFIG_LOG=y
# Disable UART console
CONFIG_UART_CONSOLE=n
# Enable RTT console
CONFIG_RTT_CONSOLE=y
CONFIG_USE_SEGGER_RTT=y
# Enable Shell # Enable Shell
CONFIG_SHELL=y CONFIG_SHELL=y
CONFIG_SHELL_BACKEND_RTT=y
CONFIG_REBOOT=y CONFIG_REBOOT=y
# Enable Settings Subsystem # Enable Settings Subsystem

View File

@@ -1,9 +1,9 @@
#include <zephyr/kernel.h> #include <zephyr/kernel.h>
#include <zephyr/settings/settings.h> #include <zephyr/settings/settings.h>
#include <zephyr/logging/log.h> #include <zephyr/logging/log.h>
#include <modbus_server.h> #include <lib/modbus_server.h>
#include <valve.h> #include <lib/valve.h>
#include <fwu.h> #include <lib/fwu.h>
LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); LOG_MODULE_REGISTER(main, LOG_LEVEL_INF);

View File

@@ -0,0 +1,5 @@
add_subdirectory_ifdef(CONFIG_LIB_FWU fwu)
add_subdirectory_ifdef(CONFIG_LIB_MODBUS_SERVER modbus_server)
add_subdirectory_ifdef(CONFIG_LIB_VALVE valve)
add_subdirectory_ifdef(CONFIG_SHELL_SYSTEM shell_system)
add_subdirectory_ifdef(CONFIG_SHELL_MODBUS shell_modbus)

8
software/lib/Kconfig Normal file
View File

@@ -0,0 +1,8 @@
menu "Irrigation system software libraries"
rsource "fwu/Kconfig"
rsource "modbus_server/Kconfig"
rsource "valve/Kconfig"
rsource "shell_system/Kconfig"
rsource "shell_modbus/Kconfig"
endmenu

View File

@@ -0,0 +1 @@
zephyr_library_sources(fwu.c)

View File

@@ -1,5 +1,5 @@
config FWU config LIB_FWU
bool "Enable Firmware Update Library" bool "Enable Firmware Update Library"
default y default y
help help
Enable the Firmware Update module. Enable the Firmware Update Library.

View File

@@ -1,8 +1,8 @@
#include "fwu.h"
#include <zephyr/kernel.h> #include <zephyr/kernel.h>
#include <zephyr/sys/crc.h> #include <zephyr/sys/crc.h>
#include <zephyr/sys/byteorder.h> #include <zephyr/sys/byteorder.h>
#include <zephyr/logging/log.h> #include <zephyr/logging/log.h>
#include <lib/fwu.h>
LOG_MODULE_REGISTER(fwu, LOG_LEVEL_INF); LOG_MODULE_REGISTER(fwu, LOG_LEVEL_INF);

View File

@@ -0,0 +1 @@
zephyr_library_sources(modbus_server.c)

View File

@@ -0,0 +1,5 @@
config LIB_MODBUS_SERVER
bool "Enable Modbus Server Library"
default y
help
Enable the Modbus Server Library.

View File

@@ -1,19 +1,23 @@
#include <zephyr/kernel.h> #include <zephyr/kernel.h>
#include "modbus_server.h" #include <zephyr/drivers/uart.h>
#include <zephyr/device.h>
#include <zephyr/modbus/modbus.h> #include <zephyr/modbus/modbus.h>
#include <zephyr/logging/log.h> #include <zephyr/logging/log.h>
#include <zephyr/settings/settings.h> #include <zephyr/settings/settings.h>
#include <zephyr/sys/reboot.h> #include <zephyr/sys/reboot.h>
#include "valve.h" #include <lib/modbus_server.h>
#include "fwu.h" #include <lib/valve.h>
#include <lib/fwu.h>
#include <zephyr/usb/usb_device.h>
LOG_MODULE_REGISTER(modbus_server, LOG_LEVEL_INF); LOG_MODULE_REGISTER(modbus_server, LOG_LEVEL_INF);
static int modbus_iface; 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 = { .user_cb = NULL, .unit_id = 1 }, .server = {.user_cb = NULL, .unit_id = 1},
.serial = { .baud = 19200, .parity = UART_CFG_PARITY_NONE }, .serial = {.baud = 19200, .parity = UART_CFG_PARITY_NONE},
}; };
static uint16_t watchdog_timeout_s = 0; static uint16_t watchdog_timeout_s = 0;
@@ -27,7 +31,8 @@ static void watchdog_timer_handler(struct k_timer *timer_id)
static inline void reset_watchdog(void) static inline void reset_watchdog(void)
{ {
if (watchdog_timeout_s > 0) { if (watchdog_timeout_s > 0)
{
k_timer_start(&watchdog_timer, K_SECONDS(watchdog_timeout_s), K_NO_WAIT); k_timer_start(&watchdog_timer, K_SECONDS(watchdog_timeout_s), K_NO_WAIT);
} }
} }
@@ -35,11 +40,20 @@ static inline void reset_watchdog(void)
static int holding_reg_rd(uint16_t addr, uint16_t *reg) static int holding_reg_rd(uint16_t addr, uint16_t *reg)
{ {
reset_watchdog(); reset_watchdog();
switch (addr) { 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_MAX_OPENING_TIME_S:
case REG_HOLDING_WATCHDOG_TIMEOUT_S: *reg = watchdog_timeout_s; break; *reg = valve_get_max_open_time();
default: *reg = 0; break; 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; return 0;
} }
@@ -47,11 +61,21 @@ static int holding_reg_rd(uint16_t addr, uint16_t *reg)
static int holding_reg_wr(uint16_t addr, uint16_t reg) static int holding_reg_wr(uint16_t addr, uint16_t reg)
{ {
reset_watchdog(); reset_watchdog();
switch (addr) { switch (addr)
{
case REG_HOLDING_VALVE_COMMAND: case REG_HOLDING_VALVE_COMMAND:
if (reg == 1) { valve_open(); } if (reg == 1)
else if (reg == 2) { valve_close(); } {
else if (reg == 0) { valve_stop(); } valve_open();
}
else if (reg == 2)
{
valve_close();
}
else if (reg == 0)
{
valve_stop();
}
break; break;
case REG_HOLDING_MAX_OPENING_TIME_S: case REG_HOLDING_MAX_OPENING_TIME_S:
valve_set_max_open_time(reg); valve_set_max_open_time(reg);
@@ -61,16 +85,20 @@ static int holding_reg_wr(uint16_t addr, uint16_t reg)
break; break;
case REG_HOLDING_WATCHDOG_TIMEOUT_S: case REG_HOLDING_WATCHDOG_TIMEOUT_S:
watchdog_timeout_s = reg; watchdog_timeout_s = reg;
if (watchdog_timeout_s > 0) { if (watchdog_timeout_s > 0)
{
LOG_INF("Watchdog enabled with %u s timeout.", watchdog_timeout_s); LOG_INF("Watchdog enabled with %u s timeout.", watchdog_timeout_s);
reset_watchdog(); reset_watchdog();
} else { }
else
{
LOG_INF("Watchdog disabled."); LOG_INF("Watchdog disabled.");
k_timer_stop(&watchdog_timer); k_timer_stop(&watchdog_timer);
} }
break; break;
case REG_HOLDING_DEVICE_RESET: case REG_HOLDING_DEVICE_RESET:
if (reg == 1) { if (reg == 1)
{
LOG_WRN("Modbus reset command received. Rebooting..."); LOG_WRN("Modbus reset command received. Rebooting...");
sys_reboot(SYS_REBOOT_WARM); sys_reboot(SYS_REBOOT_WARM);
} }
@@ -86,13 +114,32 @@ static int input_reg_rd(uint16_t addr, uint16_t *reg)
{ {
reset_watchdog(); reset_watchdog();
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: *reg = (valve_get_movement() << 8) | (valve_get_state() & 0xFF); break; {
case REG_INPUT_MOTOR_CURRENT_MA: *reg = valve_get_motor_current(); break; case REG_INPUT_VALVE_STATE_MOVEMENT:
case REG_INPUT_UPTIME_SECONDS_LOW: *reg = (uint16_t)(uptime_s & 0xFFFF); break; *reg = (valve_get_movement() << 8) | (valve_get_state() & 0xFF);
case REG_INPUT_UPTIME_SECONDS_HIGH: *reg = (uint16_t)(uptime_s >> 16); break; break;
case REG_INPUT_FWU_LAST_CHUNK_CRC: *reg = fwu_get_last_chunk_crc(); break; case REG_INPUT_MOTOR_CURRENT_MA:
default: *reg = 0; break; *reg = valve_get_motor_current();
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_get_last_chunk_crc();
break;
case REG_INPUT_FIRMWARE_VERSION_MAJOR_MINOR:
*reg = (0 << 8) | 0;
break;
case REG_INPUT_FIRMWARE_VERSION_PATCH:
*reg = 2;
break;
default:
*reg = 0;
break;
} }
return 0; return 0;
} }
@@ -109,8 +156,28 @@ int modbus_server_init(void)
{ {
k_timer_init(&watchdog_timer, watchdog_timer_handler, NULL); k_timer_init(&watchdog_timer, watchdog_timer_handler, NULL);
const char iface_name[] = {DEVICE_DT_NAME(MODBUS_NODE)}; 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;
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));
}
LOG_INF("Client connected to server on %s", dev->name);
#endif
modbus_iface = modbus_iface_get_by_name(iface_name); modbus_iface = modbus_iface_get_by_name(iface_name);
if (modbus_iface < 0) { return modbus_iface; } if (modbus_iface < 0)
{
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);
} }
@@ -122,7 +189,8 @@ int modbus_reconfigure(uint32_t baudrate, uint8_t unit_id)
int ret = modbus_init_server(modbus_iface, server_param); int ret = modbus_init_server(modbus_iface, server_param);
if (ret == 0) { if (ret == 0)
{
settings_save_one("modbus/baudrate", &baudrate, sizeof(baudrate)); settings_save_one("modbus/baudrate", &baudrate, sizeof(baudrate));
settings_save_one("modbus/unit_id", &unit_id, sizeof(unit_id)); settings_save_one("modbus/unit_id", &unit_id, sizeof(unit_id));
} }

View File

@@ -0,0 +1 @@
zephyr_library_sources(shell_modbus.c)

View File

@@ -0,0 +1,5 @@
config SHELL_MODBUS
bool "Enable Shell Modbus"
default y
help
Enable the modnbus shell commands.

View File

@@ -1,7 +1,7 @@
#include <zephyr/shell/shell.h> #include <zephyr/shell/shell.h>
#include <stdlib.h> #include <stdlib.h>
#include <modbus_server.h> #include <lib/modbus_server.h>
#include <valve.h> #include <lib/valve.h>
static int cmd_modbus_set_baud(const struct shell *sh, size_t argc, char **argv) static int cmd_modbus_set_baud(const struct shell *sh, size_t argc, char **argv)
{ {

View File

@@ -0,0 +1 @@
zephyr_library_sources(shell_system.c)

View File

@@ -0,0 +1,5 @@
config SHELL_SYSTEM
bool "Enable Shell System"
default y
help
Enable the system commands.

View File

@@ -0,0 +1 @@
zephyr_library_sources(valve.c)

View File

@@ -1,5 +1,5 @@
config VALVE config LIB_VALVE
bool "Enable Valve Library" bool "Enable Valve Library"
default y default y
help help
Enable the Valve module. Enable the Valve Library.

View File

@@ -1,7 +1,7 @@
#include "valve.h"
#include <zephyr/kernel.h> #include <zephyr/kernel.h>
#include <zephyr/settings/settings.h> #include <zephyr/settings/settings.h>
#include <zephyr/logging/log.h> #include <zephyr/logging/log.h>
#include <lib/valve.h>
LOG_MODULE_REGISTER(valve, LOG_LEVEL_INF); LOG_MODULE_REGISTER(valve, LOG_LEVEL_INF);

View File

@@ -1,7 +0,0 @@
cmake_minimum_required(VERSION 3.13.1)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(fwu)
target_sources(app PRIVATE src/fwu.c)
target_include_directories(app PUBLIC include)

View File

@@ -1,7 +0,0 @@
cmake_minimum_required(VERSION 3.13.1)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(modbus_server)
target_sources(app PRIVATE src/modbus_server.c)
target_include_directories(app PUBLIC include)

View File

@@ -1,5 +0,0 @@
config MODBUS_SERVER
bool "Enable Modbus Server Library"
default y
help
Enable the Modbus Server module.

View File

@@ -1,7 +0,0 @@
cmake_minimum_required(VERSION 3.13.1)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(valve)
target_sources(app PRIVATE src/valve.c)
target_include_directories(app PUBLIC include)