diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2020-11-08 18:31:02 -0500 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2020-11-08 18:31:02 -0500 |
commit | e12639c46f0be29461803ffa1790d6f69c16d280 (patch) | |
tree | 5195f6ce497d702cef57bd752a9ec5c054855a87 /source | |
parent | f3e4d176d5922a13ce2615895cea1e197175984a (diff) |
fixed sample rate setting
Diffstat (limited to 'source')
-rw-r--r-- | source/adc.cpp | 61 | ||||
-rw-r--r-- | source/adc.hpp | 25 | ||||
-rw-r--r-- | source/dac.cpp | 2 | ||||
-rw-r--r-- | source/main.cpp | 19 |
4 files changed, 52 insertions, 55 deletions
diff --git a/source/adc.cpp b/source/adc.cpp index a8c0e62..c8da25f 100644 --- a/source/adc.cpp +++ b/source/adc.cpp @@ -38,36 +38,23 @@ static ADCConversionGroup adc_group_config = { }; constexpr static const GPTConfig gpt_config = { - .frequency = 14400000, + .frequency = 36000000, .callback = nullptr, .cr2 = TIM_CR2_MMS_1, /* TRGO */ .dier = 0 }; +#define ADC_CCR_PRESC_DIV1 (0) +#define ADC_SAMPLE_RATE_SETTINGS_COUNT (7) static uint32_t adc_sample_rate_settings[] = { - 10, ADC_SMPR_SMP_47P5, 4608, // 3125 - 11, ADC_SMPR_SMP_12P5, 3840, // 3750 - 9, ADC_SMPR_SMP_47P5, 2304, // 6250 - 10, ADC_SMPR_SMP_12P5, 1920, // 7500 - 8, ADC_SMPR_SMP_47P5, 1152, // 12K5 - 9, ADC_SMPR_SMP_12P5, 960, // 15K - 7, ADC_SMPR_SMP_47P5, 576, // 25K - 8, ADC_SMPR_SMP_12P5, 480, // 30K - 5, ADC_SMPR_SMP_47P5, 360, // 40K - 4, ADC_SMPR_SMP_47P5, 288, // 50K - 7, ADC_SMPR_SMP_12P5, 240, // 60K - 6, ADC_SMPR_SMP_12P5, 180, // 80K - 5, ADC_SMPR_SMP_12P5, 150, // 96K - 2, ADC_SMPR_SMP_47P5, 144, // 100K - 4, ADC_SMPR_SMP_12P5, 120, // 120K - 3, ADC_SMPR_SMP_12P5, 90, // 160K - 1, ADC_SMPR_SMP_47P5, 72, // 200K - 2, ADC_SMPR_SMP_12P5, 60, // 240K - 0, ADC_SMPR_SMP_47P5, 36, // 400K - 1, ADC_SMPR_SMP_12P5, 30, // 480K - 1, ADC_SMPR_SMP_2P5, 18, // 800K - 0, ADC_SMPR_SMP_12P5, 15, // 960K - 0, ADC_SMPR_SMP_2P5, 9 // 1M6 + // Rate PLLSAI2N ADC_PRESC ADC_SMPR GPT_DIV + /* 16k */ 8, ADC_CCR_PRESC_DIV10, ADC_SMPR_SMP_12P5, 2250, + /* 48k */ 24, ADC_CCR_PRESC_DIV10, ADC_SMPR_SMP_12P5, 750, + /* 96k */ 48, ADC_CCR_PRESC_DIV10, ADC_SMPR_SMP_12P5, 375, + /* 100k */ 40, ADC_CCR_PRESC_DIV8, ADC_SMPR_SMP_12P5, 360, + /* 400k */ 40, ADC_CCR_PRESC_DIV2, ADC_SMPR_SMP_12P5, 90, + /* 1M */ 38, ADC_CCR_PRESC_DIV1, ADC_SMPR_SMP_6P5, 36, + /* 2M */ 76, ADC_CCR_PRESC_DIV1, ADC_SMPR_SMP_6P5, 18 }; static bool adc_is_read_finished = false; @@ -92,17 +79,35 @@ namespace adc void set_rate(rate new_rate) { auto index = static_cast<unsigned int>(new_rate); - auto presc = adc_sample_rate_settings[index * 3] << 18; - auto smp = adc_sample_rate_settings[index * 3 + 1]; - adc_gpt_divisor = adc_sample_rate_settings[index * 3 + 2]; + auto plln = adc_sample_rate_settings[index * 4] << RCC_PLLSAI2CFGR_PLLSAI2N_Pos; + auto presc = adc_sample_rate_settings[index * 4 + 1] << ADC_CCR_PRESC_Pos; + auto smp = adc_sample_rate_settings[index * 4 + 2]; + adc_gpt_divisor = adc_sample_rate_settings[index * 4 + 3]; adcStop(adcd); + + // Adjust PLLSAI2 + RCC->CR &= ~(RCC_CR_PLLSAI2ON); + while ((RCC->CR & RCC_CR_PLLSAI2RDY) == RCC_CR_PLLSAI2RDY); + RCC->PLLSAI2CFGR = (RCC->PLLSAI2CFGR & ~(RCC_PLLSAI2CFGR_PLLSAI2N_Msk)) | plln; + RCC->CR |= RCC_CR_PLLSAI2ON; + // Set ADC prescaler - adcd->adcc->CCR = (adcd->adcc->CCR & ~(0xF << 18)) | presc; + adcd->adcc->CCR = (adcd->adcc->CCR & ~(ADC_CCR_PRESC_Msk)) | presc; // Set sampling time adc_group_config.smpr[0] = ADC_SMPR1_SMP_AN5(smp); adcStart(adcd, &adc_config); } + + unsigned int get_rate() + { + for (unsigned int i = 0; i < ADC_SAMPLE_RATE_SETTINGS_COUNT; i++) { + if (adc_gpt_divisor == adc_sample_rate_settings[i * 3 + 3]) + return i; + } + + return 0xFF; + } unsigned int get_gpt_divisor() { diff --git a/source/adc.hpp b/source/adc.hpp index 5f41117..1431aa1 100644 --- a/source/adc.hpp +++ b/source/adc.hpp @@ -19,33 +19,18 @@ namespace adc using operation_t = void (*)(adcsample_t *buffer, size_t count); enum class rate : unsigned int { - R3125 = 0, - R3750, - R6250, - R7500, - R12K5, - R15K, - R25K, - R30K, - R40K, - R50K, - R60K, - R80K, + R16K = 0, + R48K, R96K, R100K, - R120K, - R160K, - R200K, - R240K, R400K, - R480K, - R800K, - R960K, - R1M6 + R1M, + R2M }; void init(); void set_rate(rate new_rate); + unsigned int get_rate(); unsigned int get_gpt_divisor(); adcsample_t *read(adcsample_t *buffer, size_t count); void read_start(operation_t operation_func, adcsample_t *buffer, size_t count); diff --git a/source/dac.cpp b/source/dac.cpp index 1a7254a..ed4dce1 100644 --- a/source/dac.cpp +++ b/source/dac.cpp @@ -30,7 +30,7 @@ constexpr static const DACConversionGroup dac_group_config = { }; constexpr static const GPTConfig gpt_config = { - .frequency = 14400000, + .frequency = 36000000, .callback = nullptr, .cr2 = TIM_CR2_MMS_1, /* TRGO */ .dier = 0 diff --git a/source/main.cpp b/source/main.cpp index 17b0adb..8948b8e 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -250,10 +250,16 @@ void main_loop() break; case 'r': - if (usbserial::read(&cmd[1], 1) == 1) - adc::set_rate(static_cast<adc::rate>(cmd[1])); - else + if (usbserial::read(&cmd[1], 1) == 1) { + if (cmd[1] == 0xFF) { + unsigned char r = static_cast<unsigned char>(adc::get_rate()); + usbserial::write(&r, 1); + } else { + adc::set_rate(static_cast<adc::rate>(cmd[1])); + } + } else { error_queue_add(Error::BadParamSize); + } break; // 'S' - Stops the continuous sampling/conversion. @@ -267,6 +273,10 @@ void main_loop() case 's': if (dac_samples_new != nullptr) { + auto samps = reinterpret_cast<const uint8_t *>( + const_cast<const dacsample_t *>(dac_samples_new)); + dac_samples_new = nullptr; + unsigned char buf[2] = { static_cast<unsigned char>(dac_sample_count / 2 & 0xFF), static_cast<unsigned char>(((dac_sample_count / 2) >> 8) & 0xFF) @@ -275,8 +285,6 @@ void main_loop() unsigned int total = dac_sample_count / 2 * sizeof(dacsample_t); unsigned int offset = 0; unsigned char unused; - auto samps = reinterpret_cast<const uint8_t *>( - const_cast<const dacsample_t *>(dac_samples_new)); while (total > 512) { usbserial::write(samps + offset, 512); while (usbserial::read(&unused, 1) == 0); @@ -285,7 +293,6 @@ void main_loop() } usbserial::write(samps + offset, total); while (usbserial::read(&unused, 1) == 0); - dac_samples_new = nullptr; } else { usbserial::write("\0\0", 2); } |