battery
This commit is contained in:
parent
1d5e760e89
commit
67233261a3
@ -257,6 +257,7 @@ static int gatt_svr_chr_access_batt_level(uint16_t conn_handle, uint16_t attr_ha
|
|||||||
switch(ctxt->op){
|
switch(ctxt->op){
|
||||||
case BLE_GATT_ACCESS_OP_READ_CHR:
|
case BLE_GATT_ACCESS_OP_READ_CHR:
|
||||||
{
|
{
|
||||||
|
update_battery_level(main_app_conf->battery_conf);
|
||||||
uint8_t battery_level = main_app_conf->battery_conf->data->battery_percent;
|
uint8_t battery_level = main_app_conf->battery_conf->data->battery_percent;
|
||||||
rc = os_mbuf_append(ctxt->om, &battery_level, sizeof(battery_level));
|
rc = os_mbuf_append(ctxt->om, &battery_level, sizeof(battery_level));
|
||||||
}
|
}
|
||||||
|
3
components/battery/CMakeLists.txt
Normal file
3
components/battery/CMakeLists.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
idf_component_register(SRCS "battery.c"
|
||||||
|
INCLUDE_DIRS "include"
|
||||||
|
REQUIRES nvs_flash esp_adc)
|
102
components/battery/battery.c
Normal file
102
components/battery/battery.c
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "battery.h"
|
||||||
|
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "esp_adc/adc_oneshot.h"
|
||||||
|
#include "esp_adc/adc_cali.h"
|
||||||
|
#include "esp_adc/adc_cali_scheme.h"
|
||||||
|
|
||||||
|
#define TAG "battery"
|
||||||
|
|
||||||
|
battery_conf_t* get_battery_configuration(nvs_handle_t nvs){
|
||||||
|
battery_conf_t* conf = malloc(sizeof(battery_conf_t));
|
||||||
|
|
||||||
|
conf->read_retry_nb = 5;
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(nvs_get_u16(nvs, "poll_delay", &conf->poll_delay));
|
||||||
|
|
||||||
|
uint16_t min_mv, max_mv;
|
||||||
|
nvs_get_u16(nvs, "min_mv", &min_mv);
|
||||||
|
nvs_get_u16(nvs, "max_mv", &max_mv);
|
||||||
|
|
||||||
|
battery_data_t data = {
|
||||||
|
.battery_percent = 0,
|
||||||
|
.min_mv = min_mv,
|
||||||
|
.scale = (max_mv - min_mv) / 100,
|
||||||
|
};
|
||||||
|
|
||||||
|
ESP_LOGI("CONF", "min %d, max %d, scale %d", data.min_mv, max_mv, data.scale);
|
||||||
|
|
||||||
|
battery_data_t* data_h = malloc(sizeof(battery_data_t));
|
||||||
|
memcpy(data_h, &data, sizeof(battery_data_t));
|
||||||
|
conf->data = data_h;
|
||||||
|
conf->adc_channel = CONFIG_BATTMS_CHANNEL;
|
||||||
|
return conf;
|
||||||
|
}
|
||||||
|
|
||||||
|
void update_battery_level(battery_conf_t* batt_conf){
|
||||||
|
int val_raw = 0;
|
||||||
|
for(int tries=0; tries < batt_conf->read_retry_nb; tries++){
|
||||||
|
esp_err_t res = ESP_OK;
|
||||||
|
res = adc_oneshot_read(batt_conf->adc_handle, batt_conf->adc_channel, &val_raw);
|
||||||
|
if(res == ESP_ERR_TIMEOUT)// valeur invalide
|
||||||
|
continue;
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(res);
|
||||||
|
battery_data_t* data = batt_conf->data;
|
||||||
|
int val_mv=0;
|
||||||
|
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(batt_conf->cali_handle, val_raw, &val_mv));
|
||||||
|
val_mv *= 2; // external voltage divider
|
||||||
|
if(val_mv < data->min_mv) data->battery_percent = 0;
|
||||||
|
else data->battery_percent = (val_mv - data->min_mv) / data->scale;
|
||||||
|
ESP_LOGI("BATTMS", "raw : %d, mv : %d, percent : %d, min %d, scale %d", val_raw, val_mv, data->battery_percent, data->min_mv, data->scale);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_battery_level_adc(battery_conf_t* batt_conf){
|
||||||
|
adc_oneshot_unit_init_cfg_t init_config1 = {
|
||||||
|
.unit_id = ADC_UNIT_1,
|
||||||
|
.ulp_mode = ADC_ULP_MODE_DISABLE,
|
||||||
|
};
|
||||||
|
ESP_LOGI(TAG, "pointer inir config %d", (int)&init_config1);
|
||||||
|
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &batt_conf->adc_handle));
|
||||||
|
|
||||||
|
adc_bitwidth_t bitwidth = ADC_BITWIDTH_DEFAULT;
|
||||||
|
switch(CONFIG_BATTMS_ADC_BITWIDTH){
|
||||||
|
case 9:
|
||||||
|
bitwidth = ADC_BITWIDTH_9;
|
||||||
|
break;
|
||||||
|
case 10:
|
||||||
|
bitwidth = ADC_BITWIDTH_10;
|
||||||
|
break;
|
||||||
|
case 11:
|
||||||
|
bitwidth = ADC_BITWIDTH_11;
|
||||||
|
break;
|
||||||
|
case 12:
|
||||||
|
bitwidth = ADC_BITWIDTH_12;
|
||||||
|
break;
|
||||||
|
case 13:
|
||||||
|
bitwidth = ADC_BITWIDTH_13;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
bitwidth = ADC_BITWIDTH_DEFAULT;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
adc_oneshot_chan_cfg_t config = {
|
||||||
|
.bitwidth = bitwidth,
|
||||||
|
.atten = ADC_ATTEN_DB_11,
|
||||||
|
};
|
||||||
|
ESP_ERROR_CHECK(adc_oneshot_config_channel(batt_conf->adc_handle, batt_conf->adc_channel, &config));
|
||||||
|
|
||||||
|
adc_cali_curve_fitting_config_t cali_config = {
|
||||||
|
.unit_id = ADC_UNIT_1,
|
||||||
|
.atten = ADC_ATTEN_DB_11,
|
||||||
|
.bitwidth = bitwidth,
|
||||||
|
};
|
||||||
|
ESP_ERROR_CHECK(adc_cali_create_scheme_curve_fitting(&cali_config, &batt_conf->cali_handle));
|
||||||
|
}
|
||||||
|
|
30
components/battery/include/battery.h
Normal file
30
components/battery/include/battery.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#ifndef BATTERY_H
|
||||||
|
#define BATTERY_H
|
||||||
|
|
||||||
|
#include "nvs_flash.h"
|
||||||
|
#include "esp_adc/adc_oneshot.h"
|
||||||
|
#include "esp_adc/adc_cali.h"
|
||||||
|
|
||||||
|
struct battery_data {
|
||||||
|
uint8_t battery_percent;
|
||||||
|
uint16_t min_mv; // 0% in mV
|
||||||
|
uint16_t scale; // scaling between mV reading and percent
|
||||||
|
};
|
||||||
|
typedef struct battery_data battery_data_t;
|
||||||
|
|
||||||
|
struct battery_conf {
|
||||||
|
battery_data_t* data;
|
||||||
|
adc_channel_t adc_channel;
|
||||||
|
adc_oneshot_unit_handle_t adc_handle;
|
||||||
|
adc_cali_handle_t cali_handle;
|
||||||
|
uint16_t poll_delay;
|
||||||
|
uint8_t read_retry_nb;
|
||||||
|
};
|
||||||
|
typedef struct battery_conf battery_conf_t;
|
||||||
|
|
||||||
|
battery_conf_t* get_battery_configuration(nvs_handle_t nvs);
|
||||||
|
|
||||||
|
void init_battery_level_adc(battery_conf_t* batt_conf);
|
||||||
|
void update_battery_level(battery_conf_t* batt_conf);
|
||||||
|
|
||||||
|
#endif
|
@ -1,3 +1,3 @@
|
|||||||
idf_component_register(SRCS "configuration.c"
|
idf_component_register(SRCS "configuration.c"
|
||||||
INCLUDE_DIRS "include"
|
INCLUDE_DIRS "include"
|
||||||
REQUIRES sensirion_i2c_scd4x APlib ledController nvs_flash esp_adc)
|
REQUIRES sensirion_i2c_scd4x APlib ledController battery nvs_flash)
|
||||||
|
@ -26,26 +26,3 @@ void init_conf_from_nvs(configuration_data_t* conf){
|
|||||||
ESP_ERROR_CHECK(nvs_get_str(nvs, "name", conf->hostname, &str_size));
|
ESP_ERROR_CHECK(nvs_get_str(nvs, "name", conf->hostname, &str_size));
|
||||||
ESP_LOGI("AAA", "%d", str_size);
|
ESP_LOGI("AAA", "%d", str_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
battery_conf_t* get_battery_configuration(nvs_handle_t nvs){
|
|
||||||
battery_conf_t* conf = malloc(sizeof(battery_conf_t));
|
|
||||||
ESP_ERROR_CHECK(nvs_get_u16(nvs, "poll_delay", &conf->poll_delay));
|
|
||||||
|
|
||||||
uint16_t min_v, max_v;
|
|
||||||
nvs_get_u16(nvs, "min_v", &min_v);
|
|
||||||
nvs_get_u16(nvs, "max_v", &max_v);
|
|
||||||
|
|
||||||
uint16_t min_raw = min_v * (1<<CONFIG_BATTMS_ADC_BITWIDTH) / (110*3.55);
|
|
||||||
uint16_t max_raw = max_v * (1<<CONFIG_BATTMS_ADC_BITWIDTH) / (110*3.55);
|
|
||||||
battery_data_t data = {
|
|
||||||
.battery_percent = 0,
|
|
||||||
.min_raw = min_raw,
|
|
||||||
.scale = (max_raw - min_raw) / 100,
|
|
||||||
};
|
|
||||||
|
|
||||||
battery_data_t* data_h = malloc(sizeof(battery_data_t));
|
|
||||||
memcpy(data_h, &data, sizeof(battery_data_t));
|
|
||||||
conf->data = data_h;
|
|
||||||
conf->adc_channel = CONFIG_BATTMS_CHANNEL;
|
|
||||||
return conf;
|
|
||||||
}
|
|
@ -3,28 +3,11 @@
|
|||||||
|
|
||||||
#include "nvs.h"
|
#include "nvs.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
#include "esp_adc/adc_oneshot.h"
|
|
||||||
|
|
||||||
#include "scd4x_data.h"
|
#include "scd4x_data.h"
|
||||||
#include "APlib.h"
|
#include "APlib.h"
|
||||||
#include "ledController.h"
|
#include "ledController.h"
|
||||||
|
#include "battery.h"
|
||||||
struct battery_data {
|
|
||||||
uint8_t battery_percent;
|
|
||||||
uint16_t min_raw; // 0% as a raw adc reading
|
|
||||||
uint16_t scale; // scaling between raw adc reading and percent
|
|
||||||
};
|
|
||||||
typedef struct battery_data battery_data_t;
|
|
||||||
|
|
||||||
struct battery_conf {
|
|
||||||
battery_data_t* data;
|
|
||||||
adc_channel_t adc_channel;
|
|
||||||
adc_oneshot_unit_handle_t* adc_handle;
|
|
||||||
uint16_t poll_delay;
|
|
||||||
};
|
|
||||||
typedef struct battery_conf battery_conf_t;
|
|
||||||
|
|
||||||
battery_conf_t* get_battery_configuration(nvs_handle_t nvs);
|
|
||||||
|
|
||||||
struct configuration_data {
|
struct configuration_data {
|
||||||
scd4x_data_t* measure;
|
scd4x_data_t* measure;
|
||||||
|
@ -15,5 +15,5 @@ led3_conf,data,hex2bin,06000000BB02640001FF
|
|||||||
#
|
#
|
||||||
battms,namespace,,
|
battms,namespace,,
|
||||||
poll_delay,data,u16,10000
|
poll_delay,data,u16,10000
|
||||||
min_v,data,u16,370
|
min_mv,data,u16,3700
|
||||||
max_v,data,u16,420
|
max_mv,data,u16,4200
|
|
@ -16,7 +16,6 @@
|
|||||||
#include "esp_pm.h"
|
#include "esp_pm.h"
|
||||||
#include "driver/gpio.h"
|
#include "driver/gpio.h"
|
||||||
#include "driver/i2c.h"
|
#include "driver/i2c.h"
|
||||||
#include "esp_adc/adc_oneshot.h"
|
|
||||||
|
|
||||||
#include "ledController.h"
|
#include "ledController.h"
|
||||||
#include "scd4x_i2c.h"
|
#include "scd4x_i2c.h"
|
||||||
@ -26,6 +25,8 @@
|
|||||||
#include "BTlib_nimble.h"
|
#include "BTlib_nimble.h"
|
||||||
#include "MMC56x3.h"
|
#include "MMC56x3.h"
|
||||||
|
|
||||||
|
#define TAG "MAIN"
|
||||||
|
|
||||||
void app_main(void){
|
void app_main(void){
|
||||||
init_nvs();
|
init_nvs();
|
||||||
|
|
||||||
@ -44,7 +45,7 @@ void app_main(void){
|
|||||||
.min_freq_mhz = 40,
|
.min_freq_mhz = 40,
|
||||||
.light_sleep_enable = true, // enable light sleep
|
.light_sleep_enable = true, // enable light sleep
|
||||||
};
|
};
|
||||||
//ESP_ERROR_CHECK(esp_pm_configure(&pm_config));
|
ESP_ERROR_CHECK(esp_pm_configure(&pm_config));
|
||||||
|
|
||||||
|
|
||||||
configuration_data_t* conf = malloc(sizeof(configuration_data_t));
|
configuration_data_t* conf = malloc(sizeof(configuration_data_t));
|
||||||
@ -61,8 +62,6 @@ void app_main(void){
|
|||||||
init_scd4x();
|
init_scd4x();
|
||||||
|
|
||||||
init_battery_level_adc(conf->battery_conf);
|
init_battery_level_adc(conf->battery_conf);
|
||||||
TaskHandle_t battms_update_handle;
|
|
||||||
xTaskCreate(update_battery_level, "BATTMS_update", 4096, conf->battery_conf, tskIDLE_PRIORITY, &battms_update_handle);
|
|
||||||
|
|
||||||
uint8_t id=0;
|
uint8_t id=0;
|
||||||
MMC56x3_get_product_ID(&id);
|
MMC56x3_get_product_ID(&id);
|
||||||
@ -136,32 +135,3 @@ void init_scd4x(){
|
|||||||
scd4x_stop_periodic_measurement();
|
scd4x_stop_periodic_measurement();
|
||||||
scd4x_reinit();
|
scd4x_reinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_battery_level_adc(battery_conf_t* batt_conf){
|
|
||||||
adc_oneshot_unit_init_cfg_t init_config1 = {
|
|
||||||
.unit_id = ADC_UNIT_1,
|
|
||||||
.ulp_mode = ADC_ULP_MODE_DISABLE,
|
|
||||||
};
|
|
||||||
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, batt_conf->adc_handle));
|
|
||||||
|
|
||||||
adc_oneshot_chan_cfg_t config = {
|
|
||||||
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
|
||||||
.atten = ADC_ATTEN_DB_11,
|
|
||||||
};
|
|
||||||
ESP_ERROR_CHECK(adc_oneshot_config_channel(*batt_conf->adc_handle, batt_conf->adc_channel, &config));
|
|
||||||
}
|
|
||||||
|
|
||||||
void update_battery_level(void* pvParameters){// battery_data_t* data, uint8_t channel, adc_oneshot_unit_handle_t* handle){
|
|
||||||
battery_conf_t* batt_conf = pvParameters;
|
|
||||||
int val_raw = 0;
|
|
||||||
while(1){
|
|
||||||
esp_err_t res = adc_oneshot_read(*(batt_conf->adc_handle), batt_conf->adc_channel, &val_raw);
|
|
||||||
if(res != ESP_ERR_TIMEOUT){ // valeur invalide
|
|
||||||
ESP_ERROR_CHECK(res);
|
|
||||||
battery_data_t* data = batt_conf->data;
|
|
||||||
data->battery_percent = (val_raw - data->min_raw) * data->scale;
|
|
||||||
ESP_LOGI("BATTMS", "raw : %d, percent : %d", val_raw, data->battery_percent);
|
|
||||||
}
|
|
||||||
vTaskDelay(batt_conf->poll_delay / portTICK_PERIOD_MS);
|
|
||||||
}
|
|
||||||
}
|
|
@ -10,6 +10,3 @@ void init_http_server(configuration_data_t* main_conf);
|
|||||||
void init_i2c();
|
void init_i2c();
|
||||||
void init_scd4x();
|
void init_scd4x();
|
||||||
led_disp_config_t* generate_led_conf(nvs_handle_t nvs, unsigned int nb);
|
led_disp_config_t* generate_led_conf(nvs_handle_t nvs, unsigned int nb);
|
||||||
|
|
||||||
void init_battery_level_adc(battery_conf_t* batt_conf);
|
|
||||||
void update_battery_level(void* pvParameters);
|
|
||||||
|
@ -24,7 +24,7 @@ menu "CO2Sense config"
|
|||||||
config BATTMS_ADC_BITWIDTH
|
config BATTMS_ADC_BITWIDTH
|
||||||
int "bitwidth"
|
int "bitwidth"
|
||||||
range 9 13
|
range 9 13
|
||||||
default 9
|
default 12
|
||||||
help
|
help
|
||||||
adc sample bitwidth
|
adc sample bitwidth
|
||||||
endmenu
|
endmenu
|
||||||
|
11
sdkconfig
11
sdkconfig
@ -367,16 +367,16 @@ CONFIG_SCL_PIN=9
|
|||||||
# Battery
|
# Battery
|
||||||
#
|
#
|
||||||
CONFIG_BATTMS_CHANNEL=4
|
CONFIG_BATTMS_CHANNEL=4
|
||||||
CONFIG_BATTMS_ADC_BITWIDTH=9
|
CONFIG_BATTMS_ADC_BITWIDTH=12
|
||||||
# end of Battery
|
# end of Battery
|
||||||
# end of CO2Sense config
|
# end of CO2Sense config
|
||||||
|
|
||||||
#
|
#
|
||||||
# Compiler options
|
# Compiler options
|
||||||
#
|
#
|
||||||
CONFIG_COMPILER_OPTIMIZATION_DEFAULT=y
|
# CONFIG_COMPILER_OPTIMIZATION_DEFAULT is not set
|
||||||
# CONFIG_COMPILER_OPTIMIZATION_SIZE is not set
|
# CONFIG_COMPILER_OPTIMIZATION_SIZE is not set
|
||||||
# CONFIG_COMPILER_OPTIMIZATION_PERF is not set
|
CONFIG_COMPILER_OPTIMIZATION_PERF=y
|
||||||
# CONFIG_COMPILER_OPTIMIZATION_NONE is not set
|
# CONFIG_COMPILER_OPTIMIZATION_NONE is not set
|
||||||
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE=y
|
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE=y
|
||||||
# CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT is not set
|
# CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT is not set
|
||||||
@ -1089,7 +1089,6 @@ CONFIG_FREERTOS_IDLE_TIME_BEFORE_SLEEP=3
|
|||||||
#
|
#
|
||||||
# Port
|
# Port
|
||||||
#
|
#
|
||||||
CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER=y
|
|
||||||
# CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK is not set
|
# CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK is not set
|
||||||
CONFIG_FREERTOS_TLSP_DELETION_CALLBACKS=y
|
CONFIG_FREERTOS_TLSP_DELETION_CALLBACKS=y
|
||||||
# CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP is not set
|
# CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP is not set
|
||||||
@ -1684,8 +1683,8 @@ CONFIG_LOG_BOOTLOADER_LEVEL=3
|
|||||||
CONFIG_FLASHMODE_DIO=y
|
CONFIG_FLASHMODE_DIO=y
|
||||||
# CONFIG_FLASHMODE_DOUT is not set
|
# CONFIG_FLASHMODE_DOUT is not set
|
||||||
CONFIG_MONITOR_BAUD=115200
|
CONFIG_MONITOR_BAUD=115200
|
||||||
CONFIG_OPTIMIZATION_LEVEL_DEBUG=y
|
# CONFIG_OPTIMIZATION_LEVEL_DEBUG is not set
|
||||||
CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG=y
|
# CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG is not set
|
||||||
# CONFIG_OPTIMIZATION_LEVEL_RELEASE is not set
|
# CONFIG_OPTIMIZATION_LEVEL_RELEASE is not set
|
||||||
# CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE is not set
|
# CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE is not set
|
||||||
CONFIG_OPTIMIZATION_ASSERTIONS_ENABLED=y
|
CONFIG_OPTIMIZATION_ASSERTIONS_ENABLED=y
|
||||||
|
Loading…
x
Reference in New Issue
Block a user