Use battery only if specified in devicetree

This commit is contained in:
Tobias Schramm 2021-02-09 14:34:35 +01:00 committed by Patrick Rathje
parent 0dd0ddecec
commit 17de4f2523
3 changed files with 24 additions and 4 deletions

View File

@ -4,11 +4,13 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include "battery.h"
#ifdef BATTERY_SUPPORTED
#include <device.h>
#include <drivers/adc.h>
#include "battery.h"
#define VBATT DT_PATH(vbatt)
#define BATTERY_ADC_GAIN ADC_GAIN_1_6
#define BATTERY DT_PATH(battery)
@ -74,6 +76,9 @@ uint16_t battery_get_voltage_mv() {
return battery_adc_config.value_mv;
}
#endif
#ifdef BATTERY_SOC_SUPPORTED
const uint32_t battery_soc_voltage_mv_pairs[] = DT_PROP(BATTERY, soc_voltage_mv);
#if DT_PROP_LEN(BATTERY, soc_voltage_mv) % 2 != 0
@ -113,4 +118,5 @@ uint32_t battery_voltage_mv_to_soc(uint16_t batt_mv) {
}
return last_soc;
}
}
#endif

View File

@ -1,8 +1,18 @@
#pragma once
#include <devicetree.h>
#include <stdint.h>
// Generic voltage divider based battery support
#if DT_NODE_HAS_STATUS(DT_PATH(vbatt), okay)
#define BATTERY_SUPPORTED
int battery_init();
int battery_update();
uint16_t battery_get_voltage_mv();
uint32_t battery_voltage_mv_to_soc(uint16_t batt_mv);
#endif
// SOC estimation support
#if DT_NODE_HAS_STATUS(DT_PATH(battery), okay)
#define BATTERY_SOC_SUPPORTED
uint32_t battery_voltage_mv_to_soc(uint16_t batt_mv);
#endif

View File

@ -36,6 +36,7 @@ void main(void)
platform_display_draw_string(0, 0, "Hello World!");
platform_display_draw_string(0, DISPLAY_LINE_LAST_CONTACTS_START, "Seen identifiers:");
#ifdef BATTERY_SUPPORTED
err = battery_init();
if (err) {
printk("Failed to initialize battery gauging: %d\n", err);
@ -50,6 +51,7 @@ void main(void)
platform_display_draw_string(0, DISPLAY_LINE_BATTERY_VOLTAGE, "Battery voltage:");
platform_display_draw_string(0, DISPLAY_LINE_BATTERY_VOLTAGE + 2, "Battery SOC:");
#endif
// first init everything
// Use custom randomization as the mbdet_tls context initialization messes with the Zeyhr BLE stack.
@ -98,8 +100,10 @@ void main(void)
snprintf(tmpstr, sizeof(tmpstr), "%04u mV", battery_get_voltage_mv());
platform_display_draw_string(0, DISPLAY_LINE_BATTERY_VOLTAGE + 1, tmpstr);
#ifdef BATTERY_SOC_SUPPORTED
snprintf(tmpstr, sizeof(tmpstr), "% 3u%%", battery_voltage_mv_to_soc(battery_get_voltage_mv()));
platform_display_draw_string(0, DISPLAY_LINE_BATTERY_VOLTAGE + 3, tmpstr);
#endif
}
do_covid();
do_gatt();