Move to standard Zephyr T2 workspace structure (star topology)

https://docs.zephyrproject.org/latest/guides/west/workspaces.html

Previously, west manifest and CMake entry point were in zephyr subfolder
as required for PlatformIO. As PlatformIO support was dropped, we can
follow the default Zephyr folder structure more closely.

The application is built and flashed from app subfolder, where as unit
tests are run from tests subfolder.
This commit is contained in:
Martin Jäger
2021-12-12 14:09:05 +01:00
parent 15ee7d8553
commit 8ab8ea244a
57 changed files with 67 additions and 48 deletions
+7
View File
@@ -0,0 +1,7 @@
# SPDX-License-Identifier: Apache-2.0
target_sources(app PRIVATE
oled.cpp
serial.cpp
can.cpp
)
+3
View File
@@ -0,0 +1,3 @@
# 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).
+264
View File
@@ -0,0 +1,264 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#if CONFIG_THINGSET_CAN
#include <zephyr.h>
#include <device.h>
#include <drivers/gpio.h>
#include <drivers/can.h>
#include <task_wdt/task_wdt.h>
#ifdef CONFIG_ISOTP
#include <canbus/isotp.h>
#endif
#include <logging/log.h>
LOG_MODULE_REGISTER(ext_can, CONFIG_CAN_LOG_LEVEL);
#include "data_objects.h"
#include "hardware.h"
#include "thingset.h"
#include "bat_charger.h"
#include "helper.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;
static const struct device *can_dev = DEVICE_DT_GET(DT_NODELABEL(can1));
#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 = {
.id_type = CAN_EXTENDED_IDENTIFIER,
.use_ext_addr = 0, // Normal ISO-TP addressing (using only CAN ID)
.use_fixed_addr = 1, // enable SAE J1939 compatible addressing
};
struct isotp_msg_id tx_addr = {
.id_type = CAN_EXTENDED_IDENTIFIER,
.use_ext_addr = 0, // Normal ISO-TP addressing (using only CAN ID)
.use_fixed_addr = 1, // enable SAE J1939 compatible addressing
};
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_BASE_REQRESP | TS_CAN_PRIO_REQRESP |
TS_CAN_TARGET_SET(can_node_addr);
tx_addr.ext_id = TS_CAN_BASE_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_DEFINE_MSGQ(sub_msgq, 10);
const struct zcan_filter ctrl_filter = {
.id = TS_CAN_BASE_CONTROL,
.rtr = CAN_DATAFRAME,
.id_type = CAN_EXTENDED_IDENTIFIER,
.id_mask = TS_CAN_TYPE_MASK,
.rtr_mask = 1
};
void can_pub_isr(uint32_t err_flags, void *arg)
{
// 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 zcan_frame frame = {0};
frame.id_type = CAN_EXTENDED_IDENTIFIER;
frame.rtr = CAN_DATAFRAME;
frame.id = can_id;
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) != CAN_TX_OK) {
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 zcan_frame rx_frame;
const struct device *can_en_dev = device_get_binding(DT_GPIO_LABEL(CAN_EN_GPIO, gpios));
gpio_pin_configure(can_en_dev, DT_GPIO_PIN(CAN_EN_GPIO, gpios),
DT_GPIO_FLAGS(CAN_EN_GPIO, gpios) | GPIO_OUTPUT_ACTIVE);
if (!device_is_ready(can_dev)) {
return;
}
int filter_id = can_attach_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 */
+191
View File
@@ -0,0 +1,191 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#if CONFIG_UEXT_OLED_DISPLAY
#include <math.h>
#include <stdio.h>
#include <zephyr.h>
#include <device.h>
#include <drivers/gpio.h>
#include "oled_ssd1306.h"
#include "setup.h"
#include "half_bridge.h"
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), uext_en))
#define UEXT_EN_GPIO DT_CHILD(DT_PATH(outputs), uext_en)
#endif
const unsigned char bmp_load [] = {
0x20, 0x22, 0x04, 0x70, 0x88, 0x8B, 0x88, 0x70, 0x04, 0x22, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x07, 0x04, 0x07, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
const unsigned char bmp_arrow_right [] = {
0x41, 0x63, 0x36, 0x1C
};
const unsigned char bmp_pv_panel [] = {
0x60, 0x98, 0x86, 0xC9, 0x31, 0x19, 0x96, 0x62, 0x32, 0x2C, 0xC4, 0x64, 0x98, 0x08, 0xC8, 0x30,
0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02, 0x02, 0x03, 0x04, 0x04, 0x04, 0x03, 0x00, 0x00
};
const unsigned char bmp_disconnected [] = {
0x08, 0x08, 0x08, 0x08, 0x00, 0x41, 0x63, 0x36, 0x1C, 0x1C, 0x36, 0x63, 0x41, 0x00, 0x08, 0x08,
0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
OledSSD1306 oled(DT_LABEL(DT_ALIAS(i2c_uext)));
void oled_update()
{
char buf[30];
unsigned int len;
#if CONFIG_HV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR
PowerPort &in_terminal = solar_terminal;
#elif CONFIG_HV_TERMINAL_NANOGRID
PowerPort &in_terminal = grid_terminal;
#endif
oled.clear();
oled.drawBitmap(6, 0, bmp_pv_panel, 16, 16, 1);
oled.drawBitmap(104, 0, bmp_load, 16, 16, 1);
if (half_bridge_enabled() == false) {
oled.drawBitmap(27, 3, bmp_disconnected, 32, 8, 1);
}
else {
oled.drawBitmap(34, 3, bmp_arrow_right, 5, 7, 1);
}
if (load.state == LOAD_STATE_ON) {
oled.drawBitmap(84, 3, bmp_arrow_right, 5, 7, 1);
}
else {
oled.drawBitmap(81, 3, bmp_disconnected, 17, 7, 1);
}
oled.drawRect(52, 2, 18, 9, 1); // battery shape
oled.drawRect(69, 3, 3, 7, 1); // battery terminal
if (charger.soc >= 20) {
oled.drawRect(54, 4, 2, 5, 1); // bar 1
}
if (charger.soc >= 40) {
oled.drawRect(57, 4, 2, 5, 1); // bar 2
}
if (charger.soc >= 60) {
oled.drawRect(60, 4, 2, 5, 1); // bar 3
}
if (charger.soc >= 80) {
oled.drawRect(63, 4, 2, 5, 1); // bar 4
}
if (charger.soc >= 95) {
oled.drawRect(66, 4, 2, 5, 1); // bar 5
}
// solar panel data
#if BOARD_HAS_PWM_PORT
if (pwm_switch.active()) {
#else
if (half_bridge_enabled()) {
#endif
oled.setTextCursor(0, 18);
len = snprintf(buf, sizeof(buf), "%4.0fW",
(abs(in_terminal.power) < 1) ? 0 : -in_terminal.power); // remove negative zeros
oled.writeString(buf, len);
}
else {
oled.setTextCursor(8, 18);
len = snprintf(buf, sizeof(buf), "n/a");
oled.writeString(buf, len);
}
#if BOARD_HAS_PWM_PORT
if (in_terminal.bus->voltage > bat_terminal.bus->voltage)
#endif
{
oled.setTextCursor(0, 26);
len = snprintf(buf, sizeof(buf), "%4.1fV", in_terminal.bus->voltage);
oled.writeString(buf, len);
}
// battery data
oled.setTextCursor(42, 18);
len = snprintf(buf, sizeof(buf), "%5.1fW",
(abs(bat_terminal.power) < 0.1) ? 0 : bat_terminal.power); // remove negative zeros
oled.writeString(buf, len);
oled.setTextCursor(42, 26);
len = snprintf(buf, sizeof(buf), "%5.1fV", bat_terminal.bus->voltage);
oled.writeString(buf, len);
// load data
oled.setTextCursor(90, 18);
len = snprintf(buf, sizeof(buf), "%5.1fW",
(abs(load.power) < 0.1) ? 0 : load.power); // remove negative zeros
oled.writeString(buf, len);
oled.setTextCursor(90, 26);
len = snprintf(buf, sizeof(buf), "%5.1fA\n",
(abs(load.current) < 0.1) ? 0 : load.current);
oled.writeString(buf, len);
oled.setTextCursor(0, 36);
len = snprintf(buf, sizeof(buf), "Day +%5.0fWh -%5.0fWh",
in_terminal.neg_energy_Wh, fabs(load.pos_energy_Wh));
oled.writeString(buf, len);
len = snprintf(buf, sizeof(buf), "Tot +%4.1fkWh -%4.1fkWh",
dev_stat.solar_in_total_Wh / 1000.0, fabs(dev_stat.load_out_total_Wh) / 1000.0);
oled.writeString(buf, len);
oled.setTextCursor(0, 56);
#if BOARD_HAS_PWM_PORT
bool pwm_enabled = pwm_switch.active();
float duty_cycle = pwm_switch.get_duty_cycle();
#else
bool pwm_enabled = half_bridge_enabled();
float duty_cycle = half_bridge_get_duty_cycle();
#endif
float temp = charger.ext_temp_sensor ? charger.bat_temperature:dev_stat.internal_temp;
char tC = charger.ext_temp_sensor ? 'T' : 't';
if (pwm_enabled == true) {
len = snprintf(buf, sizeof(buf), "%c %.0fC PWM %.0f%% SOC %d%%",
tC, temp, duty_cycle * 100.0, charger.soc);
oled.writeString(buf, len);
}
else {
len = snprintf(buf, sizeof(buf), "%c %.0fC PWM OFF SOC %d%%", tC, temp, charger.soc);
oled.writeString(buf, len);
}
oled.display();
}
void oled_thread()
{
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), uext_en))
const struct device *dev_uext_en = device_get_binding(DT_GPIO_LABEL(UEXT_EN_GPIO, gpios));
gpio_pin_configure(dev_uext_en, DT_GPIO_PIN(UEXT_EN_GPIO, gpios),
DT_GPIO_FLAGS(UEXT_EN_GPIO, gpios) | GPIO_OUTPUT_ACTIVE);
#endif
oled.init(CONFIG_UEXT_OLED_BRIGHTNESS);
while (true) {
oled_update();
k_sleep(K_MSEC(1000));
}
}
K_THREAD_DEFINE(oled_thread_id, 1024, oled_thread, NULL, NULL, NULL, 6, 0, 1000);
#endif /* CONFIG_UEXT_OLED_DISPLAY */
+151
View File
@@ -0,0 +1,151 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#if CONFIG_THINGSET_SERIAL
#include <string.h>
#include <stdio.h>
#include <zephyr.h>
#include <sys/printk.h>
#include <drivers/uart.h>
#include <task_wdt/task_wdt.h>
#include "thingset.h"
#include "hardware.h"
#include "data_objects.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 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[] = "serial";
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 > 1) {
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) && k_sem_take(&rx_buf_mutex, K_NO_WAIT) == 0) {
uart_fifo_read(uart_dev, &c, 1);
// \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';
}
// 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 */