From 9dce0c4181afd77413f5f622aa01330e8244ebf3 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Thu, 25 Jun 2015 01:04:07 +0300 Subject: [PATCH] Provide selection between A0 and VCC (#443, #338) --- README.md | 10 ++++++++++ cores/esp8266/Esp.h | 9 +++++++++ cores/esp8266/core_esp8266_phy.c | 7 ++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 69027a64f..70ead24dc 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,16 @@ Several APIs may be used to get flash chip info: ```ESP.getCycleCount()``` returns the cpu instruction cycle count since start as an unsigned 32-bit. This is useful for accurate timing of very short actions like bit banging. +```ESP.getVcc()``` may be used to measure supply voltage. ESP needs to reconfigure the ADC +at startup in order for this feature to be available. Add the following line to the top +of your sketch to use ```getVcc```: +``` +ADC_MODE(ADC_VCC); +``` +TOUT pin has to be disconnected in this mode. + +Note that by default ADC is configured to read from TOUT pin using ```analogRead(A0)```, and +```ESP.getVCC()``` is not available. #### OneWire (from https://www.pjrc.com/teensy/td_libs_OneWire.html) #### diff --git a/cores/esp8266/Esp.h b/cores/esp8266/Esp.h index e55cd281a..df41be399 100644 --- a/cores/esp8266/Esp.h +++ b/cores/esp8266/Esp.h @@ -61,6 +61,15 @@ enum RFMode { #define WAKE_NO_RFCAL RF_NO_CAL #define WAKE_RF_DISABLED RF_DISABLED +enum ADCMode { + ADC_TOUT = 33, + ADC_TOUT_3V3 = 33, + ADC_VCC = 255, + ADC_VDD = 255 +}; + +#define ADC_MODE(mode) extern "C" int __get_adc_mode() { return (int) (mode); } + typedef enum { FM_QIO = 0x00, FM_QOUT = 0x01, diff --git a/cores/esp8266/core_esp8266_phy.c b/cores/esp8266/core_esp8266_phy.c index 2ac33a8a6..faa601184 100644 --- a/cores/esp8266/core_esp8266_phy.c +++ b/cores/esp8266/core_esp8266_phy.c @@ -224,6 +224,7 @@ static uint8_t phy_init_data[128] = extern int __real_register_chipv6_phy(uint8_t* init_data); extern int __wrap_register_chipv6_phy(uint8_t* unused) { + phy_init_data[107] = __get_adc_mode(); return __real_register_chipv6_phy(phy_init_data); } @@ -243,6 +244,10 @@ extern int __get_rf_mode() return 0; // default mode } - +extern int __get_adc_mode() __attribute__((weak)); +extern int __get_adc_mode() +{ + return 33; // default ADC mode +}