-
Notifications
You must be signed in to change notification settings - Fork 4
Add ADC Dual Mode support with new AdvancedADCDual class #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7fb56df
44f57bd
7550c29
73b2ac6
888232d
1d11f4f
998716a
4f467e2
e3d00aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -31,6 +31,8 @@ class AdvancedADC { | |||||||||
size_t n_channels; | ||||||||||
adc_descr_t *descr; | ||||||||||
PinName adc_pins[AN_MAX_ADC_CHANNELS]; | ||||||||||
bool dualMode; | ||||||||||
uint8_t selected_adc; | ||||||||||
|
||||||||||
public: | ||||||||||
template <typename ... T> | ||||||||||
|
@@ -41,21 +43,75 @@ class AdvancedADC { | |||||||||
for (auto p : {p0, args...}) { | ||||||||||
adc_pins[n_channels++] = analogPinToPinName(p); | ||||||||||
} | ||||||||||
dualMode=false; | ||||||||||
selected_adc=0; | ||||||||||
} | ||||||||||
AdvancedADC(): n_channels(0), descr(nullptr) {} | ||||||||||
~AdvancedADC(); | ||||||||||
bool available(); | ||||||||||
SampleBuffer read(); | ||||||||||
int begin(uint32_t resolution, uint32_t sample_rate, size_t n_samples, size_t n_buffers); | ||||||||||
int begin(uint32_t resolution, uint32_t sample_rate, size_t n_samples, size_t n_buffers, size_t n_pins, pin_size_t *pins) { | ||||||||||
int getAssignedADC(); | ||||||||||
int begin(uint32_t resolution, uint32_t sample_rate, size_t n_samples, size_t n_buffers, bool do_start=true); | ||||||||||
int begin(uint32_t resolution, uint32_t sample_rate, size_t n_samples, size_t n_buffers, size_t n_pins, pin_size_t *pins, bool start=true) { | ||||||||||
if (n_pins > AN_MAX_ADC_CHANNELS) n_pins = AN_MAX_ADC_CHANNELS; | ||||||||||
for (size_t i = 0; i < n_pins; ++i) { | ||||||||||
adc_pins[i] = analogPinToPinName(pins[i]); | ||||||||||
} | ||||||||||
|
||||||||||
n_channels = n_pins; | ||||||||||
return begin(resolution, sample_rate, n_samples, n_buffers); | ||||||||||
return begin(resolution, sample_rate, n_samples, n_buffers,start); | ||||||||||
} | ||||||||||
|
||||||||||
void clear(); | ||||||||||
|
||||||||||
int start(uint32_t sample_rate); | ||||||||||
|
||||||||||
//void setADCDualMode(bool dm); | ||||||||||
//int enableDualMode(); | ||||||||||
//int disableDualMode(); | ||||||||||
|
||||||||||
int stop(); | ||||||||||
}; | ||||||||||
|
||||||||||
class AdvancedADCDual { | ||||||||||
private: | ||||||||||
size_t n_channels; | ||||||||||
pin_size_t adc_pins_unmapped[AN_MAX_ADC_CHANNELS]; | ||||||||||
AdvancedADC *adcIN1; | ||||||||||
AdvancedADC *adcIN2; | ||||||||||
Comment on lines
+80
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Those should be passed by reference. |
||||||||||
public: | ||||||||||
|
||||||||||
//For now ADC1 will always be one pin, and ADC2 will sample the rest | ||||||||||
template <typename ... T> | ||||||||||
AdvancedADCDual(pin_size_t p0, T ... args):n_channels(0), adcIN1(nullptr), adcIN2(nullptr){ | ||||||||||
static_assert(sizeof ...(args) < AN_MAX_ADC_CHANNELS+1, | ||||||||||
"A maximum of 5 channels can be sampled successively."); | ||||||||||
static_assert (sizeof ...(args) >=2, | ||||||||||
"At least two channels are required for Dual Mode ADC."); | ||||||||||
for (auto p : {p0, args...}) { | ||||||||||
adc_pins_unmapped[n_channels++] = p; | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
AdvancedADCDual(): n_channels(0), adcIN1(nullptr), adcIN2(nullptr) | ||||||||||
{ | ||||||||||
} | ||||||||||
~AdvancedADCDual(); | ||||||||||
|
||||||||||
int begin(AdvancedADC *in1, AdvancedADC *in2,uint32_t resolution, uint32_t sample_rate, size_t n_samples, size_t n_buffers); | ||||||||||
int begin(AdvancedADC *in1, AdvancedADC *in2,uint32_t resolution, uint32_t sample_rate, | ||||||||||
size_t n_samples, size_t n_buffers, size_t n_pins, pin_size_t *pins) { | ||||||||||
if (n_pins > AN_MAX_ADC_CHANNELS) n_pins = AN_MAX_ADC_CHANNELS; | ||||||||||
if(n_pins<2) //Cannot run Dual mode with less than two input pins | ||||||||||
return(0); | ||||||||||
for (size_t i = 0; i < n_pins; ++i) { | ||||||||||
adc_pins_unmapped[i] = pins[i]; | ||||||||||
Serial.println("Pin: "+String(pins[i])); | ||||||||||
} | ||||||||||
n_channels = n_pins; | ||||||||||
return begin(in1, in2, resolution, sample_rate, n_samples, n_buffers); | ||||||||||
} | ||||||||||
int stop(); | ||||||||||
}; | ||||||||||
|
||||||||||
#endif /* ARDUINO_ADVANCED_ADC_H_ */ |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -46,18 +46,18 @@ int hal_tim_config(TIM_HandleTypeDef *tim, uint32_t t_freq) { | |||||
sConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE; | ||||||
|
||||||
if (tim->Instance == TIM1) { | ||||||
__HAL_RCC_TIM1_CLK_ENABLE(); | ||||||
__HAL_RCC_TIM1_CLK_ENABLE(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please remove any spaces you added. |
||||||
} else if (tim->Instance == TIM2) { | ||||||
__HAL_RCC_TIM2_CLK_ENABLE(); | ||||||
} else if (tim->Instance == TIM3) { | ||||||
} else if (tim->Instance == TIM3) { | ||||||
__HAL_RCC_TIM3_CLK_ENABLE(); | ||||||
} else if (tim->Instance == TIM4) { | ||||||
} else if (tim->Instance == TIM4) { | ||||||
__HAL_RCC_TIM4_CLK_ENABLE(); | ||||||
} else if (tim->Instance == TIM5) { | ||||||
} else if (tim->Instance == TIM5) { | ||||||
__HAL_RCC_TIM5_CLK_ENABLE(); | ||||||
} else if (tim->Instance == TIM6) { | ||||||
} else if (tim->Instance == TIM6) { | ||||||
__HAL_RCC_TIM6_CLK_ENABLE(); | ||||||
} | ||||||
} | ||||||
|
||||||
// Init and config the timer. | ||||||
__HAL_TIM_CLEAR_FLAG(tim, TIM_FLAG_UPDATE); | ||||||
|
@@ -167,10 +167,20 @@ static uint32_t ADC_RANK_LUT[] = { | |||||
ADC_REGULAR_RANK_1, ADC_REGULAR_RANK_2, ADC_REGULAR_RANK_3, ADC_REGULAR_RANK_4, ADC_REGULAR_RANK_5 | ||||||
}; | ||||||
|
||||||
int hal_enable_dual_mode() { | ||||||
LL_ADC_SetMultimode(__LL_ADC_COMMON_INSTANCE(ADC1), LL_ADC_MULTI_DUAL_REG_SIMULT); | ||||||
return(1); | ||||||
} | ||||||
|
||||||
int hal_disable_dual_mode() { | ||||||
LL_ADC_SetMultimode(__LL_ADC_COMMON_INSTANCE(ADC1), LL_ADC_MULTI_INDEPENDENT); | ||||||
return(1); | ||||||
} | ||||||
|
||||||
int hal_adc_config(ADC_HandleTypeDef *adc, uint32_t resolution, uint32_t trigger, PinName *adc_pins, uint32_t n_channels) { | ||||||
// Set ADC clock source. | ||||||
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_CLKP); | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please remove extra spaces that you added. |
||||||
// Enable ADC clock | ||||||
if (adc->Instance == ADC1) { | ||||||
__HAL_RCC_ADC12_CLK_ENABLE(); | ||||||
|
@@ -206,7 +216,7 @@ int hal_adc_config(ADC_HandleTypeDef *adc, uint32_t resolution, uint32_t trigger | |||||
sConfig.OffsetNumber = ADC_OFFSET_NONE; | ||||||
sConfig.SingleDiff = ADC_SINGLE_ENDED; | ||||||
sConfig.SamplingTime = ADC_SAMPLETIME_8CYCLES_5; | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
for (size_t rank=0; rank<n_channels; rank++) { | ||||||
uint32_t function = pinmap_function(adc_pins[rank], PinMap_ADC); | ||||||
uint32_t channel = STM_PIN_CHANNEL(function); | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.