aboutsummaryrefslogtreecommitdiffstats
path: root/source/stmdsp/stmdsp.hpp
blob: 76ca94e09b08653d4947425e2e4d3242cfdf9166 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
 * @file stmdsp.hpp
 * @brief Interface for communication with stmdsp device over serial.
 *
 * Copyright (C) 2021 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/>.
 */

#ifndef STMDSP_HPP_
#define STMDSP_HPP_

#include <serial/serial.h>

#include <cstdint>
#include <list>
#include <memory>
#include <string>
#include <tuple>

namespace stmdsp
{
    constexpr unsigned int SAMPLES_MAX = 4096;

    enum class RunStatus : char {
        Idle = '1',
        Running,
        Recovering
    };

    enum class Error : char {
        None = 0,
        BadParam,
        BadParamSize,
        BadUserCodeLoad,
        BadUserCodeSize,
        NotIdle,
        ConversionAborted
    };

    class scanner
    {
    private:
        constexpr static const char *STMDSP_USB_ID =
#ifndef STMDSP_WIN32
            "USB VID:PID=0483:5740";
#else
            "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, /* Behind in feature support */
        L4, /* Complete feature support */
        G4  /* Currently unsupported */
    };

    class device
    {
    public:
        device(const std::string& file);
        ~device();

        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);
        unsigned int get_sample_rate();

        void continuous_start();
        void continuous_stop();

        void continuous_start_measure();
        uint32_t continuous_start_get_measurement();

        std::vector<adcsample_t> continuous_read();
        std::vector<adcsample_t> continuous_read_input();

        void 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; }

        // buffer is ELF binary
        void upload_filter(unsigned char *buffer, size_t size);
        void unload_filter();

        std::pair<RunStatus, Error> get_status();

    private:
        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;
    };
}

#endif // STMDSP_HPP_