diff --git a/components/BTlib/BTlib_nimble.c b/components/BTlib/BTlib_nimble.c index abad4ff..030d4ba 100644 --- a/components/BTlib/BTlib_nimble.c +++ b/components/BTlib/BTlib_nimble.c @@ -257,6 +257,7 @@ static int gatt_svr_chr_access_batt_level(uint16_t conn_handle, uint16_t attr_ha switch(ctxt->op){ 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; rc = os_mbuf_append(ctxt->om, &battery_level, sizeof(battery_level)); } diff --git a/components/battery/CMakeLists.txt b/components/battery/CMakeLists.txt new file mode 100644 index 0000000..474c084 --- /dev/null +++ b/components/battery/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "battery.c" + INCLUDE_DIRS "include" + REQUIRES nvs_flash esp_adc) diff --git a/components/battery/battery.c b/components/battery/battery.c new file mode 100644 index 0000000..2c230f8 --- /dev/null +++ b/components/battery/battery.c @@ -0,0 +1,102 @@ +#include +#include + +#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)); +} + diff --git a/components/battery/include/battery.h b/components/battery/include/battery.h new file mode 100644 index 0000000..486c357 --- /dev/null +++ b/components/battery/include/battery.h @@ -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 \ No newline at end of file diff --git a/components/configuration/CMakeLists.txt b/components/configuration/CMakeLists.txt index 3855e2e..fadc165 100644 --- a/components/configuration/CMakeLists.txt +++ b/components/configuration/CMakeLists.txt @@ -1,3 +1,3 @@ idf_component_register(SRCS "configuration.c" INCLUDE_DIRS "include" - REQUIRES sensirion_i2c_scd4x APlib ledController nvs_flash esp_adc) + REQUIRES sensirion_i2c_scd4x APlib ledController battery nvs_flash) diff --git a/components/configuration/configuration.c b/components/configuration/configuration.c index efc0a8c..4af7571 100644 --- a/components/configuration/configuration.c +++ b/components/configuration/configuration.c @@ -25,27 +25,4 @@ void init_conf_from_nvs(configuration_data_t* conf){ size_t str_size = sizeof(conf->hostname); ESP_ERROR_CHECK(nvs_get_str(nvs, "name", conf->hostname, &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<data = data_h; - conf->adc_channel = CONFIG_BATTMS_CHANNEL; - return conf; } \ No newline at end of file diff --git a/components/configuration/include/configuration.h b/components/configuration/include/configuration.h index b99723f..d24d629 100644 --- a/components/configuration/include/configuration.h +++ b/components/configuration/include/configuration.h @@ -3,28 +3,11 @@ #include "nvs.h" #include "nvs_flash.h" -#include "esp_adc/adc_oneshot.h" #include "scd4x_data.h" #include "APlib.h" #include "ledController.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); +#include "battery.h" struct configuration_data { scd4x_data_t* measure; diff --git a/default_conf.csv b/default_conf.csv index c048d2a..af3cec7 100644 --- a/default_conf.csv +++ b/default_conf.csv @@ -15,5 +15,5 @@ led3_conf,data,hex2bin,06000000BB02640001FF # battms,namespace,, poll_delay,data,u16,10000 -min_v,data,u16,370 -max_v,data,u16,420 \ No newline at end of file +min_mv,data,u16,3700 +max_mv,data,u16,4200 \ No newline at end of file diff --git a/main/CO2_Sense.c b/main/CO2_Sense.c index 10d5f52..8a65340 100644 --- a/main/CO2_Sense.c +++ b/main/CO2_Sense.c @@ -16,7 +16,6 @@ #include "esp_pm.h" #include "driver/gpio.h" #include "driver/i2c.h" -#include "esp_adc/adc_oneshot.h" #include "ledController.h" #include "scd4x_i2c.h" @@ -26,6 +25,8 @@ #include "BTlib_nimble.h" #include "MMC56x3.h" +#define TAG "MAIN" + void app_main(void){ init_nvs(); @@ -43,8 +44,8 @@ void app_main(void){ .max_freq_mhz = 80, .min_freq_mhz = 40, .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)); @@ -61,8 +62,6 @@ void app_main(void){ init_scd4x(); 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; MMC56x3_get_product_ID(&id); @@ -135,33 +134,4 @@ void init_scd4x(){ scd4x_wake_up(); scd4x_stop_periodic_measurement(); 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); - } } \ No newline at end of file diff --git a/main/CO2_Sense.h b/main/CO2_Sense.h index e1852fc..0ace503 100644 --- a/main/CO2_Sense.h +++ b/main/CO2_Sense.h @@ -10,6 +10,3 @@ void init_http_server(configuration_data_t* main_conf); void init_i2c(); void init_scd4x(); 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); diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index 21930a6..c1ec809 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -24,7 +24,7 @@ menu "CO2Sense config" config BATTMS_ADC_BITWIDTH int "bitwidth" range 9 13 - default 9 + default 12 help adc sample bitwidth endmenu diff --git a/nvs.bin b/nvs.bin index dfd5f4c..63d0300 100644 Binary files a/nvs.bin and b/nvs.bin differ diff --git a/sdkconfig b/sdkconfig index ee3345b..56a3080 100644 --- a/sdkconfig +++ b/sdkconfig @@ -367,16 +367,16 @@ CONFIG_SCL_PIN=9 # Battery # CONFIG_BATTMS_CHANNEL=4 -CONFIG_BATTMS_ADC_BITWIDTH=9 +CONFIG_BATTMS_ADC_BITWIDTH=12 # end of Battery # end of CO2Sense config # # Compiler options # -CONFIG_COMPILER_OPTIMIZATION_DEFAULT=y +# CONFIG_COMPILER_OPTIMIZATION_DEFAULT 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_ASSERTIONS_ENABLE=y # CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT is not set @@ -1089,7 +1089,6 @@ CONFIG_FREERTOS_IDLE_TIME_BEFORE_SLEEP=3 # # Port # -CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER=y # CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK is not set CONFIG_FREERTOS_TLSP_DELETION_CALLBACKS=y # 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_DOUT is not set CONFIG_MONITOR_BAUD=115200 -CONFIG_OPTIMIZATION_LEVEL_DEBUG=y -CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG=y +# CONFIG_OPTIMIZATION_LEVEL_DEBUG is not set +# CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG is not set # CONFIG_OPTIMIZATION_LEVEL_RELEASE is not set # CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE is not set CONFIG_OPTIMIZATION_ASSERTIONS_ENABLED=y