Halt sweep when USB buffer full

This commit is contained in:
Jan Käberich 2021-01-09 21:21:47 +01:00
parent 0f758584bf
commit 51806b936c
3 changed files with 36 additions and 2 deletions

View File

@ -223,7 +223,7 @@ void usb_init(usbd_recv_callback_t receive_callback) {
}
bool usb_transmit(const uint8_t *data, uint16_t length) {
// attempt to add data to fifo
if(usb_transmit_fifo_level + length > sizeof(usb_transmit_fifo)) {
if(length > usb_available_buffer()) {
// data won't fit, abort
return false;
}
@ -280,3 +280,7 @@ void USB_LP_IRQHandler(void)
{
HAL_PCD_IRQHandler(&hpcd_USB_FS);
}
uint16_t usb_available_buffer() {
return sizeof(usb_transmit_fifo) - usb_transmit_fifo_level;
}

View File

@ -19,6 +19,7 @@ typedef void(*usbd_recv_callback_t)(const uint8_t *buf, uint16_t len);
void usb_init(usbd_recv_callback_t receive_callback);
bool usb_transmit(const uint8_t *data, uint16_t length);
uint16_t usb_available_buffer();
void usb_log(const char *log, uint16_t length);

View File

@ -12,6 +12,7 @@
#include "FreeRTOS.h"
#include "task.h"
#include "Util.hpp"
#include "usb.h"
#define LOG_LEVEL LOG_LEVEL_INFO
#define LOG_MODULE "VNA"
@ -44,6 +45,10 @@ static_assert(alternativePrescaler * alternativeSamplerate == 102400000UL, "alte
static constexpr uint16_t alternativePhaseInc = 4096 * HW::IF2 / alternativeSamplerate;
static_assert(alternativePhaseInc * alternativeSamplerate == 4096 * HW::IF2, "DFT can not be computed for 2.IF when using alternative samplerate");
// Constants for USB buffer overflow prevention
static constexpr uint16_t maxPointsBetweenHalts = 40;
static constexpr uint32_t reservedUSBbuffer = maxPointsBetweenHalts * (sizeof(Protocol::Datapoint) + 8 /*USB packet overhead*/);
using namespace HWHAL;
bool VNA::Setup(Protocol::SweepSettings s) {
@ -106,6 +111,8 @@ bool VNA::Setup(Protocol::SweepSettings s) {
// invalidate first entry of IFTable, preventing switing of 2.LO in halted callback
IFTable[0].pointCnt = 0xFFFF;
uint16_t pointsWithoutHalt = 0;
// Transfer PLL configuration to FPGA
for (uint16_t i = 0; i < points; i++) {
bool harmonic_mixing = false;
@ -187,6 +194,17 @@ bool VNA::Setup(Protocol::SweepSettings s) {
IFdeviation, (uint32_t ) (freq / 1000000), (uint32_t ) (freq % 1000000));
}
// halt on regular intervals to prevent USB buffer overflow
if(!needs_halt) {
pointsWithoutHalt++;
if(pointsWithoutHalt > maxPointsBetweenHalts) {
needs_halt = true;
}
}
if(needs_halt) {
pointsWithoutHalt = 0;
}
FPGA::WriteSweepConfig(i, lowband, Source.GetRegisters(),
LO1.GetRegisters(), attenuator, freq, FPGA::SettlingTime::us20,
FPGA::Samples::SPPRegister, needs_halt);
@ -350,7 +368,18 @@ void VNA::SweepHalted() {
adcShifted = false;
}
FPGA::ResumeHaltedSweep();
if(usb_available_buffer() >= reservedUSBbuffer) {
// enough space available, can resume immediately
FPGA::ResumeHaltedSweep();
} else {
// USB buffer could potentially overflow before next halted point, wait until more space is available.
// This function is called from a low level interrupt, need to dispatch to lower priority to allow USB
// handling to continue
STM::DispatchToInterrupt([](){
while(usb_available_buffer() < reservedUSBbuffer);
FPGA::ResumeHaltedSweep();
});
}
}
void VNA::Stop() {