diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/adc.cpp | 5 | ||||
-rw-r--r-- | source/adc.hpp | 1 | ||||
-rw-r--r-- | source/main.cpp | 47 |
3 files changed, 51 insertions, 2 deletions
diff --git a/source/adc.cpp b/source/adc.cpp index eca0d16..c28c38f 100644 --- a/source/adc.cpp +++ b/source/adc.cpp @@ -79,6 +79,11 @@ namespace adc adcStartConversion(adcd, &adc_group_config, buffer, count); gptStartContinuous(gptd, 5); } + + void read_set_operation_func(operation_t operation_func) + { + adc_operation_func = operation_func; + } void read_stop() { diff --git a/source/adc.hpp b/source/adc.hpp index 376b156..d1cd292 100644 --- a/source/adc.hpp +++ b/source/adc.hpp @@ -32,6 +32,7 @@ namespace adc void init(); adcsample_t *read(adcsample_t *buffer, size_t count); void read_start(operation_t operation_func, adcsample_t *buffer, size_t count); + void read_set_operation_func(operation_t operation_func); void read_stop(); void set_rate(rate r); } diff --git a/source/main.cpp b/source/main.cpp index 043353d..c0f0651 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -29,8 +29,10 @@ enum class RunStatus : char }; static RunStatus run_status = RunStatus::Idle; -#define MSG_CONVFIRST (1) -#define MSG_CONVSECOND (2) +#define MSG_CONVFIRST (1) +#define MSG_CONVSECOND (2) +#define MSG_CONVFIRST_MEASURE (3) +#define MSG_CONVSECOND_MEASURE (4) static msg_t conversionMBBuffer[8]; static MAILBOX_DECL(conversionMB, conversionMBBuffer, 8); @@ -38,6 +40,8 @@ static MAILBOX_DECL(conversionMB, conversionMBBuffer, 8); static THD_WORKING_AREA(conversionThreadWA, 1024); static THD_FUNCTION(conversionThread, arg); +static time_measurement_t conversion_time_measurement; + static_assert(sizeof(adcsample_t) == sizeof(uint16_t)); static_assert(sizeof(dacsample_t) == sizeof(uint16_t)); @@ -54,6 +58,7 @@ static uint8_t elf_file_store[8192]; static elf::entry_t elf_entry = nullptr; static void signal_operate(adcsample_t *buffer, size_t count); +static void signal_operate_measure(adcsample_t *buffer, size_t count); static void main_loop(); int main() @@ -74,6 +79,7 @@ int main() usbserial::init(); // Start the conversion manager thread + chTMObjectInit(&conversion_time_measurement); chThdCreateStatic(conversionThreadWA, sizeof(conversionThreadWA), NORMALPRIO, conversionThread, nullptr); @@ -115,6 +121,21 @@ void main_loop() dac::write_start(&dac_samples[0], dac_samples.size()); break; + // 'M' - Begins continuous sampling, but measures the execution time of the first + // sample processing. This duration can be later read through 'm'. + case 'M': + run_status = RunStatus::Converting; + dac_samples.fill(0); + adc::read_start(signal_operate_measure, &adc_samples[0], adc_samples.size()); + dac::write_start(&dac_samples[0], dac_samples.size()); + break; + + // 'm' - Returns the last measured sample processing time, presumably in processor + // ticks. + case 'm': + usbserial::write(&conversion_time_measurement.last, sizeof(rtcnt_t)); + break; + // 's' - Sends the current contents of the DAC buffer back over USB. case 's': usbserial::write(dac_samples.data(), 1/*dac_samples.size()*/ * sizeof(dacsample_t)); @@ -216,6 +237,22 @@ THD_FUNCTION(conversionThread, arg) if (!samples) samples = &adc_samples[adc_samples.size() / 2]; std::copy(samples, samples + halfsize, &dac_samples[dac_samples.size() / 2]); + } else if (message == MSG_CONVFIRST_MEASURE) { + chTMStartMeasurementX(&conversion_time_measurement); + if (elf_entry) + samples = elf_entry(&adc_samples[adc_samples.size() / 2], halfsize); + chTMStopMeasurementX(&conversion_time_measurement); + if (!samples) + samples = &adc_samples[adc_samples.size() / 2]; + std::copy(samples, samples + halfsize, &dac_samples[dac_samples.size() / 2]); + } else if (message == MSG_CONVSECOND_MEASURE) { + chTMStartMeasurementX(&conversion_time_measurement); + if (elf_entry) + samples = elf_entry(&adc_samples[adc_samples.size() / 2], halfsize); + chTMStopMeasurementX(&conversion_time_measurement); + if (!samples) + samples = &adc_samples[adc_samples.size() / 2]; + std::copy(samples, samples + halfsize, &dac_samples[dac_samples.size() / 2]); } } } @@ -229,6 +266,12 @@ void signal_operate(adcsample_t *buffer, [[maybe_unused]] size_t count) chMBPostI(&conversionMB, buffer == &adc_samples[0] ? MSG_CONVFIRST : MSG_CONVSECOND); } +void signal_operate_measure(adcsample_t *buffer, [[maybe_unused]] size_t count) +{ + chMBPostI(&conversionMB, buffer == &adc_samples[0] ? MSG_CONVFIRST_MEASURE : MSG_CONVSECOND_MEASURE); + adc::read_set_operation_func(signal_operate); +} + extern "C" { __attribute__((naked)) |