@@ -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