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

Don't fail if the device sends leftover bytes before ACK/NACK #40

Merged
merged 2 commits into from
Nov 7, 2015

Conversation

g-oikonomou
Copy link
Contributor

As discussed in #13, if the device is running a firmware that prints characters over the UART, those characters will on occasion get buffered. When the bootloader receives the synch sequence (0x55, 0x55), those buffered bytes will appear on the host's serial line before the ACK/NACK sequence.

The current version of the script reads 2 bytes when expecting ACK/NACK, and it will therefore fail in the conditions discussed above (since the first 2 bytes received are buffered leftovers instead of an ACK/NACK).

This pull proposes changing _wait_for_ack to read byte-by-byte until it encounters ACK/NACK or until it times out.

To reproduce, program the device with a firmware that prints stuff: Contiki's startup sequence will do. Reset the device manually a couple of times and then perform the key press sequence to enter the bootloader. Run the script and it will likely fail under the above conditions. Then repeat the above with this patch in place and run with -V. Observe the bold line below:

Connecting to target...
*** sending synch sequence
Got 510 additional bytes before ACK/NACK

whereas normally you should see:

Connecting to target...
*** sending synch sequence
Got 0 additional bytes before ACK/NACK

A known downside of this approach is that, if the script runs while the firmware is running, it may erroneously interpret a normal printout as an ACK/NACK. However, this is unlikely because it can only ever happen if the ACK/NACK sequence gets encountered randomly during the _wait_for_ack() timeout window.

Tested with Zolertia RE-Mote (CC2538 + FT2232 (I think!)) and SmartRF06 + CC2650EM. Tested with 2.7.10 and 3.4.3.

Closes #13

As discussed in JelmerT#13, if the running firmware prints out characters over UART while the port is closed, those characters will get buffered. Synch will then fail due to those buffered characters getting sent to the host before the ACK/NACK sequence. Under those conditions, the ACK/NACK does eventually appear on the serial line, but only after a random number of "junk" characters. Because `_wait_for_ack()` currently blindly reads 2 bytes, it will fail to detect the eventual ACK/NACK and it will thus signal an error.

This commit changes `_wait_for_ack()` to keep reading byte-by-byte until it either encounters the ACK/NACK sequence or it times-out, instead of simply reading 2 bytes and failing under the aforementioned conditions.
@uknoblic
Copy link

Hi, checked with our platform (CC2538 + FTDI FT234XD). Solves the problem for us!
Thanks!

@g-oikonomou
Copy link
Contributor Author

@JelmerT just a gentle /cough on this and the other open pulls. Have you had a chance to have a look?

@JelmerT
Copy link
Owner

JelmerT commented Oct 23, 2015

I haven't pulled this one yet since I wasn't able to reproduce the problem. Not with the smartrf06 or a PL2303 (0 additional bytes). I'm going to retry with another FTDI tomorrow and report back.

Even if I can't properly reproduce (it depends on OS and driver versions after all), I'll pull it anyway since this PR also didn't break anything for me.

@JelmerT
Copy link
Owner

JelmerT commented Nov 7, 2015

As mentioned before, I can't seem to receive any 'additional' bytes on my system.
But this PR also doesn't break anything for me + we have 2 confirmed cases where it improves communication.

So this is 👍 for me.

JelmerT added a commit that referenced this pull request Nov 7, 2015
Don't fail if the device sends leftover bytes before ACK/NACK
@JelmerT JelmerT merged commit 6a353d1 into JelmerT:master Nov 7, 2015
@g-oikonomou g-oikonomou deleted the bugfix/ack-after-synch branch November 9, 2015 11:42
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

Successfully merging this pull request may close these issues.

None yet

3 participants