Fix streaming bug affecting mostly the pm5

Bug triggered with:
lf read -s 50000
hw ping

ReadLF_realtime() in lfsampling.c never knew how many samples the client wanted — for the realtime path, lf_sample_payload_t.samples was never populated on the client side, and the ARM handler in appmain.c (CMD_LF_ACQ_RAW_ADC/CMD_LF_SNIFF_RAW_ADC) ignored it anyway. So the device just streamed samples indefinitely, only checking for a stop request roughly once per 64-sample USB burst, and even after noticing it, flushed one more trailing partial burst via async_usb_write_stop().

Meanwhile the client (WaitForRawDataTimeout in comms.c) stops listening in raw mode the instant it has collected exactly the number of bytes it asked for (e.g. 50000), then switches back to normal framed-packet parsing. Since the device kept sending more bytes past that point, those extra bytes sat unread in the pipe and got misinterpreted as a PacketResponseNGPreamble/OLD-frame header, corrupting every subsequent USB exchange (hw ping, etc.) — reproducible even with a plain lf read -s 50000, no trigger/COTAG needed.

Moreover, we make sure the client doesn't stop listening, even briefly, during stream reception.
This commit is contained in:
Philippe Teuwen committed 2026-08-26 20:16:46 +02:00
1 parent 727a7c879f
commit 7fb8927523
6 files changed
+37 -18

No files matched your search

+10 -3
View File
@@ -1010,7 +1010,7 @@ static size_t communication_delay(void) {
* @param show_process print how many bytes are received
* @return the number of received bytes
*/
size_t WaitForRawDataTimeout(uint8_t *buffer, size_t len, size_t ms_timeout, bool show_process) {
size_t WaitForRawDataTimeout(uint8_t *buffer, size_t len, size_t ms_timeout, bool show_process, bool keep_raw_mode) {
uint8_t print_counter = 0;
size_t last_pos = 0;
@@ -1058,7 +1058,7 @@ size_t WaitForRawDataTimeout(uint8_t *buffer, size_t len, size_t ms_timeout, boo
last_pos = pos;
msleep(10);
}
if (pos == len && (ms_timeout != (size_t) - 1)) {
if (pos == len && (ms_timeout != (size_t) - 1) && (keep_raw_mode == false)) {
// If ms_timeout != -1, when the desired data is received, tell the arm side
// to stop the current process, and wait for some time to make sure the process
// has been stopped.
@@ -1067,7 +1067,14 @@ size_t WaitForRawDataTimeout(uint8_t *buffer, size_t len, size_t ms_timeout, boo
SendCommandNG(CMD_BREAK_LOOP, NULL, 0);
msleep(ms_timeout);
}
SetCommunicationReceiveMode(false);
// Caller is about to chain another raw read: keep raw-receive mode
// on so the comm thread never drops back into normal framed-packet
// parsing in between the two reads. Doing so - even briefly - lets it
// misparse in-flight sample bytes as a PacketResponseNG preamble and
// corrupts every USB exchange that follows.
if (keep_raw_mode == false) {
SetCommunicationReceiveMode(false);
}
pos = __atomic_load_n(&comm_raw_pos, __ATOMIC_SEQ_CST);
return pos;
}