From e053e04b0517fb5fc165477e733f63fcc2147291 Mon Sep 17 00:00:00 2001 From: rdi Date: Wed, 29 May 2024 14:30:50 +0200 Subject: [PATCH] Add sampling time and oversampling options to adc --- include/erb/daisy/AdcDaisy.h | 21 ++++++++++- include/erb/daisy/AdcDaisy.hpp | 68 ++++++++++++++++++++++++++++++---- 2 files changed, 80 insertions(+), 9 deletions(-) diff --git a/include/erb/daisy/AdcDaisy.h b/include/erb/daisy/AdcDaisy.h index b3745a292..9684766b9 100755 --- a/include/erb/daisy/AdcDaisy.h +++ b/include/erb/daisy/AdcDaisy.h @@ -17,6 +17,7 @@ erb_DISABLE_WARNINGS_DAISY #include "per/gpio.h" +#include "per/adc.h" erb_RESTORE_WARNINGS #include @@ -33,6 +34,18 @@ namespace erb +enum class AdcSamplingTimeCycles +{ + _1_5, _2_5, _8_5, +}; + +enum class AdcOversampling +{ + None, _4, _8, _16, _32 +}; + + + template class AdcDaisy { @@ -57,7 +70,7 @@ class AdcDaisy MuxAddress address = MuxAddress {}; }; - inline AdcDaisy (daisy::AdcHandle & adc, std::initializer_list channels); + inline AdcDaisy (daisy::AdcHandle & adc, std::initializer_list channels, AdcOversampling oversampling = AdcOversampling::_32, AdcSamplingTimeCycles sampling_time = AdcSamplingTimeCycles::_8_5); virtual ~AdcDaisy () = default; inline uint16_t @@ -81,6 +94,12 @@ class AdcDaisy private: + static daisy::AdcChannelConfig::ConversionSpeed + to_ConversionSpeed (AdcSamplingTimeCycles sampling_time); + + static daisy::AdcHandle::OverSampling + to_OverSampling (AdcOversampling oversampling); + std::array _data; diff --git a/include/erb/daisy/AdcDaisy.hpp b/include/erb/daisy/AdcDaisy.hpp index d62eaa849..740142d11 100755 --- a/include/erb/daisy/AdcDaisy.hpp +++ b/include/erb/daisy/AdcDaisy.hpp @@ -13,10 +13,6 @@ /*\\\ INCLUDE FILES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/ -erb_DISABLE_WARNINGS_DAISY -#include "per/adc.h" -erb_RESTORE_WARNINGS - namespace erb @@ -33,7 +29,7 @@ Name : ctor */ template -AdcDaisy ::AdcDaisy (daisy::AdcHandle & adc, std::initializer_list channels) +AdcDaisy ::AdcDaisy (daisy::AdcHandle & adc, std::initializer_list channels, AdcOversampling oversampling, AdcSamplingTimeCycles sampling_time) { std::array configs; @@ -45,7 +41,7 @@ AdcDaisy ::AdcDaisy (daisy::AdcHandle & adc, std::initializer_list < if (channel.nbr_channels == 1) { - config.InitSingle (channel.pin); + config.InitSingle (channel.pin, to_ConversionSpeed (sampling_time)); } else { @@ -54,12 +50,13 @@ AdcDaisy ::AdcDaisy (daisy::AdcHandle & adc, std::initializer_list < channel.nbr_channels, channel.address.pin_a, channel.address.pin_b, - channel.address.pin_c + channel.address.pin_c, + to_ConversionSpeed (sampling_time) ); } } - adc.Init (&configs [0], config_nbr); + adc.Init (&configs [0], config_nbr, to_OverSampling (oversampling)); config_nbr = 0; size_t channel_nbr = 0; @@ -112,6 +109,61 @@ uint16_t AdcDaisy ::read (size_t index) /*\\\ PRIVATE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/ +/* +============================================================================== +Name : to_ConversionSpeed +============================================================================== +*/ + +template +daisy::AdcChannelConfig::ConversionSpeed AdcDaisy ::to_ConversionSpeed (AdcSamplingTimeCycles sampling_time) +{ + switch (sampling_time) + { + case AdcSamplingTimeCycles::_1_5: + return daisy::AdcChannelConfig::SPEED_1CYCLES_5; + + case AdcSamplingTimeCycles::_2_5: + return daisy::AdcChannelConfig::SPEED_2CYCLES_5; + + case AdcSamplingTimeCycles::_8_5: + return daisy::AdcChannelConfig::SPEED_8CYCLES_5; + } + + __builtin_unreachable (); +} + + +/* +============================================================================== +Name : to_OverSampling +============================================================================== +*/ + +template +daisy::AdcHandle::OverSampling AdcDaisy ::to_OverSampling (AdcOversampling oversampling) +{ + switch (oversampling) + { + case AdcOversampling::None: + return daisy::AdcHandle::OVS_NONE; + + case AdcOversampling::_4: + return daisy::AdcHandle::OVS_4; + + case AdcOversampling::_8: + return daisy::AdcHandle::OVS_8; + + case AdcOversampling::_16: + return daisy::AdcHandle::OVS_16; + + case AdcOversampling::_32: + return daisy::AdcHandle::OVS_32; + } + + __builtin_unreachable (); +} + } // namespace erb