From 6c1ff0c4dff493dae40f7b8c7e5efa01c41e3393 Mon Sep 17 00:00:00 2001 From: Eduard Iten Date: Thu, 3 Jul 2025 16:58:43 +0200 Subject: [PATCH] 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. --- .vscode/settings.json | 5 ++++ lib/fwu/CMakeLists.txt | 6 +++++ lib/modbus_server/CMakeLists.txt | 6 +++++ lib/valve/CMakeLists.txt | 6 +++++ software/CMakeLists.txt | 8 ------ software/Kconfig | 1 + software/apps/slave_node/CMakeLists.txt | 27 +++---------------- software/apps/slave_node/Kconfig | 2 ++ .../slave_node/boards/bluepill_f103rb.conf | 3 ++- software/apps/slave_node/prj.conf | 1 - software/apps/slave_node/src/main.c | 6 ++--- .../fwu/include => include/lib}/fwu.h | 0 .../include => include/lib}/modbus_server.h | 0 .../valve/include => include/lib}/valve.h | 0 software/lib/CMakeLists.txt | 5 ++++ software/lib/Kconfig | 8 ++++++ software/lib/fwu/CMakeLists.txt | 1 + software/{modules => lib}/fwu/Kconfig | 4 +-- software/{modules/fwu/src => lib/fwu}/fwu.c | 2 +- software/lib/modbus_server/CMakeLists.txt | 1 + software/lib/modbus_server/Kconfig | 5 ++++ .../src => lib/modbus_server}/modbus_server.c | 8 +++--- software/lib/shell_modbus/CMakeLists.txt | 1 + software/lib/shell_modbus/Kconfig | 5 ++++ .../src => lib/shell_modbus}/shell_modbus.c | 4 +-- software/lib/shell_system/CMakeLists.txt | 1 + software/lib/shell_system/Kconfig | 5 ++++ .../src => lib/shell_system}/shell_system.c | 0 software/lib/valve/CMakeLists.txt | 1 + software/{modules => lib}/valve/Kconfig | 4 +-- .../{modules/valve/src => lib/valve}/valve.c | 2 +- software/modules/fwu/CMakeLists.txt | 7 ----- software/modules/modbus_server/CMakeLists.txt | 7 ----- software/modules/modbus_server/Kconfig | 5 ---- software/modules/valve/CMakeLists.txt | 7 ----- 35 files changed, 80 insertions(+), 74 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 lib/fwu/CMakeLists.txt create mode 100644 lib/modbus_server/CMakeLists.txt create mode 100644 lib/valve/CMakeLists.txt delete mode 100644 software/CMakeLists.txt create mode 100644 software/Kconfig create mode 100644 software/apps/slave_node/Kconfig rename software/{modules/fwu/include => include/lib}/fwu.h (100%) rename software/{modules/modbus_server/include => include/lib}/modbus_server.h (100%) rename software/{modules/valve/include => include/lib}/valve.h (100%) create mode 100644 software/lib/CMakeLists.txt create mode 100644 software/lib/Kconfig create mode 100644 software/lib/fwu/CMakeLists.txt rename software/{modules => lib}/fwu/Kconfig (53%) rename software/{modules/fwu/src => lib/fwu}/fwu.c (98%) create mode 100644 software/lib/modbus_server/CMakeLists.txt create mode 100644 software/lib/modbus_server/Kconfig rename software/{modules/modbus_server/src => lib/modbus_server}/modbus_server.c (97%) create mode 100644 software/lib/shell_modbus/CMakeLists.txt create mode 100644 software/lib/shell_modbus/Kconfig rename software/{apps/slave_node/src => lib/shell_modbus}/shell_modbus.c (98%) create mode 100644 software/lib/shell_system/CMakeLists.txt create mode 100644 software/lib/shell_system/Kconfig rename software/{apps/slave_node/src => lib/shell_system}/shell_system.c (100%) create mode 100644 software/lib/valve/CMakeLists.txt rename software/{modules => lib}/valve/Kconfig (52%) rename software/{modules/valve/src => lib/valve}/valve.c (99%) delete mode 100644 software/modules/fwu/CMakeLists.txt delete mode 100644 software/modules/modbus_server/CMakeLists.txt delete mode 100644 software/modules/modbus_server/Kconfig delete mode 100644 software/modules/valve/CMakeLists.txt diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..62b6785 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "fwu.h": "c" + } +} \ No newline at end of file diff --git a/lib/fwu/CMakeLists.txt b/lib/fwu/CMakeLists.txt new file mode 100644 index 0000000..a754440 --- /dev/null +++ b/lib/fwu/CMakeLists.txt @@ -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) diff --git a/lib/modbus_server/CMakeLists.txt b/lib/modbus_server/CMakeLists.txt new file mode 100644 index 0000000..2b76d4e --- /dev/null +++ b/lib/modbus_server/CMakeLists.txt @@ -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) diff --git a/lib/valve/CMakeLists.txt b/lib/valve/CMakeLists.txt new file mode 100644 index 0000000..68589cb --- /dev/null +++ b/lib/valve/CMakeLists.txt @@ -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) diff --git a/software/CMakeLists.txt b/software/CMakeLists.txt deleted file mode 100644 index a032c4a..0000000 --- a/software/CMakeLists.txt +++ /dev/null @@ -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/stm32g431_tests) diff --git a/software/Kconfig b/software/Kconfig new file mode 100644 index 0000000..25ba986 --- /dev/null +++ b/software/Kconfig @@ -0,0 +1 @@ +rsource "lib/Kconfig" diff --git a/software/apps/slave_node/CMakeLists.txt b/software/apps/slave_node/CMakeLists.txt index 8398d7f..8ce3635 100644 --- a/software/apps/slave_node/CMakeLists.txt +++ b/software/apps/slave_node/CMakeLists.txt @@ -1,27 +1,8 @@ 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}) -project(slave_node) -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) - -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 -) \ No newline at end of file +project(slave_node LANGUAGES C) +zephyr_include_directories(../../include) +add_subdirectory(../../lib lib) +target_sources(app PRIVATE src/main.c) \ No newline at end of file diff --git a/software/apps/slave_node/Kconfig b/software/apps/slave_node/Kconfig new file mode 100644 index 0000000..8a35138 --- /dev/null +++ b/software/apps/slave_node/Kconfig @@ -0,0 +1,2 @@ +rsource "../../lib/Kconfig" +source "Kconfig.zephyr" \ No newline at end of file diff --git a/software/apps/slave_node/boards/bluepill_f103rb.conf b/software/apps/slave_node/boards/bluepill_f103rb.conf index 975165e..17e55de 100644 --- a/software/apps/slave_node/boards/bluepill_f103rb.conf +++ b/software/apps/slave_node/boards/bluepill_f103rb.conf @@ -3,4 +3,5 @@ CONFIG_UART_CONSOLE=n # Enable RTT console CONFIG_RTT_CONSOLE=y -CONFIG_USE_SEGGER_RTT=y \ No newline at end of file +CONFIG_USE_SEGGER_RTT=y +CONFIG_SHELL_BACKEND_RTT=y \ No newline at end of file diff --git a/software/apps/slave_node/prj.conf b/software/apps/slave_node/prj.conf index 6a66456..6002c6c 100644 --- a/software/apps/slave_node/prj.conf +++ b/software/apps/slave_node/prj.conf @@ -4,7 +4,6 @@ CONFIG_LOG=y # Enable Shell CONFIG_SHELL=y -CONFIG_SHELL_BACKEND_RTT=y CONFIG_REBOOT=y # Enable Settings Subsystem diff --git a/software/apps/slave_node/src/main.c b/software/apps/slave_node/src/main.c index f25c621..37ba733 100644 --- a/software/apps/slave_node/src/main.c +++ b/software/apps/slave_node/src/main.c @@ -1,9 +1,9 @@ #include #include #include -#include -#include -#include +#include +#include +#include LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); diff --git a/software/modules/fwu/include/fwu.h b/software/include/lib/fwu.h similarity index 100% rename from software/modules/fwu/include/fwu.h rename to software/include/lib/fwu.h diff --git a/software/modules/modbus_server/include/modbus_server.h b/software/include/lib/modbus_server.h similarity index 100% rename from software/modules/modbus_server/include/modbus_server.h rename to software/include/lib/modbus_server.h diff --git a/software/modules/valve/include/valve.h b/software/include/lib/valve.h similarity index 100% rename from software/modules/valve/include/valve.h rename to software/include/lib/valve.h diff --git a/software/lib/CMakeLists.txt b/software/lib/CMakeLists.txt new file mode 100644 index 0000000..07303c6 --- /dev/null +++ b/software/lib/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/software/lib/Kconfig b/software/lib/Kconfig new file mode 100644 index 0000000..1e2390b --- /dev/null +++ b/software/lib/Kconfig @@ -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 \ No newline at end of file diff --git a/software/lib/fwu/CMakeLists.txt b/software/lib/fwu/CMakeLists.txt new file mode 100644 index 0000000..5a55591 --- /dev/null +++ b/software/lib/fwu/CMakeLists.txt @@ -0,0 +1 @@ +zephyr_library_sources(fwu.c) diff --git a/software/modules/fwu/Kconfig b/software/lib/fwu/Kconfig similarity index 53% rename from software/modules/fwu/Kconfig rename to software/lib/fwu/Kconfig index 5256448..d82fc5d 100644 --- a/software/modules/fwu/Kconfig +++ b/software/lib/fwu/Kconfig @@ -1,5 +1,5 @@ -config FWU +config LIB_FWU bool "Enable Firmware Update Library" default y help - Enable the Firmware Update module. + Enable the Firmware Update Library. diff --git a/software/modules/fwu/src/fwu.c b/software/lib/fwu/fwu.c similarity index 98% rename from software/modules/fwu/src/fwu.c rename to software/lib/fwu/fwu.c index 5a9432c..27a600f 100644 --- a/software/modules/fwu/src/fwu.c +++ b/software/lib/fwu/fwu.c @@ -1,8 +1,8 @@ -#include "fwu.h" #include #include #include #include +#include LOG_MODULE_REGISTER(fwu, LOG_LEVEL_INF); diff --git a/software/lib/modbus_server/CMakeLists.txt b/software/lib/modbus_server/CMakeLists.txt new file mode 100644 index 0000000..1426d94 --- /dev/null +++ b/software/lib/modbus_server/CMakeLists.txt @@ -0,0 +1 @@ +zephyr_library_sources(modbus_server.c) diff --git a/software/lib/modbus_server/Kconfig b/software/lib/modbus_server/Kconfig new file mode 100644 index 0000000..0a5339d --- /dev/null +++ b/software/lib/modbus_server/Kconfig @@ -0,0 +1,5 @@ +config LIB_MODBUS_SERVER + bool "Enable Modbus Server Library" + default y + help + Enable the Modbus Server Library. diff --git a/software/modules/modbus_server/src/modbus_server.c b/software/lib/modbus_server/modbus_server.c similarity index 97% rename from software/modules/modbus_server/src/modbus_server.c rename to software/lib/modbus_server/modbus_server.c index 481f38d..76f2f7a 100644 --- a/software/modules/modbus_server/src/modbus_server.c +++ b/software/lib/modbus_server/modbus_server.c @@ -5,9 +5,9 @@ #include #include #include -#include "modbus_server.h" -#include "valve.h" -#include "fwu.h" +#include +#include +#include #include @@ -135,7 +135,7 @@ static int input_reg_rd(uint16_t addr, uint16_t *reg) *reg = (0 << 8) | 0; break; case REG_INPUT_FIRMWARE_VERSION_PATCH: - *reg = 1; + *reg = 2; break; default: *reg = 0; diff --git a/software/lib/shell_modbus/CMakeLists.txt b/software/lib/shell_modbus/CMakeLists.txt new file mode 100644 index 0000000..75c8c3b --- /dev/null +++ b/software/lib/shell_modbus/CMakeLists.txt @@ -0,0 +1 @@ +zephyr_library_sources(shell_modbus.c) \ No newline at end of file diff --git a/software/lib/shell_modbus/Kconfig b/software/lib/shell_modbus/Kconfig new file mode 100644 index 0000000..7d5bc9f --- /dev/null +++ b/software/lib/shell_modbus/Kconfig @@ -0,0 +1,5 @@ +config SHELL_MODBUS + bool "Enable Shell Modbus" + default y + help + Enable the modnbus shell commands. \ No newline at end of file diff --git a/software/apps/slave_node/src/shell_modbus.c b/software/lib/shell_modbus/shell_modbus.c similarity index 98% rename from software/apps/slave_node/src/shell_modbus.c rename to software/lib/shell_modbus/shell_modbus.c index db29788..d722752 100644 --- a/software/apps/slave_node/src/shell_modbus.c +++ b/software/lib/shell_modbus/shell_modbus.c @@ -1,7 +1,7 @@ #include #include -#include -#include +#include +#include static int cmd_modbus_set_baud(const struct shell *sh, size_t argc, char **argv) { diff --git a/software/lib/shell_system/CMakeLists.txt b/software/lib/shell_system/CMakeLists.txt new file mode 100644 index 0000000..b0f8b97 --- /dev/null +++ b/software/lib/shell_system/CMakeLists.txt @@ -0,0 +1 @@ +zephyr_library_sources(shell_system.c) \ No newline at end of file diff --git a/software/lib/shell_system/Kconfig b/software/lib/shell_system/Kconfig new file mode 100644 index 0000000..08e88e3 --- /dev/null +++ b/software/lib/shell_system/Kconfig @@ -0,0 +1,5 @@ +config SHELL_SYSTEM + bool "Enable Shell System" + default y + help + Enable the system commands. \ No newline at end of file diff --git a/software/apps/slave_node/src/shell_system.c b/software/lib/shell_system/shell_system.c similarity index 100% rename from software/apps/slave_node/src/shell_system.c rename to software/lib/shell_system/shell_system.c diff --git a/software/lib/valve/CMakeLists.txt b/software/lib/valve/CMakeLists.txt new file mode 100644 index 0000000..9c87cd1 --- /dev/null +++ b/software/lib/valve/CMakeLists.txt @@ -0,0 +1 @@ +zephyr_library_sources(valve.c) \ No newline at end of file diff --git a/software/modules/valve/Kconfig b/software/lib/valve/Kconfig similarity index 52% rename from software/modules/valve/Kconfig rename to software/lib/valve/Kconfig index 2922669..ba26cb1 100644 --- a/software/modules/valve/Kconfig +++ b/software/lib/valve/Kconfig @@ -1,5 +1,5 @@ -config VALVE +config LIB_VALVE bool "Enable Valve Library" default y help - Enable the Valve module. + Enable the Valve Library. diff --git a/software/modules/valve/src/valve.c b/software/lib/valve/valve.c similarity index 99% rename from software/modules/valve/src/valve.c rename to software/lib/valve/valve.c index 48e535f..72c3a92 100644 --- a/software/modules/valve/src/valve.c +++ b/software/lib/valve/valve.c @@ -1,7 +1,7 @@ -#include "valve.h" #include #include #include +#include LOG_MODULE_REGISTER(valve, LOG_LEVEL_INF); diff --git a/software/modules/fwu/CMakeLists.txt b/software/modules/fwu/CMakeLists.txt deleted file mode 100644 index 84df04d..0000000 --- a/software/modules/fwu/CMakeLists.txt +++ /dev/null @@ -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) diff --git a/software/modules/modbus_server/CMakeLists.txt b/software/modules/modbus_server/CMakeLists.txt deleted file mode 100644 index cd18413..0000000 --- a/software/modules/modbus_server/CMakeLists.txt +++ /dev/null @@ -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) diff --git a/software/modules/modbus_server/Kconfig b/software/modules/modbus_server/Kconfig deleted file mode 100644 index fc41cdf..0000000 --- a/software/modules/modbus_server/Kconfig +++ /dev/null @@ -1,5 +0,0 @@ -config MODBUS_SERVER - bool "Enable Modbus Server Library" - default y - help - Enable the Modbus Server module. diff --git a/software/modules/valve/CMakeLists.txt b/software/modules/valve/CMakeLists.txt deleted file mode 100644 index 570eff4..0000000 --- a/software/modules/valve/CMakeLists.txt +++ /dev/null @@ -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)