Skip to content

Commit

Permalink
Merge pull request #1844 from MCUdude/avr109-autorst
Browse files Browse the repository at this point in the history
Extended parameters (`-x` and `-E`) improvements
  • Loading branch information
stefanrueger authored Jul 31, 2024
2 parents cbe0ef9 + 92a2729 commit 0fe0470
Show file tree
Hide file tree
Showing 28 changed files with 768 additions and 520 deletions.
86 changes: 61 additions & 25 deletions src/arduino.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,43 @@
#include "stk500.h"
#include "arduino.h"

static int arduino_parseextparms(const PROGRAMMER *pgm, const LISTID extparms) {
int attempts;
int rv = 0;
bool help = 0;

for (LNODEID ln = lfirst(extparms); ln; ln = lnext(ln)) {
const char *extended_param = ldata(ln);

if (sscanf(extended_param, "attempts=%i", &attempts) == 1) {
PDATA(pgm)->retry_attempts = attempts;
pmsg_info("setting number of retry attempts to %d\n", attempts);
continue;
}

if(str_eq(extended_param, "noautoreset")) {
PDATA(pgm)->autoreset = false;
continue;
}

if (str_eq(extended_param, "help")) {
help = true;
rv = LIBAVRDUDE_EXIT;
}

if (!help) {
pmsg_error("invalid extended parameter -x %s\n", extended_param);
rv = -1;
}
msg_error("%s -c %s extended options:\n", progname, pgmid);
msg_error(" -x attempts=<n> Specify the number <n> of connection retry attempts\n");
msg_error(" -x noautoreset Don't toggle RTS/DTR lines on port open to prevent a hardware reset\n");
msg_error(" -x help Show this help menu and exit\n");
return rv;
}
return rv;
}

/* read signature bytes - arduino version */
static int arduino_read_sig_bytes(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m) {
unsigned char buf[32];
Expand Down Expand Up @@ -85,28 +122,28 @@ static int arduino_open(PROGRAMMER *pgm, const char *port) {
return -1;
}

// This code assumes a negative-logic USB to TTL serial adapter
// Set RTS/DTR high to discharge the series-capacitor, if present
serial_set_dtr_rts(&pgm->fd, 0);
/*
* Long wait needed for optiboot: otherwise the second of two bootloader
* calls in quick succession fails:
*
* avrdude -c arduino -qqp m328p -U x.hex; avrdude -c arduino -qqp m328p -U x.hex
*/
usleep(250 * 1000);
// Pull the RTS/DTR line low to reset AVR
serial_set_dtr_rts(&pgm->fd, 1);
// Max 100 us: charging a cap longer creates a high reset spike above Vcc
usleep(100);
// Set the RTS/DTR line back to high, so direct connection to reset works
serial_set_dtr_rts(&pgm->fd, 0);

usleep(100 * 1000);

/*
* drain any extraneous input
*/
if(PDATA(pgm)->autoreset) {
// This code assumes a negative-logic USB to TTL serial adapter
// Set RTS/DTR high to discharge the series-capacitor, if present
serial_set_dtr_rts(&pgm->fd, 0);
/*
* Long wait needed for optiboot: otherwise the second of two bootloader
* calls in quick succession fails:
*
* avrdude -c arduino -qqp m328p -U x.hex; avrdude -c arduino -qqp m328p -U x.hex
*/
usleep(250 * 1000);
// Pull the RTS/DTR line low to reset AVR
serial_set_dtr_rts(&pgm->fd, 1);
// Max 100 us: charging a cap longer creates a high reset spike above Vcc
usleep(100);
// Set the RTS/DTR line back to high, so direct connection to reset works
serial_set_dtr_rts(&pgm->fd, 0);

usleep(100 * 1000);
}

// Drain any extraneous input
stk500_drain(pgm, 0);

if (stk500_getsync(pgm) < 0)
Expand All @@ -115,8 +152,7 @@ static int arduino_open(PROGRAMMER *pgm, const char *port) {
return 0;
}

static void arduino_close(PROGRAMMER * pgm)
{
static void arduino_close(PROGRAMMER *pgm) {
serial_close(&pgm->fd);
pgm->fd.ifd = -1;
}
Expand All @@ -129,11 +165,11 @@ void arduino_initpgm(PROGRAMMER *pgm) {
and the DTR signal is set when opening the serial port
for the Auto-Reset feature */
stk500_initpgm(pgm);

strcpy(pgm->type, "Arduino");
pgm->read_sig_bytes = arduino_read_sig_bytes;
pgm->open = arduino_open;
pgm->close = arduino_close;
pgm->parseextparams = arduino_parseextparms;

cx->avr_disableffopt = 1; // Disable trailing 0xff removal
}
29 changes: 16 additions & 13 deletions src/avr910.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,20 +308,19 @@ static int avr910_cmd(const PROGRAMMER *pgm, const unsigned char *cmd,


static int avr910_parseextparms(const PROGRAMMER *pgm, const LISTID extparms) {
LNODEID ln;
const char *extended_param;
int rv = 0;
bool help = false;

for (ln = lfirst(extparms); ln; ln = lnext(ln)) {
extended_param = ldata(ln);
for (LNODEID ln = lfirst(extparms); ln; ln = lnext(ln)) {
const char *extended_param = ldata(ln);

if (str_starts(extended_param, "devcode=")) {
int devcode;
if (sscanf(extended_param, "devcode=%i", &devcode) != 1 ||
devcode <= 0 || devcode > 255) {
pmsg_error("invalid devcode '%s'\n", extended_param);
pmsg_error("invalid device code in -x %s\n", extended_param);
rv = -1;
continue;
break;
}
pmsg_notice2("avr910_parseextparms(): devcode overwritten as 0x%02x\n", devcode);
PDATA(pgm)->devcode = devcode;
Expand All @@ -335,15 +334,19 @@ static int avr910_parseextparms(const PROGRAMMER *pgm, const LISTID extparms) {
continue;
}
if (str_eq(extended_param, "help")) {
msg_error("%s -c %s extended options:\n", progname, pgmid);
msg_error(" -xdevcode=<arg> Override device code\n");
msg_error(" -xno_blockmode Disable default checking for block transfer capability\n");
msg_error(" -xhelp Show this help menu and exit\n");
return LIBAVRDUDE_EXIT;
help = true;
rv = LIBAVRDUDE_EXIT;
}

pmsg_error("invalid extended parameter '%s'\n", extended_param);
rv = -1;
if (!help) {
pmsg_error("invalid extended parameter -x %s\n", extended_param);
rv = -1;
}
msg_error("%s -c %s extended options:\n", progname, pgmid);
msg_error(" -x devcode=<n> Set device code to <n> (0x.. hex, 0... oct or dec)\n");
msg_error(" -x no_blockmode Disable default checking for block transfer capability\n");
msg_error(" -x help Show this help menu and exit\n");
return rv;
}

return rv;
Expand Down
44 changes: 22 additions & 22 deletions src/avrdude.1
Original file line number Diff line number Diff line change
Expand Up @@ -1749,14 +1749,14 @@ Other JTAG units might require a different bit shift count.
.sp 0.5
High-voltage UPDI programming is used to enable a UPDI pin that has previously
been set to RESET or GPIO mode. Use
.Ar -xhvupdi
.Ar -x hvupdi
to enable high-voltage UPDI initialization for targets that supports this.
.It Ar vtarg=VALUE, vtarg
.Nm Power Debugger only
.sp 0.5
The voltage generator can be enabled by setting a target voltage.
The current set-voltage can be read by
.Ar -xvtarg
.Ar -x vtarg
alone.
.It Ar help
Show help menu and exit.
Expand All @@ -1769,9 +1769,9 @@ Switch programmer to AVR or PIC mode, then exit: the PICkit 4 and MPLAB SNAP
programmer can only be utilised by
.Nm
when in AVR mode. Use
.Ar -xmode=avr
.Ar -x mode=avr
for switching to AVR mode, or
.Ar -xmode=pic
.Ar -x mode=pic
for switching to PIC mode.
.It Ar help
Show help menu and exit.
Expand All @@ -1781,7 +1781,7 @@ Show help menu and exit.
.It Ar suffer=VALUE, suffer
The SUFFER register allows the user to modify the behavior of the on-board mEDBG.
The current state can be read by
.Ar -xsuffer
.Ar -x suffer
alone.
.Bl -tag -offset indent -width indent
.It Bit 7 ARDUINO:
Expand All @@ -1801,7 +1801,7 @@ Fuses are safe-masked when bit sent to 1. Fuses are unprotected when set to 0
.It Ar vtarg_switch=VALUE, vtarg_switch
The on-board target voltage switch can be turned on or off by writing a 1 or
a 0. The current state can be read by
.Ar -xvtarg_switch
.Ar -x vtarg_switch
alone.
Note that the target power switch will always be on after a power cycle.
Also note that the smaller Xplained Nano boards does not have a target power switch.
Expand All @@ -1813,7 +1813,7 @@ Show help menu and exit.
.It Ar vtarg=VALUE, vtarg
The generated on-board target voltage can be changed by specifying a new voltage.
The current set-voltage can be read by
.Ar -xvtarg
.Ar -x vtarg
alone.
.It Ar help
Show help menu and exit.
Expand All @@ -1824,26 +1824,26 @@ Show help menu and exit.
.It Ar vtarg=VALUE, vtarg
The generated on-board target voltage can be changed by specifying a new voltage.
The current set-voltage can be read by
.Ar -xvtarg
.Ar -x vtarg
alone.
.It Ar fosc=VALUE[MHz|M|kHz|k|Hz|H], fosc
Set the programmable oscillator frequency. The current frequency can be read by
.Ar -xfosc
.Ar -x fosc
alone.
.It Ar varef=VALUE, varef
The generated on-board analog reference voltage can be changed by specifying
a new reference voltage. The current reference voltage can be read by
.Ar -xvaref
.Ar -x varef
alone.
.It Ar varef[0,1]=VALUE, varef[0,1]
.Nm STK600 only
.sp 0.5
The generated on-board analog reference voltage for channel 0 or channel 1 can
be changed by specifying a new reference voltage.
The current reference voltage can be read by
.Ar -xvaref0
.Ar -x varef0
or
.Ar -xvaref1
.Ar -x varef1
alone.
.It Ar attemps[=<1..99>]
.Nm STK500V1 only
Expand Down Expand Up @@ -1894,7 +1894,7 @@ Show help menu and exit.
.It Ar Urclock
.Bl -tag -offset indent -width indent
.It Ar showall
Show all info for the connected part, then exit. The -xshow... options
Show all info for the connected part, then exit. The -x show... options
below can be used to assemble a bespoke response consisting of a subset
(or only one item) of all available relevant information about the
connected part and bootloader.
Expand All @@ -1903,7 +1903,7 @@ Show a unique Urclock ID stored in either flash or EEPROM of the MCU, then exit.
.It Ar id=<E|F>.<addr>.<len>
Historically, the Urclock ID was a six-byte unique little-endian number
stored in Urclock boards at EEPROM address 257. The location of this
number can be set by the -xid=<E|F>.<addr>.<len> extended parameter. E
number can be set by the -x id=<E|F>.<addr>.<len> extended parameter. E
stands for EEPROM and F stands for flash. A negative address addr counts
from the end of EEPROM and flash, respectively. The length len of the
Urclock ID can be between 1 and 8 bytes.
Expand All @@ -1912,7 +1912,7 @@ Show the last-modified date of the input file for the flash application,
then exit. If the input file was stdin, the date will be that of the
programming. Date and filename are part of the metadata that the urclock
programmer stores by default in high flash just under the bootloader; see also
-xnometadata.
-x nometadata.
.It Ar showfilename
Show the input filename (or title) of the last flash writing session, then exit.
.It Ar title=<string>
Expand Down Expand Up @@ -1944,7 +1944,7 @@ i.e., typically top of flash, so the urclock programmer can look up the
bootloader size itself. In backward-compatibility mode, when programming
via other bootloaders, this option can be used to tell the programmer the
size, and therefore the location, of the bootloader.
.It Ar vectornum=<arg>
.It Ar vectornum=<n>
Manual override for vector number. Urboot bootloaders put the vector
number used by a vector bootloader into a table at the top of flash, so
this option is normally not needed for urboot bootloaders. However, it is
Expand All @@ -1969,7 +1969,7 @@ Upload unchanged flash input files and trim below the bootloader if
needed. This is most useful when one has a backup of the full flash and
wants to play that back onto the device. No metadata are written in this
case and no vector patching happens either if it is a vector bootloader.
However, for vector bootloaders, even under the option -xrestore an
However, for vector bootloaders, even under the option -x restore an
input file will not be uploaded for which the reset vector does not point
to the vector bootloader. This is to avoid writing an input file to the
device that would render the vector bootloader not functional as it would
Expand All @@ -1991,15 +1991,15 @@ Do not support any metadata. The full flash besides the bootloader is
available for the application. If the application is smaller than the
available space then a metadata code byte 0xff is stored nevertheless to
indicate there are no further metadata available. In absence of
-xnometadata, the default for the urclock programmer is to write as much
-x nometadata, the default for the urclock programmer is to write as much
metadata (filename, data and store information) as the size of the
uploaded application and the other extended options allow. The subtle
difference between -xnometadata and -xnostore is that the latter always
difference between -x nometadata and -x nostore is that the latter always
explicitly stores in flash that no further metadata are available, so that
a such prepared flash can always be queried with
.Nm
-xshowall. In contrast to this, it cannot be guaranteed that a -xshowall
query on flash prepared with -xnometadata yields useful results.
-x showall. In contrast to this, it cannot be guaranteed that a -x showall
query on flash prepared with -x nometadata yields useful results.
.It Ar delay=<n>
Add a <n> ms delay after reset. This can be useful if a board takes a
particularly long time to exit from external reset. <n> can be negative,
Expand All @@ -2008,7 +2008,7 @@ shortened accordingly.
.It Ar strict
Urclock has a faster, but slightly different strategy than -c arduino to
synchronise with the bootloader; some stk500v1 bootloaders cannot cope
with this, and they need the -xstrict option.
with this, and they need the -x strict option.
.It Ar help
Show help menu and exit.
.El
Expand Down
2 changes: 1 addition & 1 deletion src/avrftdi.c
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ static int avrftdi_open(PROGRAMMER *pgm, const char *port) {
else if (pgm->usbdev[0] == 'b' || pgm->usbdev[0] == 'B')
interface = INTERFACE_B;
else {
pmsg_warning("invalid interface '%s'. Setting to Interface A\n", pgm->usbdev);
pmsg_warning("invalid interface %s; setting to Interface A\n", pgm->usbdev);
interface = INTERFACE_A;
}

Expand Down
Loading

0 comments on commit 0fe0470

Please sign in to comment.