Migrate to the v5 adc api

custom
jacqueline 2 years ago
parent 393b268e15
commit f013bab727
  1. 2
      src/drivers/CMakeLists.txt
  2. 59
      src/drivers/battery.cpp
  3. 17
      src/drivers/include/battery.hpp
  4. 5
      src/main/main.cpp

@ -2,5 +2,5 @@ idf_component_register(
SRCS "dac.cpp" "gpio_expander.cpp" "battery.cpp" "storage.cpp" "i2c.cpp" SRCS "dac.cpp" "gpio_expander.cpp" "battery.cpp" "storage.cpp" "i2c.cpp"
"spi.cpp" "display.cpp" "display_init.cpp" "spi.cpp" "display.cpp" "display_init.cpp"
INCLUDE_DIRS "include" INCLUDE_DIRS "include"
REQUIRES "esp_adc_cal" "fatfs" "result" "lvgl" "span") REQUIRES "esp_adc" "fatfs" "result" "lvgl" "span")
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS}) target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})

@ -1,31 +1,54 @@
#include "battery.hpp" #include "battery.hpp"
#include <cstdint>
#include "driver/adc.h" #include "esp_adc/adc_oneshot.h"
#include "esp_adc_cal.h"
#include "hal/adc_types.h" #include "hal/adc_types.h"
namespace drivers { namespace drivers {
static esp_adc_cal_characteristics_t calibration; static const uint8_t kAdcBitWidth = ADC_BITWIDTH_12;
static const uint8_t kAdcUnit = ADC_UNIT_1;
esp_err_t init_adc(void) { // Max battery voltage should be a little over 2V due to our divider, so we need
// Calibration should already be fused into the chip from the factory, so // the max attenuation to properly handle the full range.
// we should only need to read it back out again. static const uint8_t kAdcAttenuation = ADC_ATTEN_DB_11;
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 0, // Corresponds to GPIO 34.
&calibration); static const uint8_t kAdcChannel = ADC_CHANNEL_6;
// Max battery voltage should be a little over 2V due to our divider, so Battery::Battery() {
// we need the max attenuation to properly handle the full range. adc_oneshot_unit_init_cfg_t unit_config = {
adc1_config_width(ADC_WIDTH_BIT_12); .unit_id = kAdcUnit,
adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11); };
ESP_ERROR_CHECK(adc_oneshot_new_unit(&unit_config, &adc_handle_));
adc_oneshot_chan_cfg_t channel_config = {
.bitwidth = kAdcBitWidth,
.atten = kAdcAttenuation,
};
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle_, kAdcChannel, &channel_config));
// calibrate
// TODO: compile-time assert our scheme is available
adc_cali_line_fitting_config_t calibration_config = {
.unit_id = kAdcUnit,
.atten = kAdcAttenuation,
.bitwidth = kAdcBitWidth,
};
ESP_ERROR_CHECK(adc_cali_create_scheme_line_fitting(&calibration_config, &adc_calibration_handle_));
}
return ESP_OK; Battery::~Battery() {
adc_cali_delete_scheme_line_fitting(adc_calibration_handle_);
} }
uint32_t read_battery_voltage(void) { auto Battery::Millivolts() -> uint32_t {
// GPIO 34 // GPIO 34
int raw = adc1_get_raw(ADC1_CHANNEL_6); int raw;
return esp_adc_cal_raw_to_voltage(raw, &calibration); ESP_ERROR_CHECK(adc_oneshot_read(adc_handle, kAdcChannel &raw));
int voltage;
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc_calibration_handle, raw, &voltage));
return voltage;
} }
} // namespace drivers } // namespace drivers

@ -1,16 +1,25 @@
#pragma once #pragma once
#include <stdint.h> #include <cstdint>
#include "esp_adc/adc_oneshot.h"
#include "esp_err.h" #include "esp_err.h"
#include "result.hpp"
namespace drivers { namespace drivers {
esp_err_t init_adc(void); class Battery {
public:
Battery();
~Battery();
/** /**
* Returns the current battery level in millivolts. * Returns the current battery level in millivolts.
*/ */
uint32_t read_battery_voltage(void); auto Millivolts() -> uint32_t;
private:
adc_oneshot_handle_t adc_handle_;
adc_cali_handle_t adc_calibration_handle_;
};
} // namespace drivers } // namespace drivers

@ -92,7 +92,6 @@ extern "C" void app_main(void) {
ESP_ERROR_CHECK(gpio_install_isr_service(ESP_INTR_FLAG_LOWMED)); ESP_ERROR_CHECK(gpio_install_isr_service(ESP_INTR_FLAG_LOWMED));
ESP_ERROR_CHECK(drivers::init_i2c()); ESP_ERROR_CHECK(drivers::init_i2c());
ESP_ERROR_CHECK(drivers::init_spi()); ESP_ERROR_CHECK(drivers::init_spi());
ESP_ERROR_CHECK(drivers::init_adc());
ESP_LOGI(TAG, "Init GPIOs"); ESP_LOGI(TAG, "Init GPIOs");
drivers::GpioExpander* expander = new drivers::GpioExpander(); drivers::GpioExpander* expander = new drivers::GpioExpander();
@ -105,6 +104,10 @@ extern "C" void app_main(void) {
drivers::GpioExpander::SD_MUX_ESP); drivers::GpioExpander::SD_MUX_ESP);
}); });
ESP_LOGI(TAG, "Init battery measurement");
drivers::Battery* battery = new drivers::Battery();
ESP_LOGI(TAG, "it's reading %dmV!", battery->Millivolts());
ESP_LOGI(TAG, "Init SD card"); ESP_LOGI(TAG, "Init SD card");
auto storage_res = drivers::SdStorage::create(expander); auto storage_res = drivers::SdStorage::create(expander);
if (storage_res.has_error()) { if (storage_res.has_error()) {

Loading…
Cancel
Save