Skip to content

Commit 38bcf2c

Browse files
committed
nRF52840: Add E.getVDDH() method to get VDDH voltage
Courtesy of @fanoush
1 parent cbaf1a8 commit 38bcf2c

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Bangle.js: Fix wrapString when wrapping a non-UTF8 string containing UTF8 characters (fix #2633)
44
Ensure jshPushIOCharEvent is more stable when the FIFO is full
55
Bangle.js: g.findFont now attempts to use Intl:2 if there's room. Also fix memory leak
6+
nRF52840: Add E.getVDDH() method to get VDDH voltage
67

78
2v26 : nRF5x: ensure TIMER1_IRQHandler doesn't always wake idle loop up (fix #1900)
89
Puck.js: On v2.1 ensure Puck.mag behaves like other variants - just returning the last reading (avoids glitches when used with Puck.magOn)

src/jshardware.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ JsVarFloat jshReadTemperature();
388388

389389
/// The voltage that a reading of 1 from `analogRead` actually represents, in volts
390390
JsVarFloat jshReadVRef();
391+
/// On nRF52833/40 this is the VDDH value (before the internal voltage regulator)
392+
JsVarFloat jshReadVDDH();
391393

392394
/** Get a random number - either using special purpose hardware or by
393395
* reading noise from an analog input. If unimplemented, this should

src/jswrap_espruino.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,22 @@ While this is implemented on Espruino boards, it may not be implemented on other
182182
devices. If so it'll return NaN.
183183
*/
184184

185+
/*JSON{
186+
"type" : "staticmethod",
187+
"#if" : "defined(NRF52833) || defined(NRF52840)",
188+
"class" : "E",
189+
"name" : "getVDDH",
190+
"generate_full" : "jshReadVDDH()",
191+
"return" : ["float","The voltage on VDDH input"]
192+
}
193+
Return the voltage on VDDH input
194+
195+
**Note:** This value is calculated by reading the voltage on an internal
196+
voltage reference with the ADC. It will be slightly noisy, so if you need this
197+
for accurate measurements we'd recommend that you call this function several
198+
times and average the results.
199+
*/
200+
185201

186202
int nativeCallGetCType() {
187203
if (lex->tk == LEX_R_VOID) {

targets/nrf5x/jshardware.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,43 +2857,47 @@ JsVarFloat jshReadTemperature() {
28572857
#endif
28582858
}
28592859

2860-
// The voltage that a reading of 1 from `analogRead` actually represents
2861-
JsVarFloat jshReadVRef() {
28622860
#ifdef NRF52_SERIES
2861+
nrf_saadc_value_t jshReadVDD(nrf_saadc_input_t pin, nrf_saadc_acqtime_t time, nrf_saadc_gain_t gain) {
28632862
nrf_saadc_channel_config_t config;
2864-
config.acq_time = NRF_SAADC_ACQTIME_3US;
2865-
config.gain = NRF_SAADC_GAIN1_6; // 1/6 of input volts
2863+
config.acq_time = time;
2864+
config.gain = gain;
28662865
config.mode = NRF_SAADC_MODE_SINGLE_ENDED;
2867-
2868-
#ifdef ESPR_VREF_VDDH
2869-
config.pin_p = 0x0D; // Not in Nordic's libs, but this is VDDHDIV5 - we probably want to be looking at VDDH
2870-
config.pin_n = 0x0D;
2871-
#else
2872-
config.pin_p = NRF_SAADC_INPUT_VDD;
2873-
config.pin_n = NRF_SAADC_INPUT_VDD;
2874-
#endif
2866+
config.pin_p = pin;
2867+
config.pin_n = pin;
28752868
config.reference = NRF_SAADC_REFERENCE_INTERNAL; // 0.6v reference.
28762869
config.resistor_p = NRF_SAADC_RESISTOR_DISABLED;
28772870
config.resistor_n = NRF_SAADC_RESISTOR_DISABLED;
28782871

28792872
bool adcInUse = nrf_analog_read_start();
28802873

28812874
// make reading
2882-
JsVarFloat f;
2875+
nrf_saadc_value_t f;
28832876
do {
28842877
nrf_analog_read_interrupted = false;
28852878
nrf_saadc_enable();
28862879
nrf_saadc_resolution_set(NRF_SAADC_RESOLUTION_14BIT);
28872880
nrf_saadc_channel_init(0, &config);
28882881

2889-
f = nrf_analog_read() * (6.0 * 0.6 / 16384.0);
2882+
f = nrf_analog_read();
28902883
} while (nrf_analog_read_interrupted);
28912884
nrf_analog_read_end(adcInUse);
2892-
#ifdef ESPR_VREF_VDDH
2893-
f *= 5; // we were on VDDHDIV5
2885+
return f;
2886+
}
28942887
#endif
28952888

2896-
return f;
2889+
#if defined(NRF52833) || defined(NRF52840)
2890+
JsVarFloat jshReadVDDH() {
2891+
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.
2892+
*(2.0 * 5.0 * 0.6 / 16384.0); // gain 1/2, VDDHDIV5, 0.6v reference, 2^14 = 1.0
2893+
}
2894+
#endif
2895+
2896+
// The voltage that a reading of 1 from `analogRead` actually represents
2897+
JsVarFloat jshReadVRef() {
2898+
#ifdef NRF52_SERIES
2899+
return jshReadVDD(NRF_SAADC_INPUT_VDD, NRF_SAADC_ACQTIME_3US, NRF_SAADC_GAIN1_6)
2900+
*(6.0 * 0.6 / 16384.0); // gain 1/6, 0.6v reference, 2^14 = 1.0)
28972901
#else
28982902
const nrf_adc_config_t nrf_adc_config = {
28992903
NRF_ADC_CONFIG_RES_10BIT,

0 commit comments

Comments
 (0)