lf read: fix realtime sample streaming over USB
A realtime `lf read` streams samples straight to the host at the LF sample rate, around 125 kB/s. If the host stopped draining for even a moment, async_usb_write_requestWrite() returned false, and ReadLF_realtime() treated that as fatal: it returned PM3_EIO through a goto that also skipped async_usb_write_stop(). The IN endpoint was left busy, every later usb_write() then returned PM3_EIO, and the device went silent until it was physically replugged. A USB bus reset does not clear it -- EP0 keeps working, descriptors read fine, only bulk IN is dead. That is what a long `lf read` looked like from the client: a transfer that stopped early, and then a device that would not answer the next command. A busy endpoint is back pressure, not an error. The sampling loop now waits for the host, up to 100ms, and only gives up if it never comes back. Every exit path closes the async write. The spins inside the USB helpers are bounded and drop the stuck packet instead of turning into an infinite loop, which is the other half of why the device never recovered. Measured against a reader deliberately held at 41 kB/s: before, the stream died at 212238 of 300000 bytes and the unit needed a replug; after, 300000 of 300000 and it still answers. The last packet of a stream was being lost as well. The host's CDC read buffer is two max sized packets, and a bulk IN transfer only completes on a short packet or a full buffer, so a stream ending on a full 64 byte packet was left sitting in a half filled buffer. It showed as exact parity on the packet count: 513 packets requested -> 512 delivered 514 -> 514 515 packets requested -> 514 delivered 516 -> 516 async_usb_write_stop() now always sends a closing packet, the leftover partial bytes when there are any and a zero length packet otherwise, the same way usb_write() already did for its own transfers. On the client side a short transfer is reported instead of being presented as a complete read, the device is told to stop streaming on that path too, and the in place byte counter is repeated with its final value, since the loop only samples it every 10ms and the last line printed was stale. Lastly `lf read` and `lf sniff` cap the sample count to the graph buffer size. Anything past it was streamed and then discarded by getSamplesFromBufEx(), so `-s 1500000` spent about two extra seconds collecting 220000 samples that were thrown away, and printed "Received 1500000 / 1500000 bytes" followed by "Got 1280000 samples" with nothing to explain the gap. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
2d362813f1
commit
9e4ee9c6ad
8 files changed
+203
-26
No files matched your search
+28
-5
@@ -460,6 +460,11 @@ uint32_t SampleLF(bool verbose, uint32_t sample_size, bool ledcontrol, bool cota
|
||||
* @param reader_field - true for reading tags, false for sniffing
|
||||
* @return sampling result
|
||||
**/
|
||||
|
||||
// how long the host is given to collect a streamed USB packet before the link is declared dead?
|
||||
// ticks run at 1.5MHz, about 100ms
|
||||
#define LF_REALTIME_USB_TIMEOUT (100 * 1500)
|
||||
|
||||
int ReadLF_realtime(bool reader_field, bool cotag, uint32_t sample_limit) {
|
||||
// parameters from config and constants
|
||||
const uint8_t bits_per_sample = config.bits_per_sample;
|
||||
@@ -548,10 +553,21 @@ int ReadLF_realtime(bool reader_field, bool cotag, uint32_t sample_limit) {
|
||||
last_byte = curr_byte;
|
||||
|
||||
if (samples.total_saved == size_threshold) {
|
||||
// Request USB transmission and change FIFO bank
|
||||
|
||||
// Request USB transmission and change FIFO bank.
|
||||
// A busy IN endpoint only means the host has not collected the previous packet yet,
|
||||
if (async_usb_write_requestWrite() == false) {
|
||||
return_value = PM3_EIO;
|
||||
goto out;
|
||||
|
||||
uint32_t start_ticks = GetTicks();
|
||||
do {
|
||||
|
||||
WDT_HIT();
|
||||
if (GetTicksDelta(start_ticks) > LF_REALTIME_USB_TIMEOUT) {
|
||||
return_value = PM3_EIO;
|
||||
goto out;
|
||||
}
|
||||
|
||||
} while (async_usb_write_requestWrite() == false);
|
||||
}
|
||||
|
||||
// Reset sample
|
||||
@@ -573,9 +589,16 @@ int ReadLF_realtime(bool reader_field, bool cotag, uint32_t sample_limit) {
|
||||
}
|
||||
}
|
||||
|
||||
return_value = async_usb_write_stop();
|
||||
|
||||
out:
|
||||
// Always close the async write down, error paths included. Leaving it open
|
||||
// strands the IN endpoint with a half filled FIFO, and every later usb_write() fails
|
||||
{
|
||||
int stop_value = async_usb_write_stop();
|
||||
if (return_value == PM3_SUCCESS) {
|
||||
return_value = stop_value;
|
||||
}
|
||||
}
|
||||
|
||||
LED_D_OFF();
|
||||
|
||||
// DoAcquisition() end
|
||||
|
||||
Reference in new issue
Block a user