diff --git a/Makefile b/Makefile index 5ed363d..bee9fc3 100644 --- a/Makefile +++ b/Makefile @@ -33,6 +33,7 @@ OBJS += driver/systick.o OBJS += driver/uart.o # Main +OBJS += battery.o OBJS += board.o OBJS += dcs.o OBJS += dtmf.o diff --git a/battery.c b/battery.c new file mode 100644 index 0000000..a05e8d5 --- /dev/null +++ b/battery.c @@ -0,0 +1,77 @@ +#include "battery.h" +#include "driver/backlight.h" +#include "misc.h" + +uint16_t gBatteryCalibration[6]; +uint16_t gBatteryBootVoltage; +uint16_t gBatteryCurrent; +uint16_t gBatteryVoltages[4]; +uint16_t gBatteryVoltageAverage; + +uint8_t gBatteryDisplayLevel; + +bool gChargingWithTypeC; +bool gMaybeLowBatteryWarning; + +void BATTERY_GetReadings(bool bDisplayBatteryLevel) +{ + uint16_t Voltage; + uint8_t PreviousBatteryLevel; + + PreviousBatteryLevel = gBatteryDisplayLevel; + + Voltage = (gBatteryVoltages[0] + gBatteryVoltages[1] + gBatteryVoltages[2] + gBatteryVoltages[3]) / 4; + + if (gBatteryCalibration[5] < Voltage) { + gBatteryDisplayLevel = 6; + } else if (gBatteryCalibration[4] < Voltage) { + gBatteryDisplayLevel = 5; + } else if (gBatteryCalibration[3] < Voltage) { + gBatteryDisplayLevel = 4; + } else if (gBatteryCalibration[2] < Voltage) { + gBatteryDisplayLevel = 3; + } else if (gBatteryCalibration[1] < Voltage) { + gBatteryDisplayLevel = 2; + } else if (gBatteryCalibration[0] < Voltage) { + gBatteryDisplayLevel = 1; + } else { + gBatteryDisplayLevel = 0; + } + + gBatteryVoltageAverage = (Voltage * 760) / gBatteryCalibration[3]; + +#if 0 + if ((gScreenToDisplay == MENU) && (gMenuCursor == MENU_VOL)) { + DAT_20000370 = 1; + } +#endif + if (gBatteryCurrent < 501) { + if (gChargingWithTypeC) { + g_2000036F = 1; + } + gChargingWithTypeC = 0; + } else { + if (gChargingWithTypeC == false) { + g_2000036F = 1; + BACKLIGHT_TurnOn(); + } + gChargingWithTypeC = 1; + } + + if (PreviousBatteryLevel != gBatteryDisplayLevel) { + if (gBatteryDisplayLevel < 2) { + gMaybeLowBatteryWarning = 1; + } else { + gMaybeLowBatteryWarning = 0; + if (bDisplayBatteryLevel) { +#if 0 + GUI_DisplayBatteryLevel(gBatteryDisplayLevel); +#endif + } + } +#if 0 + //DAT_20000400 = 0; +#endif + } +} + diff --git a/battery.h b/battery.h new file mode 100644 index 0000000..11245dd --- /dev/null +++ b/battery.h @@ -0,0 +1,21 @@ +#ifndef BATTERY_H +#define BATTERY_H + +#include +#include + +extern uint16_t gBatteryCalibration[6]; +extern uint16_t gBatteryBootVoltage; +extern uint16_t gBatteryCurrent; +extern uint16_t gBatteryVoltages[4]; +extern uint16_t gBatteryVoltageAverage; + +extern uint8_t gBatteryDisplayLevel; + +extern bool gChargingWithTypeC; +extern bool gMaybeLowBatteryWarning; + +void BATTERY_GetReadings(bool bDisplayBatteryLevel); + +#endif + diff --git a/board.c b/board.c index 138f002..3c6f5dc 100644 --- a/board.c +++ b/board.c @@ -16,6 +16,7 @@ */ #include +#include "battery.h" #include "board.h" #include "bsp/dp32g030/gpio.h" #include "bsp/dp32g030/portcon.h" diff --git a/main.c b/main.c index 4c856b3..48e17c5 100644 --- a/main.c +++ b/main.c @@ -17,6 +17,7 @@ #include #include "ARMCM0.h" +#include "battery.h" #include "bsp/dp32g030/gpio.h" #include "bsp/dp32g030/portcon.h" #include "bsp/dp32g030/syscon.h" @@ -128,7 +129,7 @@ void Main(void) // TODO: EEPROM Init BK4819_Init(); - BOARD_ADC_GetBatteryInfo(&gADC_CH4_BootValue, &gADC_CH9); + BOARD_ADC_GetBatteryInfo(&gBatteryBootVoltage, &gBatteryCurrent); BOARD_EEPROM_Init(); BOARD_EEPROM_LoadMoreSettings(); diff --git a/misc.c b/misc.c index cb3e769..b336723 100644 --- a/misc.c +++ b/misc.c @@ -40,11 +40,6 @@ uint8_t gEEPROM_1EC8_0[8]; uint8_t gEEPROM_1EC8_1[8]; uint8_t gEEPROM_1EC8_2[8]; -uint16_t gBatteryCalibration[6]; -uint16_t gADC_CH4_BootValue; -uint16_t gADC_CH4[4]; -uint16_t gADC_CH9; - uint16_t gEEPROM_1F8A; uint16_t gEEPROM_1F8C; uint8_t gEEPROM_1F8E; diff --git a/misc.h b/misc.h index da8f5e3..c4fd78d 100644 --- a/misc.h +++ b/misc.h @@ -44,11 +44,6 @@ extern uint8_t gEEPROM_1EC8_0[8]; extern uint8_t gEEPROM_1EC8_1[8]; extern uint8_t gEEPROM_1EC8_2[8]; -extern uint16_t gBatteryCalibration[6]; -extern uint16_t gADC_CH4_BootValue; -extern uint16_t gADC_CH4[4]; -extern uint16_t gADC_CH9; - extern uint16_t gEEPROM_1F8A; extern uint16_t gEEPROM_1F8C; extern uint8_t gEEPROM_1F8E;