11 Commits

Author SHA1 Message Date
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
2a2890b675 Fix: hello_world prj.conf - set correct board name 2025-07-03 09:21:48 +02:00
38fd3a6aac Add hello_world Zephyr application for stm32g431_core 2025-07-03 09:15:11 +02:00
c3df6565b7 Refactor fwu library into a Zephyr module 2025-07-02 21:30:15 +02:00
140d2baa24 Fix: modbus_tool.py - replace is_connected() with is_socket_open() and fix UnboundLocalError 2025-07-02 21:05:40 +02:00
711341f362 Refactor slave_node application to use Zephyr modules 2025-07-02 20:47:16 +02:00
26 changed files with 214 additions and 55 deletions

8
software/CMakeLists.txt Normal file
View File

@@ -0,0 +1,8 @@
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/stm32g431_tests)

View File

@@ -0,0 +1,6 @@
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

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

View File

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

View File

@@ -6,14 +6,14 @@ list(APPEND BOARD_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(slave_node)
# Define a variable for the lib directory
set(LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../lib)
list(APPEND KCONFIG_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/../../modules/modbus_server/Kconfig)
list(APPEND KCONFIG_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/../../modules/valve/Kconfig)
list(APPEND KCONFIG_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/../../modules/fwu/Kconfig)
# Add the include directories for the libraries
target_include_directories(app PRIVATE
${LIB_DIR}/valve
${LIB_DIR}/modbus_server
${LIB_DIR}/fwu
${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
@@ -21,7 +21,7 @@ target_sources(app PRIVATE
src/main.c
src/shell_modbus.c
src/shell_system.c
${LIB_DIR}/valve/valve.c
${LIB_DIR}/modbus_server/modbus_server.c
${LIB_DIR}/fwu/fwu.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,6 @@
# Disable UART console
CONFIG_UART_CONSOLE=n
# Enable RTT console
CONFIG_RTT_CONSOLE=y
CONFIG_USE_SEGGER_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

@@ -2,13 +2,6 @@
CONFIG_CONSOLE=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
CONFIG_SHELL=y
CONFIG_SHELL_BACKEND_RTT=y
@@ -28,3 +21,4 @@ CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_MODBUS=y
CONFIG_MODBUS_ROLE_SERVER=y
CONFIG_MODBUS_BUFFER_SIZE=256

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,7 @@
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

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

View File

@@ -0,0 +1,7 @@
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

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

View File

@@ -1,12 +1,16 @@
#include <zephyr/kernel.h>
#include "modbus_server.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 "modbus_server.h"
#include "valve.h"
#include "fwu.h"
#include <zephyr/usb/usb_device.h>
LOG_MODULE_REGISTER(modbus_server, LOG_LEVEL_INF);
static int modbus_iface;
@@ -27,7 +31,8 @@ static void watchdog_timer_handler(struct k_timer *timer_id)
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);
}
}
@@ -35,11 +40,20 @@ static inline void reset_watchdog(void)
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;
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;
}
@@ -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)
{
reset_watchdog();
switch (addr) {
switch (addr)
{
case REG_HOLDING_VALVE_COMMAND:
if (reg == 1) { valve_open(); }
else if (reg == 2) { valve_close(); }
else if (reg == 0) { valve_stop(); }
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);
@@ -61,16 +85,20 @@ static int holding_reg_wr(uint16_t addr, uint16_t reg)
break;
case REG_HOLDING_WATCHDOG_TIMEOUT_S:
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);
reset_watchdog();
} else {
}
else
{
LOG_INF("Watchdog disabled.");
k_timer_stop(&watchdog_timer);
}
break;
case REG_HOLDING_DEVICE_RESET:
if (reg == 1) {
if (reg == 1)
{
LOG_WRN("Modbus reset command received. Rebooting...");
sys_reboot(SYS_REBOOT_WARM);
}
@@ -86,13 +114,32 @@ 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 = 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;
default: *reg = 0; break;
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_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 = 1;
break;
default:
*reg = 0;
break;
}
return 0;
}
@@ -109,8 +156,28 @@ int modbus_server_init(void)
{
k_timer_init(&watchdog_timer, watchdog_timer_handler, NULL);
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);
if (modbus_iface < 0) { return modbus_iface; }
if (modbus_iface < 0)
{
return modbus_iface;
}
server_param.server.user_cb = &mbs_cbs;
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);
if (ret == 0) {
if (ret == 0)
{
settings_save_one("modbus/baudrate", &baudrate, sizeof(baudrate));
settings_save_one("modbus/unit_id", &unit_id, sizeof(unit_id));
}

View File

@@ -0,0 +1,7 @@
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)

View File

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

View File

@@ -65,7 +65,7 @@ def poll_status(slave_id, interval):
new_data = {}
try:
if not client.is_connected():
if not client.is_socket_open():
reconnect_attempts += 1
if reconnect_attempts >= max_reconnect_attempts:
new_data["error"] = f"Failed to reconnect after {max_reconnect_attempts} attempts. Exiting."
@@ -286,9 +286,9 @@ def main_menu(stdscr, slave_id):
stdscr.addstr(h // 2 + 1, w // 2 - 25, msg.ljust(50))
else:
with status_lock: current_data = status_data.copy()
bold, normal = curses.color_pair(1) | curses.A_BOLD, curses.color_pair(1)
if current_data.get("error"): stdscr.addstr(0, 0, current_data["error"], curses.color_pair(3) | curses.A_BOLD)
else:
bold, normal = curses.color_pair(1) | curses.A_BOLD, curses.color_pair(1)
col1, col2, col3, col4 = 2, 30, 58, 88
stdscr.addstr(1, col1, "State:", bold); stdscr.addstr(1, col1 + 18, str(current_data.get('state', 'N/A')), normal)
stdscr.addstr(2, col1, "Movement:", bold); stdscr.addstr(2, col1 + 18, str(current_data.get('movement', 'N/A')), normal)