usb_serial_jtag_write_bytes() doesn't always write all bytes

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: usb_serial_jtag_write_bytes() doesn't always write all bytes

Postby ESP_Sprite » Fri Feb 24, 2023 1:28 am

Nope. I want to test your code, but I need some time and concentration to get all the parts (korvo board, sdcard) together. If you could simplify your app a bit more so I can run it on a random devkit, that'd probably make life easier for me.

jmadsenee
Posts: 30
Joined: Tue Apr 19, 2022 1:16 am

Re: usb_serial_jtag_write_bytes() doesn't always write all bytes

Postby jmadsenee » Wed Mar 01, 2023 4:24 am

Hi @ESP_Sprite, This should run on a random -S3 dev kit. When the program starts, it creates a ~550KB file in spi flash, which it then tries to YModem to a PC.

After downloading the program to the dev kit, open a terminal program such as Tera Term, connect, and press the reset button. You'll see the messages:
I (366) storage: spiflash Filesystem mounted
I (506) storage: Create text file
I (11286) storage: File written

Sending file "/data/text.txt", please start YModem receive on host ...
CCCCCCCC
As you can see, it takes a few seconds between Create text file and File written.

On Tera Term I do a File>Transfer>YMODEM>Receive... and some portion of the file transfers and then it stops. Pressing cancel (within 10s) outputs the rest of the buffer.

On occasion, the transfer completes, but most of the time it stops in random spots.

Thanks so much for looking into this!

John
Attachments
usb_serial_problem_V2.zip
(578.64 KiB) Downloaded 290 times

jmadsenee
Posts: 30
Joined: Tue Apr 19, 2022 1:16 am

Re: usb_serial_jtag_write_bytes() doesn't always write all bytes

Postby jmadsenee » Thu Mar 09, 2023 3:27 pm

Hi @ESP_Sprite, I know you're busy, but this is becoming critical for my project. Have you had a chance to look at the modified version yet? Thanks so much!

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: usb_serial_jtag_write_bytes() doesn't always write all bytes

Postby ESP_Sprite » Fri Mar 10, 2023 3:35 am

Yes, but it's still puzzling me a bit... smells like maybe a hardware race condition of some sort?

Can you double-check what I'm seeing and see what results you get when you change the '64' in this line in esp-idf to '63'?

jmadsenee
Posts: 30
Joined: Tue Apr 19, 2022 1:16 am

Re: usb_serial_jtag_write_bytes() doesn't always write all bytes

Postby jmadsenee » Thu Mar 16, 2023 1:21 am

@ESP_Sprite, Well, that seems to have done it! I ran my minimal program 10 x without a single failure. I then made the change in my real project and ran it 3 x without failure.

Is that a real solution or a kluge? Do I have to worry about it causing other problems? The console part of my app seems to work, but I did not do extensive testing.

Thanks very much for your help!

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: usb_serial_jtag_write_bytes() doesn't always write all bytes

Postby ESP_Sprite » Fri Mar 17, 2023 12:56 am

It's somewhat of a kludge; I think there's some hardware race condition when 64 bytes are written to the FIFO (which implicitly triggers a flush) and a manual flush done afterwards; by only writing 63 bytes, the automatic flush never happens and whatever triggers the bug doesn't occur. I need to poke the digital team to figure out the root cause here to see if there's a more elegant fix, but I think this should hold up as a workaround for now.

jmadsenee
Posts: 30
Joined: Tue Apr 19, 2022 1:16 am

Re: usb_serial_jtag_write_bytes() doesn't always write all bytes

Postby jmadsenee » Sat May 20, 2023 7:30 pm

Hi @ESP_Sprite, did you learn any more about this issue? Is it hardware? Is there a better fix?

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: usb_serial_jtag_write_bytes() doesn't always write all bytes

Postby ESP_Sprite » Tue May 23, 2023 1:05 pm

Not yet, our digital team is still squirreling out the root cause for this one.

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: usb_serial_jtag_write_bytes() doesn't always write all bytes

Postby ESP_Sprite » Thu Jun 01, 2023 10:38 am

It actually looks like a quirk of USB that is not handled correctly. If a packet is sent for the full 64 bytes and nothing after, the host OS will think that it's only part of an USB transfer, and will hold onto that information (=not send it to the client program) until it receives a non-64-byte packet. Normally, in an interactive session, this tends to not lead to an issue, but in your case it does, as it leaves the ymodem program waiting for data that is held by the OS and the ESP waiting for an ack.

As such, the solution to only send 63 bytes at a time is a bit crude, but certainly a valid workaround for this issue. I'll see if I can add a feature to the driver to only do this when it's needed, but looking at what I tested, there's not much speed gain to be gotten by 'properly' working around it.

Who is online

Users browsing this forum: Bing [Bot] and 83 guests