Battery voltage read implemented (TTGOv2.1)

This commit is contained in:
Klaus K Wilting 2018-06-02 22:23:50 +02:00
parent 428db80073
commit 9c60a237cf
6 changed files with 121 additions and 5 deletions

View File

@ -221,11 +221,15 @@ device answers with it's current configuration. The configuration is a C structu
0x81 get device uptime
bytes 1-7: Uptime in seconds (little endian format)
bytes 1-7: uptime in seconds (little endian format)
0x82 get device cpu temperature
bytes 1-3: chip temperature in celsius (little endian format)
bytes 1-3: chip temperature in degrees celsius (little endian format)
0x83 get device battery voltage
bytes 1-4: battery voltage in millivolt, 0 if unreadable (little endian format)
# License

View File

@ -11,7 +11,7 @@
; ---> SELECT TARGET PLATFORM HERE! <---
[platformio]
env_default = heltec
;env_default = heltec
;env_default = ttgov1
;env_default = ttgov2
;env_default = ttgov21

89
src/adcread.cpp Normal file
View File

@ -0,0 +1,89 @@
#ifdef HAS_BATTERY_PROBE
/* ADC1 Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include "globals.h"
#include <driver/adc.h>
#include <esp_adc_cal.h>
#define DEFAULT_VREF 1100 // we use adc2_vref_to_gpio() to obtain a better estimate
#define NO_OF_SAMPLES 64 // we do multisampling
// Local logging tag
static const char TAG[] = "main";
static const adc_channel_t channel = (adc_channel_t) HAS_BATTERY_PROBE;
static const adc_atten_t atten = ADC_ATTEN_DB_0;
static const adc_unit_t unit = ADC_UNIT_1;
static void check_efuse()
{
//Check if two point calibration values are burned into eFuse
if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) {
ESP_LOGI(TAG,"eFuse Two Point: Supported");
} else {
ESP_LOGI(TAG,"eFuse Two Point: NOT supported");
}
//Check Vref is burned into eFuse
if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF) == ESP_OK) {
ESP_LOGI(TAG,"eFuse Vref: Supported");
} else {
ESP_LOGI(TAG,"eFuse Vref: NOT supported");
}
}
static void print_char_val_type(esp_adc_cal_value_t val_type)
{
if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
ESP_LOGI(TAG,"Characterized using Two Point Value");
} else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
ESP_LOGI(TAG,"Characterized using eFuse Vref");
} else {
ESP_LOGI(TAG,"Characterized using Default Vref");
}
}
uint16_t read_voltage(void)
{
//Check if Two Point or Vref are burned into eFuse
check_efuse();
//Configure ADC
if (unit == ADC_UNIT_1) {
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten((adc1_channel_t)channel, atten);
} else {
adc2_config_channel_atten((adc2_channel_t)channel, atten);
}
//Characterize ADC
esp_adc_cal_characteristics_t *adc_chars = (esp_adc_cal_characteristics_t *) calloc(1, sizeof(esp_adc_cal_characteristics_t));
esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars);
print_char_val_type(val_type);
//sample ADC
uint32_t adc_reading = 0;
//Multisampling
for (int i = 0; i < NO_OF_SAMPLES; i++) {
if (unit == ADC_UNIT_1) {
adc_reading += adc1_get_raw((adc1_channel_t)channel);
} else {
int raw;
adc2_get_raw((adc2_channel_t)channel, ADC_WIDTH_BIT_12, &raw);
adc_reading += raw;
}
}
adc_reading /= NO_OF_SAMPLES;
//Convert adc_reading to voltage in mV
uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
ESP_LOGI(TAG,"Raw: %d\tVoltage: %dmV", adc_reading, voltage);
return voltage;
}
#endif // HAS_BATTERY_PROBE

View File

@ -5,6 +5,7 @@
#define HAS_DISPLAY U8X8_SSD1306_128X64_NONAME_HW_I2C
#define DISPLAY_FLIP 1 // rotated display
#define HAS_LED 23 // green on board LED_G3 (not in initial board version)
#define HAS_BATTERY_PROBE ADC1_GPIO35_CHANNEL // battery probe GPIO pin -> ADC_CHANNEL_7
// re-define pin definitions of pins_arduino.h
#define PIN_SPI_SS 18 // ESP32 GPIO18 (Pin18) -- HPD13A NSS/SEL (Pin4) SPI Chip Select Input

View File

@ -1,6 +1,6 @@
// program version - note: increment version after modifications to configData_t struct!!
#define PROGVERSION "1.3.7" // use max 10 chars here!
#define PROGVERSION "1.3.71" // use max 10 chars here!
#define PROGNAME "PAXCNT"
//--- Declarations ---

View File

@ -24,6 +24,12 @@ typedef struct {
void antenna_select(const uint8_t _ant);
#endif
// function defined in adcread.cpp
#ifdef HAS_BATTERY_PROBE
uint32_t read_voltage(void);
#endif
// help function to assign LoRa datarates to numeric spreadfactor values
void switch_lora (uint8_t sf, uint8_t tx) {
if ( tx > 20 ) return;
@ -235,6 +241,21 @@ void get_cputemp (uint8_t val) {
ESP_LOGI(TAG, "%d bytes queued in send queue", size-1);
};
void get_voltage (uint8_t val) {
ESP_LOGI(TAG, "Remote command: get battery voltage");
#ifdef HAS_BATTERY_PROBE
uint32_t voltage = read_voltage();
#else
uint32_t voltage = 0;
#endif
int size = sizeof(voltage);
unsigned char *sendData = new unsigned char[size];
memcpy(sendData, (unsigned char*)&voltage, size);
LMIC_setTxData2(RCMDPORT, sendData, size-1, 0); // send data unconfirmed on RCMD Port
delete sendData; // free memory
ESP_LOGI(TAG, "%d bytes queued in send queue", size-1);
};
// assign previously defined functions to set of numeric remote commands
// format: opcode, function, flag (1 = do make settings persistent / 0 = don't)
//
@ -257,7 +278,8 @@ cmd_t table[] = {
{0x10, set_rgblum, true},
{0x80, get_config, false},
{0x81, get_uptime, false},
{0x82, get_cputemp, false}
{0x82, get_cputemp, false},
{0x83, get_voltage, false}
};
// check and execute remote command