-
Notifications
You must be signed in to change notification settings - Fork 39
EV3 USB hacks #2356
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
base: master
Are you sure you want to change the base?
EV3 USB hacks #2356
Conversation
| // 384 bytes (triggered by 65536 % 1018 = 384). To work around this, we | ||
| // always erase two sectors to make the last chunk be twice as big | ||
| // (131072 % 1018 = 768). | ||
| const eraseSize = sectorSize * 2; // flash memory sector size |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| const eraseSize = sectorSize * 2; // flash memory sector size | |
| const eraseSize = sectorSize * 2; |
|
Affirmed that with 5ed3861, I can successfully flash the EV3 from my Windows computer. |
|
Thanks! This seems to be working, although I'm seeing a separate HID detection issue. I'll defer that to a separate issue. I noticed that the progress bar doesn't appear until after a few seconds when it hits 8%, so for a little while you're left wondering if anything happened. It would be nice to start at 0%. When I put in a dedicated progress at the start, there seems to be a separate bug that if progress is |
function computeEv3ExtraLength(firmwareLength: number): number {
// HACK: If the EV3 firmware size is not a multiple of 2 * 64KiB, then we
// need to add some padding to avoid possibly triggering a bug where small
// download payloads can cause bad replies. This is done by ensuring that
// the last chunk is 1018 bytes.
const maxChunkSize = 1018;
const eraseSize = 2 * 64 * 1024;
const remainder = firmwareLength % eraseSize;
if (remainder === 0) {
// Adding padding would cause an entire extra erase to be needed. Don't
// do that and rely on a a different workaround (always erasing two sectors).
return 0;
}
return maxChunkSize - (remainder % maxChunkSize);
}Would things be a lot simpler if we write in chunks of |
Yes, we never had such a slow first step on other hubs, so could use some work.
I tried that, but we get no response from the EV3 if the chunk size is anything other than 1018 (other than the last chunk). |
|
Ah, makes sense now. Thanks! |
|
|
||
| if (remainder === 0) { | ||
| // Adding padding would cause an entire extra erase to be needed. Don't | ||
| // do that and rely on a a different workaround (always erasing two sectors). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I'm misunderstanding this, but this doesn't seem quite right?
If we let one superchunk be S = 2 * 64 * 1024. Then for any firmware size X * S, we have remainder = 0, so we don't add padding.
Say our firmware size is X = 4, for example. Then remainder is 0 but the last write chunk will be (4 * 2* 64 * 1024) % 1018 = 18, so that is a small write that would cause the bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that the assumption here is that only the last two chunks matter, but that is not universally the case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I suppose we're sending small packets at the end of every super chunk to make that work?
This all seems a little more complicated than it needs to be 😄 I'll have a look at an alternative.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I guess you've studied this pretty well at this point and since it is working, let's proceed :)
|
Since this is titled |
Add a hack to not fail if the EV3 bootloader sends an echo of the request instead of the expected response. There is a known compatibility issue with the EV3 bootloader USB and, e.g. Windows with USB 3.0 ports. This apparently causes a race condition where commands that don't take long to process before sending a response will have the request echoed back instead of receiving the actual response. If this happens, we can ignore it and assume the command was successful. It just won't work, e.g. for the version command since that has a payload in the response.
Change from erasing 1 sector at a time to erasing 2 sectors at a time. This works around a USB issue where commands that don't take long to execute can receive an incorrect response. The point of erasing 2 sectors at a time is that when we write the data to the sectors that we just erased, the last (partial) chunk of data is now twice as large, which makes it take twice as long to write. This seems to be long enough to avoid the USB issue.
Add an extra progress between erasing and writing. This makes the progress not stall for quite as long before progressing when flashing smaller firmwares.
Add a function to pad EV3 firmware size to avoid potentially triggering bugs in the EV3 bootloader. When we do a download command with too small of a payload, the bootloader can send a bad response. We can work around this by ensuring that the firmware size is such that the last chunk is the maximum size. This way we never send a small chunk that could trigger the bug.
If we try to connect too soon, we get security errors.
This operation does not work on Windows, and does not appear to be crucial on other platforms either.
| if (hotPlugDevice !== undefined) { | ||
| yield* put(usbHotPlugConnectPybricks()); | ||
|
|
||
| // Even though we get the connect event, the browser does not |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this only apply to Linux?
We should test on a slow machine like a Raspberry Pi to make sure the delay is long enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And we should report this as a bug to Chromium. Then it could be fixed in a reliable way. For example, it could be that they are using the udev add event instead of bind that triggers the event before udev rules have run instead of after.
|
|
||
| exitStack.push(() => usbDevice.close().catch(console.debug)); | ||
|
|
||
| // Always reset the USB device to ensure it is in a known state. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we already have a good idea of what the solution will be to replace this, I think we should go ahead and implement it now instead of doing a buggy release.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't currently know what the solution is, but I'm happy to wait if you think we have one.
I've not been able to reproduce any issues without the reset so far.
| // if there is exactly one Pybricks device connected, we can connect to | ||
| // it, otherwise we should let the user choose which one to connect to | ||
| if (pybricksDevices.length === 1) { | ||
| // REVISIT: Device is passed as hotplug device, even though it is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand what needs to be revisited here. Is the problem just the technicality in the naming?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. This is not a hotplug. This gets called when you load the page and the hub is already connected. So we don't need the hotplug delay (but it doesn't hurt to keep).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be (very unlikely but still possible) that the USB was plugged in at the same time the page was loaded, so probably best to keep it and not differentiate.
Add a number of hacks to work around issues seen when flashing firmware on the EV3.
See individual commit messages for details.
Alternate to #2352.
The first patch on it's own should allow firmware flashing to complete without error (only warnings).
The rest of the patches aim to avoid triggering the warnings in the first place.