From 0452d2472c7a6a5b91f2a0129e43b3e0e18d1e76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4berich?= Date: Fri, 24 Sep 2021 22:21:38 +0200 Subject: [PATCH] Improve behaviour on full USB buffer --- Software/VNA_embedded/Application/Drivers/USB/usb.c | 5 +++++ Software/VNA_embedded/Application/Drivers/USB/usb.h | 2 +- Software/VNA_embedded/Application/Hardware.cpp | 1 + Software/VNA_embedded/Application/VNA.cpp | 12 +++++++++++- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Software/VNA_embedded/Application/Drivers/USB/usb.c b/Software/VNA_embedded/Application/Drivers/USB/usb.c index f1f2ff8..aab7c0a 100644 --- a/Software/VNA_embedded/Application/Drivers/USB/usb.c +++ b/Software/VNA_embedded/Application/Drivers/USB/usb.c @@ -284,3 +284,8 @@ void USB_LP_IRQHandler(void) uint16_t usb_available_buffer() { return sizeof(usb_transmit_fifo) - usb_transmit_fifo_level; } + +void usb_clear_buffer() { + usb_transmit_fifo_level = 0; + data_transmission_active = 0; +} diff --git a/Software/VNA_embedded/Application/Drivers/USB/usb.h b/Software/VNA_embedded/Application/Drivers/USB/usb.h index b8e1d14..db36221 100644 --- a/Software/VNA_embedded/Application/Drivers/USB/usb.h +++ b/Software/VNA_embedded/Application/Drivers/USB/usb.h @@ -21,7 +21,7 @@ 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); - +void usb_clear_buffer(); #ifdef __cplusplus } diff --git a/Software/VNA_embedded/Application/Hardware.cpp b/Software/VNA_embedded/Application/Hardware.cpp index 164784b..6a73a66 100644 --- a/Software/VNA_embedded/Application/Hardware.cpp +++ b/Software/VNA_embedded/Application/Hardware.cpp @@ -231,6 +231,7 @@ void HW::SetIdle() { FPGA::Enable(FPGA::Periphery::Port2Mixer, false); FPGA::Enable(FPGA::Periphery::RefMixer, false); FPGA::Enable(FPGA::Periphery::PortSwitch, false); + activeMode = Mode::Idle; } HW::AmplitudeSettings HW::GetAmplitudeSettings(int16_t cdbm, uint64_t freq, bool applyCorrections, bool port2) { diff --git a/Software/VNA_embedded/Application/VNA.cpp b/Software/VNA_embedded/Application/VNA.cpp index 98c74a0..9e0b4c3 100644 --- a/Software/VNA_embedded/Application/VNA.cpp +++ b/Software/VNA_embedded/Application/VNA.cpp @@ -408,7 +408,17 @@ void VNA::SweepHalted() { // 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); + uint32_t start = HAL_GetTick(); + while(usb_available_buffer() < reservedUSBbuffer) { + if(HAL_GetTick() - start > 100) { + // still no buffer space after some time, something more serious must have gone wrong + // -> abort sweep and return to idle + usb_clear_buffer(); + FPGA::AbortSweep(); + HW::SetIdle(); + return; + } + } FPGA::ResumeHaltedSweep(); }); }