NRF52840, get VDDH voltage #7756
-
As far as I understand, NRF52840 have a built-in voltage converter, allowing for direct USB power supply through VDDH pin. Can we retrieve with Espruino the actual voltage on this pin? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Yes, you need to define This however may not make sense in some(?) or even all(?) cases since you can have VDDH as 5V and still the analogVRef should be 3.3 or whatever the GPIO levels are (even 1.8V). So I made a patch here https://github.com/orgs/espruino/discussions/3419#discussioncomment-12191448 to add extra I can make PR with this, the only thing I am not sure about is the method name (getVDDH). diff --git a/src/jshardware.h b/src/jshardware.h
index e8ef97858..92e2ea167 100644
--- a/src/jshardware.h
+++ b/src/jshardware.h
@@ -441,6 +441,7 @@ JsVarFloat jshReadTemperature();
/// The voltage that a reading of 1 from `analogRead` actually represents, in volts
JsVarFloat jshReadVRef();
+JsVarFloat jshReadVRefH();
/** Get a random number - either using special purpose hardware or by
* reading noise from an analog input. If unimplemented, this should
diff --git a/src/jswrap_espruino.c b/src/jswrap_espruino.c
index 845ce53bd..cecc9b819 100644
--- a/src/jswrap_espruino.c
+++ b/src/jswrap_espruino.c
@@ -182,6 +182,20 @@ While this is implemented on Espruino boards, it may not be implemented on other
devices. If so it'll return NaN.
*/
+/*JSON{
+ "type" : "staticmethod",
+ "ifdef" : "ESPR_VREF_VDDH",
+ "class" : "E",
+ "name" : "getVDDH",
+ "generate_full" : "jshReadVRefH()",
+ "return" : ["float","The voltage on VDDH input"]
+}
+ **Note:** This value is calculated by reading the voltage on an internal
+voltage reference with the ADC. It will be slightly noisy, so if you need this
+for accurate measurements we'd recommend that you call this function several
+times and average the results.
+ */
+
int nativeCallGetCType() {
if (lex->tk == LEX_R_VOID) {
diff --git a/targets/nrf5x/jshardware.c b/targets/nrf5x/jshardware.c
index 170c1dfb9..879aaac23 100644
--- a/targets/nrf5x/jshardware.c
+++ b/targets/nrf5x/jshardware.c
@@ -2822,21 +2827,14 @@ JsVarFloat jshReadTemperature() {
#endif
}
-// The voltage that a reading of 1 from `analogRead` actually represents
-JsVarFloat jshReadVRef() {
#ifdef NRF52_SERIES
+nrf_saadc_value_t jshReadVDD(nrf_saadc_input_t pin, nrf_saadc_acqtime_t time, nrf_saadc_gain_t gain) {
nrf_saadc_channel_config_t config;
- config.acq_time = NRF_SAADC_ACQTIME_3US;
- config.gain = NRF_SAADC_GAIN1_6; // 1/6 of input volts
+ config.acq_time = time;
+ config.gain = gain;
config.mode = NRF_SAADC_MODE_SINGLE_ENDED;
-
-#ifdef ESPR_VREF_VDDH
- config.pin_p = 0x0D; // Not in Nordic's libs, but this is VDDHDIV5 - we probably want to be looking at VDDH
- config.pin_n = 0x0D;
-#else
- config.pin_p = NRF_SAADC_INPUT_VDD;
- config.pin_n = NRF_SAADC_INPUT_VDD;
-#endif
+ config.pin_p = pin;
+ config.pin_n = pin;
config.reference = NRF_SAADC_REFERENCE_INTERNAL; // 0.6v reference.
config.resistor_p = NRF_SAADC_RESISTOR_DISABLED;
config.resistor_n = NRF_SAADC_RESISTOR_DISABLED;
@@ -2844,21 +2842,32 @@ JsVarFloat jshReadVRef() {
bool adcInUse = nrf_analog_read_start();
// make reading
- JsVarFloat f;
+ nrf_saadc_value_t f;
do {
nrf_analog_read_interrupted = false;
nrf_saadc_enable();
nrf_saadc_resolution_set(NRF_SAADC_RESOLUTION_14BIT);
nrf_saadc_channel_init(0, &config);
- f = nrf_analog_read() * (6.0 * 0.6 / 16384.0);
+ f = nrf_analog_read();
} while (nrf_analog_read_interrupted);
nrf_analog_read_end(adcInUse);
+ return f;
+}
+#endif
+
#ifdef ESPR_VREF_VDDH
- f *= 5; // we were on VDDHDIV5
+JsVarFloat jshReadVRefH() {
+ return jshReadVDD(0x0D, NRF_SAADC_ACQTIME_20US, NRF_SAADC_GAIN1_2) // When using VDDHDIV5 as input, the acquisition time must be 10 µs or longer.
+ *(2.0 * 5.0 * 0.6 / 16384.0); // gain 1/2, VDDHDIV5, 0.6v reference, 2^14 = 1.0
+}
#endif
- return f;
+// The voltage that a reading of 1 from `analogRead` actually represents
+JsVarFloat jshReadVRef() {
+#ifdef NRF52_SERIES
+ return jshReadVDD(NRF_SAADC_INPUT_VDD, NRF_SAADC_ACQTIME_3US, NRF_SAADC_GAIN1_6)
+ *(6.0 * 0.6 / 16384.0); // gain 1/6, 0.6v reference, 2^14 = 1.0)
#else
const nrf_adc_config_t nrf_adc_config = {
NRF_ADC_CONFIG_RES_10BIT, |
Beta Was this translation helpful? Give feedback.
Just merged this in - slightly renamed jshReadVRefH
I think if someone made a board where it was powered from VDDH they could provide a
'DEFINES+=-DCUSTOM_GETBATTERY=jswrap_myboard_getBattery'
function like we do for Puck.js and that could use the voltage to provide a percentage? I guess exposing VDDH might be handy for something anyway though, even if it's just debugging