diff options
Diffstat (limited to 'source/stmdsp')
-rw-r--r-- | source/stmdsp/stmdsp.cpp | 334 | ||||
-rw-r--r-- | source/stmdsp/stmdsp.hpp | 133 | ||||
-rw-r--r-- | source/stmdsp/stmdsp_code.hpp | 58 |
3 files changed, 354 insertions, 171 deletions
diff --git a/source/stmdsp/stmdsp.cpp b/source/stmdsp/stmdsp.cpp index b3fc8c3..294e98f 100644 --- a/source/stmdsp/stmdsp.cpp +++ b/source/stmdsp/stmdsp.cpp @@ -13,120 +13,186 @@ #include <serial/serial.h> +#include <algorithm> + +extern void log(const std::string& str); + +std::array<unsigned int, 6> sampleRateInts {{ + 8'000, + 16'000, + 20'000, + 32'000, + 48'000, + 96'000 +}}; + namespace stmdsp { - std::list<std::string>& scanner::scan() + const std::forward_list<std::string>& scanner::scan() { auto devices = serial::list_ports(); - for (auto& device : devices) { - if (device.hardware_id.find(STMDSP_USB_ID) != std::string::npos) - m_available_devices.emplace_front(device.port); - } - + auto foundDevicesEnd = std::remove_if( + devices.begin(), devices.end(), + [](const auto& dev) { + return dev.hardware_id.find(STMDSP_USB_ID) == std::string::npos; + }); + std::transform(devices.begin(), foundDevicesEnd, + std::front_inserter(m_available_devices), + [](const auto& dev) { return dev.port; }); return m_available_devices; } - device::device(const std::string& file) : - m_serial(file, 1000000/*230400*/, serial::Timeout::simpleTimeout(50)) + device::device(const std::string& file) { - if (m_serial.isOpen()) { - m_serial.flush(); - m_serial.write("i"); - if (auto id = m_serial.read(7); id.starts_with("stmdsp")) { - if (id.back() == 'h') - m_platform = platform::H7; - else if (id.back() == 'l') - m_platform = platform::L4; - else - m_serial.close(); - } else { - m_serial.close(); - } + // This could throw! + // Note: Windows needs a not-simple, positive timeout like this to + // ensure that reads block. + m_serial.reset(new serial::Serial(file, 921'600 /*8'000'000*/, serial::Timeout(1000, 1000, 1, 1000, 1))); + + // Test the ID command. + m_serial->flush(); + m_serial->write("i"); + auto id = m_serial->read(7); + + if (id.starts_with("stmdsp")) { + if (id.back() == 'h') + m_platform = platform::H7; + else if (id.back() == 'l') + m_platform = platform::L4; + else + m_serial.release(); + } else { + m_serial.release(); } } - void device::continuous_set_buffer_size(unsigned int size) { + device::~device() + { + disconnect(); + } + + bool device::connected() { + if (m_serial && !m_serial->isOpen()) + m_serial.release(); + + return m_serial ? true : false; + } + + void device::disconnect() { + if (m_serial) + m_serial.release(); + } + + bool device::try_command(std::basic_string<uint8_t> cmd) { + bool success = false; + if (connected()) { - m_buffer_size = size; + try { + std::scoped_lock lock (m_lock); + m_serial->write(cmd.data(), cmd.size()); + success = true; + } catch (...) { + handle_disconnect(); + } + } - uint8_t request[3] = { + return success; + } + + bool device::try_read(std::basic_string<uint8_t> cmd, uint8_t *dest, unsigned int dest_size) { + bool success = false; + + if (connected() && dest && dest_size > 0) { + try { + std::scoped_lock lock (m_lock); + m_serial->write(cmd.data(), cmd.size()); + m_serial->read(dest, dest_size); + success = true; + } catch (...) { + handle_disconnect(); + } + } + + return success; + } + + void device::continuous_set_buffer_size(unsigned int size) { + if (try_command({ 'B', static_cast<uint8_t>(size), - static_cast<uint8_t>(size >> 8) - }; - m_serial.write(request, 3); + static_cast<uint8_t>(size >> 8)})) + { + m_buffer_size = size; } } - void device::set_sample_rate(unsigned int id) { - if (connected()) { - uint8_t request[2] = { + void device::set_sample_rate(unsigned int rate) { + auto it = std::find( + sampleRateInts.cbegin(), + sampleRateInts.cend(), + rate); + + if (it != sampleRateInts.cend()) { + const auto i = std::distance(sampleRateInts.cbegin(), it); + try_command({ 'r', - static_cast<uint8_t>(id) - }; - m_serial.write(request, 2); + static_cast<uint8_t>(i) + }); } } unsigned int device::get_sample_rate() { - unsigned char result = 0xFF; - - if (connected()) { - uint8_t request[2] = { - 'r', 0xFF - }; - m_serial.write(request, 2); - m_serial.read(&result, 1); + if (!is_running()) { + uint8_t result = 0xFF; + if (try_read({'r', 0xFF}, &result, 1)) + m_sample_rate = result; } - return result; + return m_sample_rate < sampleRateInts.size() ? + sampleRateInts[m_sample_rate] : + 0; } void device::continuous_start() { - if (connected()) { - m_serial.write("R"); + if (try_command({'R'})) m_is_running = true; - } } - void device::continuous_start_measure() { - if (connected()) { - m_serial.write("M"); - m_is_running = true; - } + void device::measurement_start() { + try_command({'M'}); } - uint32_t device::continuous_start_get_measurement() { + uint32_t device::measurement_read() { uint32_t count = 0; - if (connected()) { - m_serial.write("m"); - m_serial.read(reinterpret_cast<uint8_t *>(&count), sizeof(uint32_t)); - } - + try_read({'m'}, reinterpret_cast<uint8_t *>(&count), sizeof(uint32_t)); return count / 2; } std::vector<adcsample_t> device::continuous_read() { if (connected()) { - m_serial.write("s"); - unsigned char sizebytes[2]; - m_serial.read(sizebytes, 2); - unsigned int size = sizebytes[0] | (sizebytes[1] << 8); - if (size > 0) { - std::vector<adcsample_t> data (size); - unsigned int total = size * sizeof(adcsample_t); - unsigned int offset = 0; - - while (total > 512) { - m_serial.read(reinterpret_cast<uint8_t *>(&data[0]) + offset, 512); - m_serial.write("n"); - offset += 512; - total -= 512; - } - m_serial.read(reinterpret_cast<uint8_t *>(&data[0]) + offset, total); - m_serial.write("n"); - return data; + try { + m_serial->write("s"); + unsigned char sizebytes[2]; + m_serial->read(sizebytes, 2); + unsigned int size = sizebytes[0] | (sizebytes[1] << 8); + if (size > 0) { + std::vector<adcsample_t> data (size); + unsigned int total = size * sizeof(adcsample_t); + unsigned int offset = 0; + while (total > 512) { + m_serial->read(reinterpret_cast<uint8_t *>(&data[0]) + offset, 512); + m_serial->write("n"); + offset += 512; + total -= 512; + } + m_serial->read(reinterpret_cast<uint8_t *>(&data[0]) + offset, total); + m_serial->write("n"); + return data; + + } + } catch (...) { + handle_disconnect(); } } @@ -135,25 +201,29 @@ namespace stmdsp std::vector<adcsample_t> device::continuous_read_input() { if (connected()) { - m_serial.write("t"); - unsigned char sizebytes[2]; - m_serial.read(sizebytes, 2); - unsigned int size = sizebytes[0] | (sizebytes[1] << 8); - if (size > 0) { - std::vector<adcsample_t> data (size); - unsigned int total = size * sizeof(adcsample_t); - unsigned int offset = 0; - - while (total > 512) { - m_serial.read(reinterpret_cast<uint8_t *>(&data[0]) + offset, 512); - m_serial.write("n"); - offset += 512; - total -= 512; - } - m_serial.read(reinterpret_cast<uint8_t *>(&data[0]) + offset, total); - m_serial.write("n"); - return data; + try { + m_serial->write("t"); + unsigned char sizebytes[2]; + m_serial->read(sizebytes, 2); + unsigned int size = sizebytes[0] | (sizebytes[1] << 8); + if (size > 0) { + std::vector<adcsample_t> data (size); + unsigned int total = size * sizeof(adcsample_t); + unsigned int offset = 0; + while (total > 512) { + m_serial->read(reinterpret_cast<uint8_t *>(&data[0]) + offset, 512); + m_serial->write("n"); + offset += 512; + total -= 512; + } + m_serial->read(reinterpret_cast<uint8_t *>(&data[0]) + offset, total); + m_serial->write("n"); + return data; + + } + } catch (...) { + handle_disconnect(); } } @@ -161,37 +231,51 @@ namespace stmdsp } void device::continuous_stop() { - if (connected()) { - m_serial.write("S"); + if (try_command({'S'})) m_is_running = false; - } } - void device::siggen_upload(dacsample_t *buffer, unsigned int size) { + bool device::siggen_upload(dacsample_t *buffer, unsigned int size) { if (connected()) { uint8_t request[3] = { 'D', static_cast<uint8_t>(size), static_cast<uint8_t>(size >> 8) }; - m_serial.write(request, 3); - m_serial.write((uint8_t *)buffer, size * sizeof(dacsample_t)); + if (!m_is_siggening) { + try { + m_serial->write(request, 3); + m_serial->write((uint8_t *)buffer, size * sizeof(dacsample_t)); + } catch (...) { + handle_disconnect(); + } + } else { + try { + m_serial->write(request, 3); + if (m_serial->read(1)[0] == 0) + return false; + else + m_serial->write((uint8_t *)buffer, size * sizeof(dacsample_t)); + } catch (...) { + handle_disconnect(); + } + } + + return true; + } else { + return false; } } void device::siggen_start() { - if (connected()) { + if (try_command({'W'})) m_is_siggening = true; - m_serial.write("W"); - } } void device::siggen_stop() { - if (connected()) { + if (try_command({'w'})) m_is_siggening = false; - m_serial.write("w"); - } } void device::upload_filter(unsigned char *buffer, size_t size) { @@ -201,14 +285,46 @@ namespace stmdsp static_cast<uint8_t>(size), static_cast<uint8_t>(size >> 8) }; - m_serial.write(request, 3); - m_serial.write(buffer, size); + try { + m_serial->write(request, 3); + m_serial->write(buffer, size); + } catch (...) { + handle_disconnect(); + } } } void device::unload_filter() { - if (connected()) - m_serial.write("e"); + try_command({'e'}); + } + + std::pair<RunStatus, Error> device::get_status() { + std::pair<RunStatus, Error> ret; + + unsigned char buf[2]; + if (try_read({'I'}, buf, 2)) { + ret = { + static_cast<RunStatus>(buf[0]), + static_cast<Error>(buf[1]) + }; + + bool running = ret.first == RunStatus::Running; + if (m_is_running != running) + m_is_running = running; + } else if (m_disconnect_error_flag) { + m_disconnect_error_flag = false; + return {RunStatus::Idle, Error::GUIDisconnect}; + } + + return ret; } -} + + void device::handle_disconnect() + { + m_disconnect_error_flag = true; + m_serial.release(); + log("Lost connection!"); + } +} // namespace stmdsp + diff --git a/source/stmdsp/stmdsp.hpp b/source/stmdsp/stmdsp.hpp index 8da98f2..efed8a3 100644 --- a/source/stmdsp/stmdsp.hpp +++ b/source/stmdsp/stmdsp.hpp @@ -12,17 +12,89 @@ #ifndef STMDSP_HPP_ #define STMDSP_HPP_ -#include <cstdint> -#include <list> #include <serial/serial.h> + +#include <cstdint> +#include <forward_list> +#include <memory> +#include <mutex> #include <string> +#include <tuple> namespace stmdsp { + /** + * The largest possible size of an ADC or DAC sample buffer, as a sample count. + * Maximum byte size would be `SAMPLES_MAX * sizeof(XXXsample_t)`. + */ constexpr unsigned int SAMPLES_MAX = 4096; + /** + * ADC samples on all platforms are stored as 16-bit unsigned integers. + */ + using adcsample_t = uint16_t; + /** + * DAC samples on all platforms are stored as 16-bit unsigned integers. + */ + using dacsample_t = uint16_t; + + /** + * List of all available platforms. + * Note that some platforms in this list may not have complete support. + */ + enum class platform { + Unknown, + H7, /* Some feature support */ + L4, /* Complete feature support */ + G4 /* Unsupported, but planned */ + }; + + /** + * Run status states, valued to match what the stmdsp firmware reports. + */ + enum class RunStatus : char { + Idle = '1', /* Device ready for commands or execution. */ + Running, /* Device currently executing its algorithm. */ + Recovering /* Device recovering from fault caused by algorithm. */ + }; + + /** + * Error messages that are reported by the firmware. + */ + enum class Error : char { + None = 0, + BadParam, /* An invalid parameter was passed for a command. */ + BadParamSize, /* An invaild param. size was given for a command. */ + BadUserCodeLoad, /* Device failed to load the given algorithm. */ + BadUserCodeSize, /* The given algorithm is too large for the device. */ + NotIdle, /* An idle-only command was received while not Idle. */ + ConversionAborted, /* A conversion was aborted due to a fault. */ + NotRunning, /* A running-only command was received while not Running. */ + + GUIDisconnect = 100 /* The GUI lost connection with the device. */ + }; + + /** + * Provides functionality to scan the system for stmdsp devices. + * A list of devices is returned, though the GUI only interacts with one + * device at a time. + */ class scanner { + public: + /** + * Scans for connected devices, returning a list of ports with + * connected stmdsp devices. + */ + const std::forward_list<std::string>& scan(); + + /** + * Retrieves the results of the last scan(). + */ + const std::forward_list<std::string>& devices() const noexcept { + return m_available_devices; + } + private: constexpr static const char *STMDSP_USB_ID = #ifndef STMDSP_WIN32 @@ -31,54 +103,39 @@ namespace stmdsp "USB\\VID_0483&PID_5740"; #endif - public: - std::list<std::string>& scan(); - auto& devices() { - return m_available_devices; - } - - private: - std::list<std::string> m_available_devices; - }; - - using adcsample_t = uint16_t; - using dacsample_t = uint16_t; - - enum class platform { - Unknown, - H7, - L4, - G4 + std::forward_list<std::string> m_available_devices; }; class device { public: device(const std::string& file); + ~device(); - ~device() { - m_serial.close(); - } - - bool connected() { - return m_serial.isOpen(); - } + bool connected(); + void disconnect(); auto get_platform() const { return m_platform; } + void continuous_set_buffer_size(unsigned int size); unsigned int get_buffer_size() const { return m_buffer_size; } - void set_sample_rate(unsigned int id); + + void set_sample_rate(unsigned int rate); unsigned int get_sample_rate(); + void continuous_start(); - void continuous_start_measure(); - uint32_t continuous_start_get_measurement(); + void continuous_stop(); + + void measurement_start(); + uint32_t measurement_read(); + std::vector<adcsample_t> continuous_read(); std::vector<adcsample_t> continuous_read_input(); - void continuous_stop(); - void siggen_upload(dacsample_t *buffer, unsigned int size); + bool siggen_upload(dacsample_t *buffer, unsigned int size); void siggen_start(); void siggen_stop(); + bool is_siggening() const { return m_is_siggening; } bool is_running() const { return m_is_running; } @@ -86,12 +143,22 @@ namespace stmdsp void upload_filter(unsigned char *buffer, size_t size); void unload_filter(); + std::pair<RunStatus, Error> get_status(); + private: - serial::Serial m_serial; + std::unique_ptr<serial::Serial> m_serial; platform m_platform = platform::Unknown; unsigned int m_buffer_size = SAMPLES_MAX; + unsigned int m_sample_rate = 0; bool m_is_siggening = false; bool m_is_running = false; + bool m_disconnect_error_flag = false; + + std::mutex m_lock; + + bool try_command(std::basic_string<uint8_t> data); + bool try_read(std::basic_string<uint8_t> cmd, uint8_t *dest, unsigned int dest_size); + void handle_disconnect(); }; } diff --git a/source/stmdsp/stmdsp_code.hpp b/source/stmdsp/stmdsp_code.hpp index 6850459..7ba0ed2 100644 --- a/source/stmdsp/stmdsp_code.hpp +++ b/source/stmdsp/stmdsp_code.hpp @@ -118,67 +118,67 @@ return s; )cpp"; static std::string file_header_l4 = R"cpp( #include <cstdint> -#include <span> using Sample = uint16_t; -using Samples = std::span<Sample, $0>; +using Samples = Sample[$0]; +constexpr unsigned int SIZE = $0; Sample *process_data(Samples samples); extern "C" void process_data_entry() { Sample *samples; asm("mov %0, r0" : "=r" (samples)); - process_data(Samples(samples, $0)); + process_data(samples); } -static float PI = 3.14159265358979L; +static inline float PI = 3.14159265358979L; __attribute__((naked)) -auto sin(float x) { -asm("vmov.f32 r1, s0;" +static inline auto sin(float x) { + asm("vmov.f32 r1, s0;" "eor r0, r0;" "svc 1;" "vmov.f32 s0, r1;" "bx lr"); -return 0; + return 0; } __attribute__((naked)) -auto cos(float x) { -asm("vmov.f32 r1, s0;" +static inline auto cos(float x) { + asm("vmov.f32 r1, s0;" "mov r0, #1;" "svc 1;" "vmov.f32 s0, r1;" "bx lr"); -return 0; + return 0; } __attribute__((naked)) -auto tan(float x) { -asm("vmov.f32 r1, s0;" +static inline auto tan(float x) { + asm("vmov.f32 r1, s0;" "mov r0, #2;" "svc 1;" "vmov.f32 s0, r1;" "bx lr"); -return 0; + return 0; } __attribute__((naked)) -auto sqrt(float) { -asm("vsqrt.f32 s0, s0; bx lr"); -return 0; +static inline auto sqrt(float) { + asm("vsqrt.f32 s0, s0; bx lr"); + return 0; } -auto readpot1() { -Sample s; -asm("push {r4-r11}; eor r0, r0; svc 3; mov %0, r0; pop {r4-r11}" : "=r"(s)); -return s; +static inline auto param1() { + Sample s; + asm("eor r0, r0; svc 3; mov %0, r0" : "=r" (s) :: "r0"); + return s; } -auto readpot2() { -Sample s; -asm("push {r4-r11}; mov r0, #1; svc 3; mov %0, r0; pop {r4-r11}" : "=r"(s)); -return s; +static inline auto param2() { + Sample s; + asm("mov r0, #1; svc 3; mov %0, r0" : "=r" (s) :: "r0"); + return s; } -//void puts(const char *s) { -// 's' will already be in r0. -//asm("push {r4-r6}; svc 4; pop {r4-r6}"); +//static inline void puts(const char *s) { +// // 's' will already be in r0. +// asm("push {r4-r6}; svc 4; pop {r4-r6}"); //} // End stmdspgui header code @@ -187,9 +187,9 @@ return s; static std::string file_content = -R"cpp(Sample *process_data(Samples samples) +R"cpp(Sample* process_data(Samples samples) { - return samples.data(); + return samples; } )cpp"; |