1313#include <esp_adc/adc_oneshot.h>
1414
1515#include "bsp/esp-bsp.h"
16+ #include "adc_battery_estimation.h"
1617
1718int g_battery_percent ;
1819
1920static const char * TAG = "BATTERY-ADC" ;
2021
21- static adc_oneshot_unit_handle_t adc1_handle ;
22- static TaskHandle_t battery_task_handle = NULL ;
22+ static TaskHandle_t battery_monitor_task_handle ;
2323
2424static void battery_monitor_task (void * pvParameters );
2525
2626/* Pull up resistor value. Unit: KOhm */
2727#define PULL_UP_RESISTOR (51)
2828/* Pull down resistor value. Unit: KOhm */
2929#define PULL_DOWN_RESISTOR (100)
30- /* Max voltage value. Unit: mV */
31- #define MAX_REF_VOLTAGE (3469)
3230
3331esp_err_t battery_adc_init (void )
3432{
35- if (battery_task_handle != NULL ) {
36- ESP_LOGW (TAG , "Battery ADC already initialized " );
33+ if (battery_monitor_task_handle != NULL ) {
34+ ESP_LOGW (TAG , "Battery monitor task already created! " );
3735 return ESP_OK ;
3836 }
3937
40- /* ADC1 Init */
41- adc_oneshot_unit_init_cfg_t unit_init_cfg = {
42- .unit_id = ADC_UNIT_1 ,
43- .clk_src = ADC_RTC_CLK_SRC_DEFAULT ,
44- .ulp_mode = ADC_ULP_MODE_DISABLE
45- };
46- ESP_ERROR_CHECK (adc_oneshot_new_unit (& unit_init_cfg , & adc1_handle ));
47-
48- /* ADC1 Config */
49- adc_oneshot_chan_cfg_t channel_config = {
50- /* Theoretical battery voltage measurement range is about 2.45 to 2.78V.
51- * So set the input attenuation to 12 dB (about 4x) */
52- .atten = ADC_ATTEN_DB_12 ,
53- .bitwidth = ADC_BITWIDTH_12
54- };
55- ESP_ERROR_CHECK (adc_oneshot_config_channel (adc1_handle , KBD_BATTERY_MONITOR_CHANNEL , & channel_config ));
56-
57- if (xTaskCreate (battery_monitor_task , "battery_monitor" , 4096 , NULL , 2 , & battery_task_handle ) != pdPASS ) {
38+ if (xTaskCreate (battery_monitor_task , "battery_monitor" , 4096 , NULL , 2 , & battery_monitor_task_handle ) != pdPASS ) {
5839 return ESP_ERR_NO_MEM ;
5940 }
6041
@@ -64,44 +45,31 @@ esp_err_t battery_adc_init(void)
6445static void battery_monitor_task (void * pvParameters )
6546{
6647 ESP_UNUSED (pvParameters );
67- /* The table of voltage convert percent. */
68- const static int ocv_capacity_table [] = {
69- 3680 , 3700 , 3740 , 3770 , 3790 , 3820 , 3870 , 3920 , 3980 , 4060 , 4200
48+ adc_battery_estimation_t config = {
49+ .internal = {
50+ .adc_unit = KBD_BATTERY_MONITOR_ADC_UNIT ,
51+ .adc_bitwidth = ADC_BITWIDTH_DEFAULT ,
52+ /* Theoretical battery voltage measurement range is about 2.45 to 2.78V.
53+ * So set the input attenuation to 12 dB (about 4x) */
54+ .adc_atten = ADC_ATTEN_DB_12
55+ },
56+ .adc_channel = KBD_BATTERY_MONITOR_CHANNEL ,
57+ .lower_resistor = PULL_DOWN_RESISTOR ,
58+ .upper_resistor = PULL_UP_RESISTOR ,
7059 };
7160
72- int adc_raw_value ;
73- int adc_voltage ;
74- int battery_voltage ;
61+ adc_battery_estimation_handle_t adc_battery_estimation_handle = adc_battery_estimation_create (& config );
62+ if (adc_battery_estimation_handle == NULL ) {
63+ ESP_LOGE (TAG , "Initialize adc battery estimation failed! " );
64+ vTaskDelete (battery_monitor_task_handle );
65+ vTaskDelay (portMAX_DELAY );
66+ }
7567
76- int battery_level ;
68+ float capacity = 0.0f ;
7769
7870 while (1 ) {
79- adc_oneshot_read (adc1_handle , KBD_BATTERY_MONITOR_CHANNEL , & adc_raw_value );
80-
81- /* Get the battery voltage. */
82- adc_voltage = MAX_REF_VOLTAGE * adc_raw_value / 4095 ;
83- battery_voltage = adc_voltage * (PULL_UP_RESISTOR + PULL_DOWN_RESISTOR ) / PULL_DOWN_RESISTOR ;
84-
85- /* Get the percent. */
86- for (battery_level = 0 ; battery_level < 11 ; ++ battery_level ) {
87- if (battery_voltage <= ocv_capacity_table [battery_level ]) {
88- break ;
89- }
90- }
91-
92- if (battery_level == 0 ) {
93- /* Low battery. */
94- g_battery_percent = 0 ;
95- } else if (battery_level >= 11 ) {
96- /* Over voltage. */
97- g_battery_percent = 100 ;
98- } else {
99- g_battery_percent = (battery_level - 1 ) * 10 ;
100- g_battery_percent += ((battery_voltage - ocv_capacity_table [battery_level ]) * 10 / (
101- ocv_capacity_table [battery_level ] - ocv_capacity_table [battery_level - 1 ]));
102- }
103-
104- ble_hid_battery_report (g_battery_percent );
71+ adc_battery_estimation_get_capacity (adc_battery_estimation_handle , & capacity );
72+ ble_hid_battery_report ((int ) capacity );
10573
10674 /* Sample period. */
10775 vTaskDelay (pdMS_TO_TICKS (10000 ));
0 commit comments