avrdude-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avrdude-dev] Re: STK500 version 2.0 protocol


From: Martin Thomas
Subject: Re: [avrdude-dev] Re: STK500 version 2.0 protocol
Date: Tue, 1 Feb 2005 18:06:05 +0100

Hello,
it seems some work has been done double. I took
the same approach as Erik did by intruducing a "stk500v2"-
programmer and using the "Version 1" stk500*.* files as templates for the "V2-Code". "Check in" and "get Parameters" does work in
my "V2-code" but I will step into the background since I don't
think more than one developer is needed to code the basic
V2-functions and I will not have time to continue before
the weekend. Once the functions are coded others may do testing
or provide improvments and bug-fixes.

Just some remarks:
- As far as I understand it's not avrprog.exe but stk500dll.dll
that does the stk500/avrisp communication in AVR-Studio.
- Autodetection of Version 2 or Version 1 is a good idea. But
since the Version 2 protocol seems to be better and is worth the
update just using version 2 as default and print an information like "update or try stk500v1"when the connection fails might be enough.
- the version2- and version1-code should be handled in different
source modules to make them easier to maintain
- there seems to be a mistake in the data-length description. The
AppNote-text states "LSB first" but it looks like "MSB first" is correct.
- is lenMSB=(len>>8)&0xff, lenLSB=len&0xff portable to every
host (little-endian/big-endian)?


Erik,
Maybe you'd like to use parts of my preliminary "ReceiveMessage"-
function since it is implemented as a state-machine as described in the AppNote.



Martin Thomas



----
static int stk500_2_ReceiveMessage(PROGRAMMER * pgm, int display,
          unsigned char body[], size_t maxsize)
{
long timeoutval = 5; /*seconds*/

enum states {INIT, START, SNUM, SI1, SI2, TOK, DTA, CS, DONE } state;

unsigned char timeout, c, buf[2], i, check;
unsigned short len;
struct timeval tv;
double tstart, tnow;


state   = START;
timeout = 0;
i   = 0;
len = 0;

gettimeofday(&tv, NULL);
tstart = tv.tv_sec;

while ( (state != DONE ) && (!timeout) ) {
 stk500_2_recv(pgm, buf, 1);
 c = buf[0];
 check ^= c;
switch(state)
 {
 case(START):
  if (c == STK500_2_ESC) {
   check = STK500_2_ESC;
   state=SNUM;
  }
  break;
 case(SNUM):
  if (c == stk500_2_seqnum) state=SI1;
  else state = START;
  break;
 case (SI1):
  len = c * 256;
  state = SI2;
  break;
 case (SI2):
  len += c;
  state = TOK;
  break;
 case (TOK):
  if (c == STK500_2_TOKEN) state = DTA;
  else state = START;
  break;
 case (DTA):
  if ( i < maxsize ) body[i] = c;
  else {
   fprintf(stderr, "%s: stk500_2_ReceiveMessage(): buffer too small\n",
    progname);
   return -1;
  }
  if ( (i==0) && (body[0]==ANSWER_CKSUM_ERROR) ) {
   fprintf(stderr, "%s: stk500_2_ReceiveMessage(): previous packet sent with wrong 
checksum\n",
    progname);
   return -1;
  }
  i++;
  if (i == len) state = CS;
  break;
 case (CS):
  if ( check == 0 )
  {
   state = DONE;
  }
  else
  {
   state = START;
   fprintf(stderr, "%s: stk500_2_ReceiveMessage(): checksum error\n",
    progname);
  }
  break;
 default:
  fprintf(stderr, "%s: stk500_2_ReceiveMessage(): unknown state\n",
    progname);
  return -1;
 } /* switch */

 gettimeofday(&tv, NULL);
tnow = tv.tv_sec; if ( tnow-tstart > timeoutval ) { // wuff - signed/unsigned/overflow
  fprintf(stderr, "%s: stk500_2_ReceiveMessage(): timeout\n",
   progname);
  return -1;
 }

} /* while */

return 0;
}







reply via email to

[Prev in Thread] Current Thread [Next in Thread]