diff --git a/openrtx/src/core/battery.c b/openrtx/src/core/battery.c index b09a202e..f7a2e54f 100644 --- a/openrtx/src/core/battery.c +++ b/openrtx/src/core/battery.c @@ -26,17 +26,10 @@ * Obtained by multiplying the values in volt by 256. */ -#if defined CONFIG_BAT_LIPO_1S -static const uint16_t bat_v_min = 0x039C; // 3.61V -static const uint16_t bat_v_max = 0x0426; // 4.15V -#elif defined CONFIG_BAT_LIPO_2S -static const uint16_t bat_v_min = 0x071A; // 7.10V -static const uint16_t bat_v_max = 0x0819; // 8.10V -#elif defined CONFIG_BAT_LIPO_3S -static const uint16_t bat_v_min = 0x0AD4; // 10.83V -static const uint16_t bat_v_max = 0x0C73; // 12.45V -#elif defined CONFIG_BAT_NONE -// Nothing to do, just avoid arising the compiler error +#if defined(CONFIG_BAT_LIPO) +static const uint16_t vcell_max = 0x0414; // 4.08V +static const uint16_t vcell_min = 0x039C; // 3.61V +#elif defined(CONFIG_BAT_NONE) #else #error Please define a battery type into platform/targets/.../hwconfig.h #endif @@ -64,6 +57,9 @@ uint8_t battery_getCharge(uint16_t vbat) * of 0.79% and minimum error of -0.78% */ + const uint32_t vbat_max = vcell_max * CONFIG_BAT_NCELLS; + const uint32_t vbat_min = vcell_min * CONFIG_BAT_NCELLS; + uint32_t vb = vbat << 16; vb = vb / 1000; vb = (vb + 256) >> 8; @@ -72,10 +68,10 @@ uint8_t battery_getCharge(uint16_t vbat) * If the voltage is below minimum we return 0 to prevent an underflow in * the following calculation */ - if (vb < bat_v_min) return 0; + if (vb < vbat_min) return 0; - uint32_t diff = vb - bat_v_min; - uint32_t range = bat_v_max - bat_v_min; + uint32_t diff = vb - vbat_min; + uint32_t range = vbat_max - vbat_min; uint32_t result = ((diff << 8) / range) * 100; result += 128; result >>= 8; diff --git a/platform/targets/CS7000-PLUS/hwconfig.h b/platform/targets/CS7000-PLUS/hwconfig.h index 9a416a19..abd0a8d1 100644 --- a/platform/targets/CS7000-PLUS/hwconfig.h +++ b/platform/targets/CS7000-PLUS/hwconfig.h @@ -62,7 +62,8 @@ extern const struct sky73210 pll; #define CONFIG_SCREEN_BRIGHTNESS /* Battery type */ -#define CONFIG_BAT_LIPO_2S +#define CONFIG_BAT_LIPO +#define CONFIG_BAT_NCELLS 2 /* Device supports M17 mode */ #define CONFIG_M17 diff --git a/platform/targets/CS7000/hwconfig.h b/platform/targets/CS7000/hwconfig.h index e6186536..a50cf881 100644 --- a/platform/targets/CS7000/hwconfig.h +++ b/platform/targets/CS7000/hwconfig.h @@ -62,7 +62,8 @@ extern const struct sky73210 pll; #define CONFIG_SCREEN_BRIGHTNESS /* Battery type */ -#define CONFIG_BAT_LIPO_2S +#define CONFIG_BAT_LIPO +#define CONFIG_BAT_NCELLS 2 /* Device supports M17 mode */ #define CONFIG_M17 diff --git a/platform/targets/DM-1701/hwconfig.h b/platform/targets/DM-1701/hwconfig.h index 94ddd1a4..56b6cbbd 100644 --- a/platform/targets/DM-1701/hwconfig.h +++ b/platform/targets/DM-1701/hwconfig.h @@ -56,7 +56,8 @@ extern const struct Adc adc1; #define CONFIG_PIX_FMT_RGB565 /* Battery type */ -#define CONFIG_BAT_LIPO_2S +#define CONFIG_BAT_LIPO +#define CONFIG_BAT_NCELLS 2 /* Device supports M17 mode */ #define CONFIG_M17 diff --git a/platform/targets/GDx/hwconfig.h b/platform/targets/GDx/hwconfig.h index 304db077..d5c5bf5f 100644 --- a/platform/targets/GDx/hwconfig.h +++ b/platform/targets/GDx/hwconfig.h @@ -51,7 +51,8 @@ extern const struct spiDevice c6000_spi; #define CONFIG_SCREEN_BRIGHTNESS /* Battery type */ -#define CONFIG_BAT_LIPO_2S +#define CONFIG_BAT_LIPO +#define CONFIG_BAT_NCELLS 2 #ifdef __cplusplus } diff --git a/platform/targets/MD-3x0/hwconfig.h b/platform/targets/MD-3x0/hwconfig.h index 0c027c7f..d4a80ec1 100644 --- a/platform/targets/MD-3x0/hwconfig.h +++ b/platform/targets/MD-3x0/hwconfig.h @@ -61,7 +61,8 @@ extern const struct Adc adc1; #define CONFIG_SCREEN_BRIGHTNESS /* Battery type */ -#define CONFIG_BAT_LIPO_2S +#define CONFIG_BAT_LIPO +#define CONFIG_BAT_NCELLS 2 /* Device supports M17 mode */ #define CONFIG_M17 diff --git a/platform/targets/MD-UV3x0/hwconfig.h b/platform/targets/MD-UV3x0/hwconfig.h index 2e4cefd9..0f1a8354 100644 --- a/platform/targets/MD-UV3x0/hwconfig.h +++ b/platform/targets/MD-UV3x0/hwconfig.h @@ -58,7 +58,8 @@ extern const struct Adc adc1; #define CONFIG_PIX_FMT_RGB565 /* Battery type */ -#define CONFIG_BAT_LIPO_2S +#define CONFIG_BAT_LIPO +#define CONFIG_BAT_NCELLS 2 /* Device supports M17 mode */ #define CONFIG_M17 diff --git a/platform/targets/linux/hwconfig.h b/platform/targets/linux/hwconfig.h index 34c9e536..d314c025 100644 --- a/platform/targets/linux/hwconfig.h +++ b/platform/targets/linux/hwconfig.h @@ -33,7 +33,8 @@ enum Mod17Flags #define CONFIG_SCREEN_BRIGHTNESS /* Battery type */ -#define CONFIG_BAT_LIPO_2S +#define CONFIG_BAT_LIPO +#define CONFIG_BAT_NCELLS 2 /* Device supports M17 mode */ #define CONFIG_M17 diff --git a/platform/targets/ttwrplus/hwconfig.h b/platform/targets/ttwrplus/hwconfig.h index 98d6971e..3a678b63 100644 --- a/platform/targets/ttwrplus/hwconfig.h +++ b/platform/targets/ttwrplus/hwconfig.h @@ -31,7 +31,8 @@ #define CONFIG_PIX_FMT_BW #define CONFIG_GPS -#define CONFIG_BAT_LIPO_1S +#define CONFIG_BAT_LIPO +#define CONFIG_BAT_NCELLS 1 #define CONFIG_M17