2009-02-25 09:39:04 +00:00
|
|
|
/*
|
|
|
|
* avrdude - A Downloader/Uploader for AVR device programmers
|
|
|
|
* Copyright (C) 2009 Lars Immisch
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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/>.
|
2009-02-25 09:39:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* avrdude interface for Arduino programmer
|
|
|
|
*
|
|
|
|
* The Arduino programmer is mostly a STK500v1, just the signature bytes
|
|
|
|
* are read differently.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ac_cfg.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2009-10-10 21:53:26 +00:00
|
|
|
#include <unistd.h>
|
2009-02-25 09:39:04 +00:00
|
|
|
|
|
|
|
#include "avrdude.h"
|
2014-05-19 10:01:59 +00:00
|
|
|
#include "libavrdude.h"
|
2009-02-25 09:39:04 +00:00
|
|
|
#include "stk500_private.h"
|
|
|
|
#include "stk500.h"
|
2012-01-31 17:03:43 +00:00
|
|
|
#include "arduino.h"
|
2009-02-25 09:39:04 +00:00
|
|
|
|
|
|
|
/* read signature bytes - arduino version */
|
Use const in PROGRAMMER function arguments where appropriate
In order to get meaningful const properties for the PROGRAMMER, AVRPART and
AVRMEM arguments, some code needed to be moved around, otherwise a network of
"tainted" assignments risked rendering nothing const:
- Change void (*enable)(PROGRAMMER *pgm) to void (*enable)(PROGRAMMER *pgm,
const AVRPART *p); this allows changes in the PROGRAMMER structure after
the part is known. For example, use TPI, UPDI, PDI functions in that
programmer appropriate to the part. This used to be done later in the
process, eg, in the initialize() function, which "taints" all other
programmer functions wrt const and sometimes requires other finessing with
flags etc. Much clearer with the modified enable() interface.
- Move TPI initpgm-type code from initialize() to enable() --- note that
initpgm() does not have the info at the time when it is called whether or
not TPI is required
- buspirate.c: move pgm->flag to PDATA(pgm)->flag (so legitimate
modification of the flag does not change PROGRAMMER structure)
- Move AVRPART_INIT_SMC and AVRPART_WRITE bits from the flags field in
AVRPART to jtagmkII.c's private data flags32 fiels as FLAGS32_INIT_SMC and
FLAGS32_WRITE bits
- Move the xbeeResetPin component to private data in stk500.c as this is
needed by xbee when it saddles on the stk500 code (previously, the flags
component of the part was re-dedicated to this)
- Change the way the "chained" private data are used in jtag3.c whilst
keeping the PROGRAMMER structure read-only otherwise
- In stk500v2.c move the STK600 pgm update from stk500v2_initialize() to
stk500v2_enable() so the former keeps the PROGRAMMER structure read-only
(for const assertion).
- In usbasp change the code from changing PROGRAMMER functions late to
dispatching to TPI or regular SPI protocol functions at runtime; reason
being the decision whether to use TPI protocol is done at run-time
depending on the capability of the attached programmer
Also fixes Issue #1071, the treatment of default eecr value.
2022-08-17 15:05:28 +00:00
|
|
|
static int arduino_read_sig_bytes(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m) {
|
2009-02-25 09:39:04 +00:00
|
|
|
unsigned char buf[32];
|
|
|
|
|
|
|
|
/* Signature byte reads are always 3 bytes. */
|
|
|
|
|
|
|
|
if (m->size < 3) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("memsize too small for sig byte read");
|
2009-02-25 09:39:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[0] = Cmnd_STK_READ_SIGN;
|
|
|
|
buf[1] = Sync_CRC_EOP;
|
|
|
|
|
|
|
|
serial_send(&pgm->fd, buf, 2);
|
|
|
|
|
|
|
|
if (serial_recv(&pgm->fd, buf, 5) < 0)
|
|
|
|
return -1;
|
|
|
|
if (buf[0] == Resp_STK_NOSYNC) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("programmer is out of sync\n");
|
2009-02-25 09:39:04 +00:00
|
|
|
return -1;
|
|
|
|
} else if (buf[0] != Resp_STK_INSYNC) {
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_error("\n");
|
|
|
|
pmsg_error("protocol expects sync byte 0x%02x but got 0x%02x\n", Resp_STK_INSYNC, buf[0]);
|
|
|
|
return -2;
|
2009-02-25 09:39:04 +00:00
|
|
|
}
|
|
|
|
if (buf[4] != Resp_STK_OK) {
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_error("\n");
|
|
|
|
pmsg_error("protocol expects OK byte 0x%02x but got 0x%02x\n", Resp_STK_OK, buf[4]);
|
2009-02-25 09:39:04 +00:00
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
m->buf[0] = buf[1];
|
|
|
|
m->buf[1] = buf[2];
|
|
|
|
m->buf[2] = buf[3];
|
|
|
|
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
Use const in PROGRAMMER function arguments where appropriate
In order to get meaningful const properties for the PROGRAMMER, AVRPART and
AVRMEM arguments, some code needed to be moved around, otherwise a network of
"tainted" assignments risked rendering nothing const:
- Change void (*enable)(PROGRAMMER *pgm) to void (*enable)(PROGRAMMER *pgm,
const AVRPART *p); this allows changes in the PROGRAMMER structure after
the part is known. For example, use TPI, UPDI, PDI functions in that
programmer appropriate to the part. This used to be done later in the
process, eg, in the initialize() function, which "taints" all other
programmer functions wrt const and sometimes requires other finessing with
flags etc. Much clearer with the modified enable() interface.
- Move TPI initpgm-type code from initialize() to enable() --- note that
initpgm() does not have the info at the time when it is called whether or
not TPI is required
- buspirate.c: move pgm->flag to PDATA(pgm)->flag (so legitimate
modification of the flag does not change PROGRAMMER structure)
- Move AVRPART_INIT_SMC and AVRPART_WRITE bits from the flags field in
AVRPART to jtagmkII.c's private data flags32 fiels as FLAGS32_INIT_SMC and
FLAGS32_WRITE bits
- Move the xbeeResetPin component to private data in stk500.c as this is
needed by xbee when it saddles on the stk500 code (previously, the flags
component of the part was re-dedicated to this)
- Change the way the "chained" private data are used in jtag3.c whilst
keeping the PROGRAMMER structure read-only otherwise
- In stk500v2.c move the STK600 pgm update from stk500v2_initialize() to
stk500v2_enable() so the former keeps the PROGRAMMER structure read-only
(for const assertion).
- In usbasp change the code from changing PROGRAMMER functions late to
dispatching to TPI or regular SPI protocol functions at runtime; reason
being the decision whether to use TPI protocol is done at run-time
depending on the capability of the attached programmer
Also fixes Issue #1071, the treatment of default eecr value.
2022-08-17 15:05:28 +00:00
|
|
|
static int arduino_open(PROGRAMMER *pgm, const char *port) {
|
2014-02-21 13:44:11 +00:00
|
|
|
union pinfo pinfo;
|
2009-10-10 01:41:40 +00:00
|
|
|
strcpy(pgm->port, port);
|
2021-12-08 10:09:52 +00:00
|
|
|
pinfo.serialinfo.baud = pgm->baudrate? pgm->baudrate: 115200;
|
|
|
|
pinfo.serialinfo.cflags = SERIAL_8N1;
|
2014-02-21 13:44:11 +00:00
|
|
|
if (serial_open(port, pinfo, &pgm->fd)==-1) {
|
2010-10-22 14:29:56 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2009-10-10 01:41:40 +00:00
|
|
|
|
|
|
|
/* Clear DTR and RTS to unload the RESET capacitor
|
|
|
|
* (for example in Arduino) */
|
|
|
|
serial_set_dtr_rts(&pgm->fd, 0);
|
2012-01-10 07:22:20 +00:00
|
|
|
usleep(250*1000);
|
2009-10-10 01:41:40 +00:00
|
|
|
/* Set DTR and RTS back to high */
|
|
|
|
serial_set_dtr_rts(&pgm->fd, 1);
|
|
|
|
usleep(50*1000);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* drain any extraneous input
|
|
|
|
*/
|
|
|
|
stk500_drain(pgm, 0);
|
|
|
|
|
|
|
|
if (stk500_getsync(pgm) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-02 23:52:52 +00:00
|
|
|
static void arduino_close(PROGRAMMER * pgm)
|
|
|
|
{
|
|
|
|
serial_set_dtr_rts(&pgm->fd, 0);
|
|
|
|
serial_close(&pgm->fd);
|
|
|
|
pgm->fd.ifd = -1;
|
|
|
|
}
|
2009-10-10 01:41:40 +00:00
|
|
|
|
2012-01-31 17:03:43 +00:00
|
|
|
const char arduino_desc[] = "Arduino programmer";
|
|
|
|
|
Use const in PROGRAMMER function arguments where appropriate
In order to get meaningful const properties for the PROGRAMMER, AVRPART and
AVRMEM arguments, some code needed to be moved around, otherwise a network of
"tainted" assignments risked rendering nothing const:
- Change void (*enable)(PROGRAMMER *pgm) to void (*enable)(PROGRAMMER *pgm,
const AVRPART *p); this allows changes in the PROGRAMMER structure after
the part is known. For example, use TPI, UPDI, PDI functions in that
programmer appropriate to the part. This used to be done later in the
process, eg, in the initialize() function, which "taints" all other
programmer functions wrt const and sometimes requires other finessing with
flags etc. Much clearer with the modified enable() interface.
- Move TPI initpgm-type code from initialize() to enable() --- note that
initpgm() does not have the info at the time when it is called whether or
not TPI is required
- buspirate.c: move pgm->flag to PDATA(pgm)->flag (so legitimate
modification of the flag does not change PROGRAMMER structure)
- Move AVRPART_INIT_SMC and AVRPART_WRITE bits from the flags field in
AVRPART to jtagmkII.c's private data flags32 fiels as FLAGS32_INIT_SMC and
FLAGS32_WRITE bits
- Move the xbeeResetPin component to private data in stk500.c as this is
needed by xbee when it saddles on the stk500 code (previously, the flags
component of the part was re-dedicated to this)
- Change the way the "chained" private data are used in jtag3.c whilst
keeping the PROGRAMMER structure read-only otherwise
- In stk500v2.c move the STK600 pgm update from stk500v2_initialize() to
stk500v2_enable() so the former keeps the PROGRAMMER structure read-only
(for const assertion).
- In usbasp change the code from changing PROGRAMMER functions late to
dispatching to TPI or regular SPI protocol functions at runtime; reason
being the decision whether to use TPI protocol is done at run-time
depending on the capability of the attached programmer
Also fixes Issue #1071, the treatment of default eecr value.
2022-08-17 15:05:28 +00:00
|
|
|
void arduino_initpgm(PROGRAMMER *pgm) {
|
2012-01-31 17:03:43 +00:00
|
|
|
/* This is mostly a STK500; just the signature is read
|
2009-10-10 01:41:40 +00:00
|
|
|
differently than on real STK500v1
|
|
|
|
and the DTR signal is set when opening the serial port
|
|
|
|
for the Auto-Reset feature */
|
2009-02-25 09:39:04 +00:00
|
|
|
stk500_initpgm(pgm);
|
|
|
|
|
|
|
|
strcpy(pgm->type, "Arduino");
|
|
|
|
pgm->read_sig_bytes = arduino_read_sig_bytes;
|
2009-10-10 01:41:40 +00:00
|
|
|
pgm->open = arduino_open;
|
2009-11-02 23:52:52 +00:00
|
|
|
pgm->close = arduino_close;
|
2022-04-27 17:54:13 +00:00
|
|
|
|
2022-04-28 16:26:09 +00:00
|
|
|
disable_trailing_ff_removal(); /* so that arduino bootloader can ignore chip erase */
|
2009-02-25 09:39:04 +00:00
|
|
|
}
|