Skip to content

Support synchronized ADC capture #55

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7a77b31
Declared new member functions ready(), start(), and clear()
jmdodd95682 Feb 6, 2024
a76e0e4
Added member functions ready(), start() and clear()
jmdodd95682 Feb 6, 2024
d30bcea
Changed implementation to re-use begin() for backward compatibility
jmdodd95682 Feb 7, 2024
6e9040b
Changed implementation to reuse begin() with option noStart
jmdodd95682 Feb 7, 2024
563bf02
Fixed compile bug with defaulted parameters
jmdodd95682 Feb 7, 2024
13e9f0e
Fixed compile bug with defaulted parameters
jmdodd95682 Feb 7, 2024
bf6113c
Update src/AdvancedADC.cpp
leonardocavagnis Feb 8, 2024
a3bc455
Update src/AdvancedADC.cpp
leonardocavagnis Feb 8, 2024
0a854f1
Update src/AdvancedADC.cpp
leonardocavagnis Feb 8, 2024
1341867
Update src/AdvancedADC.cpp
leonardocavagnis Feb 8, 2024
8cb517d
Update src/AdvancedADC.h
leonardocavagnis Feb 8, 2024
db817ed
Update src/AdvancedADC.h
leonardocavagnis Feb 8, 2024
0d2b69c
Update src/AdvancedADC.cpp
leonardocavagnis Feb 8, 2024
d17986d
Update src/AdvancedADC.h
leonardocavagnis Feb 8, 2024
4cbc4c4
Update src/AdvancedADC.cpp
leonardocavagnis Feb 8, 2024
9e461a0
Update src/AdvancedADC.cpp
leonardocavagnis Feb 8, 2024
06a9cdb
Update src/AdvancedADC.cpp
leonardocavagnis Feb 8, 2024
2c23027
Update src/AdvancedADC.cpp
leonardocavagnis Feb 8, 2024
79e7ffc
Update src/AdvancedADC.cpp
leonardocavagnis Feb 8, 2024
1c457e9
Update src/AdvancedADC.h
leonardocavagnis Feb 8, 2024
f27400e
Update src/AdvancedADC.h
leonardocavagnis Feb 8, 2024
e988ee5
Update src/AdvancedADC.h
leonardocavagnis Feb 8, 2024
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
26 changes: 21 additions & 5 deletions src/AdvancedADC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,17 @@ DMABuffer<Sample> &AdvancedADC::read() {
return NULLBUF;
}

int AdvancedADC::begin(uint32_t resolution, uint32_t sample_rate, size_t n_samples, size_t n_buffers) {
int AdvancedADC::begin(uint32_t resolution, uint32_t sample_rate, size_t n_samples, size_t n_buffers, bool start_sampling) {
ADCName instance = ADC_NP;

// Sanity checks.
if (resolution >= AN_ARRAY_SIZE(ADC_RES_LUT) || (descr && descr->pool)) {
return 0;
}

// Clear ALTx pin.
for (size_t i=0; i<n_channels; i++) {
adc_pins[i] = (PinName) (adc_pins[i] & ~(ADC_PIN_ALT_MASK));
}

// Find an ADC that can be used with these set of pins/channels.
for (size_t i=0; instance == ADC_NP && i<AN_ARRAY_SIZE(adc_pin_alt); i++) {
// Calculate alternate function pin.
Expand Down Expand Up @@ -188,9 +186,11 @@ int AdvancedADC::begin(uint32_t resolution, uint32_t sample_rate, size_t n_sampl
if (descr->pool == nullptr) {
return 0;
}

descr->dmabuf[0] = descr->pool->allocate();
descr->dmabuf[1] = descr->pool->allocate();


// Init and config DMA.
if (hal_dma_config(&descr->dma, descr->dma_irqn, DMA_PERIPH_TO_MEMORY) < 0) {
return 0;
Expand All @@ -201,6 +201,16 @@ int AdvancedADC::begin(uint32_t resolution, uint32_t sample_rate, size_t n_sampl
return 0;
}

sampling_rate = sample_rate;
// Start sampling immediately unless start_sampling==false.
if (start_sampling) {
return start();
}

return 1;
}

int AdvancedADC::start() {
// Link DMA handle to ADC handle, and start the ADC.
__HAL_LINKDMA(&descr->adc, DMA_Handle, descr->dma);
if (HAL_ADC_Start_DMA(&descr->adc, (uint32_t *) descr->dmabuf[0]->data(), descr->dmabuf[0]->size()) != HAL_OK) {
Expand All @@ -217,7 +227,7 @@ int AdvancedADC::begin(uint32_t resolution, uint32_t sample_rate, size_t n_sampl
if (HAL_TIM_Base_Start(&descr->tim) != HAL_OK) {
return 0;
}

return 1;
}

Expand All @@ -227,6 +237,12 @@ int AdvancedADC::stop()
return 1;
}

void AdvancedADC::clear() {
if (descr && descr->pool) {
descr->pool->flush();
}
}

AdvancedADC::~AdvancedADC()
{
dac_descr_deinit(descr, true);
Expand Down
8 changes: 5 additions & 3 deletions src/AdvancedADC.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ class AdvancedADC {
~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 begin(uint32_t resolution, uint32_t sample_rate, size_t n_samples, size_t n_buffers, bool start_sampling = 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_sampling = 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_sampling);
}
void clear();
int start();
int stop();
};

Expand Down