diff --git a/software/.vscode/settings.json b/software/.vscode/settings.json index ff68cde..15b66f3 100644 --- a/software/.vscode/settings.json +++ b/software/.vscode/settings.json @@ -7,6 +7,9 @@ "modbus.h": "c", "array": "c", "string": "c", - "string_view": "c" + "string_view": "c", + "canbus.h": "c", + "kernel.h": "c", + "settings.h": "c" } } \ No newline at end of file diff --git a/software/boards/iten/valve_node/Kconfig.valve_node b/software/boards/iten/valve_node/Kconfig.valve_node index 07f18f3..a52f9b8 100644 --- a/software/boards/iten/valve_node/Kconfig.valve_node +++ b/software/boards/iten/valve_node/Kconfig.valve_node @@ -1,2 +1,18 @@ config BOARD_VALVE_NODE - select SOC_STM32F103XB \ No newline at end of file + select SOC_STM32F103XB + +mainmenu "Controller Area Network sample application" +config LOOPBACK_MODE + bool "Loopback LOOPBACK_MODE" + default n + help + Set the can controller to loopback mode. + This allows testing without a second board. + +config LOG_CAN_LEVEL + int "Log level for CAN" + default 3 + range 0 7 + help + Set the log level for CAN messages. + 0 = None, 1 = Error, 2 = Warning, 3 = Info, 4 = Debug, 5 = Trace, 6 = Debug2, 7 = Debug3 \ No newline at end of file diff --git a/software/boards/iten/valve_node/valve_node_defconfig b/software/boards/iten/valve_node/valve_node_defconfig index 3b057cd..8eb026e 100644 --- a/software/boards/iten/valve_node/valve_node_defconfig +++ b/software/boards/iten/valve_node/valve_node_defconfig @@ -12,4 +12,19 @@ CONFIG_GPIO=y CONFIG_UART_INTERRUPT_DRIVEN=y CONFIG_UART_LINE_CTRL=n CONFIG_MODBUS=y -CONFIG_MODBUS_ROLE_CLIENT=y \ No newline at end of file +CONFIG_MODBUS_ROLE_CLIENT=y + +# can config +CONFIG_CAN=y +CONFIG_CAN_INIT_PRIORITY=80 +#CONFIG_CAN_MAX_FILTER=5 + +# settings +CONFIG_FLASH=y +CONFIG_FLASH_MAP=y +CONFIG_SETTINGS=y +CONFIG_SETTINGS_RUNTIME=y +CONFIG_NVS=y +CONFIG_SETTINGS_NVS=y +CONFIG_HEAP_MEM_POOL_SIZE=256 +CONFIG_MPU_ALLOW_FLASH_WRITE=y diff --git a/software/lib/canbus.c b/software/lib/canbus.c new file mode 100644 index 0000000..5d1047d --- /dev/null +++ b/software/lib/canbus.c @@ -0,0 +1,75 @@ +#include "canbus.h" +#include +#include +#include "settings.h" + +const struct device *const can_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_canbus)); + +K_THREAD_STACK_DEFINE(rx_thread_stack, RX_THREAD_STACK_SIZE); + +static uint8_t node_id = 12; // Default node ID + +LOG_MODULE_REGISTER(canbus, CONFIG_LOG_CAN_LEVEL); + +static int canbus_set(const char *key, size_t len, settings_read_cb read_cb, void *cb_arg) +{ + // Handle setting values for CAN bus configuration + LOG_DBG("Setting CAN bus configuration: key=%s, len=%zu", key, len); + // Implement the logic to set the CAN bus configuration based on the key and value + return 0; // Return 0 on success +} + +struct settings_handler canbus_settings_handler = { + .name = "canbus", + .h_set = canbus_set, // No settings set handler +}; + +int canbus_init(void) +{ + int rc; + settings_subsys_init(); + settings_register(&canbus_settings_handler); + settings_load(); + + rc = settings_get_val_len("canbus/node_id"); + if (rc < 0) + { + LOG_ERR("Failed to check CAN bus settings: %d", rc); + return rc; + } + + else if (rc == 0) + { + LOG_WRN("No CAN bus node id found in settings, using default default (%d)", node_id); + settings_save_one("canbus/node_id", &node_id, sizeof(node_id)); + if (rc < 0) + { + LOG_ERR("Failed to save default CAN bus node id: %d", rc); + return rc; + } + } else { + rc = settings_load_one("canbus/node_id", &node_id, sizeof(node_id)); + if (rc < 0) + { + LOG_ERR("Failed to load CAN bus node id from settings: %d", rc); + return rc; + } + LOG_DBG("Loaded CAN bus node id: %d", node_id); + } + + if (!device_is_ready(can_dev)) + { + LOG_ERR("CAN device %s is not ready", can_dev->name); + return -ENODEV; + } + +#ifdef CONFIG_LOOPBACK_MODE + rc = can_set_mode(can_dev, CAN_MODE_LOOPBACK); + if (rc != 0) + { + LOG_ERR("Failed to set CAN loopback mode: %d", rc); + return rc; + } +#endif + return 0; +} \ No newline at end of file diff --git a/software/lib/canbus.h b/software/lib/canbus.h new file mode 100644 index 0000000..c58a404 --- /dev/null +++ b/software/lib/canbus.h @@ -0,0 +1,13 @@ +#ifndef __CANBUS_H__ +#define __CANBUS_H__ + +#include +#include + +#define RX_THREAD_STACK_SIZE 512 +#define RX_THREAD_PRIORITY 2 + +int canbus_init(void); +int canbus_send_message(const struct can_frame *frame); + +#endif // __CANBUS_H__ \ No newline at end of file diff --git a/software/lib/settings.h b/software/lib/settings.h new file mode 100644 index 0000000..523df6f --- /dev/null +++ b/software/lib/settings.h @@ -0,0 +1,17 @@ +#ifndef __SETTINGS_H__ +#define __SETTINGS_H__ + +#include + +#include +#include + +#if defined(CONFIG_SETTINGS_FILE) +#include +#include +#endif + +#define STORAGE_PARTITION storage_partition +#define STORAGE_PARTITION_ID FIXED_PARTITION_ID(STORAGE_PARTITION) + +#endif // __SETTINGS_H__ \ No newline at end of file diff --git a/software/test_canbus/.gitignore b/software/test_canbus/.gitignore new file mode 100644 index 0000000..378eac2 --- /dev/null +++ b/software/test_canbus/.gitignore @@ -0,0 +1 @@ +build diff --git a/software/test_canbus/.vscode/c_cpp_properties.json b/software/test_canbus/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..b1f27e8 --- /dev/null +++ b/software/test_canbus/.vscode/c_cpp_properties.json @@ -0,0 +1,16 @@ +{ + "configurations": [ + { + "name": "Win32", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [ + "_DEBUG", + "UNICODE", + "_UNICODE" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/software/test_canbus/CMakeLists.txt b/software/test_canbus/CMakeLists.txt new file mode 100644 index 0000000..3989c64 --- /dev/null +++ b/software/test_canbus/CMakeLists.txt @@ -0,0 +1,14 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +list(APPEND BOARD_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(hello_world) + +target_sources(app PRIVATE src/main.c) +target_sources(app PRIVATE ../lib/canbus.c) +target_include_directories(app PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/../lib +) diff --git a/software/test_canbus/boards/nucleo_f103rb.overlay b/software/test_canbus/boards/nucleo_f103rb.overlay new file mode 100644 index 0000000..a3e38dc --- /dev/null +++ b/software/test_canbus/boards/nucleo_f103rb.overlay @@ -0,0 +1,9 @@ +&usart1 { + status = "okay"; + current-speed = <9600>; + modbus0 { + compatible = "zephyr,modbus-serial"; + status = "okay"; + // de-gpios = <&arduino_header 15 GPIO_ACTIVE_LOW>; /* D9 */ + }; +}; \ No newline at end of file diff --git a/software/test_canbus/prj.conf b/software/test_canbus/prj.conf index 69162ba..b95fd27 100644 --- a/software/test_canbus/prj.conf +++ b/software/test_canbus/prj.conf @@ -6,8 +6,5 @@ CONFIG_UART_CONSOLE=y # Console on USART1 #CONFIG_RTT_CONSOLE=y #CONFIG_USE_SEGGER_RTT=y -CONFIG_UART_INTERRUPT_DRIVEN=y -CONFIG_UART_LINE_CTRL=n - -CONFIG_MODBUS=y -CONFIG_MODBUS_ROLE_CLIENT=y \ No newline at end of file +CONFIG_LOOPBACK_MODE=y +CONFIG_LOG_CAN_LEVEL=4 \ No newline at end of file diff --git a/software/test_canbus/src/main.c b/software/test_canbus/src/main.c new file mode 100644 index 0000000..eec6388 --- /dev/null +++ b/software/test_canbus/src/main.c @@ -0,0 +1,26 @@ +/* Testing MODBUS functionality + This code initializes a Modbus client, sets the zero point and the 2m point for water level, + and reads the water level in both meters and millimeters. + It uses the Zephyr RTOS and its Modbus library to communicate with a Modbus server. +*/ + +#include +#include +#include + +#include "canbus.h" + +LOG_MODULE_REGISTER(main, CONFIG_LOG_DEFAULT_LEVEL); + +int main(void) +{ + int rc; + rc = canbus_init(); + if (rc != 0) + { + LOG_ERR("Failed to initialize CAN bus: %d", rc); + return rc; + } + LOG_INF("CAN bus initialized successfully"); + return 0; +} diff --git a/software/test_modbus/prj.conf b/software/test_modbus/prj.conf index 69162ba..502f667 100644 --- a/software/test_modbus/prj.conf +++ b/software/test_modbus/prj.conf @@ -4,10 +4,4 @@ CONFIG_CBPRINTF_FP_SUPPORT=y CONFIG_UART_CONSOLE=y # Console on USART1 #CONFIG_RTT_CONSOLE=y -#CONFIG_USE_SEGGER_RTT=y - -CONFIG_UART_INTERRUPT_DRIVEN=y -CONFIG_UART_LINE_CTRL=n - -CONFIG_MODBUS=y -CONFIG_MODBUS_ROLE_CLIENT=y \ No newline at end of file +#CONFIG_USE_SEGGER_RTT=y \ No newline at end of file