From c2ef955a636aa9b9bc1094303f0e853d29755286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Thu, 23 Jul 2020 11:26:55 +0200 Subject: [PATCH] Replace custom print_* with Zephyr LOG_* --- platformio.ini | 2 -- src/bat_charger.cpp | 19 +++++++++++-------- src/daq.cpp | 10 +++++++--- src/daq_driver.c | 5 ++--- src/data_nodes.cpp | 12 +++++++----- src/dcdc.cpp | 25 ++++++++++++++----------- src/debug.h | 37 ------------------------------------- src/device_status.cpp | 1 - src/ext/can.cpp | 14 +++++++------- src/hardware.cpp | 4 ++-- src/load.cpp | 1 - src/load_driver.c | 3 +-- src/pwm_switch.cpp | 16 ++++++++++------ src/pwm_switch_driver.c | 1 - test/zephyr.h | 19 +++++++++++++++++++ zephyr/CMakeLists.txt | 2 -- 16 files changed, 80 insertions(+), 91 deletions(-) delete mode 100644 src/debug.h diff --git a/platformio.ini b/platformio.ini index 7d48189..78d9f0e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/bat_charger.cpp b/src/bat_charger.cpp index 7495580..a1974ec 100644 --- a/src/bat_charger.cpp +++ b/src/bat_charger.cpp @@ -8,6 +8,11 @@ #include +#ifndef UNIT_TEST +#include +LOG_MODULE_REGISTER(bat_charger, CONFIG_LOG_DEFAULT_LEVEL); +#endif + #include // for fabs function #include @@ -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; } diff --git a/src/daq.cpp b/src/daq.cpp index 97a0d66..3aafbaa 100644 --- a/src/daq.cpp +++ b/src/daq.cpp @@ -8,12 +8,16 @@ #include +#ifndef UNIT_TEST +#include +LOG_MODULE_REGISTER(daq, CONFIG_LOG_DEFAULT_LEVEL); +#endif + #include // log for thermistor calculation #include #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); } diff --git a/src/daq_driver.c b/src/daq_driver.c index cd3283e..5c2cb82 100644 --- a/src/daq_driver.c +++ b/src/daq_driver.c @@ -6,7 +6,7 @@ #include "daq.h" -#if defined(__ZEPHYR__) +#ifndef UNIT_TEST #include #include @@ -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 diff --git a/src/data_nodes.cpp b/src/data_nodes.cpp index e0b6c46..e4dd69a 100644 --- a/src/data_nodes.cpp +++ b/src/data_nodes.cpp @@ -10,6 +10,8 @@ #include #include #include +#include +LOG_MODULE_REGISTER(data_nodes, CONFIG_LOG_DEFAULT_LEVEL); #endif #include @@ -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); } } diff --git a/src/dcdc.cpp b/src/dcdc.cpp index ee63ea2..74451d9 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -10,6 +10,9 @@ #ifndef UNIT_TEST #include + +#include +LOG_MODULE_REGISTER(dcdc, CONFIG_LOG_DEFAULT_LEVEL); #endif #include // for fabs function @@ -17,7 +20,6 @@ #include #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 diff --git a/src/debug.h b/src/debug.h deleted file mode 100644 index 88078cc..0000000 --- a/src/debug.h +++ /dev/null @@ -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 - -// 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 */ diff --git a/src/device_status.cpp b/src/device_status.cpp index 8f5f262..c2af86f 100644 --- a/src/device_status.cpp +++ b/src/device_status.cpp @@ -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; diff --git a/src/ext/can.cpp b/src/ext/can.cpp index 0cfa691..ef6da71 100644 --- a/src/ext/can.cpp +++ b/src/ext/can.cpp @@ -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"); } } } diff --git a/src/hardware.cpp b/src/hardware.cpp index 98a3231..6e8a8a0 100644 --- a/src/hardware.cpp +++ b/src/hardware.cpp @@ -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(); } } diff --git a/src/load.cpp b/src/load.cpp index e98557f..a54fd39 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -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) diff --git a/src/load_driver.c b/src/load_driver.c index 7217558..0e8fa54 100644 --- a/src/load_driver.c +++ b/src/load_driver.c @@ -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 diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index d0b9e19..cb5ab78 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -8,12 +8,16 @@ #include +#ifndef UNIT_TEST +#include +LOG_MODULE_REGISTER(pwm_switch, CONFIG_LOG_DEFAULT_LEVEL); +#endif + #include #include #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."); } } } diff --git a/src/pwm_switch_driver.c b/src/pwm_switch_driver.c index 7b05dc9..54722d3 100644 --- a/src/pwm_switch_driver.c +++ b/src/pwm_switch_driver.c @@ -12,7 +12,6 @@ #include #include "daq.h" -#include "debug.h" #include "helper.h" #if defined(CONFIG_SOC_STM32L072XX) diff --git a/test/zephyr.h b/test/zephyr.h index 8e56c91..0a4a884 100644 --- a/test/zephyr.h +++ b/test/zephyr.h @@ -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) diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index cc2d053..0738b6a 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -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)