Replace custom print_* with Zephyr LOG_*

This commit is contained in:
Martin Jäger
2020-07-23 17:34:41 +02:00
parent 8e06357a7d
commit c2ef955a63
16 changed files with 80 additions and 91 deletions
-2
View File
@@ -82,10 +82,8 @@ framework =
build_flags =
-std=gnu++17
-Wall
-D LITTLE_ENDIAN
-D UNIT_TEST
-D __STDC_FORMAT_MACROS
-D DEBUG_PRINT_FLAGS=0xF
-I test
# include src directory (otherwise unit-tests will only include lib directory)
test_build_project_src = true
+11 -8
View File
@@ -8,6 +8,11 @@
#include <zephyr.h>
#ifndef UNIT_TEST
#include <logging/log.h>
LOG_MODULE_REGISTER(bat_charger, CONFIG_LOG_DEFAULT_LEVEL);
#endif
#include <math.h> // for fabs function
#include <stdio.h>
@@ -155,19 +160,17 @@ bool battery_conf_check(BatConf *bat_conf)
// - cutoff current not extremely low/high
// - capacity plausible
/*
printf("battery_conf_check: %d %d %d %d %d %d %d\n",
LOG_DBG("battery_conf_check: %d %d %d %d %d %d %d",
bat_conf->voltage_load_reconnect > (bat_conf->voltage_load_disconnect + 0.6),
bat_conf->voltage_recharge < (bat_conf->voltage_max - 0.4),
bat_conf->voltage_recharge < (bat_conf->topping_voltage - 0.4),
bat_conf->voltage_recharge > (bat_conf->voltage_load_disconnect + 1),
bat_conf->voltage_load_disconnect > (bat_conf->voltage_absolute_min + 0.4),
bat_conf->current_cutoff_CV < (bat_conf->nominal_capacity / 10.0), // C/10 or lower allowed
bat_conf->current_cutoff_CV > 0.01,
bat_conf->topping_current_cutoff < (bat_conf->nominal_capacity / 10.0),
bat_conf->topping_current_cutoff > 0.01,
(bat_conf->trickle_enabled == false ||
(bat_conf->trickle_voltage < bat_conf->voltage_max &&
(bat_conf->trickle_voltage < bat_conf->topping_voltage &&
bat_conf->trickle_voltage > bat_conf->voltage_load_disconnect))
);
*/
return (
bat_conf->voltage_load_reconnect > (bat_conf->voltage_load_disconnect + 0.4) &&
@@ -296,7 +299,7 @@ void Charger::update_soc(BatConf *bat_conf)
void Charger::enter_state(int next_state)
{
//printf("Enter State: %d\n", next_state);
LOG_DBG("Enter State: %d", next_state);
time_state_changed = uptime();
state = next_state;
}
+7 -3
View File
@@ -8,12 +8,16 @@
#include <zephyr.h>
#ifndef UNIT_TEST
#include <logging/log.h>
LOG_MODULE_REGISTER(daq, CONFIG_LOG_DEFAULT_LEVEL);
#endif
#include <math.h> // log for thermistor calculation
#include <assert.h>
#include "mcu.h"
#include "setup.h"
#include "debug.h"
// typical value for Semitec 103AT-5 thermistor: 3435
#define NTC_BETA_VALUE 3435
@@ -239,7 +243,7 @@ void high_voltage_alert()
dev_stat.set_error(ERR_BAT_OVERVOLTAGE);
print_error("High voltage alert, ADC reading: %d limit: %d\n",
LOG_ERR("High voltage alert, ADC reading: %d limit: %d\n",
adc_readings[ADC_POS(v_low)], adc_alerts_upper[ADC_POS(v_low)].limit);
}
@@ -250,7 +254,7 @@ void low_voltage_alert()
load.stop(ERR_LOAD_VOLTAGE_DIP);
#endif
print_error("Low voltage alert, ADC reading: %d limit: %d\n",
LOG_ERR("Low voltage alert, ADC reading: %d limit: %d\n",
adc_readings[ADC_POS(v_low)], adc_alerts_lower[ADC_POS(v_low)].limit);
}
+2 -3
View File
@@ -6,7 +6,7 @@
#include "daq.h"
#if defined(__ZEPHYR__)
#ifndef UNIT_TEST
#include <stdint.h>
#include <inttypes.h>
@@ -35,7 +35,6 @@
#endif
#include "dcdc.h" // for low-level control function called by DMA
#include "debug.h"
#if defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32L0X)
@@ -388,4 +387,4 @@ void daq_setup()
calibrate_current_sensors();
}
#endif // __ZEPHYR__
#endif // UNIT_TEST
+7 -5
View File
@@ -10,6 +10,8 @@
#include <zephyr.h>
#include <drivers/hwinfo.h>
#include <sys/crc.h>
#include <logging/log.h>
LOG_MODULE_REGISTER(data_nodes, CONFIG_LOG_DEFAULT_LEVEL);
#endif
#include <stdio.h>
@@ -473,7 +475,7 @@ void data_nodes_update_conf()
{
bool changed;
if (battery_conf_check(&bat_conf_user)) {
printf("New config valid and activated.\n");
LOG_INF("New config valid and activated.");
battery_conf_overwrite(&bat_conf_user, &bat_conf, &charger);
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
load.set_voltage_limits(bat_conf.voltage_load_disconnect, bat_conf.voltage_load_reconnect,
@@ -482,7 +484,7 @@ void data_nodes_update_conf()
changed = true;
}
else {
printf("Check not passed, getting back old config.\n");
LOG_ERR("Requested config change not valid and rejected.");
battery_conf_overwrite(&bat_conf, &bat_conf_user);
changed = false;
}
@@ -524,17 +526,17 @@ void thingset_auth()
if (strlen(pass_exp) == strlen(auth_password) &&
strncmp(auth_password, pass_exp, strlen(pass_exp)) == 0)
{
printf("Authenticated as expert user\n");
LOG_INF("Authenticated as expert user.");
ts.set_authentication(TS_EXP_MASK | TS_USR_MASK);
}
else if (strlen(pass_mkr) == strlen(auth_password) &&
strncmp(auth_password, pass_mkr, strlen(pass_mkr)) == 0)
{
printf("Authenticated as maker\n");
LOG_INF("Authenticated as maker.");
ts.set_authentication(TS_MKR_MASK | TS_USR_MASK);
}
else {
printf("Reset authentication\n");
LOG_INF("Reset authentication.");
ts.set_authentication(TS_USR_MASK);
}
}
+14 -11
View File
@@ -10,6 +10,9 @@
#ifndef UNIT_TEST
#include <drivers/gpio.h>
#include <logging/log.h>
LOG_MODULE_REGISTER(dcdc, CONFIG_LOG_DEFAULT_LEVEL);
#endif
#include <math.h> // for fabs function
@@ -17,7 +20,6 @@
#include <stdio.h>
#include "device_status.h"
#include "debug.h"
#include "helper.h"
#include "half_bridge.h"
#include "eeprom.h"
@@ -116,14 +118,15 @@ int Dcdc::perturb_observe_controller()
pwr_inc_goal = pwm_delta;
}
print_test("P: %.2fW (prev %.2fW), in: %.2fV %.2fA (margin %.2fA limit %.2fA), "
"out: %.2fV (target %.2fV) %.2fA (margin %.2fA limit %.2fA), "
#if CONFIG_LOG_DEFAULT_LEVEL == LOG_LEVEL_DBG
// workaround as LOG_DBG does not support float printing
printf("P: %.2fW (prev %.2fW), in: %.2fV %.2fA (limit %.2fA), "
"out: %.2fV (target %.2fV) %.2fA (limit %.2fA), "
"PWM: %.1f, chg_state: %d, pwr_inc: %d, buck/boost: %d\n",
out->power, power_prev, in->bus->voltage, in->current, in->bus->src_current_margin,
in->neg_current_limit,
out->bus->voltage, out->bus->sink_voltage_bound, out->current, out->bus->sink_current_margin,
out->pos_current_limit,
out->power, power_prev, in->bus->voltage, in->current, in->neg_current_limit,
out->bus->voltage, out->bus->sink_voltage_bound, out->current, out->pos_current_limit,
half_bridge_get_duty_cycle() * 100.0, state, pwr_inc_goal, pwr_inc_pwm_direction);
#endif
power_prev = out->power;
@@ -186,7 +189,7 @@ __weak void Dcdc::control()
half_bridge_stop();
state = DCDC_STATE_OFF;
off_timestamp = uptime();
print_info("DC/DC Stop: %s.\n",stop_reason);
printf("DC/DC Stop: %s.\n", stop_reason);
}
}
else { // half bridge is off
@@ -234,7 +237,7 @@ __weak void Dcdc::control()
}
half_bridge_start();
power_good_timestamp = uptime();
print_info("DC/DC %s mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", mode_name,
printf("DC/DC %s mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", mode_name,
hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100);
startup_delay_counter = 0; // ensure we will have a delay before next start
}
@@ -261,7 +264,7 @@ void Dcdc::test()
if (check_start_conditions() != 0) {
half_bridge_set_duty_cycle(lvs->bus->voltage / hvs->bus->voltage);
half_bridge_start();
print_info("DC/DC test mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n",
printf("DC/DC test mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n",
hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100);
}
}
@@ -283,7 +286,7 @@ void Dcdc::fuse_destruction()
static int counter = 0;
if (counter > 20) { // wait 20s to be able to send out data
print_error("Charge controller fuse destruction called!\n");
LOG_ERR("Charge controller fuse destruction called!\n");
eeprom_store_data();
half_bridge_stop();
half_bridge_init(50, 0, 0, 0.98); // reset safety limits to allow 0% duty cycle
-37
View File
@@ -1,37 +0,0 @@
/*
* Copyright (c) 2016 Martin Jäger / Libre Solar
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef DEBUG_H
#define DEBUG_H
#include <stdio.h>
// Debug printing macros
// see https://stackoverflow.com/questions/1644868/define-macro-for-debug-printing-in-c
// Set the bits in DEBUG_PRINT_FLAGS via platformio.ini to enable specific debug print levels
// Bit 0: Serious errors
#define print_error(fmt, ...) \
do { if (DEBUG_PRINT_FLAGS & (1 << 0)) printf(fmt, ##__VA_ARGS__); } while (0)
// Bit 1: Warnings
#define print_warning(fmt, ...) \
do { if (DEBUG_PRINT_FLAGS & (1 << 1)) printf(fmt, ##__VA_ARGS__); } while (0)
// Bit 2: Information only
#define print_info(fmt, ...) \
do { if (DEBUG_PRINT_FLAGS & (1 << 2)) printf(fmt, ##__VA_ARGS__); } while (0)
// Bit 3: Unit test information
#define print_test(fmt, ...) \
do { if (DEBUG_PRINT_FLAGS & (1 << 3)) printf(fmt, ##__VA_ARGS__); } while (0)
// Bit 4: Communication module special flag
#define print_comms(fmt, ...) \
do { if (DEBUG_PRINT_FLAGS & (1 << 4)) printf(fmt, ##__VA_ARGS__); } while (0)
#endif /* DEBUG_H */
-1
View File
@@ -50,7 +50,6 @@ void DeviceStatus::update_energy()
// solar voltage > battery voltage after 5 hours of night time means sunrise in the morning
// --> reset daily energy counters
if (seconds_zero_solar > 60*60*5) {
//printf("Night!\n");
day_counter++;
solar_in_total_Wh_prev = solar_in_total_Wh;
load_out_total_Wh_prev = load_out_total_Wh;
+7 -7
View File
@@ -54,7 +54,7 @@ struct isotp_recv_ctx recv_ctx;
void send_complette_cb(int error_nr, void *arg)
{
ARG_UNUSED(arg);
LOG_DBG("TX complete cb [%d]\n", error_nr);
LOG_DBG("TX complete cb [%d]", error_nr);
}
void can_rx_thread()
@@ -71,7 +71,7 @@ void can_rx_thread()
ret = isotp_bind(&recv_ctx, can_dev, &tx_addr, &rx_addr, &fc_opts, K_FOREVER);
if (ret != ISOTP_N_OK) {
LOG_DBG("Failed to bind to rx ID %d [%d]\n", rx_addr.ext_id, ret);
LOG_DBG("Failed to bind to rx ID %d [%d]", rx_addr.ext_id, ret);
return;
}
@@ -80,7 +80,7 @@ void can_rx_thread()
do {
rem_len = isotp_recv_net(&recv_ctx, &buf, K_FOREVER);
if (rem_len < 0) {
LOG_DBG("Receiving error [%d]\n", rem_len);
LOG_DBG("Receiving error [%d]", rem_len);
break;
}
if (received_len + buf->len <= sizeof(rx_buffer)) {
@@ -88,14 +88,14 @@ void can_rx_thread()
received_len += buf->len;
}
else {
LOG_DBG("RX buffer too small\n");
LOG_DBG("RX buffer too small");
break;
}
net_buf_unref(buf);
} while (rem_len);
if (received_len > 0) {
LOG_DBG("Got %d bytes via ISO-TP. Processing ThingSet message.\n", received_len);
LOG_DBG("Got %d bytes via ISO-TP. Processing ThingSet message.", received_len);
int resp_len = ts.process(rx_buffer, received_len, tx_buffer, sizeof(tx_buffer));
if (resp_len > 0) {
@@ -103,7 +103,7 @@ void can_rx_thread()
int ret = isotp_send(&send_ctx, can_dev, tx_buffer, resp_len,
&tx_addr, &rx_addr, send_complette_cb, NULL);
if (ret != ISOTP_N_OK) {
LOG_DBG("Error while sending data to ID %d [%d]\n", tx_addr.ext_id, ret);
LOG_DBG("Error while sending data to ID %d [%d]", tx_addr.ext_id, ret);
}
}
}
@@ -152,7 +152,7 @@ void can_pub_thread()
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\n");
LOG_DBG("Error sending CAN frame");
}
}
}
+2 -2
View File
@@ -59,7 +59,7 @@ void watchdog_init()
{
wdt = device_get_binding(DT_LABEL(DT_NODELABEL(iwdg)));
if (!wdt) {
LOG_DBG("Cannot get IWDG device\n");
LOG_DBG("Cannot get IWDG device");
return;
}
}
@@ -113,7 +113,7 @@ static void sw_watchdog(struct k_timer *timer_id)
for (int i = 0; i < sw_wdt_channel_count; i++) {
if ((current_time - sw_wdt_channels[i].check_in_time) > sw_wdt_channels[i].timeout) {
LOG_ERR("Watchdog channel %d triggered!\n", i);
LOG_ERR("Watchdog channel %d triggered!", i);
reset_device();
}
}
-1
View File
@@ -12,7 +12,6 @@
#include "leds.h"
#include "device_status.h"
#include "debug.h"
#include "helper.h"
#define LOAD_CURRENT_MAX DT_PROP(DT_CHILD(DT_PATH(outputs), load), current_max)
+1 -2
View File
@@ -32,7 +32,6 @@ static struct device *dev_usb;
#endif
#include "hardware.h"
#include "debug.h"
#include "leds.h"
/* TODO: porting and testing of comparator copied from mbed */
@@ -239,7 +238,7 @@ void load_cp_enable()
struct device *pwm_dev;
pwm_dev = device_get_binding(DT_OUTPUTS_CHARGE_PUMP_PWMS_CONTROLLER);
if (!pwm_dev) {
print_error("Cannot find %s!\n", DT_OUTPUTS_CHARGE_PUMP_PWMS_CONTROLLER);
LOG_ERR("Cannot find %s!\n", DT_OUTPUTS_CHARGE_PUMP_PWMS_CONTROLLER);
return;
}
// set to 50% duty cycle
+10 -6
View File
@@ -8,12 +8,16 @@
#include <zephyr.h>
#ifndef UNIT_TEST
#include <logging/log.h>
LOG_MODULE_REGISTER(pwm_switch, CONFIG_LOG_DEFAULT_LEVEL);
#endif
#include <stdio.h>
#include <time.h>
#include "mcu.h"
#include "daq.h"
#include "debug.h"
#include "helper.h"
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch))
@@ -51,13 +55,13 @@ void PwmSwitch::test()
if (pwm_active() && enable == false) {
pwm_signal_stop();
off_timestamp = uptime();
print_info("PWM test mode stop.\n");
LOG_INF("PWM test mode stop.");
}
else if (!pwm_active() && enable == true) {
// turning the PWM switch on creates a short voltage rise, so inhibit alerts by 50 ms
adc_upper_alert_inhibit(ADC_POS(v_low), 50);
pwm_signal_start(0.9);
print_info("PWM test mode start.\n");
LOG_INF("PWM test mode start.");
}
}
@@ -76,7 +80,7 @@ void PwmSwitch::control()
{
pwm_signal_stop();
off_timestamp = uptime();
print_info("PWM charger stop, current = %.3fA\n", current);
LOG_INF("PWM charger stop, current = %d mA", (int)(current * 1000));
}
else if (bus->voltage > bus->sink_control_voltage() // bus voltage above target
|| current < neg_current_limit // port current limit exceeded
@@ -94,7 +98,7 @@ void PwmSwitch::control()
// prevent very short on periods and switch completely off instead
pwm_signal_stop();
off_timestamp = uptime();
print_info("PWM charger stop, no further derating possible.\n");
LOG_INF("PWM charger stop, no further derating possible.");
}
else {
pwm_signal_duty_cycle_step(-1);
@@ -123,7 +127,7 @@ void PwmSwitch::control()
adc_upper_alert_inhibit(ADC_POS(v_low), 50);
pwm_signal_start(1);
power_good_timestamp = uptime();
print_info("PWM charger start.\n");
LOG_INF("PWM charger start.");
}
}
}
-1
View File
@@ -12,7 +12,6 @@
#include <time.h>
#include "daq.h"
#include "debug.h"
#include "helper.h"
#if defined(CONFIG_SOC_STM32L072XX)
+19
View File
@@ -21,3 +21,22 @@
#ifndef __weak
#define __weak __attribute__((__weak__))
#endif
#define printk printf
#define CONFIG_LOG_DEFAULT_LEVEL 2 // errors and warnings only
#define LOG_ERR(fmt, ...) \
if (CONFIG_LOG_DEFAULT_LEVEL & (1 << 0)) { printf(fmt, ##__VA_ARGS__); printf("\n"); }
#define LOG_WRN(fmt, ...) \
if (CONFIG_LOG_DEFAULT_LEVEL & (1 << 1)) { printf(fmt, ##__VA_ARGS__); printf("\n"); }
#define LOG_INF(fmt, ...) \
if (CONFIG_LOG_DEFAULT_LEVEL & (1 << 2)) { printf(fmt, ##__VA_ARGS__); printf("\n"); }
#define LOG_DBG(fmt, ...) \
if (CONFIG_LOG_DEFAULT_LEVEL & (1 << 3)) { printf(fmt, ##__VA_ARGS__); printf("\n"); }
// dummy
#define LOG_MODULE_REGISTER(a, b)
-2
View File
@@ -8,8 +8,6 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(libre_solar_charge_controller)
add_definitions(-DDEBUG_PRINT_FLAGS=7)
add_subdirectory(../src build/src)
add_subdirectory(../lib/thingset build/thingset)