aboutsummaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
Diffstat (limited to 'source')
-rw-r--r--source/adc.cpp34
-rw-r--r--source/adc.hpp47
-rw-r--r--source/main.cpp142
3 files changed, 155 insertions, 68 deletions
diff --git a/source/adc.cpp b/source/adc.cpp
index 0c58e21..4e68a6e 100644
--- a/source/adc.cpp
+++ b/source/adc.cpp
@@ -39,6 +39,40 @@ adcsample_t *ADCd::getSamples(adcsample_t *buffer, size_t count)
return buffer;
}
+void ADCd::setSampleRate(ADCdRate rate)
+{
+ uint32_t val = 0;
+
+ switch (rate) {
+ case ADCdRate::R2P5:
+ val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_2P5);
+ break;
+ case ADCdRate::R6P5:
+ val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_6P5);
+ break;
+ case ADCdRate::R12P5:
+ val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_12P5);
+ break;
+ case ADCdRate::R24P5:
+ val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_24P5);
+ break;
+ case ADCdRate::R47P5:
+ val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_47P5);
+ break;
+ case ADCdRate::R92P5:
+ val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_92P5);
+ break;
+ case ADCdRate::R247P5:
+ val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_247P5);
+ break;
+ case ADCdRate::R640P5:
+ val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_640P5);
+ break;
+ }
+
+ m_adc_group_config.smpr[0] = val;
+}
+
void ADCd::initPins()
{
palSetPadMode(GPIOA, 0, PAL_MODE_INPUT_ANALOG);
diff --git a/source/adc.hpp b/source/adc.hpp
index 456e697..9989883 100644
--- a/source/adc.hpp
+++ b/source/adc.hpp
@@ -21,9 +21,53 @@ struct ADCdConfig : public ADCConfig
ADCd *adcdinst;
};
+enum class ADCdRate : unsigned int {
+ R2P5,
+ R6P5,
+ R12P5,
+ R24P5,
+ R47P5,
+ R92P5,
+ R247P5,
+ R640P5
+};
+
class ADCd
{
public:
+ constexpr static const unsigned int CLOCK_RATE = 40000000;
+ constexpr static unsigned int SAMPLES_PER_SECOND(ADCdRate rate) {
+ unsigned int sps = 0;
+ switch (rate) {
+ case ADCdRate::R2P5:
+ sps = 15;
+ break;
+ case ADCdRate::R6P5:
+ sps = 19;
+ break;
+ case ADCdRate::R12P5:
+ sps = 25;
+ break;
+ case ADCdRate::R24P5:
+ sps = 37;
+ break;
+ case ADCdRate::R47P5:
+ sps = 60;
+ break;
+ case ADCdRate::R92P5:
+ sps = 105;
+ break;
+ case ADCdRate::R247P5:
+ sps = 260;
+ break;
+ case ADCdRate::R640P5:
+ sps = 653;
+ break;
+ }
+
+ return static_cast<unsigned int>(1.f / (sps / static_cast<float>(CLOCK_RATE)));
+ }
+
constexpr explicit ADCd(ADCDriver& adcd, GPTDriver& gptd) :
m_adcd(&adcd), m_gptd(&gptd), m_adc_config{},
m_adc_group_config(ADC_GROUP_CONFIG),
@@ -31,6 +75,7 @@ public:
void start();
adcsample_t *getSamples(adcsample_t *buffer, size_t count);
+ void setSampleRate(ADCdRate rate);
private:
static const GPTConfig m_gpt_config;
@@ -43,6 +88,8 @@ private:
bool m_is_adc_finished;
void initPins();
+ //void selectPins(bool a0, bool a1);
+
static void adcEndCallback(ADCDriver *adcd);
constexpr static const ADCConversionGroup ADC_GROUP_CONFIG = {
diff --git a/source/main.cpp b/source/main.cpp
index b8d2fa1..95211b4 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -1,68 +1,74 @@
-/**
- * @file main.cpp
- * @brief Program entry point.
- *
- * Copyright (C) 2020 Clyne Sullivan
- *
- * Distributed under the GNU GPL v3 or later. You should have received a copy of
- * the GNU General Public License along with this program.
- * If not, see <https://www.gnu.org/licenses/>.
- */
-
-#include "ch.h"
-#include "hal.h"
-
-#include "adc.hpp"
-#include "dac.hpp"
-#include "usbserial.hpp"
-
-#include <array>
-
-#if CACHE_LINE_SIZE > 0
-CC_ALIGN(CACHE_LINE_SIZE)
-#endif
-static std::array<adcsample_t, CACHE_SIZE_ALIGN(adcsample_t, 100)> adc_samples;
-
-int main()
-{
- halInit();
- chSysInit();
-
- palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL); // LED
-
- ADCd adc (ADCD1, GPTD4);
- adc.start();
-
- //DACd dac (DACD1, {
- // .init = 0,
- // .datamode = DAC_DHRM_12BIT_RIGHT,
- // .cr = 0
- //});
- //dac.start();
- //dac.write(0, 1024);
-
- USBSeriald usbd (SDU1);
- usbd.start();
-
- while (true) {
- if (usbd.active()) {
- // Expect to receive a byte command 'packet'.
- if (char cmd; usbd.read(&cmd) > 0) {
- switch (cmd) {
- case 'r': // Read in analog signal
- adc.getSamples(&adc_samples[0], adc_samples.size());
- usbd.write(adc_samples.data(), adc_samples.size());
- break;
- case 'i': // Identify ourself as an stmdsp device
- usbd.write("stmdsp", 6);
- break;
- default:
- break;
- }
- }
- }
-
- chThdSleepMilliseconds(250);
- }
-}
-
+/**
+ * @file main.cpp
+ * @brief Program entry point.
+ *
+ * Copyright (C) 2020 Clyne Sullivan
+ *
+ * Distributed under the GNU GPL v3 or later. You should have received a copy of
+ * the GNU General Public License along with this program.
+ * If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "ch.h"
+#include "hal.h"
+
+#include "adc.hpp"
+#include "dac.hpp"
+#include "usbserial.hpp"
+
+#include <array>
+
+static_assert(sizeof(adcsample_t) == sizeof(uint16_t));
+
+#if CACHE_LINE_SIZE > 0
+CC_ALIGN(CACHE_LINE_SIZE)
+#endif
+static std::array<adcsample_t, CACHE_SIZE_ALIGN(adcsample_t, 2048)> adc_samples;
+
+int main()
+{
+ halInit();
+ chSysInit();
+
+ palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL); // LED
+
+ ADCd adc (ADCD1, GPTD4);
+ adc.start();
+
+ //DACd dac (DACD1, {
+ // .init = 0,
+ // .datamode = DAC_DHRM_12BIT_RIGHT,
+ // .cr = 0
+ //});
+ //dac.start();
+ //dac.write(0, 1024);
+
+ USBSeriald usbd (SDU1);
+ usbd.start();
+
+ while (true) {
+ if (usbd.active()) {
+ // Expect to receive a byte command 'packet'.
+ if (char cmd[3]; usbd.read(&cmd, 1) > 0) {
+ switch (cmd[0]) {
+ case 'r': // Read in analog signal
+ if (usbd.read(&cmd[1], 2) < 2)
+ break;
+ if (auto count = std::min(static_cast<unsigned int>(cmd[1] | (cmd[2] << 8)), adc_samples.size()); count > 0) {
+ adc.getSamples(&adc_samples[0], count);
+ usbd.write(adc_samples.data(), count * sizeof(adcsample_t));
+ }
+ break;
+ case 'i': // Identify ourself as an stmdsp device
+ usbd.write("stmdsp", 6);
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ chThdSleepMilliseconds(1);
+ }
+}
+