Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random data corruption in outTransfer #403

Closed
randomricky2018 opened this issue Aug 23, 2018 · 2 comments
Closed

Random data corruption in outTransfer #403

randomricky2018 opened this issue Aug 23, 2018 · 2 comments

Comments

@randomricky2018
Copy link

Hi,

I'm a super beginner in Arduino, usb and these drivers stuff, not sure if I understand the problem correctly. :(

I was trying to write a Android Adb host using this library, and I found that the messages that I sent sometimes (or always) corrupted. (The bytes in Usb.outTransfer does not match what's being received).

Here's my thought: (correct me if i'm wrong :) )

In UsbTransfer, due to nak bug, if nak limit is > 1, it will retry when it receives nak.
To restore it from nak, we do "regWr(rSNDFIFO, *data_p);".
However, *data_p is corrupted (the content is not the original content anymore), as in bytesWr(), we do "USB_SPI.transfer(data_p, nbytes);", which read AND write bytes in data_p by SPI.
Hence, "regWr(rSNDFIFO, *data_p);" does not write the original first byte anymore, it writes a value read from SPI.

Does it make sense or if I misunderstood sth?

Thanks a lot!

@gdsports
Copy link
Contributor

gdsports commented Jun 28, 2019

I have seen this problem sending to a slow printer which generates lots of NAKs. The root cause is the SPI.transfer function reads AND writes the data buffer so in Usb.cpp|USB::OutTransfer the following line sends garbage instead of the correct value.

regWr(rSNDFIFO, *data_p);

bytesWr() calls SPI.transfer() which sends data to the USB out FIFO but also reads from the SPI bus thus over writing the original values at data_p.

bytesWr(rSNDFIFO, bytes_tosend, data_p); //filling output FIFO

My fix is to save the first byte of data_p before bytesWr() then use it for NAK retry, if needed.

		uint8_t save_byte = *data_p;
                bytesWr(rSNDFIFO, bytes_tosend, data_p); //filling output FIFO


                regWr(rSNDFIFO, save_byte);

#238 might also be related.

gdsports added a commit to gdsports/USB_Host_Shield_2.0 that referenced this issue Jun 28, 2019
This was referenced Jun 28, 2019
@Lauszus
Copy link
Collaborator

Lauszus commented Aug 5, 2019

Fixed in #473

@Lauszus Lauszus closed this as completed Aug 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants