2001-01-19 02:46:50 +00:00
|
|
|
/*
|
2003-02-08 04:17:25 +00:00
|
|
|
* avrdude - A Downloader/Uploader for AVR device programmers
|
2004-12-22 01:52:45 +00:00
|
|
|
* Copyright (C) 2000-2004 Brian S. Dean <bsd@bsdhome.com>
|
2001-01-19 02:46:50 +00:00
|
|
|
*
|
2003-02-06 19:08:33 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2001-01-19 02:46:50 +00:00
|
|
|
*
|
2003-02-06 19:08:33 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2001-01-19 02:46:50 +00:00
|
|
|
*
|
2003-02-06 19:08:33 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2012-11-20 14:03:50 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2001-01-19 02:46:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2003-02-14 20:34:03 +00:00
|
|
|
#include "ac_cfg.h"
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2003-11-22 03:25:48 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
2001-01-19 02:46:50 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
2003-02-27 20:08:54 +00:00
|
|
|
|
|
|
|
#if defined(HAVE_LIBREADLINE)
|
|
|
|
# include <readline/readline.h>
|
|
|
|
# include <readline/history.h>
|
|
|
|
#endif
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2007-01-24 21:07:54 +00:00
|
|
|
#include "avrdude.h"
|
2014-05-19 10:01:59 +00:00
|
|
|
#include "term.h"
|
2001-01-19 02:46:50 +00:00
|
|
|
|
|
|
|
struct command {
|
|
|
|
char * name;
|
2002-11-30 14:09:12 +00:00
|
|
|
int (*func)(PROGRAMMER * pgm, struct avrpart * p, int argc, char *argv[]);
|
2001-01-19 02:46:50 +00:00
|
|
|
char * desc;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_dump (PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char *argv[]);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_write (PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char *argv[]);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_erase (PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char *argv[]);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_sig (PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char *argv[]);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_part (PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char *argv[]);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_help (PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char *argv[]);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_quit (PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char *argv[]);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_send (PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char *argv[]);
|
2001-11-19 17:44:24 +00:00
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_parms (PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char *argv[]);
|
2003-07-24 21:26:28 +00:00
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_vtarg (PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char *argv[]);
|
2003-07-24 21:26:28 +00:00
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_varef (PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char *argv[]);
|
2003-07-24 21:26:28 +00:00
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_fosc (PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char *argv[]);
|
2003-07-24 21:26:28 +00:00
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_sck (PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char *argv[]);
|
2004-07-07 08:59:07 +00:00
|
|
|
|
2009-02-17 15:31:27 +00:00
|
|
|
static int cmd_spi (PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char *argv[]);
|
|
|
|
|
|
|
|
static int cmd_pgm (PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char *argv[]);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2013-09-08 19:31:48 +00:00
|
|
|
static int cmd_verbose (PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char *argv[]);
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
struct command cmd[] = {
|
2001-11-21 02:46:55 +00:00
|
|
|
{ "dump", cmd_dump, "dump memory : %s <memtype> <addr> <N-Bytes>" },
|
2001-02-08 01:05:05 +00:00
|
|
|
{ "read", cmd_dump, "alias for dump" },
|
2001-11-21 02:46:55 +00:00
|
|
|
{ "write", cmd_write, "write memory : %s <memtype> <addr> <b1> <b2> ... <bN>" },
|
2001-01-19 02:46:50 +00:00
|
|
|
{ "erase", cmd_erase, "perform a chip erase" },
|
|
|
|
{ "sig", cmd_sig, "display device signature bytes" },
|
2001-11-21 02:46:55 +00:00
|
|
|
{ "part", cmd_part, "display the current part information" },
|
|
|
|
{ "send", cmd_send, "send a raw command : %s <b1> <b2> <b3> <b4>" },
|
2022-02-14 09:30:21 +00:00
|
|
|
{ "parms", cmd_parms, "display adjustable parameters (STK500 and Curiosity Nano only)" },
|
|
|
|
{ "vtarg", cmd_vtarg, "set <V[target]> (STK500 and Curiosity Nano only)" },
|
2003-07-24 21:26:28 +00:00
|
|
|
{ "varef", cmd_varef, "set <V[aref]> (STK500 only)" },
|
|
|
|
{ "fosc", cmd_fosc, "set <oscillator frequency> (STK500 only)" },
|
2004-07-07 08:59:07 +00:00
|
|
|
{ "sck", cmd_sck, "set <SCK period> (STK500 only)" },
|
2009-02-17 15:31:27 +00:00
|
|
|
{ "spi", cmd_spi, "enter direct SPI mode" },
|
|
|
|
{ "pgm", cmd_pgm, "return to programming mode" },
|
2013-09-08 19:31:48 +00:00
|
|
|
{ "verbose", cmd_verbose, "change verbosity" },
|
2001-01-19 02:46:50 +00:00
|
|
|
{ "help", cmd_help, "help" },
|
|
|
|
{ "?", cmd_help, "help" },
|
|
|
|
{ "quit", cmd_quit, "quit" }
|
|
|
|
};
|
|
|
|
|
|
|
|
#define NCMDS (sizeof(cmd)/sizeof(struct command))
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-02-17 15:31:27 +00:00
|
|
|
static int spi_mode = 0;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int nexttok(char * buf, char ** tok, char ** next)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
char * q, * n;
|
|
|
|
|
|
|
|
q = buf;
|
2010-01-22 16:40:17 +00:00
|
|
|
while (isspace((int)*q))
|
2001-01-19 02:46:50 +00:00
|
|
|
q++;
|
2010-01-22 16:40:17 +00:00
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
/* isolate first token */
|
|
|
|
n = q+1;
|
2010-01-22 16:40:17 +00:00
|
|
|
while (*n && !isspace((int)*n))
|
2001-01-19 02:46:50 +00:00
|
|
|
n++;
|
|
|
|
|
|
|
|
if (*n) {
|
|
|
|
*n = 0;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find start of next token */
|
2010-01-22 16:40:17 +00:00
|
|
|
while (isspace((int)*n))
|
2001-01-19 02:46:50 +00:00
|
|
|
n++;
|
|
|
|
|
|
|
|
*tok = q;
|
|
|
|
*next = n;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int hexdump_line(char * buffer, unsigned char * p, int n, int pad)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
char * hexdata = "0123456789abcdef";
|
|
|
|
char * b;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
b = buffer;
|
|
|
|
|
|
|
|
j = 0;
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
if (i && ((i % 8) == 0))
|
|
|
|
b[j++] = ' ';
|
|
|
|
b[j++] = hexdata[(p[i] & 0xf0) >> 4];
|
|
|
|
b[j++] = hexdata[(p[i] & 0x0f)];
|
|
|
|
if (i < 15)
|
|
|
|
b[j++] = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=j; i<pad; i++)
|
|
|
|
b[i] = ' ';
|
|
|
|
|
|
|
|
b[i] = 0;
|
|
|
|
|
|
|
|
for (i=0; i<pad; i++) {
|
|
|
|
if (!((b[i] == '0') || (b[i] == ' ')))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int chardump_line(char * buffer, unsigned char * p, int n, int pad)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char b [ 128 ];
|
|
|
|
|
|
|
|
for (i=0; i<n; i++) {
|
2001-10-13 03:13:13 +00:00
|
|
|
memcpy(b, p, n);
|
2001-01-19 02:46:50 +00:00
|
|
|
buffer[i] = '.';
|
2010-01-22 16:40:17 +00:00
|
|
|
if (isalpha((int)(b[i])) || isdigit((int)(b[i])) || ispunct((int)(b[i])))
|
2001-01-19 02:46:50 +00:00
|
|
|
buffer[i] = b[i];
|
2010-01-22 16:40:17 +00:00
|
|
|
else if (isspace((int)(b[i])))
|
2001-01-19 02:46:50 +00:00
|
|
|
buffer[i] = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=n; i<pad; i++)
|
|
|
|
buffer[i] = ' ';
|
|
|
|
|
|
|
|
buffer[i] = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int hexdump_buf(FILE * f, int startaddr, unsigned char * buf, int len)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
int addr;
|
2013-09-03 11:05:13 +00:00
|
|
|
int n;
|
2001-01-19 02:46:50 +00:00
|
|
|
unsigned char * p;
|
|
|
|
char dst1[80];
|
|
|
|
char dst2[80];
|
|
|
|
|
|
|
|
addr = startaddr;
|
|
|
|
p = (unsigned char *)buf;
|
|
|
|
while (len) {
|
|
|
|
n = 16;
|
|
|
|
if (n > len)
|
|
|
|
n = len;
|
|
|
|
hexdump_line(dst1, p, n, 48);
|
|
|
|
chardump_line(dst2, p, n, 16);
|
|
|
|
fprintf(stdout, "%04x %s |%s|\n", addr, dst1, dst2);
|
|
|
|
len -= n;
|
|
|
|
addr += n;
|
|
|
|
p += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_dump(PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char * argv[])
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2001-11-21 18:54:11 +00:00
|
|
|
static char prevmem[128] = {0};
|
2001-01-19 02:46:50 +00:00
|
|
|
char * e;
|
2001-11-21 02:46:55 +00:00
|
|
|
unsigned char * buf;
|
2001-01-19 03:10:53 +00:00
|
|
|
int maxsize;
|
2001-11-21 02:46:55 +00:00
|
|
|
unsigned long i;
|
|
|
|
static unsigned long addr=0;
|
2001-01-19 03:10:53 +00:00
|
|
|
static int len=64;
|
2001-11-21 02:46:55 +00:00
|
|
|
AVRMEM * mem;
|
|
|
|
char * memtype = NULL;
|
|
|
|
int rc;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
if (!((argc == 2) || (argc == 4))) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "Usage: dump <memtype> [<addr> <len>]\n");
|
2001-11-21 02:46:55 +00:00
|
|
|
return -1;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
memtype = argv[1];
|
|
|
|
|
2001-11-21 18:54:11 +00:00
|
|
|
if (strncmp(prevmem, memtype, strlen(memtype)) != 0) {
|
|
|
|
addr = 0;
|
|
|
|
len = 64;
|
|
|
|
strncpy(prevmem, memtype, sizeof(prevmem)-1);
|
|
|
|
prevmem[sizeof(prevmem)-1] = 0;
|
|
|
|
}
|
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
mem = avr_locate_mem(p, memtype);
|
|
|
|
if (mem == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "\"%s\" memory type not defined for part \"%s\"\n",
|
2001-11-21 02:46:55 +00:00
|
|
|
memtype, p->desc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 4) {
|
|
|
|
addr = strtoul(argv[2], &e, 0);
|
|
|
|
if (*e || (e == argv[2])) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (dump): can't parse address \"%s\"\n",
|
2001-11-21 02:46:55 +00:00
|
|
|
progname, argv[2]);
|
2001-01-19 03:10:53 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
len = strtol(argv[3], &e, 0);
|
|
|
|
if (*e || (e == argv[3])) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (dump): can't parse length \"%s\"\n",
|
2001-11-21 02:46:55 +00:00
|
|
|
progname, argv[3]);
|
|
|
|
return -1;
|
2001-01-19 03:10:53 +00:00
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
maxsize = mem->size;
|
2001-02-08 01:05:05 +00:00
|
|
|
|
2002-11-23 00:52:15 +00:00
|
|
|
if (addr >= maxsize) {
|
|
|
|
if (argc == 2) {
|
|
|
|
/* wrap around */
|
|
|
|
addr = 0;
|
|
|
|
}
|
|
|
|
else {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (dump): address 0x%05lx is out of range for %s memory\n",
|
2014-05-18 08:41:46 +00:00
|
|
|
progname, addr, mem->desc);
|
2002-11-23 00:52:15 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
2001-01-19 03:10:53 +00:00
|
|
|
/* trim len if nessary to not read past the end of memory */
|
|
|
|
if ((addr + len) > maxsize)
|
|
|
|
len = maxsize - addr;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
|
|
|
buf = malloc(len);
|
|
|
|
if (buf == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (dump): out of memory\n", progname);
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-11-19 17:44:24 +00:00
|
|
|
for (i=0; i<len; i++) {
|
2006-11-20 15:04:09 +00:00
|
|
|
rc = pgm->read_byte(pgm, p, mem, addr+i, &buf[i]);
|
2001-11-21 02:46:55 +00:00
|
|
|
if (rc != 0) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "error reading %s address 0x%05lx of part %s\n",
|
2001-11-21 02:46:55 +00:00
|
|
|
mem->desc, addr+i, p->desc);
|
2002-01-12 01:26:09 +00:00
|
|
|
if (rc == -1)
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "read operation not supported on memory type \"%s\"\n",
|
2002-01-12 01:26:09 +00:00
|
|
|
mem->desc);
|
2001-11-21 02:46:55 +00:00
|
|
|
return -1;
|
2001-11-19 17:44:24 +00:00
|
|
|
}
|
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
|
|
|
|
hexdump_buf(stdout, addr, buf, len);
|
|
|
|
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
addr = addr + len;
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-10-14 02:53:21 +00:00
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char * argv[])
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
char * e;
|
|
|
|
int len, maxsize;
|
2001-11-21 02:46:55 +00:00
|
|
|
char * memtype;
|
|
|
|
unsigned long addr, i;
|
2002-02-14 02:48:07 +00:00
|
|
|
unsigned char * buf;
|
|
|
|
unsigned char b;
|
2001-01-19 02:46:50 +00:00
|
|
|
int rc;
|
2001-02-08 01:05:05 +00:00
|
|
|
int werror;
|
2001-11-21 02:46:55 +00:00
|
|
|
AVRMEM * mem;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
|
|
|
if (argc < 4) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "Usage: write <memtype> <addr> <byte1> "
|
2020-09-16 21:31:19 +00:00
|
|
|
"<byte2> ... <byteN>\n");
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
memtype = argv[1];
|
|
|
|
|
|
|
|
mem = avr_locate_mem(p, memtype);
|
|
|
|
if (mem == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "\"%s\" memory type not defined for part \"%s\"\n",
|
2001-11-21 02:46:55 +00:00
|
|
|
memtype, p->desc);
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
maxsize = mem->size;
|
2001-01-22 01:59:47 +00:00
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
addr = strtoul(argv[2], &e, 0);
|
|
|
|
if (*e || (e == argv[2])) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (write): can't parse address \"%s\"\n",
|
2001-01-19 02:46:50 +00:00
|
|
|
progname, argv[2]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addr > maxsize) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (write): address 0x%05lx is out of range for %s memory\n",
|
2014-05-18 08:41:46 +00:00
|
|
|
progname, addr, memtype);
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* number of bytes to write at the specified address */
|
|
|
|
len = argc - 3;
|
|
|
|
|
|
|
|
if ((addr + len) > maxsize) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (write): selected address and # bytes exceed "
|
2014-05-18 08:41:46 +00:00
|
|
|
"range for %s memory\n",
|
|
|
|
progname, memtype);
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = malloc(len);
|
|
|
|
if (buf == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (write): out of memory\n", progname);
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=3; i<argc; i++) {
|
|
|
|
buf[i-3] = strtoul(argv[i], &e, 0);
|
|
|
|
if (*e || (e == argv[i])) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (write): can't parse byte \"%s\"\n",
|
2001-01-19 02:46:50 +00:00
|
|
|
progname, argv[i]);
|
2001-11-19 17:44:24 +00:00
|
|
|
free(buf);
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-30 14:09:12 +00:00
|
|
|
pgm->err_led(pgm, OFF);
|
2001-02-08 01:05:05 +00:00
|
|
|
for (werror=0, i=0; i<len; i++) {
|
2001-11-19 17:44:24 +00:00
|
|
|
|
2002-11-30 14:09:12 +00:00
|
|
|
rc = avr_write_byte(pgm, p, mem, addr+i, buf[i]);
|
2001-01-22 01:59:47 +00:00
|
|
|
if (rc) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (write): error writing 0x%02x at 0x%05lx, rc=%d\n",
|
2002-02-14 02:48:07 +00:00
|
|
|
progname, buf[i], addr+i, rc);
|
2002-01-12 01:26:09 +00:00
|
|
|
if (rc == -1)
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "write operation not supported on memory type \"%s\"\n",
|
2014-05-18 08:41:46 +00:00
|
|
|
mem->desc);
|
2001-02-08 01:05:05 +00:00
|
|
|
werror = 1;
|
|
|
|
}
|
2002-02-14 02:48:07 +00:00
|
|
|
|
2006-11-20 15:04:09 +00:00
|
|
|
rc = pgm->read_byte(pgm, p, mem, addr+i, &b);
|
2002-02-14 02:48:07 +00:00
|
|
|
if (b != buf[i]) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (write): error writing 0x%02x at 0x%05lx cell=0x%02x\n",
|
2014-05-18 08:41:46 +00:00
|
|
|
progname, buf[i], addr+i, b);
|
2002-02-14 02:48:07 +00:00
|
|
|
werror = 1;
|
|
|
|
}
|
|
|
|
|
2001-02-08 01:05:05 +00:00
|
|
|
if (werror) {
|
2002-11-30 14:09:12 +00:00
|
|
|
pgm->err_led(pgm, ON);
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_send(PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char * argv[])
|
2001-11-19 17:44:24 +00:00
|
|
|
{
|
|
|
|
unsigned char cmd[4], res[4];
|
|
|
|
char * e;
|
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
|
2006-11-20 15:04:09 +00:00
|
|
|
if (pgm->cmd == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "The %s programmer does not support direct ISP commands.\n",
|
2014-05-18 08:41:46 +00:00
|
|
|
pgm->type);
|
2006-11-20 15:04:09 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-02-17 15:31:27 +00:00
|
|
|
if (spi_mode && (pgm->spi == NULL)) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "The %s programmer does not support direct SPI transfers.\n",
|
2014-05-18 08:41:46 +00:00
|
|
|
pgm->type);
|
2009-02-17 15:31:27 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((argc > 5) || ((argc < 5) && (!spi_mode))) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, spi_mode?
|
2009-02-17 15:31:27 +00:00
|
|
|
"Usage: send <byte1> [<byte2> [<byte3> [<byte4>]]]\n":
|
|
|
|
"Usage: send <byte1> <byte2> <byte3> <byte4>\n");
|
2001-11-19 17:44:24 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* number of bytes to write at the specified address */
|
|
|
|
len = argc - 1;
|
|
|
|
|
|
|
|
/* load command bytes */
|
|
|
|
for (i=1; i<argc; i++) {
|
|
|
|
cmd[i-1] = strtoul(argv[i], &e, 0);
|
|
|
|
if (*e || (e == argv[i])) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (send): can't parse byte \"%s\"\n",
|
2001-11-19 17:44:24 +00:00
|
|
|
progname, argv[i]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-30 14:09:12 +00:00
|
|
|
pgm->err_led(pgm, OFF);
|
2001-11-19 17:44:24 +00:00
|
|
|
|
2009-02-17 15:31:27 +00:00
|
|
|
if (spi_mode)
|
|
|
|
pgm->spi(pgm, cmd, res, argc-1);
|
|
|
|
else
|
|
|
|
pgm->cmd(pgm, cmd, res);
|
2001-11-19 17:44:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* display results
|
|
|
|
*/
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "results:");
|
2001-11-19 17:44:24 +00:00
|
|
|
for (i=0; i<len; i++)
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, " %02x", res[i]);
|
|
|
|
avrdude_message(MSG_INFO, "\n");
|
2001-11-19 17:44:24 +00:00
|
|
|
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_erase(PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char * argv[])
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: erasing chip\n", progname);
|
2002-11-30 14:09:12 +00:00
|
|
|
pgm->chip_erase(pgm, p);
|
2001-01-19 02:46:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_part(PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char * argv[])
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
fprintf(stdout, "\n");
|
2002-02-14 02:48:07 +00:00
|
|
|
avr_display(stdout, p, "", 0);
|
2001-01-19 02:46:50 +00:00
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_sig(PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char * argv[])
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
int i;
|
2001-11-21 02:46:55 +00:00
|
|
|
int rc;
|
|
|
|
AVRMEM * m;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2002-11-30 14:09:12 +00:00
|
|
|
rc = avr_signature(pgm, p);
|
2001-11-21 02:46:55 +00:00
|
|
|
if (rc != 0) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "error reading signature data, rc=%d\n",
|
2001-11-21 02:46:55 +00:00
|
|
|
rc);
|
|
|
|
}
|
2001-11-19 17:44:24 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
m = avr_locate_mem(p, "signature");
|
|
|
|
if (m == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "signature data not defined for device \"%s\"\n",
|
2014-05-18 08:41:46 +00:00
|
|
|
p->desc);
|
2001-11-21 02:46:55 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-10-11 19:36:56 +00:00
|
|
|
fprintf(stdout, "Device signature = 0x");
|
2001-11-21 02:46:55 +00:00
|
|
|
for (i=0; i<m->size; i++)
|
|
|
|
fprintf(stdout, "%02x", m->buf[i]);
|
|
|
|
fprintf(stdout, "\n\n");
|
|
|
|
}
|
2001-11-19 17:44:24 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_quit(PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char * argv[])
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2015-01-02 01:08:14 +00:00
|
|
|
/* FUSE bit verify will fail if left in SPI mode */
|
|
|
|
if (spi_mode) {
|
|
|
|
cmd_pgm(pgm, p, 0, NULL);
|
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_parms(PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char * argv[])
|
2003-07-24 21:26:28 +00:00
|
|
|
{
|
|
|
|
if (pgm->print_parms == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (parms): the %s programmer does not support "
|
2014-05-18 08:41:46 +00:00
|
|
|
"adjustable parameters\n",
|
|
|
|
progname, pgm->type);
|
2003-07-24 21:26:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pgm->print_parms(pgm);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_vtarg(PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char * argv[])
|
2003-07-24 21:26:28 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
double v;
|
|
|
|
char *endp;
|
|
|
|
|
|
|
|
if (argc != 2) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "Usage: vtarg <value>\n");
|
2003-07-24 21:26:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
v = strtod(argv[1], &endp);
|
|
|
|
if (endp == argv[1]) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (vtarg): can't parse voltage \"%s\"\n",
|
2003-07-24 21:26:28 +00:00
|
|
|
progname, argv[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (pgm->set_vtarget == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (vtarg): the %s programmer cannot set V[target]\n",
|
2003-07-24 21:26:28 +00:00
|
|
|
progname, pgm->type);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
if ((rc = pgm->set_vtarget(pgm, v)) != 0) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (vtarg): failed to set V[target] (rc = %d)\n",
|
2003-07-24 21:26:28 +00:00
|
|
|
progname, rc);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_fosc(PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char * argv[])
|
2003-07-24 21:26:28 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
double v;
|
|
|
|
char *endp;
|
|
|
|
|
|
|
|
if (argc != 2) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "Usage: fosc <value>[M|k] | off\n");
|
2003-07-24 21:26:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
v = strtod(argv[1], &endp);
|
|
|
|
if (endp == argv[1]) {
|
|
|
|
if (strcmp(argv[1], "off") == 0)
|
|
|
|
v = 0.0;
|
|
|
|
else {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (fosc): can't parse frequency \"%s\"\n",
|
2003-07-24 21:26:28 +00:00
|
|
|
progname, argv[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*endp == 'm' || *endp == 'M')
|
|
|
|
v *= 1e6;
|
|
|
|
else if (*endp == 'k' || *endp == 'K')
|
|
|
|
v *= 1e3;
|
|
|
|
if (pgm->set_fosc == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (fosc): the %s programmer cannot set oscillator frequency\n",
|
2014-05-18 08:41:46 +00:00
|
|
|
progname, pgm->type);
|
2003-07-24 21:26:28 +00:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
if ((rc = pgm->set_fosc(pgm, v)) != 0) {
|
2020-09-16 21:31:19 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (fosc): failed to set oscillator frequency (rc = %d)\n",
|
2003-07-24 21:26:28 +00:00
|
|
|
progname, rc);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_sck(PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char * argv[])
|
2004-07-07 08:59:07 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
double v;
|
|
|
|
char *endp;
|
|
|
|
|
|
|
|
if (argc != 2) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "Usage: sck <value>\n");
|
2004-07-07 08:59:07 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
v = strtod(argv[1], &endp);
|
|
|
|
if (endp == argv[1]) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (sck): can't parse period \"%s\"\n",
|
2004-07-07 08:59:07 +00:00
|
|
|
progname, argv[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
v *= 1e-6; /* Convert from microseconds to seconds. */
|
|
|
|
if (pgm->set_sck_period == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (sck): the %s programmer cannot set SCK period\n",
|
2014-05-18 08:41:46 +00:00
|
|
|
progname, pgm->type);
|
2004-07-07 08:59:07 +00:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
if ((rc = pgm->set_sck_period(pgm, v)) != 0) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (sck): failed to set SCK period (rc = %d)\n",
|
2004-07-07 08:59:07 +00:00
|
|
|
progname, rc);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_varef(PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char * argv[])
|
2003-07-24 21:26:28 +00:00
|
|
|
{
|
|
|
|
int rc;
|
2008-03-14 13:00:08 +00:00
|
|
|
unsigned int chan;
|
2003-07-24 21:26:28 +00:00
|
|
|
double v;
|
|
|
|
char *endp;
|
|
|
|
|
2008-03-14 13:00:08 +00:00
|
|
|
if (argc != 2 && argc != 3) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "Usage: varef [channel] <value>\n");
|
2003-07-24 21:26:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-03-14 13:00:08 +00:00
|
|
|
if (argc == 2) {
|
|
|
|
chan = 0;
|
|
|
|
v = strtod(argv[1], &endp);
|
|
|
|
if (endp == argv[1]) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (varef): can't parse voltage \"%s\"\n",
|
2008-03-14 13:00:08 +00:00
|
|
|
progname, argv[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
chan = strtoul(argv[1], &endp, 10);
|
|
|
|
if (endp == argv[1]) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (varef): can't parse channel \"%s\"\n",
|
2008-03-14 13:00:08 +00:00
|
|
|
progname, argv[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
v = strtod(argv[2], &endp);
|
|
|
|
if (endp == argv[2]) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (varef): can't parse voltage \"%s\"\n",
|
2008-03-14 13:00:08 +00:00
|
|
|
progname, argv[2]);
|
|
|
|
return -1;
|
|
|
|
}
|
2003-07-24 21:26:28 +00:00
|
|
|
}
|
|
|
|
if (pgm->set_varef == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (varef): the %s programmer cannot set V[aref]\n",
|
2003-07-24 21:26:28 +00:00
|
|
|
progname, pgm->type);
|
|
|
|
return -2;
|
|
|
|
}
|
2008-03-14 13:00:08 +00:00
|
|
|
if ((rc = pgm->set_varef(pgm, chan, v)) != 0) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s (varef): failed to set V[aref] (rc = %d)\n",
|
2003-07-24 21:26:28 +00:00
|
|
|
progname, rc);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int cmd_help(PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char * argv[])
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2001-10-13 03:13:13 +00:00
|
|
|
fprintf(stdout, "Valid commands:\n\n");
|
2001-01-19 02:46:50 +00:00
|
|
|
for (i=0; i<NCMDS; i++) {
|
2001-10-13 03:13:13 +00:00
|
|
|
fprintf(stdout, " %-6s : ", cmd[i].name);
|
2001-01-19 02:46:50 +00:00
|
|
|
fprintf(stdout, cmd[i].desc, cmd[i].name);
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
}
|
2001-11-21 02:46:55 +00:00
|
|
|
fprintf(stdout,
|
|
|
|
"\nUse the 'part' command to display valid memory types for use with the\n"
|
|
|
|
"'dump' and 'write' commands.\n\n");
|
2001-01-19 02:46:50 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-17 15:31:27 +00:00
|
|
|
static int cmd_spi(PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char * argv[])
|
|
|
|
{
|
2022-01-01 19:58:26 +00:00
|
|
|
if (pgm->setpin != NULL) {
|
|
|
|
pgm->setpin(pgm, PIN_AVR_RESET, 1);
|
|
|
|
spi_mode = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
avrdude_message(MSG_INFO, "`spi' command unavailable for this programmer type\n");
|
|
|
|
return -1;
|
2009-02-17 15:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int cmd_pgm(PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char * argv[])
|
|
|
|
{
|
2022-01-01 19:58:26 +00:00
|
|
|
if (pgm->setpin != NULL) {
|
|
|
|
pgm->setpin(pgm, PIN_AVR_RESET, 0);
|
|
|
|
spi_mode = 0;
|
|
|
|
pgm->initialize(pgm, p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
avrdude_message(MSG_INFO, "`pgm' command unavailable for this programmer type\n");
|
|
|
|
return -1;
|
2009-02-17 15:31:27 +00:00
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2013-09-08 19:31:48 +00:00
|
|
|
static int cmd_verbose(PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char * argv[])
|
|
|
|
{
|
|
|
|
int nverb;
|
|
|
|
char *endp;
|
|
|
|
|
|
|
|
if (argc != 1 && argc != 2) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "Usage: verbose [<value>]\n");
|
2013-09-08 19:31:48 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (argc == 1) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "Verbosity level: %d\n", verbose);
|
2013-09-08 19:31:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
nverb = strtol(argv[1], &endp, 0);
|
|
|
|
if (endp == argv[2]) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: can't parse verbosity level \"%s\"\n",
|
2013-09-08 19:31:48 +00:00
|
|
|
progname, argv[2]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (nverb < 0) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: verbosity level must be positive: %d\n",
|
2013-09-08 19:31:48 +00:00
|
|
|
progname, nverb);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
verbose = nverb;
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "New verbosity level: %d\n", verbose);
|
2013-09-08 19:31:48 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int tokenize(char * s, char *** argv)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2021-11-12 22:11:49 +00:00
|
|
|
int i, n, l, k, nargs, offset;
|
2001-01-19 02:46:50 +00:00
|
|
|
int len, slen;
|
|
|
|
char * buf;
|
|
|
|
int bufsize;
|
|
|
|
char ** bufv;
|
2021-11-12 22:11:49 +00:00
|
|
|
char * bufp;
|
2001-01-19 02:46:50 +00:00
|
|
|
char * q, * r;
|
|
|
|
char * nbuf;
|
|
|
|
char ** av;
|
|
|
|
|
|
|
|
slen = strlen(s);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize allow for 20 arguments, use realloc to grow this if
|
|
|
|
* necessary
|
|
|
|
*/
|
|
|
|
nargs = 20;
|
|
|
|
bufsize = slen + 20;
|
|
|
|
buf = malloc(bufsize);
|
|
|
|
bufv = (char **) malloc(nargs*sizeof(char *));
|
|
|
|
for (i=0; i<nargs; i++) {
|
|
|
|
bufv[i] = NULL;
|
|
|
|
}
|
|
|
|
buf[0] = 0;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
l = 0;
|
|
|
|
nbuf = buf;
|
|
|
|
r = s;
|
|
|
|
while (*r) {
|
|
|
|
nexttok(r, &q, &r);
|
|
|
|
strcpy(nbuf, q);
|
|
|
|
bufv[n] = nbuf;
|
|
|
|
len = strlen(q);
|
|
|
|
l += len + 1;
|
|
|
|
nbuf += len + 1;
|
|
|
|
nbuf[0] = 0;
|
|
|
|
n++;
|
|
|
|
if ((n % 20) == 0) {
|
2022-01-06 10:28:39 +00:00
|
|
|
char *buf_tmp;
|
|
|
|
char **bufv_tmp;
|
2001-01-19 02:46:50 +00:00
|
|
|
/* realloc space for another 20 args */
|
|
|
|
bufsize += 20;
|
|
|
|
nargs += 20;
|
2021-11-12 22:11:49 +00:00
|
|
|
bufp = buf;
|
2022-01-06 10:28:39 +00:00
|
|
|
buf_tmp = realloc(buf, bufsize);
|
|
|
|
if (buf_tmp == NULL) {
|
|
|
|
free(buf);
|
|
|
|
free(bufv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buf = buf_tmp;
|
|
|
|
bufv_tmp = realloc(bufv, nargs*sizeof(char *));
|
|
|
|
if (bufv_tmp == NULL) {
|
|
|
|
free(buf);
|
|
|
|
free(bufv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
bufv = bufv_tmp;
|
2001-01-19 02:46:50 +00:00
|
|
|
nbuf = &buf[l];
|
2021-11-12 22:11:49 +00:00
|
|
|
/* correct bufv pointers */
|
|
|
|
k = buf - bufp;
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
bufv[i] = bufv[i] + k;
|
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
for (i=n; i<nargs; i++)
|
|
|
|
bufv[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have parsed all the args, n == argc, bufv contains an array of
|
|
|
|
* pointers to each arg, and buf points to one memory block that
|
|
|
|
* contains all the args, back to back, seperated by a nul
|
|
|
|
* terminator. Consilidate bufv and buf into one big memory block
|
|
|
|
* so that the code that calls us, will have an easy job of freeing
|
|
|
|
* this memory.
|
|
|
|
*/
|
|
|
|
av = (char **) malloc(slen + n + (n+1)*sizeof(char *));
|
|
|
|
q = (char *)&av[n+1];
|
|
|
|
memcpy(q, buf, l);
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
offset = bufv[i] - buf;
|
|
|
|
av[i] = q + offset;
|
|
|
|
}
|
|
|
|
av[i] = NULL;
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
free(bufv);
|
|
|
|
|
|
|
|
*argv = av;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int do_cmd(PROGRAMMER * pgm, struct avrpart * p,
|
|
|
|
int argc, char * argv[])
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int hold;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = strlen(argv[0]);
|
|
|
|
hold = -1;
|
|
|
|
for (i=0; i<NCMDS; i++) {
|
|
|
|
if (strcasecmp(argv[0], cmd[i].name) == 0) {
|
2002-11-30 14:09:12 +00:00
|
|
|
return cmd[i].func(pgm, p, argc, argv);
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
else if (strncasecmp(argv[0], cmd[i].name, len)==0) {
|
|
|
|
if (hold != -1) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: command \"%s\" is ambiguous\n",
|
2001-01-19 02:46:50 +00:00
|
|
|
progname, argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
hold = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hold != -1)
|
2002-11-30 14:09:12 +00:00
|
|
|
return cmd[hold].func(pgm, p, argc, argv);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: invalid command \"%s\"\n",
|
2001-01-19 02:46:50 +00:00
|
|
|
progname, argv[0]);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-27 20:08:54 +00:00
|
|
|
char * terminal_get_input(const char *prompt)
|
|
|
|
{
|
2022-01-07 12:15:55 +00:00
|
|
|
#if defined(HAVE_LIBREADLINE) && !defined(WIN32)
|
2003-02-27 20:08:54 +00:00
|
|
|
char *input;
|
|
|
|
input = readline(prompt);
|
|
|
|
if ((input != NULL) && (strlen(input) >= 1))
|
|
|
|
add_history(input);
|
|
|
|
|
|
|
|
return input;
|
|
|
|
#else
|
|
|
|
char input[256];
|
|
|
|
printf("%s", prompt);
|
|
|
|
if (fgets(input, sizeof(input), stdin))
|
|
|
|
{
|
|
|
|
/* FIXME: readline strips the '\n', should this too? */
|
2004-06-24 11:05:07 +00:00
|
|
|
return strdup(input);
|
2003-02-27 20:08:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-30 14:09:12 +00:00
|
|
|
int terminal_mode(PROGRAMMER * pgm, struct avrpart * p)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
char * cmdbuf;
|
2003-02-27 20:08:54 +00:00
|
|
|
int i;
|
2001-01-19 02:46:50 +00:00
|
|
|
char * q;
|
|
|
|
int rc;
|
|
|
|
int argc;
|
|
|
|
char ** argv;
|
|
|
|
|
|
|
|
rc = 0;
|
2003-02-27 20:08:54 +00:00
|
|
|
while ((cmdbuf = terminal_get_input("avrdude> ")) != NULL) {
|
2001-01-19 02:46:50 +00:00
|
|
|
/*
|
|
|
|
* find the start of the command, skipping any white space
|
|
|
|
*/
|
|
|
|
q = cmdbuf;
|
2010-01-22 16:40:17 +00:00
|
|
|
while (*q && isspace((int)*q))
|
2001-01-19 02:46:50 +00:00
|
|
|
q++;
|
|
|
|
|
|
|
|
/* skip blank lines and comments */
|
|
|
|
if (!*q || (*q == '#'))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* tokenize command line */
|
|
|
|
argc = tokenize(q, &argv);
|
2022-01-06 10:28:39 +00:00
|
|
|
if (argc < 0) {
|
|
|
|
free(cmdbuf);
|
|
|
|
return argc;
|
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
|
|
|
|
fprintf(stdout, ">>> ");
|
|
|
|
for (i=0; i<argc; i++)
|
|
|
|
fprintf(stdout, "%s ", argv[i]);
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
|
|
|
/* run the command */
|
2002-11-30 14:09:12 +00:00
|
|
|
rc = do_cmd(pgm, p, argc, argv);
|
2001-01-19 02:46:50 +00:00
|
|
|
free(argv);
|
|
|
|
if (rc > 0) {
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(cmdbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|