]> code.bitgloo.com Git - clyne/stmdsp.git/commitdiff
fixed sample rate issues; up to 88kS/s
authorClyne Sullivan <clyne@bitgloo.com>
Fri, 16 Oct 2020 16:16:53 +0000 (12:16 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Fri, 16 Oct 2020 16:16:53 +0000 (12:16 -0400)
cfg/mcuconf.h
gui/wxmain.cpp
gui/wxmain.hpp
source/adc.cpp
source/dac.cpp
source/main.cpp

index d346e022eddd132dfb94413156d4c42b295e0332..cd6d0bb7b2e113b831ffa2a93a410cee2f838f74 100644 (file)
 #define STM32_MCOSEL                        STM32_MCOSEL_NOCLOCK\r
 #define STM32_MCOPRE                        STM32_MCOPRE_DIV1\r
 #define STM32_LSCOSEL                       STM32_LSCOSEL_NOCLOCK\r
-#define STM32_PLLSAI1N_VALUE                72\r
+#define STM32_PLLSAI1N_VALUE                44\r
 #define STM32_PLLSAI1P_VALUE                7\r
 #define STM32_PLLSAI1Q_VALUE                6\r
-#define STM32_PLLSAI1R_VALUE                6\r
+#define STM32_PLLSAI1R_VALUE                8\r
 #define STM32_PLLSAI2N_VALUE                72\r
 #define STM32_PLLSAI2P_VALUE                7\r
 #define STM32_PLLSAI2R_VALUE                6\r
@@ -88,7 +88,7 @@
 #define STM32_SAI1SEL                       STM32_SAI1SEL_OFF\r
 #define STM32_SAI2SEL                       STM32_SAI2SEL_OFF\r
 #define STM32_CLK48SEL                      STM32_CLK48SEL_MSI\r
-#define STM32_ADCSEL                        STM32_ADCSEL_SYSCLK\r
+#define STM32_ADCSEL                        STM32_ADCSEL_PLLSAI1\r
 #define STM32_SWPMI1SEL                     STM32_SWPMI1SEL_PCLK1\r
 #define STM32_DFSDMSEL                      STM32_DFSDMSEL_PCLK2\r
 #define STM32_RTCSEL                        STM32_RTCSEL_LSI\r
 #define STM32_ADC_ADC1_DMA_IRQ_PRIORITY     5\r
 #define STM32_ADC_ADC2_DMA_IRQ_PRIORITY     5\r
 #define STM32_ADC_ADC3_DMA_IRQ_PRIORITY     5\r
-#define STM32_ADC_ADC123_CLOCK_MODE         ADC_CCR_CKMODE_AHB_DIV1\r
-#define STM32_ADC_ADC123_PRESC              ADC_CCR_PRESC_DIV2\r
+#define STM32_ADC_ADC123_CLOCK_MODE         ADC_CCR_CKMODE_ADCCK\r
+#define STM32_ADC_ADC123_PRESC              ADC_CCR_PRESC_DIV10\r
+\r
+//#define ADC123_PRESC_VALUE 1\r
 \r
 /*\r
  * CAN driver system settings.\r
index da80d1bee2c57165b62881d412f20b703ec72572..82e6ed5cce133a0aec7ca67387b9e71859735a31 100644 (file)
@@ -5,12 +5,10 @@
 #include <wx/menu.h>
 #include <wx/msgdlg.h>
 #include <wx/sizer.h>
+#include <wx/statusbr.h>
 
 enum Id {
-    Single = 1,
-    ConnectDevice,
-    UploadFilter,
-    RenderTimer,
+    MeasureTimer = 1,
 
     MFileNew,
     MFileOpen,
@@ -27,6 +25,10 @@ enum Id {
 
 MainFrame::MainFrame() : wxFrame(nullptr, -1, "stmdspgui", wxPoint(50, 50), wxSize(640, 800))
 {
+    m_status_bar = new wxStatusBar(this);
+    m_status_bar->SetStatusText("Ready.");
+    SetStatusBar(m_status_bar);
+
     auto menubar = new wxMenuBar;
     auto menuFile = new wxMenu;
     Bind(wxEVT_MENU, &MainFrame::onFileNew, this, Id::MFileNew, wxID_ANY,
@@ -46,9 +48,7 @@ MainFrame::MainFrame() : wxFrame(nullptr, -1, "stmdspgui", wxPoint(50, 50), wxSi
     menuRun->AppendSeparator();
     Bind(wxEVT_MENU, &MainFrame::onRunStart, this, Id::MRunStart, wxID_ANY,
          menuRun->Append(MRunStart, "&Start"));
-    m_run_measure = menuRun->AppendCheckItem(MRunMeasure, "&Measure code speed");
-    m_run_measure_value = menuRun->Append(wxID_ANY, "Last time:");
-    m_run_measure_value->Enable(false);
+    m_run_measure = menuRun->AppendCheckItem(MRunMeasure, "&Measure code time");
     menuRun->AppendSeparator();
     Bind(wxEVT_MENU, &MainFrame::onRunCompile, this, Id::MRunCompile, wxID_ANY,
          menuRun->Append(MRunCompile, "&Compile code"));
@@ -67,56 +67,20 @@ MainFrame::MainFrame() : wxFrame(nullptr, -1, "stmdspgui", wxPoint(50, 50), wxSi
     prepareEditor();
     window->Add(m_text_editor, 1, wxEXPAND | wxALL, 10);
 
-    //m_signal_area = new wxControl(this, wxID_ANY, wxDefaultPosition, wxSize(600, 200));
-    //window->Add(m_signal_area, 1, wxEXPAND | wxALL, 10);
-
     SetSizerAndFit(window);
 
-    //m_render_timer = new wxTimer(this, Id::RenderTimer);
-
-    //Bind(wxEVT_PAINT, &MainFrame::onPaint, this, wxID_ANY);
-    //Bind(wxEVT_TIMER, &MainFrame::onRenderTimer, this, Id::RenderTimer);
+    m_measure_timer = new wxTimer(this, Id::MeasureTimer);
+    Bind(wxEVT_TIMER, &MainFrame::onMeasureTimer, this, Id::MeasureTimer);
 }
 
-void MainFrame::onPaint([[maybe_unused]] wxPaintEvent& pe)
+void MainFrame::onMeasureTimer([[maybe_unused]] wxTimerEvent&)
 {
-    auto *dc = new wxClientDC(this);
-    auto region = m_signal_area->GetRect();
-    dc->SetClippingRegion(region);
-
-    dc->SetBrush(*wxBLACK_BRUSH); dc->SetPen(*wxBLACK_PEN);
-    dc->DrawRectangle(region);
-
-    if (m_device_samples.size() > 0) {
-        dc->SetPen(*wxRED_PEN);
-        auto points = new wxPoint[m_device_samples.size()];
-        const float spacing = static_cast<float>(region.GetWidth()) / m_device_samples.size();
-        float x = 0;
-        for (auto ptr = points; auto sample : m_device_samples) {
-            *ptr++ = wxPoint {
-                static_cast<int>(x),
-                region.GetHeight() - sample * region.GetHeight() / 4096
-            };
-            x += spacing;
-        }
-        dc->DrawLines(m_device_samples.size(), points, region.GetX(), region.GetY());
-        delete[] points;
+    if (m_status_bar && m_device) {
+        m_status_bar->SetStatusText(wxString::Format(wxT("Execution time: %u cycles"),
+                                                     m_device->continuous_start_get_measurement()));
     }
 }
 
-void MainFrame::onRenderTimer([[maybe_unused]] wxTimerEvent& te)
-{
-    updateDrawing();
-}
-
-void MainFrame::updateDrawing()
-{
-    if (m_device_samples = m_device_samples_future.get(); m_device_samples.size() > 0)
-        this->RefreshRect(m_signal_area->GetRect());
-
-    requestSamples();
-}
-
 void MainFrame::prepareEditor()
 {
     m_text_editor->SetLexer(wxSTC_LEX_CPP);
@@ -146,7 +110,7 @@ void MainFrame::prepareEditor()
 
     // a sample list of keywords, I haven't included them all to keep it short...
     m_text_editor->SetKeyWords(0,
-        wxT("return for while do break continue if else goto"));
+        wxT("return for while do break continue if else goto asm"));
     m_text_editor->SetKeyWords(1,
         wxT("void char short int long auto float double unsigned signed "
             "volatile static const constexpr constinit consteval "
@@ -205,10 +169,13 @@ wxString MainFrame::compileEditorCode()
 
     wxString make_command = wxString("make -C ") + temp_file_name.BeforeLast('/') +
                             " -f " + temp_file_name + "make";
-    if (system(make_command.ToAscii()) == 0)
+    if (system(make_command.ToAscii()) == 0) {
+        m_status_bar->SetStatusText("Compilation succeeded.");
         return temp_file_name + ".o";
-    else
+    } else {
+        m_status_bar->SetStatusText("Compilation failed.");
         return "";
+    }
 }
 
 void MainFrame::onFileNew([[maybe_unused]] wxCommandEvent&)
@@ -221,6 +188,7 @@ R"cpp(adcsample_t *process_data(adcsample_t *samples, unsigned int size)
 }
 )cpp");
     m_text_editor->DiscardEdits();
+    m_status_bar->SetStatusText("Ready.");
 }
 
 void MainFrame::onFileOpen([[maybe_unused]] wxCommandEvent&)
@@ -237,6 +205,7 @@ void MainFrame::onFileOpen([[maybe_unused]] wxCommandEvent&)
                 m_open_file_path = openDialog.GetPath();
                 m_text_editor->SetText(buffer);
                 m_text_editor->DiscardEdits();
+                m_status_bar->SetStatusText("Ready.");
             }
             delete[] buffer;
         }
@@ -253,8 +222,13 @@ void MainFrame::onFileSave(wxCommandEvent& ce)
                 file.Write(m_text_editor->GetText());
                 file.Close();
                 m_text_editor->DiscardEdits();
+                m_status_bar->SetStatusText("Saved.");
+            } else {
+                m_status_bar->SetStatusText("Save failed: couldn't open file.");
             }
         }
+    } else {
+        m_status_bar->SetStatusText("No modifications to save.");
     }
 }
 
@@ -271,8 +245,13 @@ void MainFrame::onFileSaveAs([[maybe_unused]] wxCommandEvent& ce)
                 file.Close();
                 m_text_editor->DiscardEdits();
                 m_open_file_path = saveDialog.GetPath();
+                m_status_bar->SetStatusText("Saved.");
+            } else {
+                m_status_bar->SetStatusText("Save failed: couldn't open file.");
             }
         }
+    } else {
+        m_status_bar->SetStatusText("No modifications to save.");
     }
 }
 
@@ -291,16 +270,21 @@ void MainFrame::onRunConnect(wxCommandEvent& ce)
             m_device = new stmdsp::device(devices.front());
             if (m_device->connected()) {
                 menuItem->SetItemLabel("&Disconnect");
+                m_status_bar->SetStatusText("Connected.");
             } else {
                 delete m_device;
                 m_device = nullptr;
                 menuItem->SetItemLabel("&Connect");
+                m_status_bar->SetStatusText("Failed to connect.");
             }
+        } else {
+            m_status_bar->SetStatusText("No devices found.");
         }
     } else {
         delete m_device;
         m_device = nullptr;
         menuItem->SetItemLabel("&Connect");
+        m_status_bar->SetStatusText("Disconnected.");
     }
 }
 
@@ -308,30 +292,28 @@ void MainFrame::onRunStart(wxCommandEvent& ce)
 {
     auto menuItem = dynamic_cast<wxMenuItem *>(ce.GetEventUserData());
 
-    if (!m_render_timer->IsRunning()) {
+    if (!m_is_running) {
         if (m_device != nullptr && m_device->connected()) {
-            if (m_run_measure && m_run_measure->IsChecked())
+            if (m_run_measure && m_run_measure->IsChecked()) {
                 m_device->continuous_start_measure();
-            else
+                m_measure_timer->StartOnce(1000);
+            } else {
                 m_device->continuous_start();
+            }
 
-            m_device_samples_future = std::async(std::launch::async,
-                                                 []() { return decltype(m_device_samples)(); });
-            m_render_timer->Start(1000);
             menuItem->SetItemLabel("&Stop");
+            m_status_bar->SetStatusText("Running.");
+            m_is_running = true;
         } else {
             wxMessageBox("No device connected!", "Run", wxICON_WARNING);
+            m_status_bar->SetStatusText("Please connect.");
         }
     } else {
-        m_render_timer->Stop();
         m_device->continuous_stop();
 
         menuItem->SetItemLabel("&Start");
-        if (m_run_measure && m_run_measure_value) {
-            m_run_measure_value->SetItemLabel(
-                m_run_measure->IsChecked() ? wxString::Format(wxT("Last time: %u"),m_device->continuous_start_get_measurement())
-                                           : "Last time: --");
-        }
+        m_status_bar->SetStatusText("Ready.");
+        m_is_running = false;
     }
 }
 
@@ -349,22 +331,24 @@ void MainFrame::onRunUpload([[maybe_unused]] wxCommandEvent&)
             if (m_device != nullptr && m_device->connected()) {
                 file_stream.ReadAll(buffer, size);
                 m_device->upload_filter(buffer, size);
+                m_status_bar->SetStatusText("Code uploaded.");
             } else {
                 wxMessageBox("No device connected!", "Run", wxICON_WARNING);
+                m_status_bar->SetStatusText("Please connect.");
             }
+        } else {
+             m_status_bar->SetStatusText("Couldn't load compiled code.");
         }
     }
 }
 
 void MainFrame::onRunUnload([[maybe_unused]] wxCommandEvent&)
 {
-    if (m_device != nullptr && m_device->connected())
+    if (m_device != nullptr && m_device->connected()) {
         m_device->unload_filter();
-}
-
-void MainFrame::requestSamples()
-{
-    m_device_samples_future = std::async(std::launch::async,
-                                         [this]() { return m_device->continuous_read(); });
+        m_status_bar->SetStatusText("Unloaded code.");
+    } else {
+        m_status_bar->SetStatusText("No device connected.");
+    }
 }
 
index 4a8914ddd65db5976678a426527d7a203b886333..b8ff11d2ae664de7be08f4e8d2b5bacf2f34d5d4 100644 (file)
@@ -20,9 +20,6 @@ class MainFrame : public wxFrame
 {
 public:
     MainFrame();
-    
-    void onPaint(wxPaintEvent& pe);
-    void onRenderTimer(wxTimerEvent& te);
 
     void onFileNew(wxCommandEvent&);
     void onFileOpen(wxCommandEvent&);
@@ -36,22 +33,19 @@ public:
     void onRunUpload(wxCommandEvent&);
     void onRunUnload(wxCommandEvent&);
 
-    void requestSamples();
-    void updateDrawing();
+    void onMeasureTimer(wxTimerEvent& te);
 
 private:
-    bool m_is_rendering = false;
-    wxTimer *m_render_timer = nullptr;
+    bool m_is_running = false;
     wxComboBox *m_device_combo = nullptr;
     wxStyledTextCtrl *m_text_editor = nullptr;
     wxControl *m_signal_area = nullptr;
     wxMenuItem *m_run_measure = nullptr;
-    wxMenuItem *m_run_measure_value = nullptr;
+    wxTimer *m_measure_timer = nullptr;
+    wxStatusBar *m_status_bar = nullptr;
     wxString m_open_file_path;
 
     stmdsp::device *m_device = nullptr;
-    std::future<std::vector<stmdsp::adcsample_t>> m_device_samples_future;
-    std::vector<stmdsp::adcsample_t> m_device_samples;
 
     bool tryDevice();
     void prepareEditor();
index c28c38fc9b67a548f264ca1b7f6b1ae2cabe13b5..6e02dde0481ebb3802b2f05b923a916bc92fb13f 100644 (file)
@@ -29,7 +29,7 @@ static ADCConversionGroup adc_group_config = {
     .cfgr2 = 0,
     .tr1 = ADC_TR(0, 4095),
     .smpr = {
-        ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_2P5), 0
+        ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_12P5), 0
     },
     .sqr = {
         ADC_SQR1_SQ1_N(ADC_CHANNEL_IN5),
@@ -38,7 +38,7 @@ static ADCConversionGroup adc_group_config = {
 };
 
 constexpr static const GPTConfig gpt_config = {
-    .frequency = 600000,
+    .frequency = 8000000,
     .callback = nullptr,
     .cr2 = TIM_CR2_MMS_1, /* TRGO */
     .dier = 0
@@ -65,7 +65,7 @@ namespace adc
         adc_is_read_finished = false;
         adc_group_config.circular = false;
         adcStartConversion(adcd, &adc_group_config, buffer, count);
-        gptStartContinuous(gptd, 5);
+        gptStartContinuous(gptd, 2);
         while (!adc_is_read_finished);
         return buffer;
     }
@@ -77,7 +77,7 @@ namespace adc
         adc_operation_func = operation_func;
         adc_group_config.circular = true;
         adcStartConversion(adcd, &adc_group_config, buffer, count);
-        gptStartContinuous(gptd, 5);
+        gptStartContinuous(gptd, 2);
     }
 
     void read_set_operation_func(operation_t operation_func)
@@ -113,7 +113,7 @@ namespace adc
             val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_24P5);
             break;
         case rate::R47P5:
-            val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_47P5);
+            val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_47P5);////
             break;
         case rate::R92P5:
             val = ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_92P5);
index 9f5e45729c664d42317882aadbca4273c4703e88..e0f62a5525adf3db71e4d32f328d887795869cd2 100644 (file)
@@ -28,7 +28,7 @@ constexpr static const DACConversionGroup dac_group_config = {
 };
 
 constexpr static const GPTConfig gpt_config = {
-  .frequency = 600000,
+  .frequency = 440000,
   .callback = nullptr,
   .cr2 = TIM_CR2_MMS_1, /* TRGO */
   .dier = 0
index 800005c61029663245cda51e15efb1cc8e180e7d..9dcd72f3c9c1c6036f0bb98d0a3083520eb2f05a 100644 (file)
@@ -19,7 +19,7 @@
 
 #include <array>
 
-constexpr unsigned int MAX_SAMPLE_BUFFER_SIZE = 2048;
+constexpr unsigned int MAX_SAMPLE_BUFFER_SIZE = 8000;//2048;
 
 enum class RunStatus : char
 {
@@ -54,7 +54,7 @@ CC_ALIGN(CACHE_LINE_SIZE)
 #endif
 static std::array<dacsample_t, CACHE_SIZE_ALIGN(dacsample_t, MAX_SAMPLE_BUFFER_SIZE)> dac_samples;
 
-static uint8_t elf_file_store[8192];
+static uint8_t elf_file_store[12288];
 static elf::entry_t elf_entry = nullptr;
 
 static void signal_operate(adcsample_t *buffer, size_t count);
@@ -161,6 +161,8 @@ void main_loop()
                     if (unsigned int binarySize = cmd[1] | (cmd[2] << 8); binarySize < sizeof(elf_file_store)) {
                         usbserial::read(elf_file_store, binarySize);
                         elf_entry = elf::load(elf_file_store);
+                    } else {
+                        elf_entry = nullptr;
                     }
                     break;