fixed command receiving code

pull/1/head
Clyne 4 years ago
parent 875b6a0620
commit 546fb2d9dc

@ -1,72 +1,74 @@
/** /**
* @file main.cpp * @file main.cpp
* @brief Program entry point. * @brief Program entry point.
* *
* Copyright (C) 2020 Clyne Sullivan * Copyright (C) 2020 Clyne Sullivan
* *
* Distributed under the GNU GPL v3 or later. You should have received a copy of * Distributed under the GNU GPL v3 or later. You should have received a copy of
* the GNU General Public License along with this program. * the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>. * If not, see <https://www.gnu.org/licenses/>.
*/ */
#include "ch.h" #include "ch.h"
#include "hal.h" #include "hal.h"
#include "adc.hpp" #include "adc.hpp"
#include "dac.hpp" #include "dac.hpp"
#include "usbserial.hpp" #include "usbserial.hpp"
#include <array> #include <array>
static_assert(sizeof(adcsample_t) == sizeof(uint16_t)); static_assert(sizeof(adcsample_t) == sizeof(uint16_t));
#if CACHE_LINE_SIZE > 0 #if CACHE_LINE_SIZE > 0
CC_ALIGN(CACHE_LINE_SIZE) CC_ALIGN(CACHE_LINE_SIZE)
#endif #endif
static std::array<adcsample_t, CACHE_SIZE_ALIGN(adcsample_t, 2048)> adc_samples; static std::array<adcsample_t, CACHE_SIZE_ALIGN(adcsample_t, 2048)> adc_samples;
int main() int main()
{ {
halInit(); halInit();
chSysInit(); chSysInit();
palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL); // LED palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL); // LED
ADCd adc (ADCD1, GPTD4); ADCd adc (ADCD1, GPTD4);
adc.start(); adc.start();
//DACd dac (DACD1, { //DACd dac (DACD1, {
// .init = 0, // .init = 0,
// .datamode = DAC_DHRM_12BIT_RIGHT, // .datamode = DAC_DHRM_12BIT_RIGHT,
// .cr = 0 // .cr = 0
//}); //});
//dac.start(); //dac.start();
//dac.write(0, 1024); //dac.write(0, 1024);
USBSeriald usbd (SDU1); USBSeriald usbd (SDU1);
usbd.start(); usbd.start();
while (true) { while (true) {
if (usbd.active()) { if (usbd.active()) {
// Expect to receive a byte command 'packet'. // Expect to receive a byte command 'packet'.
if (char cmd[3]; usbd.read(&cmd, 3) > 0) { if (char cmd[3]; usbd.read(&cmd, 1) > 0) {
switch (cmd[0]) { switch (cmd[0]) {
case 'r': // Read in analog signal case 'r': // Read in analog signal
if (auto count = std::min(static_cast<unsigned int>(cmd[1] | (cmd[2] << 8)), adc_samples.size()); count > 0) { if (usbd.read(&cmd[1], 2) < 2)
adc.getSamples(&adc_samples[0], count); break;
usbd.write(adc_samples.data(), count * sizeof(adcsample_t)); if (auto count = std::min(static_cast<unsigned int>(cmd[1] | (cmd[2] << 8)), adc_samples.size()); count > 0) {
} adc.getSamples(&adc_samples[0], count);
break; usbd.write(adc_samples.data(), count * sizeof(adcsample_t));
case 'i': // Identify ourself as an stmdsp device }
usbd.write("stmdsp", 6); break;
break; case 'i': // Identify ourself as an stmdsp device
default: usbd.write("stmdsp", 6);
break; break;
} default:
} break;
} }
}
chThdSleepMilliseconds(1); }
}
} chThdSleepMilliseconds(1);
}
}

Loading…
Cancel
Save