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

Resend sync request with each retry. #29

Merged
merged 1 commit into from Apr 5, 2018

Conversation

General-Fault
Copy link
Contributor

@General-Fault General-Fault commented Apr 4, 2018

Fixes issue #28
Clear the input buffer and resend the sync request with each sync retry for STK500v1.
This partially reverses the change to commit 7765e6d, but it brings it closer inline to what avrdude does.

This is the code from avrdude 6.3 for reference:

int stk500_getsync(PROGRAMMER * pgm)
{
  unsigned char buf[32], resp[32];
  int attempt;

  /*
   * get in sync */
  buf[0] = Cmnd_STK_GET_SYNC;
  buf[1] = Sync_CRC_EOP;
  
  /*
   * First send and drain a few times to get rid of line noise 
   */
   
  stk500_send(pgm, buf, 2);
  stk500_drain(pgm, 0);
  stk500_send(pgm, buf, 2);
  stk500_drain(pgm, 0);

  for (attempt = 0; attempt < MAX_SYNC_ATTEMPTS; attempt++) {
    stk500_send(pgm, buf, 2);
    stk500_recv(pgm, resp, 1);
    if (resp[0] == Resp_STK_INSYNC){
      break;
    }
    avrdude_message(MSG_INFO, "%s: stk500_getsync() attempt %d of %d: not in sync: resp=0x%02x\n",
                    progname, attempt + 1, MAX_SYNC_ATTEMPTS, resp[0]);
  }
  if (attempt == MAX_SYNC_ATTEMPTS) {
    stk500_drain(pgm, 0);
    return -1;
  }

  if (stk500_recv(pgm, resp, 1) < 0)
    return -1;
  if (resp[0] != Resp_STK_OK) {
    avrdude_message(MSG_INFO, "%s: stk500_getsync(): can't communicate with device: "
                    "resp=0x%02x\n",
                    progname, resp[0]);
    return -1;
  }

  return 0;
}

As you can see, it does a send-drain-send-drain then goes into a send-read loop. When I replicated this pattern exactly, the next read would timeout. I'm not sure what that's about - but a drain-send-read loop addresses the problem as far as my testing went.

@christophediericx
Copy link
Collaborator

It is an absolute pleasure to receive a pull request which is as clearly described and provides the amount of detail as presented here: awesome work. I'll merge the pull request right now, but I will only have time to fully test it (and prepare a new release) over the course of the next couple of days (in the weekend at the latest).

Thanks!

@christophediericx christophediericx merged commit 7781f94 into twinearthsoftware:master Apr 5, 2018
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

2 participants