aboutsummaryrefslogtreecommitdiffstats
path: root/source/main.cpp
blob: b8d2fa148d0f847dca09f5ecc5d19654d56846cd (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
/**
 * @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);
	}
}