Skip to content

AdvancedADC: Increase the maximum number of channels. #61

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

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/AdvancedADC.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,23 @@ class AdvancedADC {
template <typename ... T>
AdvancedADC(pin_size_t p0, T ... args): n_channels(0), descr(nullptr) {
static_assert(sizeof ...(args) < AN_MAX_ADC_CHANNELS,
"A maximum of 5 channels can be sampled successively.");
"A maximum of 16 channels can be sampled successively.");

for (auto p : {p0, args...}) {
adc_pins[n_channels++] = analogPinToPinName(p);
}
}
AdvancedADC(): n_channels(0), descr(nullptr) {}
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) {
if (n_pins > AN_MAX_ADC_CHANNELS) n_pins = AN_MAX_ADC_CHANNELS;
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) {
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]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/AdvancedAnalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ enum {
typedef uint16_t Sample; // Sample type used for ADC/DAC.
typedef DMABuffer<Sample> &SampleBuffer;

#define AN_MAX_ADC_CHANNELS (5)
#define AN_MAX_ADC_CHANNELS (16)
#define AN_MAX_DAC_CHANNELS (1)
#define AN_ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))

Expand Down
7 changes: 5 additions & 2 deletions src/HALConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ int hal_dac_config(DAC_HandleTypeDef *dac, uint32_t channel, uint32_t trigger) {
}

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
ADC_REGULAR_RANK_1, ADC_REGULAR_RANK_2, ADC_REGULAR_RANK_3, ADC_REGULAR_RANK_4,
ADC_REGULAR_RANK_5, ADC_REGULAR_RANK_6, ADC_REGULAR_RANK_7, ADC_REGULAR_RANK_8,
ADC_REGULAR_RANK_9, ADC_REGULAR_RANK_10, ADC_REGULAR_RANK_11, ADC_REGULAR_RANK_12,
ADC_REGULAR_RANK_13, ADC_REGULAR_RANK_14, ADC_REGULAR_RANK_15, ADC_REGULAR_RANK_16
};

int hal_adc_config(ADC_HandleTypeDef *adc, uint32_t resolution, uint32_t trigger, PinName *adc_pins, uint32_t n_channels) {
Expand Down Expand Up @@ -196,7 +199,7 @@ int hal_adc_config(ADC_HandleTypeDef *adc, uint32_t resolution, uint32_t trigger
adc->Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
adc->Init.ConversionDataManagement = ADC_CONVERSIONDATA_DMA_CIRCULAR;

if (HAL_ADC_Init(adc) != HAL_OK
if (HAL_ADC_Init(adc) != HAL_OK
|| HAL_ADCEx_Calibration_Start(adc, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED) != HAL_OK) {
return -1;
}
Expand Down
Loading