Skip to content

Commit

Permalink
Merge pull request #103 from Repeerc/main
Browse files Browse the repository at this point in the history
Charging function optimization.
  • Loading branch information
Repeerc authored Dec 8, 2022
2 parents b916b3b + 8667c65 commit f85e083
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 37 deletions.
3 changes: 2 additions & 1 deletion OSLoader/Config/SystemConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@
#define SIZE_SWAPAREA_MB (VM_RAM_SIZE / 1048576)

#define CPU_DIVIDE_NORMAL 1
#define CPU_DIVIDE_IDLE_INTIAL 6
#define CPU_DIVIDE_PWRSAVE 3
#define CPU_DIVIDE_IDLE_INTIAL 10

#define FLASH_LOADER_BLOCK 22
#define FLASH_CONFIG_BLOCK 23
Expand Down
17 changes: 12 additions & 5 deletions OSLoader/HAL/Hardware/stmp_clkctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#define PLL_FREQ_HZ (480000000UL)

bool g_slowdown_enable = true;
int g_slowdown_enable = 0;
static uint8_t min_cpu_frac_sd = CPU_DIVIDE_IDLE_INTIAL;

static void PLLEnable(bool enable) {
Expand Down Expand Up @@ -50,20 +50,27 @@ void enterSlowDown()

void exitSlowDown()
{
if(g_slowdown_enable)
if(g_slowdown_enable == 1)
{
setCPUDivider(CPU_DIVIDE_NORMAL);
}else if(g_slowdown_enable == 2){
setCPUDivider(CPU_DIVIDE_PWRSAVE);
}else{
setCPUDivider(CPU_DIVIDE_NORMAL);
}

}


void slowDownEnable(bool enable)
void slowDownEnable(int mode)
{
g_slowdown_enable = enable;
if(!g_slowdown_enable)
g_slowdown_enable = mode;
if(g_slowdown_enable == 0)
{
setCPUDivider(CPU_DIVIDE_NORMAL);
}else if(g_slowdown_enable == 2)
{
setCPUDivider(CPU_DIVIDE_PWRSAVE);
}
}

Expand Down
26 changes: 15 additions & 11 deletions OSLoader/HAL/Hardware/stmp_power.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ void portChargeEnable(bool enable)
{
HW_POWER_CHARGE.B.PWD_BATTCHRG = 0;
HW_POWER_VDDDCTRL.B.DISABLE_FET = 0;
HW_POWER_5VCTRL.B.ENABLE_DCDC = 1;
}else{
HW_POWER_CHARGE.B.PWD_BATTCHRG = 1;
HW_POWER_VDDDCTRL.B.DISABLE_FET = 1;
HW_POWER_5VCTRL.B.ENABLE_DCDC = 0;
}
}

Expand Down Expand Up @@ -157,16 +159,18 @@ void portPowerInit()
*/


/*
BF_WR(POWER_DCLIMITS, POSLIMIT_BUCK, 0x60);
BF_WR(POWER_DCLIMITS, POSLIMIT_BOOST, 0x60);
BF_WR(POWER_DCLIMITS, NEGLIMIT, 0x6F);
*/
BF_WR(POWER_DCLIMITS, POSLIMIT_BUCK, 0xF);

BF_WR(POWER_DCLIMITS, POSLIMIT_BOOST, 0xF);
BF_WR(POWER_DCLIMITS, POSLIMIT_BUCK, 0xF);
BF_WR(POWER_DCLIMITS, NEGLIMIT, 0x6F);


//BF_WR(POWER_DCLIMITS, POSLIMIT_BUCK, 0xF);
//BF_WR(POWER_DCLIMITS, POSLIMIT_BOOST, 0xF);
//BF_WR(POWER_DCLIMITS, NEGLIMIT, 0x6F);



//BF_SET(POWER_LOOPCTRL, RCSCALE_THRESH);
//HW_POWER_LOOPCTRL.B.EN_RCSCALE = 3;

Expand All @@ -190,18 +194,18 @@ void portPowerInit()
HW_POWER_LOOPCTRL.B.DC_C = 0;


HW_POWER_5VCTRL.B.OTG_PWRUP_CMPS = 1; //VBUSVALID comparators are enabled
HW_POWER_5VCTRL.B.OTG_PWRUP_CMPS = 1; //VBUSVALID comparators are enabled !
HW_POWER_5VCTRL.B.VBUSVALID_5VDETECT = 1;
HW_POWER_5VCTRL.B.ILIMIT_EQ_ZERO = 0; //short

HW_POWER_5VCTRL.B.PWDN_5VBRNOUT = 0;

HW_POWER_5VCTRL.B.ENABLE_DCDC = 0;
HW_POWER_5VCTRL.B.ENABLE_DCDC = 0; //Enable DC_DC later

portDelayus(100);

HW_POWER_BATTMONITOR.B.BRWNOUT_LVL = 0; // 0.79 V
HW_POWER_BATTMONITOR.B.BRWNOUT_PWD = 1;
HW_POWER_BATTMONITOR.B.BRWNOUT_PWD = 0;
HW_POWER_BATTMONITOR.B.EN_BATADJ = 1;
HW_POWER_BATTMONITOR.B.PWDN_BATTBRNOUT = 1;

Expand Down Expand Up @@ -243,9 +247,9 @@ void portPowerInit()

//HW_POWER_5VCTRL.B.EN_BATT_PULLDN = 1;

BF_WR(POWER_VDDDCTRL, TRG, (uint8_t)((1.43 - 0.8)/0.025) ); // val = (TAG_v - 0.8v)/0.025v, 0.8~1.45, 1.2
BF_WR(POWER_VDDDCTRL, TRG, (uint8_t)((1.44 - 0.8)/0.025) ); // val = (TAG_v - 0.8v)/0.025v, 0.8~1.45, 1.2
portDelayus(200);
BF_WR(POWER_VDDACTRL, TRG, (uint8_t)((1.75 - 1.5)/0.025) ); // val = (TAG_v - 1.5v)/0.025v, 1.5~1.95, 1.75
BF_WR(POWER_VDDACTRL, TRG, (uint8_t)((1.8 - 1.5)/0.025) ); // val = (TAG_v - 1.5v)/0.025v, 1.5~1.95, 1.75
portDelayus(200);
BF_WR(POWER_VDDIOCTRL, TRG, (uint8_t)((3.3 - 2.8)/0.025)); // val = (TAG_v - 2.8v)/0.025v, 2.8~3.575, 3.1
portDelayus(100);
Expand Down
2 changes: 1 addition & 1 deletion OSLoader/HAL/board_up.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void portGetCoreFreqDIV(uint32_t *CPU_DIV, uint32_t *CPU_Frac, uint32_t *HCLK_DI

void enterSlowDown();
void exitSlowDown();
void slowDownEnable(bool enable);
void slowDownEnable(int mode);
void setSlowDownMinCpuFrac(uint8_t frac);

void stmp_audio_init();
Expand Down
36 changes: 25 additions & 11 deletions OSLoader/start.c
Original file line number Diff line number Diff line change
Expand Up @@ -637,15 +637,14 @@ void __attribute__((target("thumb"))) vMainThread_thumb_entry(void *pvParameters

vTaskDelay(pdMS_TO_TICKS(1000));

HW_POWER_5VCTRL.B.ENABLE_DCDC = 1;
HW_POWER_5VCTRL.B.ENABLE_DCDC = 0;

HW_POWER_CHARGE.B.CHRG_STS_OFF = 0;

HW_POWER_CHARGE.B.BATTCHRG_I = 1 << 5;
HW_POWER_CHARGE.B.STOP_ILIMIT = 0;

HW_POWER_CHARGE.B.PWD_BATTCHRG = 1;

HW_POWER_VDDDCTRL.B.DISABLE_FET = 1;

// HW_POWER_VDDACTRL.B.DISABLE_FET = 1;
Expand Down Expand Up @@ -879,20 +878,35 @@ void vBatteryMon(void *__n) {
vdd5v_voltage = (int)(portLRADCConvCh(5, 5) * 0.45 * 4);
coreTemp = (int)((portLRADCConvCh(4, 5) - portLRADCConvCh(3, 5)) * 1.012 / 4 - 273.15);

if (t % 5 == 0) {
extern bool g_chargeEnable;
if (g_chargeEnable) {
if (batt_voltage >= 1420) {

g_core_temp = coreTemp;
g_batt_volt = batt_voltage;
HW_POWER_5VCTRL.B.ENABLE_DCDC = 0;
printf("Disable DCDC\n");

if (portGetBatteryMode() == 0) {
printf("Battery = Li-ion\n");
} else {
printf("Battery = Single AA or AAA\n");
extern bool g_chargeEnable;
if (batt_voltage >= 1500) {
portChargeEnable(false);
}; //
}
}

if (t % 3 == 0) {

g_core_temp = coreTemp;
g_batt_volt = batt_voltage;
/*
if (portGetBatteryMode() == 0) {
printf("Battery = Li-ion\n");
} else {
printf("Battery = Single AA or AAA\n");
}
*/
printf("Batt. voltage:%ld mv, adc:%ld\n", batt_voltage, vatt_adc);
printf("VDDIO: %d mV\n", (int)(portLRADCConvCh(6, 5) * 0.9));
printf("VDD5V: %ld mV\n", vdd5v_voltage);
printf("VBG: %d mV\n", (int)(portLRADCConvCh(2, 5) * 0.45));
// printf("VBG: %d mV\n", (int)(portLRADCConvCh(2, 5) * 0.45));
printf("Core Temp: %d ℃\n", coreTemp);
printf("Power Speed:%lu\n", portGetPWRSpeed());
}
Expand All @@ -907,7 +921,7 @@ void vBatteryMon(void *__n) {
if (show_bat_val < 800) {
show_bat_val = 800;
}
vTaskDelay(pdMS_TO_TICKS(2000));
vTaskDelay(pdMS_TO_TICKS(1000));
n = 0;
if (((show_bat_val - 800) * 100 / (1500 - 800)) >= ((100 / 4) * 1))
n |= (1 << 0);
Expand Down
18 changes: 12 additions & 6 deletions System/UICore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,12 +702,15 @@ void keyMsg(uint32_t key, int state) {
if (curPage == 3) {
switch (page3Subpage) {
case 0:
if (power_save == 'X') {
if (power_save == 'A') {
power_save = 'B';
ll_cpu_slowdown_enable(2);
} else if (power_save == 'B') {
power_save = ' ';
ll_cpu_slowdown_enable(false);
} else {
power_save = 'X';
ll_cpu_slowdown_enable(true);
ll_cpu_slowdown_enable(0);
} else if (power_save == ' ') {
power_save = 'A';
ll_cpu_slowdown_enable(1);
}
break;

Expand Down Expand Up @@ -743,7 +746,10 @@ void keyMsg(uint32_t key, int state) {

ll_charge_enable(false);
} else {

if (power_save != 'B'){
power_save = 'B';
ll_cpu_slowdown_enable(2);
}
ll_charge_enable(true);
}
break;
Expand Down
2 changes: 1 addition & 1 deletion System/sys_llapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ DECDEF_LLSWI(uint32_t, ll_get_core_temp, (void)
DECDEF_LLSWI(uint32_t, ll_get_cur_freq, (void) ,LL_FAST_SWI_CORE_CUR_FREQ );
DECDEF_LLSWI(uint32_t, ll_get_charge_status, (void) ,LL_FAST_SWI_GET_CHARGE_STATUS );
DECDEF_LLSWI(uint32_t, ll_charge_enable, (bool enable) ,LL_SWI_CHARGE_ENABLE );
DECDEF_LLSWI(uint32_t, ll_cpu_slowdown_enable, (bool enable) ,LL_SWI_SLOW_DOWN_ENABLE );
DECDEF_LLSWI(uint32_t, ll_cpu_slowdown_enable, (int mode) ,LL_SWI_SLOW_DOWN_ENABLE );
DECDEF_LLSWI(uint32_t, ll_cpu_slowdown_min_frac, (uint32_t val) ,LL_SWI_SLOW_DOWN_MINFRAC );
DECDEF_LLSWI(uint32_t, ll_rtc_get_sec, (void) ,LL_FAST_SWI_RTC_GET_SEC );
DECDEF_LLSWI(void, ll_rtc_set_sec, (uint32_t val) ,LL_FAST_SWI_RTC_SET_SEC );
Expand Down
2 changes: 1 addition & 1 deletion System/sys_llapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ DECDEF_LLSWI(uint32_t, ll_get_core_temp, (void)
DECDEF_LLSWI(uint32_t, ll_get_cur_freq, (void) ,LL_FAST_SWI_CORE_CUR_FREQ );
DECDEF_LLSWI(uint32_t, ll_get_charge_status, (void) ,LL_FAST_SWI_GET_CHARGE_STATUS );
DECDEF_LLSWI(uint32_t, ll_charge_enable, (bool enable) ,LL_SWI_CHARGE_ENABLE );
DECDEF_LLSWI(uint32_t, ll_cpu_slowdown_enable, (bool enable) ,LL_SWI_SLOW_DOWN_ENABLE );
DECDEF_LLSWI(uint32_t, ll_cpu_slowdown_enable, (int mode) ,LL_SWI_SLOW_DOWN_ENABLE );
DECDEF_LLSWI(uint32_t, ll_cpu_slowdown_min_frac, (uint32_t val) ,LL_SWI_SLOW_DOWN_MINFRAC );
DECDEF_LLSWI(uint32_t, ll_rtc_get_sec, (void) ,LL_FAST_SWI_RTC_GET_SEC );
DECDEF_LLSWI(void, ll_rtc_set_sec, (uint32_t val) ,LL_FAST_SWI_RTC_SET_SEC );
Expand Down

0 comments on commit f85e083

Please sign in to comment.