Add Clang-Format file and apply it

The format tries to maintain the previously used Libre Solar firmware coding
style as much as possible.

Applying the format fixed some issues and re-ordered some parts of the code.
This commit is contained in:
Martin Jäger
2022-01-26 13:56:31 +01:00
committed by Martin Jäger
parent 2359a96bbe
commit 5bdbecf19d
45 changed files with 1036 additions and 986 deletions
+31 -31
View File
@@ -6,11 +6,11 @@
#if CONFIG_THINGSET_CAN
#include <zephyr.h>
#include <device.h>
#include <drivers/gpio.h>
#include <drivers/can.h>
#include <drivers/gpio.h>
#include <task_wdt/task_wdt.h>
#include <zephyr.h>
#ifdef CONFIG_ISOTP
#include <canbus/isotp.h>
@@ -19,11 +19,11 @@
#include <logging/log.h>
LOG_MODULE_REGISTER(ext_can, CONFIG_CAN_LOG_LEVEL);
#include "bat_charger.h"
#include "data_objects.h"
#include "hardware.h"
#include "thingset.h"
#include "bat_charger.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)
@@ -38,23 +38,23 @@ 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
#define RX_THREAD_PRIORITY 2
const struct isotp_fc_opts fc_opts = {
.bs = 8, // block size
.stmin = 1 // minimum separation time = 100 ms
.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
.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
.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;
@@ -71,7 +71,7 @@ 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 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)) {
@@ -80,10 +80,10 @@ void can_isotp_thread()
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);
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) {
@@ -124,8 +124,8 @@ void can_isotp_thread()
}
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);
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);
}
@@ -134,13 +134,13 @@ void can_isotp_thread()
}
K_THREAD_DEFINE(can_isotp, RX_THREAD_STACK_SIZE, can_isotp_thread, NULL, NULL, NULL,
RX_THREAD_PRIORITY, 0, 1500);
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)
#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);
@@ -149,20 +149,20 @@ const struct zcan_filter ctrl_filter = {
.rtr = CAN_DATAFRAME,
.id_type = CAN_EXTENDED_IDENTIFIER,
.id_mask = TS_CAN_TYPE_MASK,
.rtr_mask = 1
.rtr_mask = 1,
};
void can_pub_isr(int error, void *arg)
{
// Do nothing. Publication messages are fire and forget.
// 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};
struct zcan_frame frame = { 0 };
frame.id_type = CAN_EXTENDED_IDENTIFIER;
frame.rtr = CAN_DATAFRAME;
frame.id = can_id;
frame.rtr = CAN_DATAFRAME;
frame.id = can_id;
memcpy(frame.data, can_data, 8);
if (data_len >= 0) {
@@ -184,7 +184,7 @@ void can_pubsub_thread()
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);
DT_GPIO_FLAGS(CAN_EN_GPIO, gpios) | GPIO_OUTPUT_ACTIVE);
if (!device_is_ready(can_dev)) {
return;
@@ -240,9 +240,9 @@ void can_pubsub_thread()
// 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
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);
@@ -254,7 +254,7 @@ void can_pubsub_thread()
}
}
next_pub += 100; // 100 ms period (currently fixed)
next_pub += 100; // 100 ms period (currently fixed)
count++;
}
}
+31 -32
View File
@@ -9,36 +9,34 @@
#include <math.h>
#include <stdio.h>
#include <zephyr.h>
#include <device.h>
#include <drivers/gpio.h>
#include <zephyr.h>
#include "oled_ssd1306.h"
#include "setup.h"
#include "half_bridge.h"
#include "setup.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 [] = {
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
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_arrow_right[] = { 0x41, 0x63, 0x36, 0x1C };
const unsigned char bmp_pv_panel [] = {
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
0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02, 0x02, 0x03, 0x04, 0x04, 0x04, 0x03, 0x00, 0x00,
};
const unsigned char bmp_disconnected [] = {
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
0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
OledSSD1306 oled(DT_LABEL(DT_ALIAS(i2c_uext)));
@@ -73,23 +71,23 @@ void oled_update()
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
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
oled.drawRect(54, 4, 2, 5, 1); // bar 1
}
if (charger.soc >= 40) {
oled.drawRect(57, 4, 2, 5, 1); // bar 2
oled.drawRect(57, 4, 2, 5, 1); // bar 2
}
if (charger.soc >= 60) {
oled.drawRect(60, 4, 2, 5, 1); // bar 3
oled.drawRect(60, 4, 2, 5, 1); // bar 3
}
if (charger.soc >= 80) {
oled.drawRect(63, 4, 2, 5, 1); // bar 4
oled.drawRect(63, 4, 2, 5, 1); // bar 4
}
if (charger.soc >= 95) {
oled.drawRect(66, 4, 2, 5, 1); // bar 5
oled.drawRect(66, 4, 2, 5, 1); // bar 5
}
// solar panel data
@@ -100,7 +98,8 @@ void oled_update()
#endif
oled.setTextCursor(0, 18);
len = snprintf(buf, sizeof(buf), "%4.0fW",
(abs(in_terminal.power) < 1) ? 0 : -in_terminal.power); // remove negative zeros
(abs(in_terminal.power) < 1) ? 0
: -in_terminal.power); // remove negative zeros
oled.writeString(buf, len);
}
else {
@@ -119,8 +118,9 @@ void oled_update()
// 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
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);
@@ -129,19 +129,18 @@ void oled_update()
// load data
oled.setTextCursor(90, 18);
len = snprintf(buf, sizeof(buf), "%5.1fW",
(abs(load.power) < 0.1) ? 0 : load.power); // remove negative zeros
(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);
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));
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);
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);
@@ -154,12 +153,12 @@ void oled_update()
float duty_cycle = half_bridge_get_duty_cycle();
#endif
float temp = charger.ext_temp_sensor ? charger.bat_temperature:dev_stat.internal_temp;
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);
len = snprintf(buf, sizeof(buf), "%c %.0fC PWM %.0f%% SOC %d%%", tC, temp,
duty_cycle * 100.0, charger.soc);
oled.writeString(buf, len);
}
else {
@@ -175,7 +174,7 @@ 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);
DT_GPIO_FLAGS(UEXT_EN_GPIO, gpios) | GPIO_OUTPUT_ACTIVE);
#endif
oled.init(CONFIG_UEXT_OLED_BRIGHTNESS);
+11 -12
View File
@@ -6,17 +6,17 @@
#if CONFIG_THINGSET_SERIAL
#include <string.h>
#include <stdio.h>
#include <string.h>
#include <zephyr.h>
#include <sys/printk.h>
#include <drivers/uart.h>
#include <sys/printk.h>
#include <task_wdt/task_wdt.h>
#include <zephyr.h>
#include "thingset.h"
#include "hardware.h"
#include "data_objects.h"
#include "hardware.h"
#include "thingset.h"
#if CONFIG_UEXT_SERIAL_THINGSET
#define UART_DEVICE_NODE DT_ALIAS(uart_uext)
@@ -34,8 +34,8 @@ 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
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;
@@ -59,8 +59,7 @@ void serial_process_command()
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));
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]);
@@ -76,7 +75,7 @@ void serial_process_command()
/*
* Read characters from stream until line end \n is detected, afterwards signal available command.
*/
void serial_cb(const struct device *dev, void* user_data)
void serial_cb(const struct device *dev, void *user_data)
{
uint8_t c;
@@ -92,8 +91,8 @@ void serial_cb(const struct device *dev, void* user_data)
// 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';
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';