Skip to content

Commit

Permalink
Add ConversionSpeed configuration to ADC class (#579)
Browse files Browse the repository at this point in the history
* added ConversionSpeed to ADC class (based on v3.0.0 release)

* style fix.

* removed commented-out code clutter.

---------

Co-authored-by: stephenhensley <[email protected]>
  • Loading branch information
stephenhensley and stephenhensley authored Jun 16, 2023
1 parent e933241 commit 25b61a3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
46 changes: 39 additions & 7 deletions src/per/adc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,21 @@ static dsy_adc adc;

// Begin AdcChannelConfig Implementations

void AdcChannelConfig::InitSingle(dsy_gpio_pin pin)
void AdcChannelConfig::InitSingle(dsy_gpio_pin pin,
AdcChannelConfig::ConversionSpeed speed)
{
pin_.pin = pin;
mux_channels_ = 0;
pin_.mode = DSY_GPIO_MODE_ANALOG;
pin_.pull = DSY_GPIO_NOPULL;
speed_ = speed;
}
void AdcChannelConfig::InitMux(dsy_gpio_pin adc_pin,
size_t mux_channels,
dsy_gpio_pin mux_0,
dsy_gpio_pin mux_1,
dsy_gpio_pin mux_2)
void AdcChannelConfig::InitMux(dsy_gpio_pin adc_pin,
size_t mux_channels,
dsy_gpio_pin mux_0,
dsy_gpio_pin mux_1,
dsy_gpio_pin mux_2,
AdcChannelConfig::ConversionSpeed speed)
{
size_t pins_to_init;
// Init ADC Pin
Expand All @@ -228,6 +231,7 @@ void AdcChannelConfig::InitMux(dsy_gpio_pin adc_pin,
mux_pin_[i].mode = DSY_GPIO_MODE_OUTPUT_PP;
mux_pin_[i].pull = DSY_GPIO_NOPULL;
}
speed_ = speed;
}

// Begin AdcHandle Implementations
Expand Down Expand Up @@ -359,14 +363,42 @@ void AdcHandle::Init(AdcChannelConfig* cfg,
}
// Configure Regular Channel
// Configure Shared settings for all channels.
sConfig.SamplingTime = ADC_SAMPLETIME_8CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
for(uint8_t i = 0; i < adc.channels; i++)
{
const auto& cfg = adc.pin_cfg[i];

/** Handle per-channel conversions */
switch(cfg.speed_)
{
case AdcChannelConfig::SPEED_1CYCLES_5:
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
break;
case AdcChannelConfig::SPEED_2CYCLES_5:
sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
break;
case AdcChannelConfig::SPEED_8CYCLES_5:
sConfig.SamplingTime = ADC_SAMPLETIME_8CYCLES_5;
break;
case AdcChannelConfig::SPEED_16CYCLES_5:
sConfig.SamplingTime = ADC_SAMPLETIME_16CYCLES_5;
break;
case AdcChannelConfig::SPEED_32CYCLES_5:
sConfig.SamplingTime = ADC_SAMPLETIME_32CYCLES_5;
break;
case AdcChannelConfig::SPEED_64CYCLES_5:
sConfig.SamplingTime = ADC_SAMPLETIME_64CYCLES_5;
break;
case AdcChannelConfig::SPEED_387CYCLES_5:
sConfig.SamplingTime = ADC_SAMPLETIME_387CYCLES_5;
break;
case AdcChannelConfig::SPEED_810CYCLES_5:
sConfig.SamplingTime = ADC_SAMPLETIME_810CYCLES_5;
break;
}

// init ADC pin
dsy_gpio_init(&cfg.pin_);

Expand Down
39 changes: 28 additions & 11 deletions src/per/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,49 @@ struct AdcChannelConfig
MUX_SEL_LAST, /**< & */
};

/** \brief per channel conversion speed added to fixed time based on bitdepth, etc. */
enum ConversionSpeed
{
SPEED_1CYCLES_5,
SPEED_2CYCLES_5,
SPEED_8CYCLES_5,
SPEED_16CYCLES_5,
SPEED_32CYCLES_5,
SPEED_64CYCLES_5,
SPEED_387CYCLES_5,
SPEED_810CYCLES_5,
};

/** Initializes a single ADC pin as an ADC.
\param pin Pin to init.
\param speed conversion speed for this pin defaults to 8.5 cycles
*/
void InitSingle(dsy_gpio_pin pin);
void InitSingle(dsy_gpio_pin pin, ConversionSpeed speed = SPEED_8CYCLES_5);

/**
Initializes a single ADC pin as a Multiplexed ADC.
Requires a CD405X Multiplexer connected to the pin.
You only need to supply the mux pins that are required,
e.g. a 4052 mux would only require mux_0 and mux_1.
Internal Callbacks handle the pin addressing.
\param adc_pin &
\param mux_channels must be 1-8
\param mux_0 First mux pin
\param mux_1 Second mux pin
\param mux_2 Third mux pin
\param adc_pin &
\param speed conversion speed for this pin defaults to 8.5 cycles
*/
void InitMux(dsy_gpio_pin adc_pin,
size_t mux_channels,
dsy_gpio_pin mux_0,
dsy_gpio_pin mux_1 = {DSY_GPIOX, 0},
dsy_gpio_pin mux_2 = {DSY_GPIOX, 0});

dsy_gpio pin_; /**< & */
dsy_gpio mux_pin_[MUX_SEL_LAST]; /**< & */
uint8_t mux_channels_; /**< & */
void InitMux(dsy_gpio_pin adc_pin,
size_t mux_channels,
dsy_gpio_pin mux_0,
dsy_gpio_pin mux_1 = {DSY_GPIOX, 0},
dsy_gpio_pin mux_2 = {DSY_GPIOX, 0},
ConversionSpeed speed = SPEED_8CYCLES_5);

dsy_gpio pin_; /**< & */
dsy_gpio mux_pin_[MUX_SEL_LAST]; /**< & */
uint8_t mux_channels_; /**< & */
ConversionSpeed speed_;
};

/**
Expand Down

0 comments on commit 25b61a3

Please sign in to comment.