Compare commits
2 Commits
dc83c27306
...
ade514cbf3
| Author | SHA1 | Date |
|---|---|---|
|
|
ade514cbf3 | |
|
|
bd42d85783 |
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
@ -1,2 +1,18 @@
|
|||
config BOARD_VALVE_NODE
|
||||
select SOC_STM32F103XB
|
||||
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
|
||||
|
|
@ -7,3 +7,24 @@ CONFIG_CONSOLE=y
|
|||
|
||||
# enable GPIO
|
||||
CONFIG_GPIO=y
|
||||
|
||||
# modbus config
|
||||
CONFIG_UART_INTERRUPT_DRIVEN=y
|
||||
CONFIG_UART_LINE_CTRL=n
|
||||
CONFIG_MODBUS=y
|
||||
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
|
||||
|
|
|
|||
|
|
@ -0,0 +1,75 @@
|
|||
#include "canbus.h"
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#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;
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef __CANBUS_H__
|
||||
#define __CANBUS_H__
|
||||
|
||||
#include <zephyr/drivers/can.h>
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
#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__
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef __SETTINGS_H__
|
||||
#define __SETTINGS_H__
|
||||
|
||||
#include <zephyr/settings/settings.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <zephyr/sys/printk.h>
|
||||
|
||||
#if defined(CONFIG_SETTINGS_FILE)
|
||||
#include <zephyr/fs/fs.h>
|
||||
#include <zephyr/fs/littlefs.h>
|
||||
#endif
|
||||
|
||||
#define STORAGE_PARTITION storage_partition
|
||||
#define STORAGE_PARTITION_ID FIXED_PARTITION_ID(STORAGE_PARTITION)
|
||||
|
||||
#endif // __SETTINGS_H__
|
||||
|
|
@ -0,0 +1 @@
|
|||
build
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Win32",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**"
|
||||
],
|
||||
"defines": [
|
||||
"_DEBUG",
|
||||
"UNICODE",
|
||||
"_UNICODE"
|
||||
]
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
|
|
@ -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
|
||||
)
|
||||
|
|
@ -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 */
|
||||
};
|
||||
};
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
CONFIG_LOG=y
|
||||
CONFIG_LOG_DEFAULT_LEVEL=3
|
||||
CONFIG_CBPRINTF_FP_SUPPORT=y
|
||||
|
||||
CONFIG_UART_CONSOLE=y # Console on USART1
|
||||
#CONFIG_RTT_CONSOLE=y
|
||||
#CONFIG_USE_SEGGER_RTT=y
|
||||
|
||||
CONFIG_LOOPBACK_MODE=y
|
||||
CONFIG_LOG_CAN_LEVEL=4
|
||||
|
|
@ -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 <zephyr/logging/log.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
|
@ -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
|
||||
#CONFIG_USE_SEGGER_RTT=y
|
||||
Loading…
Reference in New Issue