diff --git a/src/per/adc.cpp b/src/per/adc.cpp index c0fa4cbb9..1ac21acf0 100644 --- a/src/per/adc.cpp +++ b/src/per/adc.cpp @@ -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 @@ -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 @@ -359,7 +363,6 @@ 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; @@ -367,6 +370,35 @@ void AdcHandle::Init(AdcChannelConfig* cfg, { 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_); diff --git a/src/per/adc.h b/src/per/adc.h index bb3161a04..14db03927 100644 --- a/src/per/adc.h +++ b/src/per/adc.h @@ -39,10 +39,24 @@ 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. @@ -50,21 +64,24 @@ struct AdcChannelConfig 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_; }; /**