aboutsummaryrefslogtreecommitdiffstats
path: root/source/conversion.hpp
blob: a26dd199515bf033c25f69912ebf0d7014bbe177 (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
/**
 * @file conversion.hpp
 * @brief Manages algorithm application (converts input samples to output).
 *
 * 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_CONVERSION_HPP
#define STMDSP_CONVERSION_HPP

#include "ch.h"
#include "hal.h"

#include <array>

constexpr unsigned int CONVERSION_THREAD_STACK_SIZE = 
#if defined(TARGET_PLATFORM_H7)
                                                  62 * 1024;
#else
                                                  15 * 1024;
#endif

class ConversionManager
{
public:
    static void begin();

    // Begins sample conversion.
    static void start();
    // Begins conversion with execution time measured.
    static void startMeasured();
    // Stops conversion.
    static void stop();

    static thread_t *getMonitorHandle();

    // Internal only: Aborts a running conversion.
    static void abort(bool fpu_stacked = true);

private:
    static void threadMonitor(void *);
    static void threadRunnerEntry(void *stack);

    static void threadRunner(void *);
    static void adcReadHandler(adcsample_t *buffer, size_t);
    static void adcReadHandlerMeasure(adcsample_t *buffer, size_t);

    static thread_t *m_thread_monitor;
    static thread_t *m_thread_runner;

    static std::array<char, 1024> m_thread_monitor_stack;
    static std::array<char, THD_WORKING_AREA_SIZE(128)> m_thread_runner_entry_stack;
    static std::array<char, CONVERSION_THREAD_STACK_SIZE> m_thread_runner_stack;

    static std::array<msg_t, 2> m_mailbox_buffer;
    static mailbox_t m_mailbox;
};

#endif // STMDSP_CONVERSION_HPP