#include "custom_utils.h"
-#define batteryservice_VOLTAGE_MAX (4.2f)
-#define batteryservice_VOLTAGE_MIN (3.2f)
-
bool batteryservice_get_voltage_by_channel(float *voltage, uint8_t channel);
battery_info_t *get_current_battery_info(void);
static bool g_batter_service_initialled = false;
errcode_t uapi_battery_init(void);
#include "tcxo.h"
-uint8_t batteryservice_get_voltage(float *voltage)
-{
- uint8_t percent = 0;
- battery_info_t *current_battery_info = get_current_battery_info();
- *voltage = current_battery_info->battery_voltage;
- if (*voltage <= batteryservice_VOLTAGE_MIN)
- return 0;
- if (*voltage >= batteryservice_VOLTAGE_MAX)
- return 100;
- percent = (*voltage - batteryservice_VOLTAGE_MIN)*100/(batteryservice_VOLTAGE_MAX - batteryservice_VOLTAGE_MIN) ;
- return percent;
-}
-uint8_t batteryservice_get_battery_temp(float *temp)
-{
- battery_info_t *current_battery_info = get_current_battery_info();
- *temp = current_battery_info->battery_ntc_temp;
-
- return 0;
-}
-uint8_t batteryservice_get_pcb_temp(float *temp)
-{
- battery_info_t *current_battery_info = get_current_battery_info();
- *temp = current_battery_info->pcb_ntc_temp;
- return 0;
-}
+
/**************************Battery Check Timer***********************/
/**
* 电池电压读取逻辑:
bool last_battery_charge_enable_status=true; // true上次刚设置在充电状态,false 上次设置不在充电状态。注意这个是充电IC的充电开关设置,并非真正处于充电状态的标识。
static unsigned long g_app_queue;
#define BATTERY_CMD_CHECK_VOLTAGE 0x11
-#define BATTERY_CMD_DISABLE_CHARGE 0x12
#define BATTERY_CMD_CHARGE_PLUG 0x13
#define BATTERY_CMD_CHARGE_UNPLUG 0x14
+
+#define BATTERY_LOWPOWER_ALARM_POWERLEVEL 15
+#define BATTERY_LOWPOWER_ALARM_SHUTDOWNLEVEL 5
+#define BATTERY_DISABLE_CHARGE_HI_TEMP_VALUE 50 //停止充电温度
+
+#define BATTERY_VOLTAGE_DEBOUNCE 2
+
static uint8_t g_check_count = 0;
static void battery_check_timer_callback(void* data);
osTimerStart(g_battery_timer, rtc_ms);
}
void send_battery_svr_cmd_msg(uint8_t cmd);
-static void battery_check_and_enable_charge(void);
+static void battery_check_voltage(void);
static void battery_check_timer_callback(void * data)
else
return 0;
}
-static void battery_check_and_enable_charge(void)
+
+
+static void battery_check_voltage(void)
{
battery_info_t *current_battery_info = get_current_battery_info();
batteryservice_get_voltage_by_channel(¤t_battery_info->battery_voltage,ADC_CHANNEL_BAT_VOLTAGE);
batteryservice_get_voltage_by_channel(¤t_battery_info->battery_ntc_temp,ADC_CHANNEL_BAT_TEMP);
uapi_tcxo_delay_ms(20);
batteryservice_get_voltage_by_channel(¤t_battery_info->pcb_ntc_temp,ADC_CHANNEL_PCB_TEMP);
- uapi_tcxo_delay_ms(20);
- enable_charge();
+ uapi_tcxo_delay_ms(20);
+}
+/**
+ * 充电中则使能充电IC,并根据电量确认充电电流.
+ * 未插入充电线,则停止充电IC
+ * 兼顾低电量报警检测
+*/
+static void battery_check_chargement(void)
+{
+ float voltage;
+ float bat_temp;
+ uint8_t percent = batteryservice_get_voltage(&voltage);
+ batteryservice_get_battery_temp(&bat_temp);
+
+ if(uapi_get_charger_plug_status()){
+
+ /**
+ * 如果充电过程中,电池温度>=BATTERY_DISABLE_CHARGE_HI_TEMP_VALUE,则停止充电
+ * 否则按正常流程充电
+ */
+ if(bat_temp >= BATTERY_DISABLE_CHARGE_HI_TEMP_VALUE)
+ {
+ disable_charge();
+ }else{
+ enable_charge();
+ /**
+ * check and control charge current event
+ * larger than 90%, use low charge current
+ * other stat , use high charge current
+ * pay attention to : debounce
+ */
+ if(percent >=90) enable_02c();
+ else disable_02c();
+ PRINT("%d\r\n",BATTERY_VOLTAGE_DEBOUNCE);
+ }
+
+ }else{
+ //冗余操作,理论上不必在此关闭充电IC
+ disable_charge();
+
+ /**
+ * if no in_charge then :
+ * check and report low power event
+ * report alarm lowpower event at 15%
+ * report alarm shutdown event at 5%
+ * pay attention to : debounce
+ */
+ if (percent >0 && percent <= BATTERY_LOWPOWER_ALARM_POWERLEVEL)
+ {
+ low_power_play_toUI(0);
+ }else if (percent <= BATTERY_LOWPOWER_ALARM_SHUTDOWNLEVEL)
+ {
+ low_power_play_toUI(1);
+ }
+ }
+
}
static void battery_check_task(void *arg)
{
common_notify_mail_t mail;
uint32_t msgsize = (uint32_t)sizeof(common_notify_mail_t);
+
+ /**开机初始化电量及充电状态 Start*/
if(uapi_get_charger_plug_status())
{ //如果充电线插入状态,为了获取准确电池数据,需要先停止充电一秒,再继续获取电量。
disable_charge();
uapi_tcxo_delay_ms(1000);
- battery_check_and_enable_charge();
+ battery_check_voltage();
}else{
- battery_check_and_enable_charge();
+ battery_check_voltage();
}
-
+ battery_check_chargement();
+ /**开机初始化电量及充电状态 End*/
while (1) {
ret = osal_msg_queue_read_copy(g_app_queue, (void *)&mail, &msgsize,
OSAL_MSGQ_WAIT_FOREVER);
if (is_xts_test_done() == 0)
continue;
#endif
- // get_charge_stat();
if (mail.comm.event == BATTERY_CMD_CHECK_VOLTAGE) {
+
g_check_count++;
// PRINT("BATTERY_CMD_CHECK_VOLTAGE g_check_count %d\r\n",g_check_count);
if(last_battery_charge_enable_status == true){
}else if (last_battery_charge_enable_status == false) {
if (g_check_count*CHARGE_CHECK_TIMER_CYCLE_MS >= DISABLE_CHARGE_CHECK_MS)
{
- // PRINT("BATTERY_CMD_CHECK_VOLTAGE battery_check_and_enable_charge \r\n");
-
+ // PRINT("BATTERY_CMD_CHECK_VOLTAGE battery_check_voltage \r\n");
g_check_count = 0 ;
//超时交换状态
- battery_check_and_enable_charge();
+ battery_check_voltage();
last_battery_charge_enable_status = true;
// charger_plug_event_ui_sendmsg_toUI(1);
+ //运行在battery_check_voltage之后,不然开始充电时电压检测值的误差会比较大
+ battery_check_chargement();
}
}
}else if (mail.comm.event == BATTERY_CMD_CHARGE_PLUG) {
// PRINT("[battery][service] charger_plug_event_ui_sendmsg_toUI plugged \n");
+ battery_check_chargement();
charger_plug_event_ui_sendmsg_toUI(1);
}else if (mail.comm.event == BATTERY_CMD_CHARGE_UNPLUG) {
// PRINT("[battery][service] charger_plug_event_ui_sendmsg_toUI unplugged \n");
+ disable_charge();
charger_plug_event_ui_sendmsg_toUI(0);
}
- }
+ }// while (1)
return 0;
}
-errcode_t Play_shutdown_animation_notify(uint8_t plugged)
+errcode_t Play_shutdown_animation_notify(uint8_t alarm_level)
{
- ChargerEventPublish(TOPIC_EVENT_LOWPOWER_ALARM,3);
- // return 0;
+ ChargerEventPublish(TOPIC_EVENT_LOWPOWER_ALARM,alarm_level);
return 0;
}
-
-errcode_t low_power_play_toUI(uint8_t plugged)
+/**
+ * alarm_level = 0, 提醒充电, 1,倒计时关机
+*/
+errcode_t low_power_play_toUI(uint8_t alarm_level)
{
/**通过UI事件发送Notify*/
- GraphicService::GetInstance()->PostGraphicEvent(std::bind(Play_shutdown_animation_notify, plugged));
+ GraphicService::GetInstance()->PostGraphicEvent(std::bind(Play_shutdown_animation_notify, alarm_level));
return ERRCODE_SUCC;
}
PRINT("[battery][service] battery_charger_plug_event_callback plugged=%u \n",plugged);
+ battery_info_t *batinfo = get_current_battery_info();
+
if(plugged == 1){
// PRINT("[battery][service] battery_charger_plug plugged \n");
send_battery_svr_cmd_msg(BATTERY_CMD_CHARGE_PLUG);
+ batinfo->usb_plugged = true;
}else{
// PRINT("[battery][service] battery_charger_plug unplugged \n");
send_battery_svr_cmd_msg(BATTERY_CMD_CHARGE_UNPLUG);
+ batinfo->usb_plugged = false;
}
return ERRCODE_SUCC;
}
+
+
void uapi_battery_create_queue(void)
{
if (osal_msg_queue_create("battery_queue", APP_BOARD_MSG_COUNT, &g_app_queue,