From 2ceb20f4d48a8f163528f86fda484e977438bc80 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 20 Aug 2020 17:00:55 -0400 Subject: [PATCH] added adc stream framework --- source/adc.cpp | 38 ++++++++++++++++++++++++++++++++++++-- source/adc.hpp | 4 ++++ source/main.cpp | 13 +++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/source/adc.cpp b/source/adc.cpp index 851b461..e754e0e 100644 --- a/source/adc.cpp +++ b/source/adc.cpp @@ -45,6 +45,10 @@ constexpr static const GPTConfig gpt_config = { }; static bool adc_is_read_finished = false; +static bool adc_is_read_continuous = false; +static adcsample_t *adc_current_buffer = nullptr; +static size_t adc_current_buffer_size = 0; +static adc_operation_t adc_operation_func = nullptr; void adc_init() { @@ -58,16 +62,46 @@ void adc_init() adcsample_t *adc_read(adcsample_t *buffer, size_t count) { adc_is_read_finished = false; + adc_is_read_continuous = false; + adc_group_config.circular = false; adcStartConversion(adcd, &adc_group_config, buffer, count); gptStartContinuous(gptd, 100); // 10kHz while (!adc_is_read_finished); return buffer; } -void adc_read_callback([[maybe_unused]] ADCDriver *driver) +void adc_read_start(adc_operation_t operation_func, adcsample_t *buffer, size_t count) { + adc_is_read_continuous = true; + adc_current_buffer = buffer; + adc_current_buffer_size = count; + adc_operation_func = operation_func; + adc_group_config.circular = true; + adcStartConversion(adcd, &adc_group_config, buffer, count); + gptStartContinuous(gptd, 100); // 10kHz +} + +void adc_read_stop() +{ + adc_is_read_continuous = false; gptStopTimer(gptd); - adc_is_read_finished = true; +} + +void adc_read_callback(ADCDriver *driver) +{ + if (!adc_is_read_continuous) { + gptStopTimer(gptd); + adc_is_read_finished = true; + } else if (adc_operation_func != nullptr) { + auto half_size = adc_current_buffer_size / 2; + if (driver->state == ADC_ACTIVE) { + // Half full + adc_operation_func(adc_current_buffer, half_size); + } else if (driver->state == ADC_COMPLETE) { + // Second half full + adc_operation_func(adc_current_buffer + half_size, half_size); + } + } } void adc_set_rate(ADCRate rate) diff --git a/source/adc.hpp b/source/adc.hpp index 4c3836e..15e343a 100644 --- a/source/adc.hpp +++ b/source/adc.hpp @@ -25,8 +25,12 @@ enum class ADCRate { R640P5 }; +using adc_operation_t = void (*)(adcsample_t *buffer, size_t count); + void adc_init(); adcsample_t *adc_read(adcsample_t *buffer, size_t count); +void adc_read_start(adc_operation_t operation_func, adcsample_t *buffer, size_t count); +void adc_read_stop(); void adc_set_rate(ADCRate rate); #endif // STMDSP_ADC_HPP_ diff --git a/source/main.cpp b/source/main.cpp index cfd20a9..07c73ba 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -26,6 +26,8 @@ CC_ALIGN(CACHE_LINE_SIZE) static std::array adc_samples; static std::array dac_samples; +static void signal_operate(adcsample_t *buffer, size_t count); + int main() { halInit(); @@ -51,6 +53,12 @@ int main() usbserial_write(adc_samples.data(), count * sizeof(adcsample_t)); } break; + case 'R': + adc_read_start(signal_operate, &adc_samples[0], adc_samples.size() * sizeof(adcsample_t)); + break; + case 'S': + adc_read_stop(); + break; case 'W': if (usbserial_read(&cmd[1], 2) < 2) break; @@ -77,3 +85,8 @@ int main() } } +void signal_operate([[maybe_unused]] adcsample_t *buffer, [[maybe_unused]] size_t count) +{ + +} +