startet canbus implementation
This commit is contained in:
75
software/lib/canbus.c
Normal file
75
software/lib/canbus.c
Normal file
@@ -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;
|
||||
}
|
||||
13
software/lib/canbus.h
Normal file
13
software/lib/canbus.h
Normal file
@@ -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__
|
||||
17
software/lib/settings.h
Normal file
17
software/lib/settings.h
Normal file
@@ -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__
|
||||
Reference in New Issue
Block a user