added adc stream framework

pull/3/head
Clyne 4 years ago
parent 7f59ca704b
commit 2ceb20f4d4

@ -45,6 +45,10 @@ constexpr static const GPTConfig gpt_config = {
}; };
static bool adc_is_read_finished = false; 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() void adc_init()
{ {
@ -58,16 +62,46 @@ void adc_init()
adcsample_t *adc_read(adcsample_t *buffer, size_t count) adcsample_t *adc_read(adcsample_t *buffer, size_t count)
{ {
adc_is_read_finished = false; adc_is_read_finished = false;
adc_is_read_continuous = false;
adc_group_config.circular = false;
adcStartConversion(adcd, &adc_group_config, buffer, count); adcStartConversion(adcd, &adc_group_config, buffer, count);
gptStartContinuous(gptd, 100); // 10kHz gptStartContinuous(gptd, 100); // 10kHz
while (!adc_is_read_finished); while (!adc_is_read_finished);
return buffer; 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); 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) void adc_set_rate(ADCRate rate)

@ -25,8 +25,12 @@ enum class ADCRate {
R640P5 R640P5
}; };
using adc_operation_t = void (*)(adcsample_t *buffer, size_t count);
void adc_init(); void adc_init();
adcsample_t *adc_read(adcsample_t *buffer, size_t count); 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); void adc_set_rate(ADCRate rate);
#endif // STMDSP_ADC_HPP_ #endif // STMDSP_ADC_HPP_

@ -26,6 +26,8 @@ CC_ALIGN(CACHE_LINE_SIZE)
static std::array<adcsample_t, CACHE_SIZE_ALIGN(adcsample_t, 2048)> adc_samples; static std::array<adcsample_t, CACHE_SIZE_ALIGN(adcsample_t, 2048)> adc_samples;
static std::array<dacsample_t, CACHE_SIZE_ALIGN(dacsample_t, 2048)> dac_samples; static std::array<dacsample_t, CACHE_SIZE_ALIGN(dacsample_t, 2048)> dac_samples;
static void signal_operate(adcsample_t *buffer, size_t count);
int main() int main()
{ {
halInit(); halInit();
@ -51,6 +53,12 @@ int main()
usbserial_write(adc_samples.data(), count * sizeof(adcsample_t)); usbserial_write(adc_samples.data(), count * sizeof(adcsample_t));
} }
break; 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': case 'W':
if (usbserial_read(&cmd[1], 2) < 2) if (usbserial_read(&cmd[1], 2) < 2)
break; break;
@ -77,3 +85,8 @@ int main()
} }
} }
void signal_operate([[maybe_unused]] adcsample_t *buffer, [[maybe_unused]] size_t count)
{
}

Loading…
Cancel
Save