/**
* @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 .
*/
#include "ch.h"
#include "hal.h"
#include "adc.hpp"
#include "dac.hpp"
#include "usbserial.hpp"
#include
static_assert(sizeof(adcsample_t) == sizeof(uint16_t));
#if CACHE_LINE_SIZE > 0
CC_ALIGN(CACHE_LINE_SIZE)
#endif
static std::array adc_samples;
static std::array dac_samples;
static void signal_operate(adcsample_t *buffer, size_t count);
int main()
{
halInit();
chSysInit();
palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL); // LED
adc_init();
dac_init();
usbserial_init();
static unsigned int dac_sample_count = 2048;
while (true) {
if (usbserial_is_active()) {
// Expect to receive a byte command 'packet'.
if (char cmd[3]; usbserial_read(&cmd, 1) > 0) {
switch (cmd[0]) {
case 'r': // Read in analog signal
if (usbserial_read(&cmd[1], 2) < 2)
break;
if (auto count = std::min(static_cast(cmd[1] | (cmd[2] << 8)), adc_samples.size()); count > 0) {
adc_read(&adc_samples[0], count);
usbserial_write(adc_samples.data(), count * sizeof(adcsample_t));
}
break;
case 'R':
adc_read_start(signal_operate, &adc_samples[0], adc_samples.size() * sizeof(adcsample_t));
break;
case 'S':
adc_read_stop();
break;
case 'W':
if (usbserial_read(&cmd[1], 2) < 2)
break;
if (auto count = std::min(static_cast(cmd[1] | (cmd[2] << 8)), dac_samples.size()); count > 0)
dac_sample_count = count;
else
dac_write_stop();
break;
case 'w':
if (usbserial_read(&dac_samples[0], 2 * dac_sample_count) != 2 * dac_sample_count)
break;
dac_write_start(&dac_samples[0], dac_sample_count);
break;
case 'i': // Identify ourself as an stmdsp device
usbserial_write("stmdsp", 6);
break;
default:
break;
}
}
}
chThdSleepMilliseconds(1);
}
}
void signal_operate([[maybe_unused]] adcsample_t *buffer, [[maybe_unused]] size_t count)
{
}