Switch to ThingSet SDK

This commit is contained in:
Martin Jäger
2026-03-09 08:18:21 +01:00
parent 167fc5d3ac
commit b59cede24f
29 changed files with 900 additions and 1929 deletions
-60
View File
@@ -325,64 +325,8 @@ config UEXT_OLED_BRIGHTNESS
range 1 255
default 1
config UEXT_SERIAL_THINGSET
depends on THINGSET_SERIAL
bool "Use UEXT serial for ThingSet interface"
default y
help
If the ThingSet interface is not attached to the serial port in the UEXT connector, the
serial in the LS.one port or the debug connector is used (if existing).
endmenu # UEXT port
menu "ThingSet interfaces"
config THINGSET_SERIAL
bool "ThingSet serial interface"
config THINGSET_SERIAL_RX_BUF_SIZE
depends on THINGSET_SERIAL
int "ThingSet serial RX buffer size"
range 64 2048
default 512
config THINGSET_SERIAL_TX_BUF_SIZE
depends on THINGSET_SERIAL
int "ThingSet serial TX buffer size"
range 256 2048
default 1024
config THINGSET_SERIAL_PUB_DEFAULT
bool "Enable serial publication messages at startup"
depends on THINGSET_SERIAL
default y
config THINGSET_CAN
depends on CAN
bool "ThingSet CAN interface"
config THINGSET_CAN_PUB_DEFAULT
bool "Enable CAN publication messages at startup"
depends on THINGSET_CAN
default y
config THINGSET_CAN_DEFAULT_NODE_ID
depends on THINGSET_CAN
int "ThingSet CAN default node ID"
range 0 255
default 20
config THINGSET_EXPERT_PASSWORD
string "ThingSet expert user password"
default "expert123"
config THINGSET_MAKER_PASSWORD
string "ThingSet maker password"
default "maker456"
endmenu # ThingSet interfaces
menu "Logging setup"
config CAN_LOG_LEVEL
@@ -473,9 +417,5 @@ endif
# include custom Kconfig if existing
orsource "../src/custom/Kconfig"
#if 0
#orsource "modules/thingset/zephyr/Kconfig.thingset"
#endif
# include main Zephyr menu entries from Zephyr root directory
source "Kconfig.zephyr"
+8
View File
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_FLASH_LOG_LEVEL_WRN=y
CONFIG_NVS=y
CONFIG_NVS_LOG_LEVEL_WRN=y
+2 -4
View File
@@ -1,10 +1,8 @@
/*
* Copyright (c) 2019 STMicroelectronics.
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
*
* This is a dummy charge controller board for testing new features using the STM32G474 MCU
*
* IMPORTANT: You have to remove the VREF jumper on the Nucleo board to make it work. Otherwise
+8 -7
View File
@@ -29,16 +29,17 @@ CONFIG_LOG=y
CONFIG_LOG_MODE_MINIMAL=y
CONFIG_THINGSET=y
CONFIG_THINGSET_NESTED_JSON=y
# ThingSet protocol interface via UART serial
CONFIG_THINGSET_SDK=y
CONFIG_THINGSET_STORAGE=y
CONFIG_THINGSET_SERIAL=y
CONFIG_THINGSET_STORAGE_DATA_VERSION=6
CONFIG_THINGSET_NODE_NAME="Libre Solar MPPT"
# Must be lower than LED thread to avoid flickering in boards with charlieplexing
CONFIG_THINGSET_SDK_THREAD_PRIORITY=6
# Uncomment to disable regular data publication on ThingSet serial at startup
#CONFIG_THINGSET_SERIAL_PUB_DEFAULT=n
# Uncomment to use LS.one or debug RX/TX connector instead of UEXT serial for ThingSet
#CONFIG_UEXT_SERIAL_THINGSET=n
#CONFIG_THINGSET_REPORTING_LIVE_ENABLE_PRESET=n
# Uncomment to enable I2C OLED display in UEXT connector
#CONFIG_UEXT_OLED_DISPLAY=y
+5 -6
View File
@@ -4,14 +4,13 @@ zephyr_include_directories(.)
target_sources(app PRIVATE
bat_charger.cpp
data_objects.cpp
data_storage.cpp
daq.cpp
daq_driver.c
device_status.cpp
dcdc.cpp
half_bridge.c
hardware.cpp
oled.cpp
leds.cpp
load.cpp
load_driver.c
@@ -22,14 +21,14 @@ target_sources(app PRIVATE
setup.cpp
)
add_subdirectory(ext)
if(${CONFIG_CUSTOM_DATA_OBJECTS_FILE})
target_sources(app PRIVATE data_objects_custom.cpp)
target_sources(app PRIVATE data_objects_custom.cpp)
else()
target_sources(app PRIVATE data_objects.cpp)
endif()
# Using this option enables user-specific customization of the firmware. The
# custom subfolder should have its own CMakeLists.txt file.
if(${CONFIG_CUSTOMIZATION})
add_subdirectory(custom build/custom)
add_subdirectory(custom build/custom)
endif()
+811 -1008
View File
File diff suppressed because it is too large Load Diff
+7 -51
View File
@@ -19,35 +19,13 @@
* Groups / first layer data object IDs
*/
#define ID_ROOT 0x00
#define ID_DEVICE 0x01
#define ID_BATTERY 0x02
#define ID_CHARGER 0x03
#define ID_SOLAR 0x04
#define ID_LOAD 0x05
#define ID_USB 0x06
#define ID_NANOGRID 0x07
#define ID_DFU 0x0F
#define ID_PUB 0x100
#define ID_CTRL 0x8000
/*
* Subset definitions for statements and publish/subscribe
*/
#define SUBSET_SER (1U << 0) // UART serial
#define SUBSET_CAN (1U << 1) // CAN bus
#define SUBSET_NVM (1U << 2) // data that should be stored in EEPROM
#define SUBSET_CTRL (1U << 3) // control data sent and received via CAN
/*
* Data object versioning for EEPROM
*
* Increment the version number each time any data object IDs stored in NVM are changed. Otherwise
* data might get corrupted.
*/
#define DATA_OBJECTS_VERSION 05
extern bool pub_serial_enable;
extern bool pub_can_enable;
#define ID_DEVICE 0x04
#define ID_BATTERY 0x05
#define ID_CHARGER 0x06
#define ID_SOLAR 0x07
#define ID_LOAD 0x08
#define ID_USB 0x09
#define ID_NANOGRID 0x0A
/**
* Callback function to be called when conf values were changed
@@ -59,26 +37,4 @@ void data_objects_update_conf();
*/
void data_objects_init();
/**
* Callback to provide authentication mechanism via ThingSet
*/
void thingset_auth();
/**
* Alphabet used for base32 encoding
*
* https://en.wikipedia.org/wiki/Base32#Crockford's_Base32
*/
const char alphabet_crockford[] = "0123456789abcdefghjkmnpqrstvwxyz";
/**
* Convert numeric device ID to base32 string
*/
void uint64_to_base32(uint64_t in, char *out, size_t size, const char *alphabet);
/**
* Update control values received via CAN
*/
void update_control();
#endif /* DATA_OBJECTS_H */
-290
View File
@@ -1,290 +0,0 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "data_storage.h"
#include <zephyr/kernel.h>
#include "data_objects.h"
#include "helper.h"
#include "mcu.h"
#include "thingset.h"
#include "zephyr/toolchain.h"
#include <stdio.h>
#ifdef CONFIG_SOC_FAMILY_STM32
#include <stm32_ll_bus.h>
#include <zephyr/device.h>
LOG_MODULE_REGISTER(nvs, CONFIG_DATA_STORAGE_LOG_LEVEL);
K_MUTEX_DEFINE(data_buf_lock);
// Buffer used by store and restore functions (must be word-aligned for hardware CRC calculation)
__maybe_unused static uint8_t buf[512] __aligned(sizeof(uint32_t));
extern ThingSet ts;
uint32_t _calc_crc(const uint8_t *buf, size_t len)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_CRC);
// we keep standard polynomial 0x04C11DB7 (same for STM32L0 and STM32F0)
// and we don't care for endianness here
CRC->CR |= CRC_CR_RESET;
for (size_t i = 0; i < len; i += 4) {
// printf("CRC buf: %.8x, CRC->DR: %.8x\n", *((uint32_t*)&buf[i]), CRC->DR);
size_t remaining_bytes = len - i;
if (remaining_bytes >= 4) {
CRC->DR = *((uint32_t *)&buf[i]);
}
else {
// ignore bytes >= len if len is not a multiple of 4
CRC->DR = *((uint32_t *)&buf[i]) & (0xFFFFFFFFU >> (32 - remaining_bytes * 8));
}
}
LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_CRC);
return CRC->DR;
}
#endif /* UNIT_TEST */
#ifdef CONFIG_EEPROM
#include <zephyr/drivers/eeprom.h>
/*
* EEPROM header bytes:
* - 0-1: Data objects version number
* - 2-3: Number of data bytes
* - 4-7: CRC32
*
* Data starts from byte 8
*/
#define EEPROM_HEADER_SIZE 8
static const struct device *eeprom_dev = DEVICE_DT_GET(DT_NODELABEL(eeprom));
void data_storage_read()
{
int err;
if (!device_is_ready(eeprom_dev)) {
LOG_ERR("EEPROM device not ready");
return;
}
uint8_t buf_header[EEPROM_HEADER_SIZE] = {};
err = eeprom_read(eeprom_dev, 0, buf_header, EEPROM_HEADER_SIZE);
if (err != 0) {
LOG_ERR("EEPROM read error %d", err);
return;
}
uint16_t version = *((uint16_t *)&buf_header[0]);
uint16_t len = *((uint16_t *)&buf_header[2]);
uint32_t crc = *((uint32_t *)&buf_header[4]);
LOG_DBG("EEPROM header restore: ver %d, len %d, CRC %.8x", version, len, (unsigned int)crc);
LOG_DBG("Header: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x", buf_header[0], buf_header[1],
buf_header[2], buf_header[3], buf_header[4], buf_header[5], buf_header[6],
buf_header[7]);
if (version == DATA_OBJECTS_VERSION && len <= sizeof(buf)) {
k_mutex_lock(&data_buf_lock, K_FOREVER);
err = eeprom_read(eeprom_dev, EEPROM_HEADER_SIZE, buf, len);
// printf("Data (len=%d): ", len);
// for (int i = 0; i < len; i++) printf("%.2x ", buf[i]);
if (_calc_crc(buf, len) == crc) {
int status = ts.bin_import(buf, sizeof(buf), TS_WRITE_MASK, SUBSET_NVM);
LOG_INF("EEPROM read and data objects updated, ThingSet result: 0x%x", status);
}
else {
LOG_ERR("EEPROM data CRC invalid, expected 0x%x (data_len = %d)", (unsigned int)crc,
len);
}
k_mutex_unlock(&data_buf_lock);
}
else {
LOG_INF("EEPROM empty or data layout version changed");
}
}
void data_storage_write()
{
int err;
if (!device_is_ready(eeprom_dev)) {
LOG_ERR("EEPROM device not ready");
return;
}
k_mutex_lock(&data_buf_lock, K_FOREVER);
int len = ts.bin_export(buf + EEPROM_HEADER_SIZE, sizeof(buf) - EEPROM_HEADER_SIZE, SUBSET_NVM);
uint32_t crc = _calc_crc(buf + EEPROM_HEADER_SIZE, len);
*((uint16_t *)&buf[0]) = (uint16_t)DATA_OBJECTS_VERSION;
*((uint16_t *)&buf[2]) = (uint16_t)(len);
*((uint32_t *)&buf[4]) = crc;
// printf("Data (len=%d): ", len);
// for (int i = 0; i < len; i++) printf("%.2x ", buf[i + EEPROM_HEADER_SIZE]);
LOG_DBG("Header: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x", buf[0], buf[1], buf[2], buf[3],
buf[4], buf[5], buf[6], buf[7]);
if (len == 0) {
LOG_ERR("EEPROM data could not be stored. ThingSet error (len = %d)", len);
}
else {
err = eeprom_write(eeprom_dev, 0, buf, len + EEPROM_HEADER_SIZE);
if (err == 0) {
LOG_INF("EEPROM data successfully stored");
}
else {
LOG_ERR("EEPROM write error %d", err);
}
}
k_mutex_unlock(&data_buf_lock);
}
#elif defined(CONFIG_NVS)
#include <zephyr/drivers/flash.h>
#include <zephyr/fs/nvs.h>
#include <zephyr/storage/flash_map.h>
/*
* NVS header bytes:
* - 0-1: Data objects version number
*
* Data starts from byte 2
*/
#define NVS_HEADER_SIZE 2
#define NVS_PARTITION storage_partition
#define THINGSET_DATA_ID 1
static struct nvs_fs fs;
static bool nvs_initialized = false;
static int data_storage_init()
{
int err;
struct flash_pages_info page_info;
fs.flash_device = FIXED_PARTITION_DEVICE(NVS_PARTITION);
if (!device_is_ready(fs.flash_device)) {
LOG_ERR("Flash device not ready");
return -ENODEV;
}
fs.offset = FIXED_PARTITION_OFFSET(NVS_PARTITION);
err = flash_get_page_info_by_offs(fs.flash_device, fs.offset, &page_info);
if (err) {
LOG_ERR("Unable to get flash page info");
return err;
}
fs.sector_size = page_info.size;
fs.sector_count = FIXED_PARTITION_SIZE(NVS_PARTITION) / page_info.size;
err = nvs_mount(&fs);
if (err) {
LOG_ERR("NVS mount failed: %d", err);
return err;
}
nvs_initialized = true;
return 0;
}
void data_storage_read()
{
if (!nvs_initialized) {
int err = data_storage_init();
if (err) {
return;
}
}
int num_bytes = nvs_read(&fs, THINGSET_DATA_ID, &buf, sizeof(buf));
if (num_bytes < 0) {
LOG_INF("NVS empty (read error %d)", num_bytes);
return;
}
uint16_t version = *((uint16_t *)&buf[0]);
if (version == DATA_OBJECTS_VERSION) {
k_mutex_lock(&data_buf_lock, K_FOREVER);
int status = ts.bin_import(buf + NVS_HEADER_SIZE, num_bytes - NVS_HEADER_SIZE,
TS_WRITE_MASK, SUBSET_NVM);
LOG_INF("NVS read and data objects updated, ThingSet result: 0x%x", status);
k_mutex_unlock(&data_buf_lock);
}
else {
LOG_INF("NVS data layout version changed");
}
}
void data_storage_write()
{
if (!nvs_initialized) {
int err = data_storage_init();
if (err) {
return;
}
}
k_mutex_lock(&data_buf_lock, K_FOREVER);
*((uint16_t *)&buf[0]) = (uint16_t)DATA_OBJECTS_VERSION;
int len = ts.bin_export(buf + NVS_HEADER_SIZE, sizeof(buf) - NVS_HEADER_SIZE, SUBSET_NVM);
if (len == 0) {
LOG_ERR("NVS data could not be stored. ThingSet error (len = %d)", len);
}
else {
int ret = nvs_write(&fs, THINGSET_DATA_ID, &buf, len + NVS_HEADER_SIZE);
if (ret == len + NVS_HEADER_SIZE) {
LOG_DBG("NVS data successfully stored");
}
else if (ret == 0) {
LOG_DBG("NVS data unchanged");
}
else {
LOG_ERR("NVS write error %d", ret);
}
}
k_mutex_unlock(&data_buf_lock);
}
#else
void data_storage_write()
{}
void data_storage_read()
{}
#endif
void data_storage_update()
{
if (uptime() % DATA_UPDATE_INTERVAL == 0 && uptime() > 0) {
data_storage_write();
}
}
-33
View File
@@ -1,33 +0,0 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef DATA_STORAGE_H_
#define DATA_STORAGE_H_
#define DATA_UPDATE_INTERVAL (6 * 60 * 60) // update every 6 hours
/**
* @file
*
* @brief Handling of internal or external EEPROM to store device configuration
*/
/**
* Store current charge controller data to EEPROM
*/
void data_storage_write();
/**
* Restore charge controller data from EEPROM and write to variables in RAM
*/
void data_storage_read();
/**
* Stores data to EEPROM every 6 hours (can be called regularly)
*/
void data_storage_update();
#endif /* DATA_STORAGE_H_ */
+5 -2
View File
@@ -15,12 +15,15 @@
#include <stdio.h>
#include <stdlib.h> // for min/max function
#include "data_storage.h"
#include "device_status.h"
#include "half_bridge.h"
#include "helper.h"
#include "setup.h"
#include <thingset.h>
#include <thingset/sdk.h>
#include <thingset/storage.h>
LOG_MODULE_REGISTER(dcdc, CONFIG_DCDC_LOG_LEVEL);
#if BOARD_HAS_DCDC
@@ -363,7 +366,7 @@ void Dcdc::fuse_destruction()
if (counter > 20) { // wait 20s to be able to send out data
LOG_ERR("Charge controller fuse destruction called!\n");
data_storage_write();
thingset_storage_save();
half_bridge_stop();
half_bridge_init(50, 0, 0, 0.98); // reset safety limits to allow 0% duty cycle
half_bridge_set_duty_cycle(0);
-7
View File
@@ -1,7 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
target_sources(app PRIVATE
oled.cpp
serial.cpp
can.cpp
)
-3
View File
@@ -1,3 +0,0 @@
# Folder src/ext
This folder contains code for all **ext**ensions (displays etc. connected to UEXT port) and **ext**ernal communication interfaces (UART or CAN).
-259
View File
@@ -1,259 +0,0 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#if CONFIG_THINGSET_CAN
#include <zephyr/device.h>
#include <zephyr/drivers/can.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/kernel.h>
#include <zephyr/task_wdt/task_wdt.h>
#ifdef CONFIG_ISOTP
#include <zephyr/canbus/isotp.h>
#endif
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(ext_can, CONFIG_CAN_LOG_LEVEL);
#include "bat_charger.h"
#include "data_objects.h"
#include "hardware.h"
#include "helper.h"
#include "thingset.h"
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), can_en))
#define CAN_EN_GPIO DT_CHILD(DT_PATH(outputs), can_en)
#endif
extern ThingSet ts;
extern uint16_t can_node_addr;
extern Charger charger;
#if DT_NODE_EXISTS(DT_NODELABEL(can1))
static const struct device *can_dev = DEVICE_DT_GET(DT_NODELABEL(can1));
#elif DT_NODE_EXISTS(DT_NODELABEL(fdcan1))
static const struct device *can_dev = DEVICE_DT_GET(DT_NODELABEL(fdcan1));
#endif
#ifdef CONFIG_ISOTP
#define RX_THREAD_STACK_SIZE 1024
#define RX_THREAD_PRIORITY 2
const struct isotp_fc_opts fc_opts = {
.bs = 8, // block size
.stmin = 1, // minimum separation time = 100 ms
};
struct isotp_msg_id rx_addr = {
// enable SAE J1939 compatible addressing
.flags = ISOTP_MSG_FIXED_ADDR | ISOTP_MSG_IDE,
};
struct isotp_msg_id tx_addr = {
// enable SAE J1939 compatible addressing
.flags = ISOTP_MSG_FIXED_ADDR | ISOTP_MSG_IDE,
};
static struct isotp_recv_ctx recv_ctx;
static struct isotp_send_ctx send_ctx;
void send_complete_cb(int error_nr, void *arg)
{
ARG_UNUSED(arg);
LOG_DBG("TX complete callback, err: %d", error_nr);
}
void can_isotp_thread()
{
int ret, rem_len, resp_len;
unsigned int req_len;
struct net_buf *buf;
static uint8_t rx_buffer[600]; // large enough to receive a 512k flash page for DFU
static uint8_t tx_buffer[1000];
if (!device_is_ready(can_dev)) {
return;
}
while (1) {
/* re-assign address in every loop as it may have been changed via ThingSet */
rx_addr.ext_id =
TS_CAN_TYPE_REQRESP | TS_CAN_PRIO_REQRESP | TS_CAN_TARGET_SET(can_node_addr);
tx_addr.ext_id =
TS_CAN_TYPE_REQRESP | TS_CAN_PRIO_REQRESP | TS_CAN_SOURCE_SET(can_node_addr);
ret = isotp_bind(&recv_ctx, can_dev, &rx_addr, &tx_addr, &fc_opts, K_FOREVER);
if (ret != ISOTP_N_OK) {
LOG_DBG("Failed to bind to rx ID %d [%d]", rx_addr.ext_id, ret);
return;
}
req_len = 0;
do {
rem_len = isotp_recv_net(&recv_ctx, &buf, K_FOREVER);
if (rem_len < 0) {
LOG_DBG("Receiving error [%d]", rem_len);
break;
}
if (req_len + buf->len <= sizeof(rx_buffer)) {
memcpy(&rx_buffer[req_len], buf->data, buf->len);
}
req_len += buf->len;
net_buf_unref(buf);
} while (rem_len);
// we need to unbind the receive ctx so that control frames are received in the send ctx
isotp_unbind(&recv_ctx);
if (req_len > sizeof(rx_buffer)) {
LOG_DBG("RX buffer too small");
tx_buffer[0] = TS_STATUS_REQUEST_TOO_LARGE;
resp_len = 1;
}
else if (req_len > 0 && rem_len == 0) {
LOG_INF("Got %d bytes via ISO-TP. Processing ThingSet message.", req_len);
resp_len = ts.process(rx_buffer, req_len, tx_buffer, sizeof(tx_buffer));
LOG_DBG("TX buf: %x %x %x %x", tx_buffer[0], tx_buffer[1], tx_buffer[2], tx_buffer[3]);
}
else {
tx_buffer[0] = TS_STATUS_INTERNAL_SERVER_ERR;
resp_len = 1;
}
if (resp_len > 0) {
ret = isotp_send(&send_ctx, can_dev, tx_buffer, resp_len, &recv_ctx.tx_addr,
&recv_ctx.rx_addr, send_complete_cb, NULL);
if (ret != ISOTP_N_OK) {
LOG_DBG("Error while sending data to ID %d [%d]", tx_addr.ext_id, ret);
}
}
}
}
K_THREAD_DEFINE(can_isotp, RX_THREAD_STACK_SIZE, can_isotp_thread, NULL, NULL, NULL,
RX_THREAD_PRIORITY, 0, 1500);
#endif /* CONFIG_ISOTP */
// below defines should go into the ThingSet library
#define TS_CAN_SOURCE_GET(id) (((uint32_t)id & TS_CAN_SOURCE_MASK) >> TS_CAN_SOURCE_POS)
#define TS_CAN_DATA_ID_GET(id) (((uint32_t)id & TS_CAN_DATA_ID_MASK) >> TS_CAN_DATA_ID_POS)
CAN_MSGQ_DEFINE(sub_msgq, 10);
const struct can_filter ctrl_filter = {
.id = TS_CAN_TYPE_PUBSUB,
.mask = TS_CAN_TYPE_MASK,
.flags = CAN_FILTER_IDE,
};
void can_pub_isr(const struct device *dev, int error, void *user_data)
{
// Do nothing. Publication messages are fire and forget.
}
void can_pub_send(uint32_t can_id, uint8_t can_data[8], uint8_t data_len)
{
struct can_frame frame = { 0 };
frame.id = can_id;
frame.flags = CAN_FRAME_IDE;
memcpy(frame.data, can_data, 8);
if (data_len >= 0) {
frame.dlc = data_len;
if (can_send(can_dev, &frame, K_MSEC(10), can_pub_isr, NULL) != 0) {
LOG_DBG("Error sending CAN frame");
}
}
}
void can_pubsub_thread()
{
int wdt_channel = task_wdt_add(2000, task_wdt_callback, (void *)k_current_get());
unsigned int can_id;
uint8_t can_data[8];
struct can_frame rx_frame;
if (!device_is_ready(can_dev)) {
return;
}
int filter_id = can_add_rx_filter_msgq(can_dev, &sub_msgq, &ctrl_filter);
if (filter_id < 0) {
LOG_ERR("Unable to attach ISR [%d]", filter_id);
return;
}
int64_t next_pub = k_uptime_get();
uint32_t count = 0;
while (1) {
task_wdt_feed(wdt_channel);
if (pub_can_enable) {
int data_len = 0;
int start_pos = 0;
while (count % 10 == 0) {
// normal objects: only every second
data_len = ts.bin_pub_can(start_pos, SUBSET_CAN, can_node_addr, can_id, can_data);
if (data_len < 0) {
// finished with all IDs
break;
}
else {
can_pub_send(can_id, can_data, data_len);
}
}
data_len = 0;
start_pos = 0;
while (true) {
// control objects: every 100 ms
data_len = ts.bin_pub_can(start_pos, SUBSET_CTRL, can_node_addr, can_id, can_data);
if (data_len < 0) {
break;
}
else {
can_pub_send(can_id, can_data, data_len);
}
}
}
// wait for incoming messages until the next pub message has to be sent out
while (k_msgq_get(&sub_msgq, &rx_frame, K_TIMEOUT_ABS_MS(next_pub)) != -EAGAIN) {
// process message
uint16_t data_id = TS_CAN_DATA_ID_GET(rx_frame.id);
uint8_t sender_addr = TS_CAN_SOURCE_GET(rx_frame.id);
// control message received?
if (data_id > 0x8000 && sender_addr < can_node_addr) {
uint8_t buf[4 + 8]; // ThingSet bin headers + CAN frame payload
buf[0] = 0xA1; // CBOR: map with 1 element
buf[1] = 0x19; // CBOR: uint16 follows
buf[2] = data_id >> 8;
buf[3] = data_id;
memcpy(&buf[4], rx_frame.data, 8);
int status = ts.bin_import(buf, 4 + rx_frame.dlc, TS_WRITE_MASK, SUBSET_CTRL);
if (status == TS_STATUS_CHANGED) {
charger.time_last_ctrl_msg = uptime();
}
}
}
next_pub += 100; // 100 ms period (currently fixed)
count++;
}
}
K_THREAD_DEFINE(can_pubsub, 1024, can_pubsub_thread, NULL, NULL, NULL, 6, 0, 1000);
#endif /* CONFIG_THINGSET_CAN */
-164
View File
@@ -1,164 +0,0 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#if CONFIG_THINGSET_SERIAL
#include <stdio.h>
#include <string.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/task_wdt/task_wdt.h>
#include "data_objects.h"
#include "hardware.h"
#include "thingset.h"
#if CONFIG_UEXT_SERIAL_THINGSET
#define UART_DEVICE_NODE DT_ALIAS(uart_uext)
#elif DT_NODE_EXISTS(DT_ALIAS(uart_dbg))
#define UART_DEVICE_NODE DT_ALIAS(uart_dbg)
#else
// cppcheck-suppress preprocessorErrorDirective
#error "No UART for ThingSet serial defined."
#endif
static const struct device *uart_dev = DEVICE_DT_GET(UART_DEVICE_NODE);
static char tx_buf[CONFIG_THINGSET_SERIAL_TX_BUF_SIZE];
static char rx_buf[CONFIG_THINGSET_SERIAL_RX_BUF_SIZE];
static volatile size_t rx_buf_pos = 0;
static bool discard_buffer;
static struct k_sem command_flag; // used as an event to signal a received command
static struct k_sem rx_buf_mutex; // binary semaphore used as mutex in ISR context
extern ThingSet ts;
const char serial_subset_path[] = "mSerial";
static ThingSetDataObject *serial_subset;
void serial_pub_msg()
{
if (pub_serial_enable) {
int len = ts.txt_statement(tx_buf, sizeof(tx_buf), serial_subset);
for (int i = 0; i < len; i++) {
uart_poll_out(uart_dev, tx_buf[i]);
}
uart_poll_out(uart_dev, '\n');
}
}
void serial_process_command()
{
// commands must have 2 or more characters
if (rx_buf_pos > 0) {
printf("Received Request (%d bytes): %s\n", strlen(rx_buf), rx_buf);
int len = ts.process((uint8_t *)rx_buf, strlen(rx_buf), (uint8_t *)tx_buf, sizeof(tx_buf));
for (int i = 0; i < len; i++) {
uart_poll_out(uart_dev, tx_buf[i]);
}
uart_poll_out(uart_dev, '\n');
}
// release buffer and start waiting for new commands
rx_buf_pos = 0;
k_sem_give(&rx_buf_mutex);
}
/*
* Read characters from stream until line end \n is detected, afterwards signal available command.
*/
void serial_cb(const struct device *dev, void *user_data)
{
uint8_t c;
if (!uart_irq_update(uart_dev)) {
return;
}
while (uart_irq_rx_ready(uart_dev)) {
uart_fifo_read(uart_dev, &c, 1);
if (k_sem_take(&rx_buf_mutex, K_NO_WAIT) != 0) {
// buffer not available: drop character
discard_buffer = true;
continue;
}
// \r\n and \n are markers for line end, i.e. command end
// we accept this at any time, even if the buffer is 'full', since
// there is always one last character left for the \0
if (c == '\n') {
if (rx_buf_pos > 0 && rx_buf[rx_buf_pos - 1] == '\r') {
rx_buf[rx_buf_pos - 1] = '\0';
}
else {
rx_buf[rx_buf_pos] = '\0';
}
if (discard_buffer) {
rx_buf_pos = 0;
discard_buffer = false;
k_sem_give(&rx_buf_mutex);
}
else {
// start processing command and keep the rx_buf_mutex locked
k_sem_give(&command_flag);
}
return;
}
// backspace allowed if there is something in the buffer already
else if (rx_buf_pos > 0 && c == '\b') {
rx_buf_pos--;
}
// Fill the buffer up to all but 1 character (the last character is reserved for '\0')
// Characters beyond the size of the buffer are dropped.
else if (rx_buf_pos < (sizeof(rx_buf) - 1)) {
rx_buf[rx_buf_pos++] = c;
}
k_sem_give(&rx_buf_mutex);
}
}
void serial_thread()
{
k_sem_init(&command_flag, 0, 1);
k_sem_init(&rx_buf_mutex, 1, 1);
__ASSERT_NO_MSG(device_is_ready(uart_dev));
uart_irq_callback_user_data_set(uart_dev, serial_cb, NULL);
uart_irq_rx_enable(uart_dev);
serial_subset = ts.get_endpoint(serial_subset_path, strlen(serial_subset_path));
// below process loop should be run at least once per sec, setting watchdog timeout to 1.5s
int wdt_channel = task_wdt_add(1500, task_wdt_callback, (void *)k_current_get());
int64_t t_start = k_uptime_get();
while (true) {
if (k_sem_take(&command_flag, K_TIMEOUT_ABS_MS(t_start)) == 0) {
serial_process_command();
}
else {
// semaphore timed out (should happen exactly every 1 second)
t_start += 1000;
serial_pub_msg();
}
task_wdt_feed(wdt_channel);
}
}
K_THREAD_DEFINE(serial_thread_id, 1280, serial_thread, NULL, NULL, NULL, 6, 0, 1000);
#endif /* CONFIG_THINGSET_SERIAL */
+6 -8
View File
@@ -14,19 +14,19 @@
#include "helper.h"
#include "setup.h"
#include "thingset.h" // handles access to internal data via communication interfaces
#include "bat_charger.h" // battery settings and charger state machine
#include "daq.h" // ADC using DMA and conversion to measurement values
#include "data_objects.h" // for access to internal data via ThingSet
#include "data_storage.h" // non-volatile data storage (e.g. EEPROM)
#include "dcdc.h" // DC/DC converter control (hardware independent)
#include "device_status.h" // log data (error memory, min/max measurements, etc.)
#include "half_bridge.h" // PWM generation for DC/DC converter
#include "hardware.h" // hardware-related functions like load switch, LED control, watchdog, etc.
#include "leds.h" // LED switching using charlieplexing
#include "load.h" // load and USB output management
#include "hardware.h" // hardware-related functions like load switch, LED control, watchdog, etc.
#include "leds.h" // LED switching using charlieplexing
#include "load.h" // load and USB output management
#if BOARD_HAS_PWM_PORT
#include "pwm_switch.h" // PWM charge controller
#endif
int main(void)
{
@@ -53,7 +53,7 @@ int main(void)
grid_terminal.init_nanogrid();
#endif
// read custom configuration from EEPROM
// make sure loaded configuration is valid
data_objects_init();
// Data Acquisition (DAQ) setup
@@ -122,8 +122,6 @@ int main(void)
leds_update_soc(charger.soc, false);
#endif
data_storage_update();
t_start += 1000;
k_sleep(K_TIMEOUT_ABS_MS(t_start));
}
-1
View File
@@ -17,7 +17,6 @@
#include "board.h"
#include "daq.h" // ADC using DMA and conversion to measurement values
#include "data_objects.h" // for access to internal data via ThingSet
#include "data_storage.h" // external I2C EEPROM
#include "dcdc.h" // DC/DC converter control (hardware independent)
#include "device_status.h" // log data (error memory, min/max measurements, etc.)
#include "half_bridge.h" // PWM generation for DC/DC converter
-2
View File
@@ -46,8 +46,6 @@ extern LoadOutput load;
extern LoadOutput usb_pwr;
#endif
extern ThingSet ts; // defined in data_objects.cpp
extern uint32_t timestamp;
/**
@@ -23,6 +23,7 @@
};
chosen {
thingset,serial = &usart2;
zephyr,console = &usart1;
zephyr,shell-uart = &usart1;
zephyr,sram = &sram0;
@@ -23,6 +23,8 @@
};
chosen {
thingset,can = &fdcan1;
thingset,serial = &usart2;
zephyr,console = &usart1;
zephyr,shell-uart = &usart1;
zephyr,sram = &sram0;
@@ -142,6 +144,10 @@
apb2-prescaler = <1>;
};
&rng {
status = "okay";
};
&usart1 {
pinctrl-0 = <&usart1_tx_pa9 &usart1_rx_pa10>;
pinctrl-names = "default";
@@ -17,7 +17,11 @@ CONFIG_UART_CONSOLE=y
CONFIG_PWM=y
CONFIG_CAN=y
CONFIG_THINGSET_CAN=y
# RNG required for automatic CAN node address assignment
CONFIG_ENTROPY_GENERATOR=y
CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR=y
CONFIG_CAN=y
CONFIG_ISOTP=y
CONFIG_ISOTP_FAST=y
CONFIG_THINGSET_CAN=y
@@ -25,6 +25,7 @@
};
chosen {
thingset,serial = &usart3;
zephyr,console = &usart1;
zephyr,shell-uart = &usart1;
zephyr,sram = &sram0;
@@ -133,7 +134,6 @@
};
aliases {
uart-dbg = &usart1;
uart-uext = &usart3;
spi-uext = &spi1;
i2c-uext = &i2c1;
@@ -25,6 +25,8 @@
};
chosen {
thingset,can = &can1;
thingset,serial = &usart2;
zephyr,console = &usart1;
zephyr,shell-uart = &usart1;
zephyr,sram = &sram0;
@@ -25,6 +25,8 @@
};
chosen {
thingset,can = &fdcan1;
thingset,serial = &usart2;
zephyr,console = &usart1;
zephyr,shell-uart = &usart1;
zephyr,sram = &sram0;
@@ -135,6 +137,10 @@
apb2-prescaler = <1>;
};
&rng {
status = "okay";
};
&usart1 {
pinctrl-0 = <&usart1_tx_pa9 &usart1_rx_pa10>;
pinctrl-names = "default";
@@ -18,7 +18,11 @@ CONFIG_I2C=y
CONFIG_EEPROM=y
CONFIG_EEPROM_AT24=y
CONFIG_CAN=y
CONFIG_THINGSET_CAN=y
# RNG required for automatic CAN node address assignment
CONFIG_ENTROPY_GENERATOR=y
CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR=y
CONFIG_CAN=y
CONFIG_ISOTP=y
CONFIG_ISOTP_FAST=y
CONFIG_THINGSET_CAN=y
@@ -27,6 +27,7 @@
};
chosen {
thingset,serial = &usart2;
zephyr,console = &usart1;
zephyr,shell-uart = &usart1;
zephyr,sram = &sram0;
+6 -14
View File
@@ -21,22 +21,14 @@ CONFIG_LOG=y
CONFIG_LOG_MODE_MINIMAL=y
CONFIG_CBPRINTF_FP_SUPPORT=y
# RNG required for ThingSet node ID generation in native_posix
CONFIG_ENTROPY_GENERATOR=y
CONFIG_THINGSET=y
CONFIG_THINGSET_SDK=y
# ThingSet protocol interface via UART serial
CONFIG_THINGSET_SERIAL=n
# Uncomment to disable regular data publication on ThingSet serial at startup
CONFIG_THINGSET_SERIAL_PUB_DEFAULT=n
# Uncomment to use LS.one or debug RX/TX connector instead of UEXT serial for ThingSet
CONFIG_UEXT_SERIAL_THINGSET=n
# Uncomment to enable I2C OLED display in UEXT connector
#CONFIG_UEXT_OLED_DISPLAY=y
# Select initial battery configuration: GEL (default), FLOODED, AGM, LFP, NMC or NMC_HV
#CONFIG_BAT_TYPE_???=y
# Change number of cells considering selected type (12V/24V battery by default if not set)
#CONFIG_BAT_NUM_CELLS=?
# Disable regular data publication on ThingSet serial at startup
CONFIG_THINGSET_REPORTING_LIVE_ENABLE_PRESET=n
+5 -2
View File
@@ -18,8 +18,11 @@
extern "C" {
void setUp(void) {}
void tearDown(void) {}
void setUp(void)
{}
void tearDown(void)
{}
} /* extern "C" */
+8 -3
View File
@@ -17,14 +17,19 @@ manifest:
- cmsis_6
- edtt
- hal_stm32
- zcbor
- name: oled-display-library
remote: libresolar
revision: 523b26c103de56714fcda655702d600870a671bc
path: modules/oled-display
- name: thingset-device-library
- name: thingset-zephyr-sdk
remote: thingset
revision: 9630a357de071ac63bfbbaf9d91c6c937663601f
path: modules/thingset
revision: 5d5c6b031ba543d67a3dc6904ccfc987ebdadc52
path: thingset-zephyr-sdk
import:
name-allowlist:
- thingset-node-c
- zcbor
- name: unity
remote: throwtheswitch
revision: 74cde089e65c3435ce9aa87d5c69f4f16b7f6ade