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
@@ -21,6 +21,10 @@
|
||||
|
||||
#include "common.h"
|
||||
|
||||
// How many times the async write helpers spin on the IN endpoint before they give up on the host.
|
||||
// usb_check() stays true for as long as the device is enumerated
|
||||
#define ASYNC_WRITE_SPIN_LIMIT 0x00020000
|
||||
|
||||
void usb_disable(void);
|
||||
void usb_enable(void);
|
||||
bool usb_check(void);
|
||||
@@ -33,6 +37,7 @@ int usb_write(const uint8_t *data, size_t len);
|
||||
int async_usb_write_start(void);
|
||||
void async_usb_write_pushByte(uint8_t data);
|
||||
bool async_usb_write_requestWrite(void);
|
||||
void async_usb_write_discard(void);
|
||||
int async_usb_write_stop(void);
|
||||
|
||||
void usb_update_serial(uint64_t newSerialNumber);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "pm3_cmd.h"
|
||||
#include "ticks_apis.h"
|
||||
#include "wdt_apis.h"
|
||||
#include "usb_cdc_apis.h"
|
||||
#include "usb_read_ng.h"
|
||||
#include "usb_cdc_desc.h"
|
||||
@@ -552,10 +553,25 @@ int async_usb_write_start(void) {
|
||||
otg_device_type *dev = OTG_DEVICE(udev->usb_reg);
|
||||
|
||||
// check usb state
|
||||
if (!usb_check()) return PM3_EIO;
|
||||
if (!usb_check()) {
|
||||
return PM3_EIO;
|
||||
}
|
||||
|
||||
// wait for tx end if working...
|
||||
while (!is_write_completed(ept_in)) if (!usb_check()) return PM3_EIO;
|
||||
uint32_t spins = 0;
|
||||
while (!is_write_completed(ept_in)) {
|
||||
|
||||
if (!usb_check()) {
|
||||
return PM3_EIO;
|
||||
}
|
||||
|
||||
WDT_HIT();
|
||||
|
||||
if (++spins > ASYNC_WRITE_SPIN_LIMIT) {
|
||||
async_usb_write_discard();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// disable fifo empty irq.
|
||||
dev->diepempmsk &= ~(1 << (USBD_CDC_BULK_IN_EPT & 0x7F));
|
||||
@@ -617,25 +633,57 @@ bool async_usb_write_requestWrite(void) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop a transfer the host refuses to collect.
|
||||
* Disables the IN endpoint and flushes its tx fifo, otherwise the endpoint
|
||||
* stays busy forever and every later usb_write() bails out, which looks
|
||||
* like a dead device.
|
||||
*/
|
||||
void async_usb_write_discard(void) {
|
||||
usbd_ept_in_check_fifo(udev, USBD_CDC_BULK_IN_EPT & 0x7F);
|
||||
async_write_buf_select = 0;
|
||||
async_write_index = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop send and wait finish.
|
||||
* @return SUCCESS if send stop success, otherwise ERROR
|
||||
*/
|
||||
int async_usb_write_stop(void) {
|
||||
otg_eptin_type *ept_in = USB_INEPT(udev->usb_reg, (USBD_CDC_BULK_IN_EPT & 0x7F));
|
||||
uint32_t spins = 0;
|
||||
|
||||
// Wait for the end of transfer
|
||||
while (!is_write_completed(ept_in)) if (!usb_check()) return PM3_EIO;
|
||||
while (!is_write_completed(ept_in)) {
|
||||
|
||||
// still have data on local buffer, we need send before write stop.
|
||||
if (async_write_index != 0) {
|
||||
if (!async_usb_write_requestWrite()) {
|
||||
if (!usb_check()) {
|
||||
return PM3_EIO;
|
||||
}
|
||||
|
||||
WDT_HIT();
|
||||
|
||||
if (++spins > ASYNC_WRITE_SPIN_LIMIT) {
|
||||
async_usb_write_discard();
|
||||
return PM3_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
// Send a final packet. When the local buffer still holds data this flushes it,
|
||||
// if empty, send a zero length packet.
|
||||
if (!async_usb_write_requestWrite()) {
|
||||
return PM3_EIO;
|
||||
}
|
||||
|
||||
// Wait for the end of fifo flush transfer.
|
||||
while (!is_write_completed(ept_in)) if (!usb_check()) return PM3_EIO;
|
||||
spins = 0;
|
||||
while (!is_write_completed(ept_in)) {
|
||||
if (!usb_check()) return PM3_EIO;
|
||||
WDT_HIT();
|
||||
if (++spins > ASYNC_WRITE_SPIN_LIMIT) {
|
||||
async_usb_write_discard();
|
||||
return PM3_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "proxmark3_arm.h"
|
||||
#include "usart_defs.h"
|
||||
#include "ticks_apis.h"
|
||||
#include "wdt_apis.h"
|
||||
#include "usb_read_ng.h"
|
||||
#include "usb_cdc_desc.h"
|
||||
|
||||
@@ -529,16 +530,44 @@ int async_usb_write_start(void) {
|
||||
return PM3_EIO;
|
||||
}
|
||||
|
||||
uint32_t spins = 0;
|
||||
while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXPKTRDY) {
|
||||
|
||||
if (usb_check() == false) {
|
||||
return PM3_EIO;
|
||||
}
|
||||
|
||||
WDT_HIT();
|
||||
|
||||
if (++spins > ASYNC_WRITE_SPIN_LIMIT) {
|
||||
async_usb_write_discard();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
isAsyncRequestFinished = false;
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
/*
|
||||
*----------------------------------------------------------------------------
|
||||
* \fn async_usb_write_discard
|
||||
* \brief Drop a packet the host refuses to collect
|
||||
*
|
||||
* Without this the endpoint stays busy forever and usb_write() keeps bailing
|
||||
* out with PM3_EIO, which looks like a dead device.
|
||||
*----------------------------------------------------------------------------
|
||||
*/
|
||||
void async_usb_write_discard(void) {
|
||||
// Clearing TXPKTRDY by hand is not enough on this silicon, the FIFO keeps
|
||||
// its content. Reset the endpoint the same way the bus reset handler does
|
||||
// and re-enable it, so the next usb_write() starts from a clean FIFO.
|
||||
pUdp->UDP_RSTEP |= (1u << AT91C_EP_IN);
|
||||
pUdp->UDP_RSTEP &= ~(1u << AT91C_EP_IN);
|
||||
pUdp->UDP_CSR[AT91C_EP_IN] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_IN);
|
||||
isAsyncRequestFinished = true;
|
||||
}
|
||||
|
||||
/*
|
||||
*----------------------------------------------------------------------------
|
||||
* \fn async_usb_write_pushByte
|
||||
@@ -602,25 +631,46 @@ inline bool async_usb_write_requestWrite(void) {
|
||||
*----------------------------------------------------------------------------
|
||||
*/
|
||||
int async_usb_write_stop(void) {
|
||||
|
||||
uint32_t spins = 0;
|
||||
|
||||
// Wait for the end of transfer
|
||||
while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXPKTRDY) {
|
||||
|
||||
if (usb_check() == false) {
|
||||
return PM3_EIO;
|
||||
}
|
||||
|
||||
WDT_HIT();
|
||||
|
||||
if (++spins > ASYNC_WRITE_SPIN_LIMIT) {
|
||||
async_usb_write_discard();
|
||||
return PM3_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
// clear transmission completed flag
|
||||
UDP_CLEAR_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXCOMP);
|
||||
while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP) {};
|
||||
|
||||
// FIFO is not empty, request a write in non-ping-pong mode
|
||||
if (isAsyncRequestFinished == false) {
|
||||
// FIFO is not empty, request a write in non-ping-pong mode.
|
||||
// Must send ZLP to close transfer
|
||||
{
|
||||
UDP_SET_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXPKTRDY);
|
||||
|
||||
spins = 0;
|
||||
while (!(pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) {
|
||||
|
||||
if (usb_check() == false) {
|
||||
return PM3_EIO;
|
||||
}
|
||||
|
||||
WDT_HIT();
|
||||
|
||||
if (++spins > ASYNC_WRITE_SPIN_LIMIT) {
|
||||
async_usb_write_discard();
|
||||
return PM3_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
UDP_CLEAR_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXCOMP);
|
||||
|
||||
Reference in new issue
Block a user