firmware reorganize; added more sample rates

pull/3/head
Clyne 4 years ago
parent 1ade2969fb
commit 0fde1b98ee

@ -69,16 +69,15 @@ MainFrame::MainFrame() : wxFrame(nullptr, wxID_ANY, "stmdspgui", wxPoint(50, 50)
auto comp = new wxButton(panelToolbar, Id::MCodeCompile, "Compile"); auto comp = new wxButton(panelToolbar, Id::MCodeCompile, "Compile");
static const wxString srateValues[] = { static const wxString srateValues[] = {
"16 kS/s", "16 kS/s",
"20 kS/s",
"32 kS/s",
"48 kS/s", "48 kS/s",
"96 kS/s", "60 kS/s",
"100 kS/s", "96 kS/s"
"200 kS/s",
"1 MS/s",
"2 MS/s"
}; };
m_rate_select = new wxComboBox(panelToolbar, wxID_ANY, m_rate_select = new wxComboBox(panelToolbar, wxID_ANY,
wxEmptyString, wxDefaultPosition, wxDefaultSize, wxEmptyString, wxDefaultPosition, wxDefaultSize,
7, srateValues, wxCB_READONLY); 6, srateValues, wxCB_READONLY);
m_rate_select->Disable(); m_rate_select->Disable();
sizerToolbar->Add(comp, 0, wxLEFT, 4); sizerToolbar->Add(comp, 0, wxLEFT, 4);
@ -410,7 +409,6 @@ void MainFrame::onRunConnect(wxCommandEvent& ce)
delete m_device; delete m_device;
m_device = nullptr; m_device = nullptr;
m_rate_select->Disable();
menuItem->SetItemLabel("&Connect"); menuItem->SetItemLabel("&Connect");
m_status_bar->SetStatusText("Failed to connect."); m_status_bar->SetStatusText("Failed to connect.");
} }
@ -420,6 +418,7 @@ void MainFrame::onRunConnect(wxCommandEvent& ce)
} else { } else {
delete m_device; delete m_device;
m_device = nullptr; m_device = nullptr;
m_rate_select->Disable();
menuItem->SetItemLabel("&Connect"); menuItem->SetItemLabel("&Connect");
m_status_bar->SetStatusText("Disconnected."); m_status_bar->SetStatusText("Disconnected.");
} }

@ -11,19 +11,17 @@
#include "adc.hpp" #include "adc.hpp"
constexpr static const auto adcd = &ADCD1; ADCDriver *ADC::m_driver = &ADCD1;
constexpr static const auto gptd = &GPTD6; GPTDriver *ADC::m_timer = &GPTD6;
constexpr static const ADCConfig adc_config = { const ADCConfig ADC::m_config = {
.difsel = 0 .difsel = 0
}; };
static void adc_read_callback(ADCDriver *); ADCConversionGroup ADC::m_group_config = {
.circular = true,
static ADCConversionGroup adc_group_config = {
.circular = false,
.num_channels = 1, .num_channels = 1,
.end_cb = adc_read_callback, .end_cb = ADC::conversionCallback,
.error_cb = nullptr, .error_cb = nullptr,
.cfgr = ADC_CFGR_EXTEN_RISING | ADC_CFGR_EXTSEL_SRC(13), /* TIM4_TRGO */ .cfgr = ADC_CFGR_EXTEN_RISING | ADC_CFGR_EXTSEL_SRC(13), /* TIM4_TRGO */
.cfgr2 = 0,//ADC_CFGR2_ROVSE | ADC_CFGR2_OVSR_0 | ADC_CFGR2_OVSS_1, // Oversampling 2x .cfgr2 = 0,//ADC_CFGR2_ROVSE | ADC_CFGR2_OVSR_0 | ADC_CFGR2_OVSS_1, // Oversampling 2x
@ -37,132 +35,111 @@ static ADCConversionGroup adc_group_config = {
} }
}; };
constexpr static const GPTConfig gpt_config = { const GPTConfig ADC::m_timer_config = {
.frequency = 36000000, .frequency = 36000000,
.callback = nullptr, .callback = nullptr,
.cr2 = TIM_CR2_MMS_1, /* TRGO */ .cr2 = TIM_CR2_MMS_1, /* TRGO */
.dier = 0 .dier = 0
}; };
#define ADC_CCR_PRESC_DIV1 (0) std::array<std::array<uint32_t, 4>, 6> ADC::m_rate_presets = {{
#define ADC_SAMPLE_RATE_SETTINGS_COUNT (7)
static uint32_t adc_sample_rate_settings[] = {
// Rate PLLSAI2N ADC_PRESC ADC_SMPR GPT_DIV // Rate PLLSAI2N ADC_PRESC ADC_SMPR GPT_DIV
/* 16k */ 8, ADC_CCR_PRESC_DIV10, ADC_SMPR_SMP_12P5, 2250, {/* 16k */ 8, ADC_CCR_PRESC_DIV10, ADC_SMPR_SMP_12P5, 2250},
/* 48k */ 24, ADC_CCR_PRESC_DIV10, ADC_SMPR_SMP_12P5, 750, {/* 20k */ 10, ADC_CCR_PRESC_DIV10, ADC_SMPR_SMP_12P5, 1800},
/* 96k */ 48, ADC_CCR_PRESC_DIV10, ADC_SMPR_SMP_12P5, 375, {/* 32k */ 16, ADC_CCR_PRESC_DIV10, ADC_SMPR_SMP_12P5, 1125},
/* 100k */ 40, ADC_CCR_PRESC_DIV8, ADC_SMPR_SMP_12P5, 360, {/* 48k */ 24, ADC_CCR_PRESC_DIV10, ADC_SMPR_SMP_12P5, 750},
/* 400k */ 40, ADC_CCR_PRESC_DIV2, ADC_SMPR_SMP_12P5, 90, {/* 60k */ 30, ADC_CCR_PRESC_DIV10, ADC_SMPR_SMP_12P5, 600},
/* 1M */ 38, ADC_CCR_PRESC_DIV1, ADC_SMPR_SMP_6P5, 36, {/* 96k */ 48, ADC_CCR_PRESC_DIV10, ADC_SMPR_SMP_12P5, 375}
/* 2M */ 76, ADC_CCR_PRESC_DIV1, ADC_SMPR_SMP_6P5, 18 }};
};
adcsample_t *ADC::m_current_buffer = nullptr;
size_t ADC::m_current_buffer_size = 0;
ADC::Operation ADC::m_operation = nullptr;
static bool adc_is_read_finished = false; unsigned int ADC::m_timer_divisor = 2;
static adcsample_t *adc_current_buffer = nullptr;
static size_t adc_current_buffer_size = 0;
static adc::operation_t adc_operation_func = nullptr;
static unsigned int adc_gpt_divisor = 1;
namespace adc void ADC::begin()
{ {
void init()
{
palSetPadMode(GPIOA, 0, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOA, 0, PAL_MODE_INPUT_ANALOG);
gptStart(gptd, &gpt_config); adcStart(m_driver, &m_config);
adcStart(adcd, &adc_config); adcSTM32EnableVREF(m_driver);
adcSTM32EnableVREF(adcd); gptStart(m_timer, &m_timer_config);
set_rate(rate::R96K); setRate(Rate::R96K);
} }
void set_rate(rate new_rate) void ADC::start(adcsample_t *buffer, size_t count, Operation operation)
{ {
auto index = static_cast<unsigned int>(new_rate); m_current_buffer = buffer;
auto plln = adc_sample_rate_settings[index * 4] << RCC_PLLSAI2CFGR_PLLSAI2N_Pos; m_current_buffer_size = count;
auto presc = adc_sample_rate_settings[index * 4 + 1] << ADC_CCR_PRESC_Pos; m_operation = operation;
auto smp = adc_sample_rate_settings[index * 4 + 2];
adc_gpt_divisor = adc_sample_rate_settings[index * 4 + 3]; adcStartConversion(m_driver, &m_group_config, buffer, count);
gptStartContinuous(m_timer, m_timer_divisor);
}
adcStop(adcd); void ADC::stop()
{
gptStopTimer(m_timer);
adcStopConversion(m_driver);
m_current_buffer = nullptr;
m_current_buffer_size = 0;
m_operation = nullptr;
}
void ADC::setRate(ADC::Rate rate)
{
auto& preset = m_rate_presets[static_cast<int>(rate)];
auto plln = preset[0] << RCC_PLLSAI2CFGR_PLLSAI2N_Pos;
auto presc = preset[1] << ADC_CCR_PRESC_Pos;
auto smp = preset[2];
m_timer_divisor = preset[3];
adcStop(m_driver);
// Adjust PLLSAI2 // Adjust PLLSAI2
RCC->CR &= ~(RCC_CR_PLLSAI2ON); RCC->CR &= ~(RCC_CR_PLLSAI2ON);
while ((RCC->CR & RCC_CR_PLLSAI2RDY) == RCC_CR_PLLSAI2RDY); while ((RCC->CR & RCC_CR_PLLSAI2RDY) == RCC_CR_PLLSAI2RDY);
RCC->PLLSAI2CFGR = (RCC->PLLSAI2CFGR & ~(RCC_PLLSAI2CFGR_PLLSAI2N_Msk)) | plln; RCC->PLLSAI2CFGR = (RCC->PLLSAI2CFGR & ~(RCC_PLLSAI2CFGR_PLLSAI2N_Msk)) | plln;
RCC->CR |= RCC_CR_PLLSAI2ON; RCC->CR |= RCC_CR_PLLSAI2ON;
// Set ADC prescaler // Set ADC prescaler
adcd->adcc->CCR = (adcd->adcc->CCR & ~(ADC_CCR_PRESC_Msk)) | presc; m_driver->adcc->CCR = (m_driver->adcc->CCR & ~(ADC_CCR_PRESC_Msk)) | presc;
// Set sampling time // Set sampling time
adc_group_config.smpr[0] = ADC_SMPR1_SMP_AN5(smp); m_group_config.smpr[0] = ADC_SMPR1_SMP_AN5(smp);
adcStart(adcd, &adc_config);
}
unsigned int get_rate() adcStart(m_driver, &m_config);
{ }
for (unsigned int i = 0; i < ADC_SAMPLE_RATE_SETTINGS_COUNT; i++) {
if (adc_gpt_divisor == adc_sample_rate_settings[i * 3 + 3])
return i;
}
return 0xFF;
}
unsigned int get_gpt_divisor()
{
return adc_gpt_divisor;
}
adcsample_t *read(adcsample_t *buffer, size_t count) void ADC::setOperation(ADC::Operation operation)
{ {
adc_is_read_finished = false; m_operation = operation;
adc_group_config.circular = false; }
adcStartConversion(adcd, &adc_group_config, buffer, count);
gptStartContinuous(gptd, adc_gpt_divisor);
while (!adc_is_read_finished);
return buffer;
}
void read_start(operation_t operation_func, adcsample_t *buffer, size_t count) int ADC::getRate()
{ {
adc_current_buffer = buffer; for (unsigned int i = 0; i < m_rate_presets.size(); i++) {
adc_current_buffer_size = count; if (m_timer_divisor == m_rate_presets[i][3])
adc_operation_func = operation_func; return i;
adc_group_config.circular = true;
adcStartConversion(adcd, &adc_group_config, buffer, count);
gptStartContinuous(gptd, adc_gpt_divisor);
} }
void read_set_operation_func(operation_t operation_func) return -1;
{ }
adc_operation_func = operation_func;
}
void read_stop() unsigned int ADC::getTimerDivisor()
{ {
gptStopTimer(gptd); return m_timer_divisor;
adcStopConversion(adcd);
adc_group_config.circular = false;
adc_current_buffer = nullptr;
adc_current_buffer_size = 0;
adc_operation_func = nullptr;
}
} }
void adc_read_callback(ADCDriver *driver) void ADC::conversionCallback(ADCDriver *driver)
{ {
if (adc_group_config.circular) { if (m_operation != nullptr) {
if (adc_operation_func != nullptr) { auto half_size = m_current_buffer_size / 2;
auto half_size = adc_current_buffer_size / 2;
if (adcIsBufferComplete(driver)) if (adcIsBufferComplete(driver))
adc_operation_func(adc_current_buffer + half_size, half_size); m_operation(m_current_buffer + half_size, half_size);
else else
adc_operation_func(adc_current_buffer, half_size); m_operation(m_current_buffer, half_size);
}
} else {
gptStopTimer(gptd);
adc_is_read_finished = true;
} }
} }

@ -14,29 +14,52 @@
#include "hal.h" #include "hal.h"
namespace adc #include <array>
class ADC
{ {
using operation_t = void (*)(adcsample_t *buffer, size_t count); public:
using Operation = void (*)(adcsample_t *buffer, size_t count);
enum class rate : unsigned int { enum class Rate : int {
//R8K = 0,
R16K = 0, R16K = 0,
R20K,
R32K,
R48K, R48K,
R96K, R60K,
R100K, R96K
R400K,
R1M,
R2M
}; };
void init(); static void begin();
void set_rate(rate new_rate);
unsigned int get_rate(); static void start(adcsample_t *buffer, size_t count, Operation operation);
unsigned int get_gpt_divisor(); static void stop();
adcsample_t *read(adcsample_t *buffer, size_t count);
void read_start(operation_t operation_func, adcsample_t *buffer, size_t count); static void setRate(Rate rate);
void read_set_operation_func(operation_t operation_func); static void setOperation(Operation operation);
void read_stop();
} static int getRate();
static unsigned int getTimerDivisor();
private:
static ADCDriver *m_driver;
static GPTDriver *m_timer;
static const ADCConfig m_config;
static /*const*/ ADCConversionGroup m_group_config;
static const GPTConfig m_timer_config;
static std::array<std::array<uint32_t, 4>, 6> m_rate_presets;
static adcsample_t *m_current_buffer;
static size_t m_current_buffer_size;
static Operation m_operation;
static unsigned int m_timer_divisor;
static void conversionCallback(ADCDriver *);
};
#endif // STMDSP_ADC_HPP_ #endif // STMDSP_ADC_HPP_

@ -9,67 +9,63 @@
* If not, see <https://www.gnu.org/licenses/>. * If not, see <https://www.gnu.org/licenses/>.
*/ */
#include "adc.hpp" #include "adc.hpp" // ADC::getTimerDivisor
#include "dac.hpp" #include "dac.hpp"
constexpr static const auto dacd = &DACD1; DACDriver *DAC::m_driver[2] = {
constexpr static const auto dacd2 = &DACD2; &DACD1, &DACD2
constexpr static const auto gptd = &GPTD7; };
GPTDriver *DAC::m_timer = &GPTD7;
int DAC::m_timer_user_count = 0;
constexpr static const DACConfig dac_config = { const DACConfig DAC::m_config = {
.init = 0, .init = 0,
.datamode = DAC_DHRM_12BIT_RIGHT, .datamode = DAC_DHRM_12BIT_RIGHT,
.cr = 0 .cr = 0
}; };
constexpr static const DACConversionGroup dac_group_config = { const DACConversionGroup DAC::m_group_config = {
.num_channels = 1, .num_channels = 1,
.end_cb = nullptr, .end_cb = nullptr,
.error_cb = nullptr, .error_cb = nullptr,
.trigger = DAC_TRG(2) .trigger = DAC_TRG(2)
}; };
constexpr static const GPTConfig gpt_config = { const GPTConfig DAC::m_timer_config = {
.frequency = 36000000, .frequency = 36000000,
.callback = nullptr, .callback = nullptr,
.cr2 = TIM_CR2_MMS_1, /* TRGO */ .cr2 = TIM_CR2_MMS_1, /* TRGO */
.dier = 0 .dier = 0
}; };
static unsigned int dacs_running = 0; void DAC::begin()
namespace dac
{ {
void init()
{
palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
dacStart(dacd, &dac_config); dacStart(m_driver[0], &m_config);
dacStart(dacd2, &dac_config); dacStart(m_driver[1], &m_config);
gptStart(gptd, &gpt_config); gptStart(m_timer, &m_timer_config);
} }
void write_start(unsigned int channel, dacsample_t *buffer, size_t count) void DAC::start(int channel, dacsample_t *buffer, size_t count)
{ {
if (channel < 2) { if (channel >= 0 && channel < 2) {
dacStartConversion(channel == 0 ? dacd : dacd2, &dac_group_config, buffer, count); dacStartConversion(m_driver[channel], &m_group_config, buffer, count);
if (dacs_running == 0) if (m_timer_user_count == 0)
gptStartContinuous(gptd, adc::get_gpt_divisor()); gptStartContinuous(m_timer, ADC::getTimerDivisor());
dacs_running |= 1 << channel; m_timer_user_count++;
}
} }
}
void write_stop(unsigned int channel) void DAC::stop(int channel)
{ {
if (channel < 2) { if (channel >= 0 && channel < 2) {
dacStopConversion(channel == 0 ? dacd : dacd2); dacStopConversion(m_driver[channel]);
dacs_running &= ~(1 << channel); if (--m_timer_user_count == 0)
if (dacs_running == 0) gptStopTimer(m_timer);
gptStopTimer(gptd);
}
} }
} }

@ -13,13 +13,25 @@
#define STMDSP_DAC_HPP_ #define STMDSP_DAC_HPP_
#include "hal.h" #include "hal.h"
#undef DAC
namespace dac class DAC
{ {
void init(); public:
void write_start(unsigned int channel, dacsample_t *buffer, size_t count); static void begin();
void write_stop(unsigned int channel);
} static void start(int channel, dacsample_t *buffer, size_t count);
static void stop(int channel);
private:
static DACDriver *m_driver[2];
static GPTDriver *m_timer;
static int m_timer_user_count;
static const DACConfig m_config;
static const DACConversionGroup m_group_config;
static const GPTConfig m_timer_config;
};
#endif // STMDSP_DAC_HPP_ #endif // STMDSP_DAC_HPP_

@ -23,9 +23,9 @@ constexpr static auto ptr_from_offset(void *base, uint32_t offset)
return reinterpret_cast<T>(reinterpret_cast<uint8_t *>(base) + offset); return reinterpret_cast<T>(reinterpret_cast<uint8_t *>(base) + offset);
} }
namespace elf { namespace ELF {
entry_t load(void *elf_data) Entry load(void *elf_data)
{ {
// Check the ELF's header signature // Check the ELF's header signature
auto ehdr = reinterpret_cast<Elf32_Ehdr *>(elf_data); auto ehdr = reinterpret_cast<Elf32_Ehdr *>(elf_data);
@ -54,8 +54,8 @@ entry_t load(void *elf_data)
} }
return loaded ? reinterpret_cast<entry_t>(ehdr->e_entry) : nullptr; return loaded ? reinterpret_cast<Entry>(ehdr->e_entry) : nullptr;
} }
} // namespace elf } // namespace ELF

@ -15,11 +15,11 @@
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
namespace elf namespace ELF
{ {
using entry_t = uint16_t *(*)(uint16_t *, size_t); using Entry = uint16_t *(*)(uint16_t *, size_t);
entry_t load(void *elf_data); Entry load(void *elf_data);
} }
#endif // ELF_LOAD_HPP_ #endif // ELF_LOAD_HPP_

@ -22,7 +22,7 @@
constexpr unsigned int MAX_ELF_FILE_SIZE = 8 * 1024; constexpr unsigned int MAX_ELF_FILE_SIZE = 8 * 1024;
constexpr unsigned int MAX_ERROR_QUEUE_SIZE = 8; constexpr unsigned int MAX_ERROR_QUEUE_SIZE = 8;
constexpr unsigned int MAX_SAMPLE_BUFFER_SIZE = 6000; // operate on buffers size this / 2 constexpr unsigned int MAX_SAMPLE_BUFFER_SIZE = 6000; // operate on buffers size this / 2
constexpr unsigned int MAX_SIGGEN_BUFFER_SIZE = 3000; constexpr unsigned int MAX_SIGGEN_BUFFER_SIZE = MAX_SAMPLE_BUFFER_SIZE / 2;
enum class RunStatus : char enum class RunStatus : char
{ {
@ -84,8 +84,8 @@ CC_ALIGN(CACHE_LINE_SIZE)
#endif #endif
static std::array<dacsample_t, CACHE_SIZE_ALIGN(dacsample_t, MAX_SIGGEN_BUFFER_SIZE)> dac2_samples; static std::array<dacsample_t, CACHE_SIZE_ALIGN(dacsample_t, MAX_SIGGEN_BUFFER_SIZE)> dac2_samples;
static uint8_t elf_file_store[MAX_ELF_FILE_SIZE]; static unsigned char elf_file_store[MAX_ELF_FILE_SIZE];
static elf::entry_t elf_entry = nullptr; static ELF::Entry elf_entry = nullptr;
static void signal_operate(adcsample_t *buffer, size_t count); static void signal_operate(adcsample_t *buffer, size_t count);
static void signal_operate_measure(adcsample_t *buffer, size_t count); static void signal_operate_measure(adcsample_t *buffer, size_t count);
@ -104,9 +104,9 @@ int main()
palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL); palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL);
palClearPad(GPIOA, 5); palClearPad(GPIOA, 5);
adc::init(); ADC::begin();
dac::init(); DAC::begin();
usbserial::init(); USBSerial::begin();
// Start the conversion manager thread // Start the conversion manager thread
chTMObjectInit(&conversion_time_measurement); chTMObjectInit(&conversion_time_measurement);
@ -125,22 +125,24 @@ void main_loop()
{ {
while (1) { while (1) {
if (usbserial::is_active()) { if (USBSerial::isActive()) {
// Attempt to receive a command packet // Attempt to receive a command packet
if (char cmd[3]; usbserial::read(&cmd, 1) > 0) { if (unsigned char cmd[3]; USBSerial::read(&cmd[0], 1) > 0) {
// Packet received, first byte represents the desired command/action // Packet received, first byte represents the desired command/action
switch (cmd[0]) { switch (cmd[0]) {
case 'a': case 'a':
usbserial::write(adc_samples.data(), adc_sample_count * sizeof(adcsample_t)); USBSerial::write((uint8_t *)adc_samples.data(),
adc_sample_count * sizeof(adcsample_t));
break; break;
case 'A': case 'A':
usbserial::read(&adc_samples[0], adc_sample_count * sizeof(adcsample_t)); USBSerial::read((uint8_t *)&adc_samples[0],
adc_sample_count * sizeof(adcsample_t));
break; break;
case 'B': case 'B':
if (run_status == RunStatus::Idle) { if (run_status == RunStatus::Idle) {
if (usbserial::read(&cmd[1], 2) == 2) { if (USBSerial::read(&cmd[1], 2) == 2) {
unsigned int count = cmd[1] | (cmd[2] << 8); unsigned int count = cmd[1] | (cmd[2] << 8);
if (count <= MAX_SAMPLE_BUFFER_SIZE / 2) { if (count <= MAX_SAMPLE_BUFFER_SIZE / 2) {
adc_sample_count = count * 2; adc_sample_count = count * 2;
@ -157,14 +159,16 @@ void main_loop()
break; break;
case 'd': case 'd':
usbserial::write(dac_samples.data(), dac_sample_count * sizeof(dacsample_t)); USBSerial::write((uint8_t *)dac_samples.data(),
dac_sample_count * sizeof(dacsample_t));
break; break;
case 'D': case 'D':
if (usbserial::read(&cmd[1], 2) == 2) { if (USBSerial::read(&cmd[1], 2) == 2) {
unsigned int count = cmd[1] | (cmd[2] << 8); unsigned int count = cmd[1] | (cmd[2] << 8);
if (count <= MAX_SIGGEN_BUFFER_SIZE) { if (count <= MAX_SIGGEN_BUFFER_SIZE) {
dac2_sample_count = count; dac2_sample_count = count;
usbserial::read(&dac2_samples[0], dac2_sample_count * sizeof(dacsample_t)); USBSerial::read((uint8_t *)&dac2_samples[0],
dac2_sample_count * sizeof(dacsample_t));
} else { } else {
error_queue_add(Error::BadParam); error_queue_add(Error::BadParam);
} }
@ -176,12 +180,12 @@ void main_loop()
// 'E' - Reads in and loads the compiled conversion code binary from USB. // 'E' - Reads in and loads the compiled conversion code binary from USB.
case 'E': case 'E':
if (run_status == RunStatus::Idle) { if (run_status == RunStatus::Idle) {
if (usbserial::read(&cmd[1], 2) == 2) { if (USBSerial::read(&cmd[1], 2) == 2) {
// Only load the binary if it can fit in the memory reserved for it. // Only load the binary if it can fit in the memory reserved for it.
unsigned int size = cmd[1] | (cmd[2] << 8); unsigned int size = cmd[1] | (cmd[2] << 8);
if (size < sizeof(elf_file_store)) { if (size < sizeof(elf_file_store)) {
usbserial::read(elf_file_store, size); USBSerial::read(elf_file_store, size);
elf_entry = elf::load(elf_file_store); elf_entry = ELF::load(elf_file_store);
if (elf_entry == nullptr) if (elf_entry == nullptr)
error_queue_add(Error::BadUserCodeLoad); error_queue_add(Error::BadUserCodeLoad);
@ -203,17 +207,17 @@ void main_loop()
// 'i' - Sends an identifying string to confirm that this is the stmdsp device. // 'i' - Sends an identifying string to confirm that this is the stmdsp device.
case 'i': case 'i':
usbserial::write("stmdsp", 6); USBSerial::write((uint8_t *)"stmdsp", 6);
break; break;
// 'I' - Sends the current run status. // 'I' - Sends the current run status.
case 'I': case 'I':
{ {
char buf[2] = { unsigned char buf[2] = {
static_cast<char>(run_status), static_cast<unsigned char>(run_status),
static_cast<char>(error_queue_pop()) static_cast<unsigned char>(error_queue_pop())
}; };
usbserial::write(buf, sizeof(buf)); USBSerial::write(buf, sizeof(buf));
} }
break; break;
@ -223,8 +227,8 @@ void main_loop()
if (run_status == RunStatus::Idle) { if (run_status == RunStatus::Idle) {
run_status = RunStatus::Running; run_status = RunStatus::Running;
dac_samples.fill(0); dac_samples.fill(0);
adc::read_start(signal_operate_measure, &adc_samples[0], adc_sample_count); ADC::start(&adc_samples[0], adc_sample_count, signal_operate_measure);
dac::write_start(0, &dac_samples[0], dac_sample_count); DAC::start(0, &dac_samples[0], dac_sample_count);
} else { } else {
error_queue_add(Error::NotIdle); error_queue_add(Error::NotIdle);
} }
@ -233,7 +237,7 @@ void main_loop()
// 'm' - Returns the last measured sample processing time, presumably in processor // 'm' - Returns the last measured sample processing time, presumably in processor
// ticks. // ticks.
case 'm': case 'm':
usbserial::write(&conversion_time_measurement.last, sizeof(rtcnt_t)); USBSerial::write((uint8_t *)&conversion_time_measurement.last, sizeof(rtcnt_t));
break; break;
// 'R' - Begin continuous sampling/conversion of the ADC. Samples will go through // 'R' - Begin continuous sampling/conversion of the ADC. Samples will go through
@ -242,20 +246,20 @@ void main_loop()
if (run_status == RunStatus::Idle) { if (run_status == RunStatus::Idle) {
run_status = RunStatus::Running; run_status = RunStatus::Running;
dac_samples.fill(0); dac_samples.fill(0);
adc::read_start(signal_operate, &adc_samples[0], adc_sample_count); ADC::start(&adc_samples[0], adc_sample_count, signal_operate);
dac::write_start(0, &dac_samples[0], dac_sample_count); DAC::start(0, &dac_samples[0], dac_sample_count);
} else { } else {
error_queue_add(Error::NotIdle); error_queue_add(Error::NotIdle);
} }
break; break;
case 'r': case 'r':
if (usbserial::read(&cmd[1], 1) == 1) { if (USBSerial::read(&cmd[1], 1) == 1) {
if (cmd[1] == 0xFF) { if (cmd[1] == 0xFF) {
unsigned char r = static_cast<unsigned char>(adc::get_rate()); unsigned char r = static_cast<unsigned char>(ADC::getRate());
usbserial::write(&r, 1); USBSerial::write(&r, 1);
} else { } else {
adc::set_rate(static_cast<adc::rate>(cmd[1])); ADC::setRate(static_cast<ADC::Rate>(cmd[1]));
} }
} else { } else {
error_queue_add(Error::BadParamSize); error_queue_add(Error::BadParamSize);
@ -265,8 +269,8 @@ void main_loop()
// 'S' - Stops the continuous sampling/conversion. // 'S' - Stops the continuous sampling/conversion.
case 'S': case 'S':
if (run_status == RunStatus::Running) { if (run_status == RunStatus::Running) {
dac::write_stop(0); DAC::stop(0);
adc::read_stop(); ADC::stop();
run_status = RunStatus::Idle; run_status = RunStatus::Idle;
} }
break; break;
@ -281,28 +285,28 @@ void main_loop()
static_cast<unsigned char>(dac_sample_count / 2 & 0xFF), static_cast<unsigned char>(dac_sample_count / 2 & 0xFF),
static_cast<unsigned char>(((dac_sample_count / 2) >> 8) & 0xFF) static_cast<unsigned char>(((dac_sample_count / 2) >> 8) & 0xFF)
}; };
usbserial::write(buf, 2); USBSerial::write(buf, 2);
unsigned int total = dac_sample_count / 2 * sizeof(dacsample_t); unsigned int total = dac_sample_count / 2 * sizeof(dacsample_t);
unsigned int offset = 0; unsigned int offset = 0;
unsigned char unused; unsigned char unused;
while (total > 512) { while (total > 512) {
usbserial::write(samps + offset, 512); USBSerial::write(samps + offset, 512);
while (usbserial::read(&unused, 1) == 0); while (USBSerial::read(&unused, 1) == 0);
offset += 512; offset += 512;
total -= 512; total -= 512;
} }
usbserial::write(samps + offset, total); USBSerial::write(samps + offset, total);
while (usbserial::read(&unused, 1) == 0); while (USBSerial::read(&unused, 1) == 0);
} else { } else {
usbserial::write("\0\0", 2); USBSerial::write((uint8_t *)"\0\0", 2);
} }
break; break;
case 'W': case 'W':
dac::write_start(1, &dac2_samples[0], dac2_sample_count); DAC::start(1, &dac2_samples[0], dac2_sample_count);
break; break;
case 'w': case 'w':
dac::write_stop(1); DAC::stop(1);
break; break;
default: default:
@ -318,8 +322,8 @@ void main_loop()
void conversion_abort() void conversion_abort()
{ {
elf_entry = nullptr; elf_entry = nullptr;
dac::write_stop(0); DAC::stop(0);
adc::read_stop(); ADC::stop();
error_queue_add(Error::ConversionAborted); error_queue_add(Error::ConversionAborted);
} }
@ -380,7 +384,7 @@ void signal_operate(adcsample_t *buffer, [[maybe_unused]] size_t count)
void signal_operate_measure(adcsample_t *buffer, [[maybe_unused]] size_t count) void signal_operate_measure(adcsample_t *buffer, [[maybe_unused]] size_t count)
{ {
chMBPostI(&conversionMB, buffer == &adc_samples[0] ? MSG_CONVFIRST_MEASURE : MSG_CONVSECOND_MEASURE); chMBPostI(&conversionMB, buffer == &adc_samples[0] ? MSG_CONVFIRST_MEASURE : MSG_CONVSECOND_MEASURE);
adc::read_set_operation_func(signal_operate); ADC::setOperation(signal_operate);
} }
extern "C" { extern "C" {

@ -11,42 +11,42 @@
#include "usbserial.hpp" #include "usbserial.hpp"
#include "usbcfg.h" SerialUSBDriver *USBSerial::m_driver = &SDU1;
constexpr static const auto sdud = &SDU1; void USBSerial::begin()
namespace usbserial
{ {
void init()
{
palSetPadMode(GPIOA, 11, PAL_MODE_ALTERNATE(10)); palSetPadMode(GPIOA, 11, PAL_MODE_ALTERNATE(10));
palSetPadMode(GPIOA, 12, PAL_MODE_ALTERNATE(10)); palSetPadMode(GPIOA, 12, PAL_MODE_ALTERNATE(10));
sduObjectInit(sdud); sduObjectInit(m_driver);
sduStart(sdud, &serusbcfg); sduStart(m_driver, &serusbcfg);
// Reconnect bus so device can re-enumerate on reset // Reconnect bus so device can re-enumerate on reset
usbDisconnectBus(serusbcfg.usbp); usbDisconnectBus(serusbcfg.usbp);
chThdSleepMilliseconds(1500); chThdSleepMilliseconds(1500);
usbStart(serusbcfg.usbp, &usbcfg); usbStart(serusbcfg.usbp, &usbcfg);
usbConnectBus(serusbcfg.usbp); usbConnectBus(serusbcfg.usbp);
} }
bool is_active() bool USBSerial::isActive()
{ {
return sdud->config->usbp->state == USB_ACTIVE; if (auto config = m_driver->config; config != nullptr) {
if (auto usbp = config->usbp; usbp != nullptr)
return usbp->state == USB_ACTIVE;
} }
size_t read(void *buffer, size_t count) return false;
{ }
auto bss = reinterpret_cast<BaseSequentialStream *>(sdud);
return streamRead(bss, static_cast<uint8_t *>(buffer), count);
}
size_t write(const void *buffer, size_t count) size_t USBSerial::read(unsigned char *buffer, size_t count)
{ {
auto bss = reinterpret_cast<BaseSequentialStream *>(sdud); auto bss = reinterpret_cast<BaseSequentialStream *>(m_driver);
return streamWrite(bss, static_cast<const uint8_t *>(buffer), count); return streamRead(bss, buffer, count);
} }
size_t USBSerial::write(const unsigned char *buffer, size_t count)
{
auto bss = reinterpret_cast<BaseSequentialStream *>(m_driver);
return streamWrite(bss, buffer, count);
} }

@ -12,16 +12,21 @@
#ifndef STMDSP_USBSERIAL_HPP_ #ifndef STMDSP_USBSERIAL_HPP_
#define STMDSP_USBSERIAL_HPP_ #define STMDSP_USBSERIAL_HPP_
#include "hal.h" #include "usbcfg.h"
namespace usbserial class USBSerial
{ {
void init(); public:
bool is_active(); static void begin();
size_t read(void *buffer, size_t count); static bool isActive();
size_t write(const void *buffer, size_t count);
} static size_t read(unsigned char *buffer, size_t count);
static size_t write(const unsigned char *buffer, size_t count);
private:
static SerialUSBDriver *m_driver;
};
#endif // STMDSP_USBSERIAL_HPP_ #endif // STMDSP_USBSERIAL_HPP_

Loading…
Cancel
Save