This commit is contained in:
helloyifa 2025-07-17 01:25:17 +08:00
commit cf0d3cf76a
5 changed files with 66 additions and 8 deletions

View File

@ -4,7 +4,7 @@
#include "bk_gpio.h" #include "bk_gpio.h"
#include "bk_uart.h" #include "bk_uart.h"
#define BAT_MONITOR_DEBUG //#define BAT_MONITOR_DEBUG
#ifdef BAT_MONITOR_DEBUG #ifdef BAT_MONITOR_DEBUG
#define BAT_MONITOR_PRT os_printf #define BAT_MONITOR_PRT os_printf

View File

@ -30,11 +30,14 @@
static const uint32_t s_factory_volume = 7; static const uint32_t s_factory_volume = 7;
static const uint32_t s_bat_offset = 0;
const struct factory_config_t s_platform_config[] = { const struct factory_config_t s_platform_config[] = {
{"sys_initialized", (void *)"1", 1, BK_FALSE, 1}, // first config used to check whether factory config initialized. {"sys_initialized", (void *)"1", 1, BK_FALSE, 1}, // first config used to check whether factory config initialized.
{"volume", (void *)&s_factory_volume, 4, BK_TRUE, 4}, {"volume", (void *)&s_factory_volume, 4, BK_TRUE, 4},
{"d_network_id", (void *)"\0", 1, BK_TRUE, 198}, {"d_network_id", (void *)"\0", 1, BK_TRUE, 198},
{"d_agent_info", (void *)"\0", 1, BK_TRUE, 162}, {"d_agent_info", (void *)"\0", 1, BK_TRUE, 162},
{"bat_offset", (void *)&s_bat_offset, 4, BK_TRUE, 4},
}; };
static const struct factory_config_t *s_user_reg_config = NULL; static const struct factory_config_t *s_user_reg_config = NULL;

View File

@ -48,6 +48,8 @@
#include <os/os.h> #include <os/os.h>
#include "sys_driver.h" #include "sys_driver.h"
#include <bat_main.h> #include <bat_main.h>
#if (CONFIG_SYS_CPU0)
#define BAT_DETEC_ADC_CLK 203125 #define BAT_DETEC_ADC_CLK 203125
#define BAT_DETEC_ADC_STEADY_CTRL 7 #define BAT_DETEC_ADC_STEADY_CTRL 7
#define BAT_DETEC_ADC_SAMPLE_RATE 0 #define BAT_DETEC_ADC_SAMPLE_RATE 0
@ -56,6 +58,8 @@
#define BATTERY_MIN_VOLTAGE 3400 #define BATTERY_MIN_VOLTAGE 3400
static int batt_percent = 0; static int batt_percent = 0;
int bat_offset = 0;
uint16_t vol;
static battery_main_event_callback_t s_battery_main_event_callback = NULL; static battery_main_event_callback_t s_battery_main_event_callback = NULL;
int battery_main_event_callback_register(battery_main_event_callback_t callback) int battery_main_event_callback_register(battery_main_event_callback_t callback)
{ {
@ -100,7 +104,8 @@ static uint16_t prvCalculateVoltage(void)
static bk_err_t prvStartBatteryAdcOneTime() static bk_err_t prvStartBatteryAdcOneTime()
{ {
// uint16_t value = 0; // uint16_t value = 0;
uint16_t vol;
uint16_t currend_vol;
// int i1 = 0; // int i1 = 0;
// if (vol == NULL) { // if (vol == NULL) {
// BAT_MONITOR_WPRT("Error: vol pointer is NULL\r\n"); // BAT_MONITOR_WPRT("Error: vol pointer is NULL\r\n");
@ -162,23 +167,26 @@ static bk_err_t prvStartBatteryAdcOneTime()
// } // }
vol = prvCalculateVoltage(); vol = prvCalculateVoltage();
bk_printf("adc prevol %d offset %d \n", vol,bat_offset);
currend_vol = vol+ bat_offset;
// BAT_MONITOR_PRT("ADC VALUE: %d .\r\n", *vol); // BAT_MONITOR_PRT("ADC VALUE: %d .\r\n", *vol);
gpio_dev_unmap(GPIO_50); gpio_dev_unmap(GPIO_50);
bk_gpio_enable_input(GPIO_50); bk_gpio_enable_input(GPIO_50);
rtos_delay_milliseconds(100); rtos_delay_milliseconds(100);
uint8_t insert_value = bk_gpio_get_input(GPIO_50); uint8_t insert_value = bk_gpio_get_input(GPIO_50);
bk_printf("Charging insert_value %d \n", insert_value); bk_printf("Charging insert_value %d \n", insert_value);
bk_printf("ADC bat_value: %d mv\r\n", vol); bk_printf("ADC currend_vol: %d mv\r\n", currend_vol);
batt_percent=(vol-BATTERY_MIN_VOLTAGE)*100/(BATTERY_MAX_VOLTAGE-BATTERY_MIN_VOLTAGE); batt_percent=(currend_vol-BATTERY_MIN_VOLTAGE)*100/(BATTERY_MAX_VOLTAGE-BATTERY_MIN_VOLTAGE);
if(vol < BATTERY_MIN_VOLTAGE){ if(currend_vol < BATTERY_MIN_VOLTAGE){
batt_percent=0; batt_percent=0;
} }
else if(vol > BATTERY_MAX_VOLTAGE){ else if(currend_vol > BATTERY_MAX_VOLTAGE){
batt_percent = 100; batt_percent = 100;
} }
bk_printf("batt_percent:%d\r\n",batt_percent); bk_printf("batt_percent:%d\r\n",batt_percent);
if (vol < 3500) if (currend_vol < 3500)
{ {
if (s_battery_main_event_callback && insert_value == 0) if (s_battery_main_event_callback && insert_value == 0)
{ {
@ -228,6 +236,11 @@ void init_bat_timer()
{ {
bk_printf("rtos_start_bat_timer fail\r\n"); bk_printf("rtos_start_bat_timer fail\r\n");
} }
bk_config_read("bat_offset", (void *)&bat_offset, 4);
bk_printf("init_bat_timer_offset %d\r\n",bat_offset);
prvStartBatteryAdcOneTime(); prvStartBatteryAdcOneTime();
} }
@ -254,7 +267,26 @@ void poweroff_voice()
int battery_get_percent(){ int battery_get_percent(){
return batt_percent; return batt_percent;
} }
//AT+BAT_CALIBRTATION
int battery_calibration(){
bk_err_t ret = prvStartBatteryAdcOneTime();
if (ret == BK_OK)
{
ret = 1;
bat_offset = 3980-vol;
bk_config_write("bat_offset", (void *)&bat_offset, 4);
bk_config_sync_flash_safely();
bk_printf("BAT_CALIBRTATION SUCC ret: %d new_offset = %d\r\n", ret,bat_offset);
}else{
ret = 0;
bk_printf("Failed to BAT_CALIBRTATION, err: %d\r\n", ret);
}
return ret;
}
#endif

View File

@ -24,6 +24,8 @@ int battery_main_event_callback_register(battery_main_event_callback_t callback)
void init_bat_timer(); void init_bat_timer();
void poweron_voice(); void poweron_voice();
void poweroff_voice(); void poweroff_voice();
int battery_calibration();
/** /**
* *
*/ */

View File

@ -836,6 +836,27 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
ws2812_led_stop_ota(); ws2812_led_stop_ota();
}
/**
* APP
*/
else if (NULL != (ptr = strstr((char *)recv, "AT+BAT_CALIBRTATION")))
{
wboard_loge("AT+BAT_CALIBRTATION!!");
int ret = battery_calibration();
bk_printf("AT+BAT_CALIBRTATION ret: %d\r\n", ret);
uint8_t str_respone[32] = {0};
memset(str_respone, 0, sizeof(str_respone));
if(ret == 1){
sprintf((char *)str_respone, "%s", "AT+BAT_CALIBRTATION=OK");
}else{
sprintf((char *)str_respone, "%s", "AT+BAT_CALIBRTATION=FAIL");
}
wifi_boarding_notify(str_respone, strlen((char *)&str_respone));
} }
/** /**
* 12. * 12.