diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2020-10-22 09:18:18 -0400 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2020-10-22 09:18:18 -0400 |
commit | 2c9892cc3e68f00e71cf3d3b066a0c7080e1e220 (patch) | |
tree | f161e25567c9622ad2a6129d317faf0169b333ea /source | |
parent | 4b523f98c86f7f953b5c19fe414811c27857d78a (diff) |
set sampling rate
Diffstat (limited to 'source')
-rw-r--r-- | source/adc.cpp | 75 | ||||
-rw-r--r-- | source/adc.hpp | 35 | ||||
-rw-r--r-- | source/main.cpp | 13 |
3 files changed, 72 insertions, 51 deletions
diff --git a/source/adc.cpp b/source/adc.cpp index 9fd3285..0318a6b 100644 --- a/source/adc.cpp +++ b/source/adc.cpp @@ -44,6 +44,32 @@ constexpr static const GPTConfig gpt_config = { .dier = 0 }; +static uint32_t adc_sample_rate_settings[] = { + 10, ADC_SMPR_SMP_47P5, // 3125 + 11, ADC_SMPR_SMP_12P5, // 3750 + 9, ADC_SMPR_SMP_47P5, // 6250 + 10, ADC_SMPR_SMP_12P5, // 7500 + 8, ADC_SMPR_SMP_47P5, // 12K5 + 9, ADC_SMPR_SMP_12P5, // 15K + 7, ADC_SMPR_SMP_47P5, // 25K + 8, ADC_SMPR_SMP_12P5, // 30K + 5, ADC_SMPR_SMP_47P5, // 40K + 4, ADC_SMPR_SMP_47P5, // 50K + 7, ADC_SMPR_SMP_12P5, // 60K + 6, ADC_SMPR_SMP_12P5, // 80K + 5, ADC_SMPR_SMP_12P5, // 96K + 2, ADC_SMPR_SMP_47P5, // 100K + 4, ADC_SMPR_SMP_12P5, // 120K + 3, ADC_SMPR_SMP_12P5, // 160K + 1, ADC_SMPR_SMP_47P5, // 200K + 2, ADC_SMPR_SMP_12P5, // 240K + 0, ADC_SMPR_SMP_47P5, // 400K + 1, ADC_SMPR_SMP_12P5, // 480K + 1, ADC_SMPR_SMP_2P5, // 800K + 0, ADC_SMPR_SMP_12P5, // 960K + 0, ADC_SMPR_SMP_2P5 // 1M6 +}; + static bool adc_is_read_finished = false; static adcsample_t *adc_current_buffer = nullptr; static size_t adc_current_buffer_size = 0; @@ -59,6 +85,19 @@ namespace adc adcStart(adcd, &adc_config); adcSTM32EnableVREF(adcd); } + + void set_rate(rate new_rate) + { + auto presc = adc_sample_rate_settings[static_cast<unsigned int>(new_rate) * 2] << 18; + auto smp = adc_sample_rate_settings[static_cast<unsigned int>(new_rate) * 2 + 1]; + + adcStop(adcd); + // Set ADC prescaler + adcd->adcc->CCR = (adcd->adcc->CCR & ~(0xF << 18)) | presc; + // Set sampling time + adc_group_config.smpr[0] = ADC_SMPR1_SMP_AN5(smp); + adcStart(adcd, &adc_config); + } adcsample_t *read(adcsample_t *buffer, size_t count) { @@ -94,42 +133,8 @@ namespace adc adc_current_buffer_size = 0; adc_operation_func = nullptr; } - - void set_rate(rate r) - { - uint32_t val = 0; - - switch (r) { - case rate::R2P5: - val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_2P5); - break; - case rate::R6P5: - val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_6P5); - break; - case rate::R12P5: - val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_12P5); - break; - case rate::R24P5: - val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_24P5); - break; - case rate::R47P5: - val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_47P5); - break; - case rate::R92P5: - val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_92P5); - break; - case rate::R247P5: - val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_247P5); - break; - case rate::R640P5: - val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_640P5); - break; - } - - adc_group_config.smpr[0] = val; - } } - + void adc_read_callback(ADCDriver *driver) { if (adc_group_config.circular) { diff --git a/source/adc.hpp b/source/adc.hpp index d1cd292..d31d60a 100644 --- a/source/adc.hpp +++ b/source/adc.hpp @@ -18,23 +18,38 @@ namespace adc { using operation_t = void (*)(adcsample_t *buffer, size_t count); - enum class rate { - R2P5, - R6P5, - R12P5, - R24P5, - R47P5, - R92P5, - R247P5, - R640P5 + enum class rate : unsigned int { + R3125 = 0, + R3750, + R6250, + R7500, + R12K5, + R15K, + R25K, + R30K, + R40K, + R50K, + R60K, + R80K, + R96K, + R100K, + R120K, + R160K, + R200K, + R240K, + R400K, + R480K, + R800K, + R960K, + R1M6 }; void init(); + void set_rate(rate new_rate); 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); } #endif // STMDSP_ADC_HPP_ diff --git a/source/main.cpp b/source/main.cpp index 7a55b6c..67a39a3 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -253,14 +253,15 @@ void main_loop() } break; - //case 'r': - // if (usbserial::read(&cmd[1], 1) == 1) { + case 'r': + if (usbserial::read(&cmd[1], 1) == 1) { + adc::set_rate(static_cast<adc::rate>(cmd[1])); // adc_preloaded = cmd[1] & (1 << 0); // dac_preloaded = cmd[1] & (1 << 1); - // } else { - // error_queue_add(Error::BadParamSize); - // } - // break; + } else { + error_queue_add(Error::BadParamSize); + } + break; // 'S' - Stops the continuous sampling/conversion. case 'S': |