Skip to content

Commit

Permalink
ESP32-C6: battery voltage (ADC) feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lyusupov committed Feb 1, 2024
1 parent 9632a38 commit f3a9ca5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion software/firmware/source/SoftRF/src/platform/ESP32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3753,7 +3753,7 @@ static void ESP32_Battery_setup()
#elif defined(CONFIG_IDF_TARGET_ESP32C3)
calibrate_voltage((adc1_channel_t) ADC1_GPIO1_CHANNEL);
#elif defined(CONFIG_IDF_TARGET_ESP32C6)
/* TBD */
calibrate_voltage(SOC_GPIO_PIN_C6_BATTERY);
#else
#error "This ESP32 family build variant is not supported!"
#endif /* CONFIG_IDF_TARGET_ESP32 */
Expand Down
24 changes: 22 additions & 2 deletions software/firmware/source/libraries/ADC/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,28 @@
#include "battery.h"

#if defined(CONFIG_IDF_TARGET_ESP32C6)
void calibrate_voltage(adc1_channel_t channel, adc_atten_t atten) { /* TBD */ }
uint16_t read_voltage() { /* TBD */ return 0; }
static uint8_t adc_pin = 1;

void calibrate_voltage(uint8_t pin, adc_attenuation_t atten) {
adc_pin = pin;
analogReadResolution(12);
analogSetPinAttenuation(adc_pin, atten);
}

uint16_t read_voltage() {
// multisample ADC
uint32_t adc_reading = 0;

for (int i = 0; i < NO_OF_SAMPLES; i++) {
adc_reading += analogReadMilliVolts(adc_pin);
yield();
}

adc_reading /= NO_OF_SAMPLES;

return (uint16_t) adc_reading;
}

#else

// Local logging tag
Expand Down
14 changes: 6 additions & 8 deletions software/firmware/source/libraries/ADC/battery.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
#ifndef _BATTERY_H
#define _BATTERY_H

uint16_t read_voltage(void);

#if defined(CONFIG_IDF_TARGET_ESP32C6)
/* TBD */
typedef unsigned int adc1_channel_t;
typedef unsigned int adc_atten_t;
#define ADC_ATTEN_DB_11 0
void calibrate_voltage(uint8_t, adc_attenuation_t atten = ADC_11db);
#else
#include <driver/adc.h>
#include <esp_adc_cal.h>

void calibrate_voltage(adc1_channel_t, adc_atten_t atten = ADC_ATTEN_DB_11);
#endif /* CONFIG_IDF_TARGET_ESP32C6 */

#define DEFAULT_VREF 1100 // tbd: use adc2_vref_to_gpio() for better estimate
#define NO_OF_SAMPLES 32 // we do some multisampling to get better values

uint16_t read_voltage(void);
void calibrate_voltage(adc1_channel_t, adc_atten_t atten = ADC_ATTEN_DB_11);

#endif
#endif /* _BATTERY_H */

0 comments on commit f3a9ca5

Please sign in to comment.