Skip to content

Commit

Permalink
Add sampling time and oversampling options to adc
Browse files Browse the repository at this point in the history
  • Loading branch information
ohmtech-rdi committed May 29, 2024
1 parent 5c47674 commit e053e04
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 9 deletions.
21 changes: 20 additions & 1 deletion include/erb/daisy/AdcDaisy.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

erb_DISABLE_WARNINGS_DAISY
#include "per/gpio.h"
#include "per/adc.h"
erb_RESTORE_WARNINGS

#include <array>
Expand All @@ -33,6 +34,18 @@ namespace erb



enum class AdcSamplingTimeCycles
{
_1_5, _2_5, _8_5,
};

enum class AdcOversampling
{
None, _4, _8, _16, _32
};



template <size_t MaxNbrData>
class AdcDaisy
{
Expand All @@ -57,7 +70,7 @@ class AdcDaisy
MuxAddress address = MuxAddress {};
};

inline AdcDaisy (daisy::AdcHandle & adc, std::initializer_list <Channel> channels);
inline AdcDaisy (daisy::AdcHandle & adc, std::initializer_list <Channel> channels, AdcOversampling oversampling = AdcOversampling::_32, AdcSamplingTimeCycles sampling_time = AdcSamplingTimeCycles::_8_5);
virtual ~AdcDaisy () = default;

inline uint16_t
Expand All @@ -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 <uint16_t *, MaxNbrData>
_data;

Expand Down
68 changes: 60 additions & 8 deletions include/erb/daisy/AdcDaisy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@

/*\\\ INCLUDE FILES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/

erb_DISABLE_WARNINGS_DAISY
#include "per/adc.h"
erb_RESTORE_WARNINGS



namespace erb
Expand All @@ -33,7 +29,7 @@ Name : ctor
*/

template <size_t MaxNbrData>
AdcDaisy <MaxNbrData>::AdcDaisy (daisy::AdcHandle & adc, std::initializer_list <Channel> channels)
AdcDaisy <MaxNbrData>::AdcDaisy (daisy::AdcHandle & adc, std::initializer_list <Channel> channels, AdcOversampling oversampling, AdcSamplingTimeCycles sampling_time)
{
std::array <daisy::AdcChannelConfig, MaxNbrData> configs;

Expand All @@ -45,7 +41,7 @@ AdcDaisy <MaxNbrData>::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
{
Expand All @@ -54,12 +50,13 @@ AdcDaisy <MaxNbrData>::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;
Expand Down Expand Up @@ -112,6 +109,61 @@ uint16_t AdcDaisy <MaxNbrData>::read (size_t index)

/*\\\ PRIVATE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/

/*
==============================================================================
Name : to_ConversionSpeed
==============================================================================
*/

template <size_t MaxNbrData>
daisy::AdcChannelConfig::ConversionSpeed AdcDaisy <MaxNbrData>::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 <size_t MaxNbrData>
daisy::AdcHandle::OverSampling AdcDaisy <MaxNbrData>::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
Expand Down

0 comments on commit e053e04

Please sign in to comment.