From 6ffacaef055fe89fd2e6a76dae4fe0c28c36034b Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Wed, 19 Feb 2020 13:19:30 -0500 Subject: [PATCH 01/10] Added API call to return ADC reading in volts, scaled by configurable system ADC reference voltage. --- drivers/AnalogIn.h | 10 ++++++++++ drivers/mbed_lib.json | 5 ++++- drivers/source/AnalogIn.cpp | 5 +++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/drivers/AnalogIn.h b/drivers/AnalogIn.h index 9ff9ec7ee36..0b04b5c1ac8 100644 --- a/drivers/AnalogIn.h +++ b/drivers/AnalogIn.h @@ -93,6 +93,16 @@ class AnalogIn { */ unsigned short read_u16(); + /** Read the input voltage in volts. The output depends on the target board's + * ADC reference voltage (typically equal to supply voltage). The ADC reference voltage + * sets the maximum voltage the ADC can quantify (ie: Vin == Vref when ADC output == ADC_MAX_VALUE) + * + * The target's ADC reference voltage can be configured by overriding "drivers.adc_vref" + * + * @returns A floating-point value representing the current input voltage, measured in volts. + */ + float read_volts(); + /** An operator shorthand for read() * * The float() operator can be used as a shorthand for read() to simplify common code sequences diff --git a/drivers/mbed_lib.json b/drivers/mbed_lib.json index 614baa955e1..8caeebad2c2 100644 --- a/drivers/mbed_lib.json +++ b/drivers/mbed_lib.json @@ -41,6 +41,9 @@ "qspi_csn": { "help": "QSPI chip select pin", "value": "QSPI_FLASH1_CSN" - } + }, + "adc_vref": { + "help": "Reference voltage for ADC (float)", + "value": 3.3f } } diff --git a/drivers/source/AnalogIn.cpp b/drivers/source/AnalogIn.cpp index 63d910c2270..084930f4ec3 100644 --- a/drivers/source/AnalogIn.cpp +++ b/drivers/source/AnalogIn.cpp @@ -54,6 +54,11 @@ unsigned short AnalogIn::read_u16() return ret; } +float AnalogIn::read_volts() { + float ret = this->read(); + return (ret*MBED_CONF_DRIVERS_ADC_VREF); +} + } // namespace mbed #endif From 5a543918ca0e79a41b2d724b7bf60716f8bc1bb0 Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Wed, 19 Feb 2020 13:27:08 -0500 Subject: [PATCH 02/10] Added API to reconfigure ADC reference voltage at runtime. --- drivers/AnalogIn.h | 14 ++++++++++++++ drivers/mbed_lib.json | 4 ++-- drivers/source/AnalogIn.cpp | 10 +++++++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/drivers/AnalogIn.h b/drivers/AnalogIn.h index 0b04b5c1ac8..ed6c14b69d9 100644 --- a/drivers/AnalogIn.h +++ b/drivers/AnalogIn.h @@ -103,6 +103,17 @@ class AnalogIn { */ float read_volts(); + /** + * Sets this ADC instance's reference voltage. + * + * Defaults to the configurable MBED_CONF_DRIVERS_DEFAULT_ADC_VREF setting. + * + * The ADC's reference voltage is used to scale the output when calling AnalogIn::read_volts + * + * @param[in] vref New ADC reference voltage for this ADC instance. + */ + void set_reference_voltage(float vref); + /** An operator shorthand for read() * * The float() operator can be used as a shorthand for read() to simplify common code sequences @@ -141,6 +152,9 @@ class AnalogIn { analogin_t _adc; static SingletonPtr _mutex; + + float vref; + #endif //!defined(DOXYGEN_ONLY) }; diff --git a/drivers/mbed_lib.json b/drivers/mbed_lib.json index 8caeebad2c2..6da62309795 100644 --- a/drivers/mbed_lib.json +++ b/drivers/mbed_lib.json @@ -42,8 +42,8 @@ "help": "QSPI chip select pin", "value": "QSPI_FLASH1_CSN" }, - "adc_vref": { - "help": "Reference voltage for ADC (float)", + "default_adc_vref": { + "help": "Default reference voltage for ADC (float)", "value": 3.3f } } diff --git a/drivers/source/AnalogIn.cpp b/drivers/source/AnalogIn.cpp index 084930f4ec3..1370a718630 100644 --- a/drivers/source/AnalogIn.cpp +++ b/drivers/source/AnalogIn.cpp @@ -23,14 +23,14 @@ namespace mbed { SingletonPtr AnalogIn::_mutex; -AnalogIn::AnalogIn(PinName pin) +AnalogIn::AnalogIn(PinName pin) : vref(MBED_CONF_DRIVERS_DEFAULT_ADC_VREF) { lock(); analogin_init(&_adc, pin); unlock(); } -AnalogIn::AnalogIn(const PinMap &pinmap) +AnalogIn::AnalogIn(const PinMap &pinmap) : vref(MBED_CONF_DRIVERS_DEFAULT_ADC_VREF) { lock(); analogin_init_direct(&_adc, &pinmap); @@ -56,7 +56,11 @@ unsigned short AnalogIn::read_u16() float AnalogIn::read_volts() { float ret = this->read(); - return (ret*MBED_CONF_DRIVERS_ADC_VREF); + return (ret*this->vref); +} + +void AnalogIn::set_reference_voltage(float vref) { + this->vref = vref; } } // namespace mbed From 3717c982e3f5574e2e1ff9d7728c2720559ff781 Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Wed, 19 Feb 2020 13:42:49 -0500 Subject: [PATCH 03/10] Rename default adc vref parameter. --- drivers/mbed_lib.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/mbed_lib.json b/drivers/mbed_lib.json index 6da62309795..6df4df5f638 100644 --- a/drivers/mbed_lib.json +++ b/drivers/mbed_lib.json @@ -42,7 +42,7 @@ "help": "QSPI chip select pin", "value": "QSPI_FLASH1_CSN" }, - "default_adc_vref": { + "default-adc-vref": { "help": "Default reference voltage for ADC (float)", "value": 3.3f } From 2db99171176bf35f790f7dca840efa5ffeb123c6 Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Wed, 19 Feb 2020 13:45:16 -0500 Subject: [PATCH 04/10] Add Vin and Vref to ignore list in spell checker CI --- tools/test/travis-ci/doxy-spellchecker/ignore.en.pws | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/test/travis-ci/doxy-spellchecker/ignore.en.pws b/tools/test/travis-ci/doxy-spellchecker/ignore.en.pws index bda5d742a43..29f2d083610 100644 --- a/tools/test/travis-ci/doxy-spellchecker/ignore.en.pws +++ b/tools/test/travis-ci/doxy-spellchecker/ignore.en.pws @@ -114,4 +114,6 @@ api uart chrono Hinnant +Vin +Vref _doxy_ From b76300f57c4703d085df91e8dd4c540afaf191ae Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Wed, 19 Feb 2020 13:58:31 -0500 Subject: [PATCH 05/10] Add reference voltage getter and clarify some comments --- drivers/AnalogIn.h | 16 ++++++++++++---- drivers/source/AnalogIn.cpp | 4 ++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/drivers/AnalogIn.h b/drivers/AnalogIn.h index ed6c14b69d9..d227edfd3f8 100644 --- a/drivers/AnalogIn.h +++ b/drivers/AnalogIn.h @@ -93,7 +93,8 @@ class AnalogIn { */ unsigned short read_u16(); - /** Read the input voltage in volts. The output depends on the target board's + /** + * Read the input voltage in volts. The output depends on the target board's * ADC reference voltage (typically equal to supply voltage). The ADC reference voltage * sets the maximum voltage the ADC can quantify (ie: Vin == Vref when ADC output == ADC_MAX_VALUE) * @@ -104,16 +105,23 @@ class AnalogIn { float read_volts(); /** - * Sets this ADC instance's reference voltage. + * Sets this AnalogIn instance's reference voltage. * * Defaults to the configurable MBED_CONF_DRIVERS_DEFAULT_ADC_VREF setting. * - * The ADC's reference voltage is used to scale the output when calling AnalogIn::read_volts + * The AnalogIn's reference voltage is used to scale the output when calling AnalogIn::read_volts * - * @param[in] vref New ADC reference voltage for this ADC instance. + * @param[in] vref New ADC reference voltage for this AnalogIn instance. */ void set_reference_voltage(float vref); + /** + * Gets this AnalogIn instance's reference voltage. + * + * @returns A floating-point value representing this AnalogIn's reference voltage, measured in volts. + */ + float get_reference_voltage(void); + /** An operator shorthand for read() * * The float() operator can be used as a shorthand for read() to simplify common code sequences diff --git a/drivers/source/AnalogIn.cpp b/drivers/source/AnalogIn.cpp index 1370a718630..df8fa659175 100644 --- a/drivers/source/AnalogIn.cpp +++ b/drivers/source/AnalogIn.cpp @@ -63,6 +63,10 @@ void AnalogIn::set_reference_voltage(float vref) { this->vref = vref; } +float AnalogIn::get_reference_voltage(void) { + return this->vref; +} + } // namespace mbed #endif From 7b708436326e06305131a467a8f334024a83f619 Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Wed, 19 Feb 2020 14:00:49 -0500 Subject: [PATCH 06/10] Fix styling and fix mbed_lib.json syntax error --- drivers/mbed_lib.json | 3 ++- drivers/source/AnalogIn.cpp | 12 +++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/drivers/mbed_lib.json b/drivers/mbed_lib.json index 6df4df5f638..1e70e5e4c9a 100644 --- a/drivers/mbed_lib.json +++ b/drivers/mbed_lib.json @@ -44,6 +44,7 @@ }, "default-adc-vref": { "help": "Default reference voltage for ADC (float)", - "value": 3.3f + "value": "3.3f" + } } } diff --git a/drivers/source/AnalogIn.cpp b/drivers/source/AnalogIn.cpp index df8fa659175..a26b01bdfd1 100644 --- a/drivers/source/AnalogIn.cpp +++ b/drivers/source/AnalogIn.cpp @@ -54,16 +54,18 @@ unsigned short AnalogIn::read_u16() return ret; } -float AnalogIn::read_volts() { - float ret = this->read(); - return (ret*this->vref); +float AnalogIn::read_volts() +{ + return (this->read() * this->vref); } -void AnalogIn::set_reference_voltage(float vref) { +void AnalogIn::set_reference_voltage(float vref) +{ this->vref = vref; } -float AnalogIn::get_reference_voltage(void) { +float AnalogIn::get_reference_voltage(void) +{ return this->vref; } From 5d3d6335669aef7e26c6de5e6f0e26a6d507dd11 Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Fri, 3 Apr 2020 04:21:08 -0400 Subject: [PATCH 07/10] Update voltage wording to be consistent --- drivers/AnalogIn.h | 2 +- drivers/source/AnalogIn.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/AnalogIn.h b/drivers/AnalogIn.h index d227edfd3f8..dafb0f50edb 100644 --- a/drivers/AnalogIn.h +++ b/drivers/AnalogIn.h @@ -102,7 +102,7 @@ class AnalogIn { * * @returns A floating-point value representing the current input voltage, measured in volts. */ - float read_volts(); + float read_voltage(); /** * Sets this AnalogIn instance's reference voltage. diff --git a/drivers/source/AnalogIn.cpp b/drivers/source/AnalogIn.cpp index a26b01bdfd1..8c77426b31f 100644 --- a/drivers/source/AnalogIn.cpp +++ b/drivers/source/AnalogIn.cpp @@ -54,7 +54,7 @@ unsigned short AnalogIn::read_u16() return ret; } -float AnalogIn::read_volts() +float AnalogIn::read_voltage() { return (this->read() * this->vref); } From 746f2f5fb58215c9449b8b73ae68034a86de597c Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Fri, 3 Apr 2020 04:25:29 -0400 Subject: [PATCH 08/10] Changed default ADC vref configuration from driver-level to target-level --- drivers/AnalogIn.h | 4 ++-- drivers/mbed_lib.json | 6 +----- drivers/source/AnalogIn.cpp | 4 ++-- targets/targets.json | 4 ++++ 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/drivers/AnalogIn.h b/drivers/AnalogIn.h index dafb0f50edb..17a2a2fc1e4 100644 --- a/drivers/AnalogIn.h +++ b/drivers/AnalogIn.h @@ -98,7 +98,7 @@ class AnalogIn { * ADC reference voltage (typically equal to supply voltage). The ADC reference voltage * sets the maximum voltage the ADC can quantify (ie: Vin == Vref when ADC output == ADC_MAX_VALUE) * - * The target's ADC reference voltage can be configured by overriding "drivers.adc_vref" + * The target's ADC reference voltage can be configured by overriding "target.adc_vref" * * @returns A floating-point value representing the current input voltage, measured in volts. */ @@ -107,7 +107,7 @@ class AnalogIn { /** * Sets this AnalogIn instance's reference voltage. * - * Defaults to the configurable MBED_CONF_DRIVERS_DEFAULT_ADC_VREF setting. + * Defaults to the configurable MBED_CONF_TARGET_DEFAULT_ADC_VREF setting. * * The AnalogIn's reference voltage is used to scale the output when calling AnalogIn::read_volts * diff --git a/drivers/mbed_lib.json b/drivers/mbed_lib.json index 1e70e5e4c9a..614baa955e1 100644 --- a/drivers/mbed_lib.json +++ b/drivers/mbed_lib.json @@ -41,10 +41,6 @@ "qspi_csn": { "help": "QSPI chip select pin", "value": "QSPI_FLASH1_CSN" - }, - "default-adc-vref": { - "help": "Default reference voltage for ADC (float)", - "value": "3.3f" - } + } } } diff --git a/drivers/source/AnalogIn.cpp b/drivers/source/AnalogIn.cpp index 8c77426b31f..0f0529e42cb 100644 --- a/drivers/source/AnalogIn.cpp +++ b/drivers/source/AnalogIn.cpp @@ -23,14 +23,14 @@ namespace mbed { SingletonPtr AnalogIn::_mutex; -AnalogIn::AnalogIn(PinName pin) : vref(MBED_CONF_DRIVERS_DEFAULT_ADC_VREF) +AnalogIn::AnalogIn(PinName pin) : vref(MBED_CONF_TARGET_DEFAULT_ADC_VREF) { lock(); analogin_init(&_adc, pin); unlock(); } -AnalogIn::AnalogIn(const PinMap &pinmap) : vref(MBED_CONF_DRIVERS_DEFAULT_ADC_VREF) +AnalogIn::AnalogIn(const PinMap &pinmap) : vref(MBED_CONF_TARGET_DEFAULT_ADC_VREF) { lock(); analogin_init_direct(&_adc, &pinmap); diff --git a/targets/targets.json b/targets/targets.json index 135eddf484a..44b47378fe7 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -80,6 +80,10 @@ "xip-enable": { "help": "Enable Execute In Place (XIP) on this target. Value is only significant if the board has executable external storage such as QSPIF. If this is enabled, customize the linker file to choose what text segments are placed on external storage", "value": false + }, + "default-adc-vref": { + "help": "Default reference voltage for ADC (float)", + "value": "3.3f" } } }, From aeb91c259893aae7ec7c25ebf77750a26672207e Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Thu, 30 Apr 2020 12:23:03 -0400 Subject: [PATCH 09/10] Updated default vref to be NAN. Made vref an optional constructor arg --- drivers/AnalogIn.h | 10 ++++++---- drivers/source/AnalogIn.cpp | 12 ++++++------ targets/targets.json | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/drivers/AnalogIn.h b/drivers/AnalogIn.h index 17a2a2fc1e4..8fab087cdbe 100644 --- a/drivers/AnalogIn.h +++ b/drivers/AnalogIn.h @@ -25,6 +25,8 @@ #include "platform/SingletonPtr.h" #include "platform/PlatformMutex.h" +#include + namespace mbed { /** \defgroup mbed-os-public Public API */ @@ -71,14 +73,14 @@ class AnalogIn { * * @param pinmap reference to structure which holds static pinmap. */ - AnalogIn(const PinMap &pinmap); - AnalogIn(const PinMap &&) = delete; // prevent passing of temporary objects + AnalogIn(const PinMap &pinmap, float vref = MBED_CONF_TARGET_DEFAULT_ADC_VREF); + AnalogIn(const PinMap &&, float vref = MBED_CONF_TARGET_DEFAULT_ADC_VREF) = delete; // prevent passing of temporary objects /** Create an AnalogIn, connected to the specified pin * * @param pin AnalogIn pin to connect to */ - AnalogIn(PinName pin); + AnalogIn(PinName pin, float vref = MBED_CONF_TARGET_DEFAULT_ADC_VREF); /** Read the input voltage, represented as a float in the range [0.0, 1.0] * @@ -161,7 +163,7 @@ class AnalogIn { analogin_t _adc; static SingletonPtr _mutex; - float vref; + float _vref; #endif //!defined(DOXYGEN_ONLY) diff --git a/drivers/source/AnalogIn.cpp b/drivers/source/AnalogIn.cpp index 0f0529e42cb..cd498f90f73 100644 --- a/drivers/source/AnalogIn.cpp +++ b/drivers/source/AnalogIn.cpp @@ -23,21 +23,21 @@ namespace mbed { SingletonPtr AnalogIn::_mutex; -AnalogIn::AnalogIn(PinName pin) : vref(MBED_CONF_TARGET_DEFAULT_ADC_VREF) + +AnalogIn::AnalogIn(PinName pin, float vref) : _vref(vref) { lock(); analogin_init(&_adc, pin); unlock(); } -AnalogIn::AnalogIn(const PinMap &pinmap) : vref(MBED_CONF_TARGET_DEFAULT_ADC_VREF) +AnalogIn::AnalogIn(const PinMap &pinmap, float vref) : _vref(vref) { lock(); analogin_init_direct(&_adc, &pinmap); unlock(); } - float AnalogIn::read() { lock(); @@ -56,17 +56,17 @@ unsigned short AnalogIn::read_u16() float AnalogIn::read_voltage() { - return (this->read() * this->vref); + return (this->read() * this->_vref); } void AnalogIn::set_reference_voltage(float vref) { - this->vref = vref; + this->_vref = vref; } float AnalogIn::get_reference_voltage(void) { - return this->vref; + return this->_vref; } } // namespace mbed diff --git a/targets/targets.json b/targets/targets.json index 44b47378fe7..75cbe81df48 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -83,7 +83,7 @@ }, "default-adc-vref": { "help": "Default reference voltage for ADC (float)", - "value": "3.3f" + "value": "NAN" } } }, From 7f19f8226e539dc9abcfed2a537a7b9c30df196d Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Wed, 20 May 2020 06:23:22 -0400 Subject: [PATCH 10/10] Revised doxygen comments and fixed code style inconsistencies --- drivers/AnalogIn.h | 16 +++++++++++----- drivers/source/AnalogIn.cpp | 8 ++++---- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/drivers/AnalogIn.h b/drivers/AnalogIn.h index 8fab087cdbe..38c1fc130d6 100644 --- a/drivers/AnalogIn.h +++ b/drivers/AnalogIn.h @@ -72,6 +72,9 @@ class AnalogIn { /** Create an AnalogIn, connected to the specified pin * * @param pinmap reference to structure which holds static pinmap. + * @param vref (optional) Reference voltage of this AnalogIn instance (defaults to target.default-adc-vref). + * + * @note An input voltage at or above the given vref value will produce a 1.0 result when `read` is called */ AnalogIn(const PinMap &pinmap, float vref = MBED_CONF_TARGET_DEFAULT_ADC_VREF); AnalogIn(const PinMap &&, float vref = MBED_CONF_TARGET_DEFAULT_ADC_VREF) = delete; // prevent passing of temporary objects @@ -79,6 +82,9 @@ class AnalogIn { /** Create an AnalogIn, connected to the specified pin * * @param pin AnalogIn pin to connect to + * @param vref (optional) Reference voltage of this AnalogIn instance (defaults to target.default-adc-vref). + * + * @note An input voltage at or above the given vref value will produce a 1.0 result when `read` is called */ AnalogIn(PinName pin, float vref = MBED_CONF_TARGET_DEFAULT_ADC_VREF); @@ -98,9 +104,11 @@ class AnalogIn { /** * Read the input voltage in volts. The output depends on the target board's * ADC reference voltage (typically equal to supply voltage). The ADC reference voltage - * sets the maximum voltage the ADC can quantify (ie: Vin == Vref when ADC output == ADC_MAX_VALUE) + * sets the maximum voltage the ADC can quantify (ie: ADC output == ADC_MAX_VALUE when Vin == Vref) * - * The target's ADC reference voltage can be configured by overriding "target.adc_vref" + * The target's default ADC reference voltage is determined by the configuration + * option target.default-adc_vref. The reference voltage for a particular input + * can be manually specified by either the constructor or `AnalogIn::set_reference_voltage`. * * @returns A floating-point value representing the current input voltage, measured in volts. */ @@ -109,8 +117,6 @@ class AnalogIn { /** * Sets this AnalogIn instance's reference voltage. * - * Defaults to the configurable MBED_CONF_TARGET_DEFAULT_ADC_VREF setting. - * * The AnalogIn's reference voltage is used to scale the output when calling AnalogIn::read_volts * * @param[in] vref New ADC reference voltage for this AnalogIn instance. @@ -122,7 +128,7 @@ class AnalogIn { * * @returns A floating-point value representing this AnalogIn's reference voltage, measured in volts. */ - float get_reference_voltage(void); + float get_reference_voltage() const; /** An operator shorthand for read() * diff --git a/drivers/source/AnalogIn.cpp b/drivers/source/AnalogIn.cpp index cd498f90f73..596d034f194 100644 --- a/drivers/source/AnalogIn.cpp +++ b/drivers/source/AnalogIn.cpp @@ -56,17 +56,17 @@ unsigned short AnalogIn::read_u16() float AnalogIn::read_voltage() { - return (this->read() * this->_vref); + return read() * _vref; } void AnalogIn::set_reference_voltage(float vref) { - this->_vref = vref; + _vref = vref; } -float AnalogIn::get_reference_voltage(void) +float AnalogIn::get_reference_voltage(void) const { - return this->_vref; + return _vref; } } // namespace mbed