From dcca02ebbf3003e2d070a30cdf5c9890b363b761 Mon Sep 17 00:00:00 2001 From: "Brian S. Dean" Date: Sun, 1 Dec 2002 04:30:01 +0000 Subject: [PATCH] Add basic support for STK500. git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@160 81a1dc3b-b13d-400b-aceb-764788c761c2 --- avrdude.conf.sample | 74 +++++ avrpart.h | 59 ++++ config_gram.y | 11 +- lexer.l | 1 + pgm.h | 4 + ppi.c | 78 +++++ ppi.h | 74 +---- stk500.c | 715 ++++++++++++++++++++++++++++++++++++++++++++ stk500.h | 41 +++ stk500_private.h | 103 +++++++ 10 files changed, 1086 insertions(+), 74 deletions(-) create mode 100644 avrpart.h create mode 100644 stk500.c create mode 100644 stk500.h create mode 100644 stk500_private.h diff --git a/avrdude.conf.sample b/avrdude.conf.sample index 5ff61e08..1385c09f 100644 --- a/avrdude.conf.sample +++ b/avrdude.conf.sample @@ -118,6 +118,12 @@ programmer miso = 10; ; +programmer + id = "stk500"; + desc = "Atmel STK500"; + type = stk500; +; + programmer id = "stk200"; desc = "STK200"; @@ -156,9 +162,64 @@ programmer ; + + + + + + +#define ATTINY10 0x10 +#define ATTINY11 0x11 +#define ATTINY12 0x12 + +#define ATTINY22 0x20 +#define ATTINY26 0x21 +#define ATTINY28 0x22 + +#define AT90S1200 0x33 + +#define AT90S2313 0x40 +#define AT90S2323 0x41 +#define AT90S2333 0x42 +#define AT90S2343 0x43 + +#define AT90S4414 0x50 +#define AT90S4433 0x51 +#define AT90S4434 0x52 + +#define AT90S8515 0x60 +#define AT90S8535 0x61 +#define AT90C8534 0x62 +#define ATMEGA8515 0x63 +#define ATMEGA8535 0x64 + +#define ATMEGA8 0x70 + +#define ATMEGA161 0x80 +#define ATMEGA163 0x81 +#define ATMEGA16 0x82 +#define ATMEGA162 0x83 +#define ATMEGA169 0x84 + +#define ATMEGA323 0x90 +#define ATMEGA32 0x91 + +#define ATMEGA103 0xB1 +#define ATMEGA128 0xB2 + +#define AT86RF401 0xD0 + +#define AT89START 0xE0 +#define AT89S51 0xE0 +#define AT89S52 0xE1 + + + + part id = "t15"; desc = "ATtiny15"; + devicecode = 0x13; chip_erase_delay = 20000; pgm_enable = "1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1", "x x x x x x x x x x x x x x x x"; @@ -242,6 +303,7 @@ part part id = "1200"; desc = "AT90S1200"; + devicecode = 0x33; chip_erase_delay = 20000; pgm_enable = "1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1", "x x x x x x x x x x x x x x x x"; @@ -318,6 +380,7 @@ part part id = "4414"; desc = "AT90S4414"; + devicecode = 0x50; chip_erase_delay = 20000; pgm_enable = "1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1", "x x x x x x x x x x x x x x x x"; @@ -374,6 +437,7 @@ part part id = "2313"; desc = "AT90S2313"; + devicecode = 0x40; chip_erase_delay = 20000; pgm_enable = "1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1", "x x x x x x x x x x x x x x x x"; @@ -435,6 +499,7 @@ part part id = "2333"; desc = "AT90S2333"; + devicecode = 0x42; chip_erase_delay = 20000; pgm_enable = "1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1", "x x x x x x x x x x x x x x x x"; @@ -512,6 +577,7 @@ part part id = "4433"; desc = "AT90S4433"; + devicecode = 0x51; chip_erase_delay = 20000; pgm_enable = "1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1", "x x x x x x x x x x x x x x x x"; @@ -589,6 +655,7 @@ part part id = "4434"; desc = "AT90S4434"; + devicecode = 0x52; chip_erase_delay = 20000; pgm_enable = "1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1", "x x x x x x x x x x x x x x x x"; @@ -665,6 +732,7 @@ part part id = "8515"; desc = "AT90S8515"; + devicecode = 0x63; chip_erase_delay = 20000; pgm_enable = "1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1", "x x x x x x x x x x x x x x x x"; @@ -735,6 +803,7 @@ part part id = "8535"; desc = "AT90S8535"; + devicecode = 0x64; chip_erase_delay = 20000; pgm_enable = "1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1", "x x x x x x x x x x x x x x x x"; @@ -791,6 +860,7 @@ part part id = "m103"; desc = "ATMEGA103"; + devicecode = 0xB1; chip_erase_delay = 112000; pgm_enable = "1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1", "x x x x x x x x x x x x x x x x"; @@ -879,6 +949,7 @@ part part id = "m128"; desc = "ATMEGA128"; + devicecode = 0xB2; chip_erase_delay = 9000; pgm_enable = "1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1", "x x x x x x x x x x x x x x x x"; @@ -991,6 +1062,7 @@ part part id = "m16"; desc = "ATMEGA16"; + devicecode = 0x82; chip_erase_delay = 9000; pgm_enable = "1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1", "x x x x x x x x x x x x x x x x"; @@ -1091,6 +1163,7 @@ part part id = "m163"; desc = "ATMEGA163"; + devicecode = 0x81; chip_erase_delay = 32000; pgm_enable = "1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1", "x x x x x x x x x x x x x x x x"; @@ -1200,6 +1273,7 @@ part part id = "m8"; desc = "ATMEGA8"; + devicecode = 0x70; chip_erase_delay = 9000; pgm_enable = "1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1", "x x x x x x x x x x x x x x x x"; diff --git a/avrpart.h b/avrpart.h new file mode 100644 index 00000000..3072ca3b --- /dev/null +++ b/avrpart.h @@ -0,0 +1,59 @@ +#ifndef __avrpart_h__ +#define __avrpart_h__ + +#include "lists.h" + +/* + * AVR serial programming instructions + */ +enum { + AVR_OP_READ, + AVR_OP_WRITE, + AVR_OP_READ_LO, + AVR_OP_READ_HI, + AVR_OP_WRITE_LO, + AVR_OP_WRITE_HI, + AVR_OP_LOADPAGE_LO, + AVR_OP_LOADPAGE_HI, + AVR_OP_WRITEPAGE, + AVR_OP_CHIP_ERASE, + AVR_OP_PGM_ENABLE, + AVR_OP_MAX +}; + + +enum { + AVR_CMDBIT_IGNORE, /* bit is ignored on input and output */ + AVR_CMDBIT_VALUE, /* bit is set to 0 or 1 for input or output */ + AVR_CMDBIT_ADDRESS, /* this bit represents an input address bit */ + AVR_CMDBIT_INPUT, /* this bit is an input bit */ + AVR_CMDBIT_OUTPUT /* this bit is an output bit */ +}; + +/* + * serial programming instruction bit specifications + */ +typedef struct cmdbit { + int type; /* AVR_CMDBIT_* */ + int bitno; /* which input bit to use for this command bit */ + int value; /* bit value if type == AVR_CMDBIT_VALUD */ +} CMDBIT; + +typedef struct opcode { + CMDBIT bit[32]; /* opcode bit specs */ +} OPCODE; + + +#define AVR_DESCLEN 64 +#define AVR_IDLEN 32 +typedef struct avrpart { + char desc[AVR_DESCLEN]; /* long part name */ + char id[AVR_IDLEN]; /* short part name */ + int devicecode; /* Atmel STK500 device code */ + int chip_erase_delay; /* microseconds */ + OPCODE * op[AVR_OP_MAX]; /* opcodes */ + + LISTID mem; /* avr memory definitions */ +} AVRPART; + +#endif diff --git a/config_gram.y b/config_gram.y index d800c932..8b6bbbc4 100644 --- a/config_gram.y +++ b/config_gram.y @@ -48,6 +48,7 @@ %token K_BUFF %token K_CHIP_ERASE_DELAY %token K_DESC +%token K_DEVICECODE %token K_EEPROM %token K_ERRLED %token K_FLASH @@ -221,7 +222,7 @@ prog_parm : K_TYPE TKN_EQUAL K_STK500 { { - fprintf(stderr, "%s: programmer 'stk500' not yet supported\n", progname); + stk500_initpgm(current_prog); } } | @@ -328,6 +329,13 @@ part_parm : free_token($3); } | + K_DEVICECODE TKN_EQUAL TKN_NUMBER { + { + current_part->devicecode = $3->value.number; + free_token($3); + } + } | + K_CHIP_ERASE_DELAY TKN_EQUAL TKN_NUMBER { current_part->chip_erase_delay = $3->value.number; @@ -461,6 +469,7 @@ mem_spec : #include "config.h" #include "lists.h" #include "pindefs.h" +#include "ppi.h" #include "pgm.h" #include "avr.h" diff --git a/lexer.l b/lexer.l index 5acf8b81..ef1e9862 100644 --- a/lexer.l +++ b/lexer.l @@ -128,6 +128,7 @@ banked { yylval=NULL; return K_PAGED; } buff { yylval=NULL; return K_BUFF; } chip_erase_delay { yylval=NULL; return K_CHIP_ERASE_DELAY; } desc { yylval=NULL; return K_DESC; } +devicecode { yylval=NULL; return K_DEVICECODE; } eeprom { yylval=NULL; return K_EEPROM; } errled { yylval=NULL; return K_ERRLED; } flash { yylval=NULL; return K_FLASH; } diff --git a/pgm.h b/pgm.h index 9738c011..3d5f1738 100644 --- a/pgm.h +++ b/pgm.h @@ -32,6 +32,8 @@ #ifndef __pgm_h__ #define __pgm_h__ +#include + #include "avrpart.h" #include "lists.h" #include "pindefs.h" @@ -41,11 +43,13 @@ #define OFF 0 #define PGM_DESCLEN 80 +#define PGM_PORTLEN PATH_MAX #define PGM_TYPELEN 32 typedef struct programmer_t { LISTID id; char desc[PGM_DESCLEN]; char type[PGM_TYPELEN]; + char port[PGM_PORTLEN]; unsigned int pinno[N_PINS]; int ppidata; int fd; diff --git a/ppi.c b/ppi.c index 521f5e42..54faf5e4 100644 --- a/ppi.c +++ b/ppi.c @@ -46,6 +46,15 @@ extern char * progname; extern int do_cycles; +/* + * PPI registers + */ +enum { + PPIDATA, + PPICTRL, + PPISTATUS +}; + struct ppipins_t { int pin; int reg; @@ -80,6 +89,75 @@ char * ppi_version = "$Id$"; + +int ppi_getops (int reg, unsigned long * get, unsigned long * set); + +int ppi_set (int fd, int reg, int bit); + +int ppi_clr (int fd, int reg, int bit); + +int ppi_get (int fd, int reg, int bit); + +int ppi_toggle (int fd, int reg, int bit); + +int ppi_getall (int fd, int reg); + +int ppi_setall (int fd, int reg, int val); + +int ppi_pulse (int fd, int reg, int bit); + +int ppi_setpin (int fd, int pin, int value); + +int ppi_getpin (int fd, int pin); + +int ppi_pulsepin (int fd, int pin); + +int ppi_getpinreg (int pin); + +int ppi_sense (int fd); + + +int ppi_rdy_led (PROGRAMMER * pgm, int value); + +int ppi_err_led (PROGRAMMER * pgm, int value); + +int ppi_pgm_led (PROGRAMMER * pgm, int value); + +int ppi_vfy_led (PROGRAMMER * pgm, int value); + +int ppi_cmd (PROGRAMMER * pgm, unsigned char cmd[4], + unsigned char res[4]); + +int ppi_chip_erase (PROGRAMMER * pgm, AVRPART * p); + +int ppi_program_enable (PROGRAMMER * pgm, AVRPART * p); + +void ppi_powerup (PROGRAMMER * pgm); + +void ppi_powerdown (PROGRAMMER * pgm); + +int ppi_initialize (PROGRAMMER * pgm, AVRPART * p); + +int ppi_save (PROGRAMMER * pgm); + +void ppi_restore (PROGRAMMER * pgm); + +void ppi_disable (PROGRAMMER * pgm); + +void ppi_enable (PROGRAMMER * pgm); + +void ppi_open (PROGRAMMER * pgm, char * port); + +void ppi_close (PROGRAMMER * pgm); + + + + + + + + + static char vccpins_buf[64]; char * vccpins_str(unsigned int pmask) { diff --git a/ppi.h b/ppi.h index 9b2a3bf8..36e12caf 100644 --- a/ppi.h +++ b/ppi.h @@ -35,82 +35,10 @@ #include "config.h" -/* - * PPI registers - */ -enum { - PPIDATA, - PPICTRL, - PPISTATUS -}; - -int ppi_getops (int reg, unsigned long * get, unsigned long * set); - -int ppi_set (int fd, int reg, int bit); - -int ppi_clr (int fd, int reg, int bit); - -int ppi_get (int fd, int reg, int bit); - -int ppi_toggle (int fd, int reg, int bit); - -int ppi_getall (int fd, int reg); - -int ppi_setall (int fd, int reg, int val); - -int ppi_pulse (int fd, int reg, int bit); - -int ppi_setpin (int fd, int pin, int value); - -int ppi_getpin (int fd, int pin); - -int ppi_pulsepin (int fd, int pin); +void ppi_initpgm (PROGRAMMER * pgm); int ppi_getpinmask(int pin); -int ppi_getpinreg (int pin); - -int ppi_sense (int fd); - - -void ppi_initpgm (PROGRAMMER * pgm); - - -int ppi_rdy_led (PROGRAMMER * pgm, int value); - -int ppi_err_led (PROGRAMMER * pgm, int value); - -int ppi_pgm_led (PROGRAMMER * pgm, int value); - -int ppi_vfy_led (PROGRAMMER * pgm, int value); - -int ppi_cmd (PROGRAMMER * pgm, unsigned char cmd[4], - unsigned char res[4]); - -int ppi_chip_erase (PROGRAMMER * pgm, AVRPART * p); - -int ppi_program_enable (PROGRAMMER * pgm, AVRPART * p); - -void ppi_powerup (PROGRAMMER * pgm); - -void ppi_powerdown (PROGRAMMER * pgm); - -int ppi_initialize (PROGRAMMER * pgm, AVRPART * p); - -int ppi_save (PROGRAMMER * pgm); - -void ppi_restore (PROGRAMMER * pgm); - -void ppi_disable (PROGRAMMER * pgm); - -void ppi_enable (PROGRAMMER * pgm); - -void ppi_open (PROGRAMMER * pgm, char * port); - -void ppi_close (PROGRAMMER * pgm); - -void ppi_initpgm (PROGRAMMER * pgm); - #endif diff --git a/stk500.c b/stk500.c new file mode 100644 index 00000000..24c7ac6f --- /dev/null +++ b/stk500.c @@ -0,0 +1,715 @@ +/* + * Copyright 2002 Brian S. Dean + * All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY BRIAN S. DEAN ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BRIAN S. DEAN BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + */ + +/* $Id$ */ + +#include +#include +#include +#include +#include +#include +#include + +#include "avr.h" +#include "pgm.h" +#include "stk500_private.h" + + +extern char * progname; +extern int do_cycles; + +char * stk500_version = "$Id$"; + + +int static send(PROGRAMMER * pgm, char * buf, int buflen) +{ + struct timeval timeout; + fd_set wfds; + int nfds; + int rc; + + if (!buflen) + return 0; + + timeout.tv_sec = 0; + timeout.tv_usec = 500000; + + while (buflen) { + FD_ZERO(&wfds); + FD_SET(pgm->fd, &wfds); + + reselect: + nfds = select(pgm->fd+1, NULL, &wfds, NULL, &timeout); + if (nfds == 0) { + fprintf(stderr, "%s: send(): programmer is not responding on %s\n", + progname, pgm->port); + exit(1); + } + else if (nfds == -1) { + if (errno == EINTR) { + goto reselect; + } + else { + fprintf(stderr, "%s: send(): select(): %s\n", + progname, strerror(errno)); + exit(1); + } + } + + rc = write(pgm->fd, buf, 1); + if (rc < 0) { + fprintf(stderr, "%s: send(): write error: %s\n", + progname, strerror(errno)); + exit(1); + } + buf++; + buflen--; + } + + return 0; +} + + + +int static recv(PROGRAMMER * pgm, char * buf, int n) +{ + struct timeval timeout; + fd_set rfds; + int nfds; + int rc; + + timeout.tv_sec = 0; + timeout.tv_usec = 500000; + + while (n) { + FD_ZERO(&rfds); + FD_SET(pgm->fd, &rfds); + + reselect: + nfds = select(pgm->fd+1, &rfds, NULL, NULL, &timeout); + if (nfds == 0) { + fprintf(stderr, "%s: recv(): programmer is not responding on %s\n", + progname, pgm->port); + exit(1); + } + else if (nfds == -1) { + if (errno == EINTR) { + goto reselect; + } + else { + fprintf(stderr, "%s: recv(): select(): %s\n", + progname, strerror(errno)); + exit(1); + } + } + + rc = read(pgm->fd, buf, 1); + if (rc < 0) { + fprintf(stderr, "%s: recv(): read error: %s\n", + progname, strerror(errno)); + exit(1); + } + buf++; + n--; + } + + return 0; +} + + +int static drain(PROGRAMMER * pgm, int display) +{ + struct timeval timeout; + fd_set rfds; + int nfds; + int rc; + unsigned char buf; + + timeout.tv_sec = 0; + timeout.tv_usec = 250000; + + if (display) { + fprintf(stderr, "drain>"); + } + + while (1) { + FD_ZERO(&rfds); + FD_SET(pgm->fd, &rfds); + + reselect: + nfds = select(pgm->fd+1, &rfds, NULL, NULL, &timeout); + if (nfds == 0) { + if (display) { + fprintf(stderr, "fd, &buf, 1); + if (rc < 0) { + fprintf(stderr, "%s: drain(): read error: %s\n", + progname, strerror(errno)); + exit(1); + } + if (display) { + fprintf(stderr, "%02x ", buf); + } + } +} + + +static int getsync(PROGRAMMER * pgm) +{ + unsigned char buf[32], resp[32]; + + /* + * get in sync + */ + buf[0] = Cmnd_STK_GET_SYNC; + buf[1] = Sync_CRC_EOP; + send(pgm, buf, 2); + recv(pgm, resp, 1); + if (resp[0] != Resp_STK_INSYNC) { + fprintf(stderr, + "%s: stk500_open(): not in sync: resp=0x%02x\n", + progname, resp[0]); + drain(pgm, 0); + exit(1); + } + + recv(pgm, resp, 1); + if (resp[0] != Resp_STK_OK) { + fprintf(stderr, + "%s: stk500_open(): can't communicate with device: resp=0x%02x\n", + progname, resp[0]); + exit(1); + } + + return 0; +} + + +int stk500_rdy_led(PROGRAMMER * pgm, int value) +{ + return 0; +} + +int stk500_err_led(PROGRAMMER * pgm, int value) +{ + return 0; +} + +int stk500_pgm_led(PROGRAMMER * pgm, int value) +{ + return 0; +} + +int stk500_vfy_led(PROGRAMMER * pgm, int value) +{ + return 0; +} + + +/* + * transmit an AVR device command and return the results; 'cmd' and + * 'res' must point to at least a 4 byte data buffer + */ +int stk500_cmd(PROGRAMMER * pgm, unsigned char cmd[4], unsigned char res[4]) +{ + unsigned char buf[32]; + + buf[0] = Cmnd_STK_UNIVERSAL; + buf[1] = cmd[0]; + buf[2] = cmd[1]; + buf[3] = cmd[2]; + buf[4] = cmd[3]; + buf[5] = Sync_CRC_EOP; + + send(pgm, buf, 6); + + recv(pgm, buf, 1); + if (buf[0] != Resp_STK_INSYNC) { + fprintf(stderr, "%s: programmer is out of sync\n", progname); + exit(1); + } + + res[0] = cmd[1]; + res[1] = cmd[2]; + res[2] = cmd[3]; + recv(pgm, &res[3], 1); + + recv(pgm, buf, 1); + if (buf[0] != Resp_STK_OK) { + fprintf(stderr, "%s: protocol error\n", progname); + exit(1); + } + + return 0; +} + + + +/* + * issue the 'chip erase' command to the AVR device + */ +int stk500_chip_erase(PROGRAMMER * pgm, AVRPART * p) +{ + unsigned char cmd[4]; + unsigned char res[4]; + int cycles; + int rc; + + if (p->op[AVR_OP_CHIP_ERASE] == NULL) { + fprintf(stderr, "chip erase instruction not defined for part \"%s\"\n", + p->desc); + return -1; + } + + rc = avr_get_cycle_count(pgm, p, &cycles); + + /* + * only print out the current cycle count if we aren't going to + * display it below + */ + if (!do_cycles && ((rc >= 0) && (cycles != 0xffffffff))) { + fprintf(stderr, + "%s: current erase-rewrite cycle count is %d%s\n", + progname, cycles, + do_cycles ? "" : " (if being tracked)"); + } + + pgm->pgm_led(pgm, ON); + + memset(cmd, 0, sizeof(cmd)); + + avr_set_bits(p->op[AVR_OP_CHIP_ERASE], cmd); + pgm->cmd(pgm, cmd, res); + usleep(p->chip_erase_delay); + pgm->initialize(pgm, p); + + pgm->pgm_led(pgm, OFF); + + if (do_cycles && (cycles != -1)) { + if (cycles == 0x00ffff) { + cycles = 0; + } + cycles++; + fprintf(stderr, "%s: erase-rewrite cycle count is now %d\n", + progname, cycles); + avr_put_cycle_count(pgm, p, cycles); + } + + return 0; +} + +/* + * issue the 'program enable' command to the AVR device + */ +int stk500_program_enable(PROGRAMMER * pgm, AVRPART * p) +{ + unsigned char buf[16]; + int tries=0; + + retry: + + tries++; + + buf[0] = Cmnd_STK_ENTER_PROGMODE; + buf[1] = Sync_CRC_EOP; + + send(pgm, buf, 2); + recv(pgm, buf, 1); + if (buf[0] == Resp_STK_NOSYNC) { + if (tries > 33) { + fprintf(stderr, "%s: stk500_program_enable(): can't get into sync\n", + progname); + return -1; + } + getsync(pgm); + goto retry; + } + else if (buf[0] != Resp_STK_INSYNC) { + fprintf(stderr, + "%s: stk500_initialize(): (a) protocol error, " + "expect=0x%02x, resp=0x%02x\n", + progname, Resp_STK_INSYNC, buf[0]); + return -1; + } + + recv(pgm, buf, 1); + if (buf[0] == Resp_STK_OK) { + return 0; + } + else if (buf[0] == Resp_STK_NODEVICE) { + fprintf(stderr, "%s: stk500_program_enable(): no device\n", + progname); + return -1; + } + + fprintf(stderr, "%s: stk500_program_enamble(): unkown response=0x%02x\n", + progname, buf[0]); + + return -1; +} + + +/* + * apply power to the AVR processor + */ +void stk500_powerup(PROGRAMMER * pgm) +{ + return; +} + + +/* + * remove power from the AVR processor + */ +void stk500_powerdown(PROGRAMMER * pgm) +{ + return; +} + + +/* + * initialize the AVR device and prepare it to accept commands + */ +int stk500_initialize(PROGRAMMER * pgm, AVRPART * p) +{ + unsigned char buf[32]; + AVRMEM * m; + int tries; + + /* + * set device programming parameters + */ + buf[0] = Cmnd_STK_SET_DEVICE; + + buf[1] = p->devicecode; + buf[2] = 0; /* device revision */ + buf[3] = 0; /* parallel and serial programming */ + buf[4] = 1; /* full parallel interface */ + buf[5] = 1; /* polling supported - XXX need this in config file */ + buf[6] = 1; /* programming is self-timed - XXX need in config file */ + + m = avr_locate_mem(p, "lock"); + if (m) + buf[7] = m->size; + else + buf[7] = 0; + + /* + * number of fuse bytes + */ + buf[8] = 0; + m = avr_locate_mem(p, "fuse"); + if (m) + buf[8] += m->size; + m = avr_locate_mem(p, "lfuse"); + if (m) + buf[8] += m->size; + m = avr_locate_mem(p, "hfuse"); + if (m) + buf[8] += m->size; + m = avr_locate_mem(p, "efuse"); + if (m) + buf[8] += m->size; + + m = avr_locate_mem(p, "flash"); + if (m) { + buf[9] = m->readback[0]; + buf[10] = m->readback[1]; + if (m->paged) { + buf[13] = (m->page_size >> 8) & 0x00ff; + buf[14] = m->page_size & 0x00ff; + } + buf[17] = (m->size >> 24) & 0xff; + buf[18] = (m->size >> 16) & 0xff; + buf[19] = (m->size >> 8) & 0xff; + buf[20] = m->size & 0xff; + } + else { + buf[9] = 0xff; + buf[10] = 0xff; + buf[13] = 0; + buf[14] = 0; + buf[17] = 0; + buf[18] = 0; + buf[19] = 0; + buf[20] = 0; + } + + m = avr_locate_mem(p, "eeprom"); + if (m) { + buf[11] = m->readback[0]; + buf[12] = m->readback[1]; + buf[15] = (m->size >> 8) & 0x00ff; + buf[16] = m->size & 0x00ff; + } + else { + buf[11] = 0xff; + buf[12] = 0xff; + buf[15] = 0; + buf[16] = 0; + } + + buf[21] = Sync_CRC_EOP; + + tries = 0; + retry: + tries++; + send(pgm, buf, 22); + recv(pgm, buf, 1); + if (buf[0] == Resp_STK_NOSYNC) { + fprintf(stderr, + "%s: stk500_initialize(): programmer not in sync, resp=0x%02x\n", + progname, buf[0]); + if (tries > 33) + return -1; + getsync(pgm); + goto retry; + return -1; + } + else if (buf[0] != Resp_STK_INSYNC) { + fprintf(stderr, + "%s: stk500_initialize(): (a) protocol error, " + "expect=0x%02x, resp=0x%02x\n", + progname, Resp_STK_INSYNC, buf[0]); + return -1; + } + + recv(pgm, buf, 1); + if (buf[0] != Resp_STK_OK) { + fprintf(stderr, + "%s: stk500_initialize(): (b) protocol error, " + "expect=0x%02x, resp=0x%02x\n", + progname, Resp_STK_OK, buf[0]); + return -1; + } + + return pgm->program_enable(pgm, p); +} + + +int stk500_save(PROGRAMMER * pgm) +{ + return 0; +} + +void stk500_restore(PROGRAMMER * pgm) +{ + return; +} + +void stk500_disable(PROGRAMMER * pgm) +{ + unsigned char buf[16]; + int tries=0; + + retry: + + tries++; + + buf[0] = Cmnd_STK_LEAVE_PROGMODE; + buf[1] = Sync_CRC_EOP; + + send(pgm, buf, 2); + recv(pgm, buf, 1); + if (buf[0] == Resp_STK_NOSYNC) { + if (tries > 33) { + fprintf(stderr, "%s: stk500_program_enable(): can't get into sync\n", + progname); + return; + } + getsync(pgm); + goto retry; + } + else if (buf[0] != Resp_STK_INSYNC) { + fprintf(stderr, + "%s: stk500_initialize(): (a) protocol error, " + "expect=0x%02x, resp=0x%02x\n", + progname, Resp_STK_INSYNC, buf[0]); + return; + } + + recv(pgm, buf, 1); + if (buf[0] == Resp_STK_OK) { + return; + } + else if (buf[0] == Resp_STK_NODEVICE) { + fprintf(stderr, "%s: stk500_program_enable(): no device\n", + progname); + return; + } + + fprintf(stderr, "%s: stk500_program_enamble(): unkown response=0x%02x\n", + progname, buf[0]); + + return; +} + +void stk500_enable(PROGRAMMER * pgm) +{ + return; +} + + +int static set_tty_attr(int fd) +{ + int rc; + struct termios termios; + + if (!isatty(fd)) + return -1; + + /* + * initialize terminal modes + */ + rc = tcgetattr(fd, &termios); + if (rc < 0) { + fprintf(stderr, "%s: tcgetattr() failed, %s", + progname, strerror(errno)); + return -errno; + } + +#if 1 + termios.c_iflag &= ~(INPCK | IXOFF | IXON); + termios.c_cflag &= ~(HUPCL | CSTOPB | CRTSCTS); + termios.c_cflag |= (CLOCAL | CREAD); + termios.c_cc [VMIN] = 1; + termios.c_cc [VTIME] = 0; +#else + termios.c_iflag = 0; + termios.c_oflag = 0; + termios.c_cflag &= ~ (PARENB | CSIZE | CSTOPB); + termios.c_cflag |= (CS8 | HUPCL | CREAD | CLOCAL); + termios.c_lflag = 0; + termios.c_cc[VMIN] = 1; + termios.c_cc[VTIME] = 0; +#endif + + cfsetospeed(&termios, B115200); + cfsetispeed(&termios, B115200); + + rc = tcsetattr(fd, TCSANOW, &termios); + if (rc < 0) { + fprintf(stderr, "%s: tcsetattr() failed, %s", progname, strerror(errno)); + return -errno; + } + + return 0; +} + + +void stk500_open(PROGRAMMER * pgm, char * port) +{ + int rc; + + strcpy(pgm->port, port); + + /* + * open the serial port + */ + pgm->fd = open(port, O_RDWR | O_NOCTTY /*| O_NONBLOCK*/); + if (pgm->fd < 0) { + fprintf(stderr, "%s: can't open device \"%s\": %s\n", + progname, port, strerror(errno)); + exit(1); + } + + /* + * set serial line attributes + */ + rc = set_tty_attr(pgm->fd); + if (rc) { + fprintf(stderr, "%s: can't set attributes for device \"%s\"\n", + progname, port); + exit(1); + } + + /* + * drain any extraneous input + */ + drain(pgm, 0); + + getsync(pgm); + + drain(pgm, 0); +} + + +void stk500_close(PROGRAMMER * pgm) +{ + close(pgm->fd); + pgm->fd = -1; +} + + +void stk500_display(PROGRAMMER * pgm, char * p) +{ + return; +} + + +void stk500_initpgm(PROGRAMMER * pgm) +{ + strcpy(pgm->type, "STK500"); + + pgm->rdy_led = stk500_rdy_led; + pgm->err_led = stk500_err_led; + pgm->pgm_led = stk500_pgm_led; + pgm->vfy_led = stk500_vfy_led; + pgm->initialize = stk500_initialize; + pgm->display = stk500_display; + pgm->save = stk500_save; + pgm->restore = stk500_restore; + pgm->enable = stk500_enable; + pgm->disable = stk500_disable; + pgm->powerup = stk500_powerup; + pgm->powerdown = stk500_powerdown; + pgm->program_enable = stk500_program_enable; + pgm->chip_erase = stk500_chip_erase; + pgm->cmd = stk500_cmd; + pgm->open = stk500_open; + pgm->close = stk500_close; +} + + diff --git a/stk500.h b/stk500.h new file mode 100644 index 00000000..04811e83 --- /dev/null +++ b/stk500.h @@ -0,0 +1,41 @@ +/* + * Copyright 2002 Brian S. Dean + * All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY BRIAN S. DEAN ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BRIAN S. DEAN BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + */ + +/* $Id$ */ + +#ifndef __stk500_h__ +#define __stk500_h__ + +#include "config.h" + +void stk500_initpgm (PROGRAMMER * pgm); + +#endif + + diff --git a/stk500_private.h b/stk500_private.h new file mode 100644 index 00000000..8c56bd62 --- /dev/null +++ b/stk500_private.h @@ -0,0 +1,103 @@ +//**** ATMEL AVR - A P P L I C A T I O N N O T E ************************ +//* +//* Title: AVR061 - STK500 Communication Protocol +//* Filename: command.h +//* Version: 1.0 +//* Last updated: 09.09.2002 +//* +//* Support E-mail: avr@atmel.com +//* +//************************************************************************** + +// *****************[ STK Message constants ]*************************** + +#define STK_SIGN_ON_MESSAGE "AVR STK" // Sign on string for Cmnd_STK_GET_SIGN_ON + +// *****************[ STK Response constants ]*************************** + +#define Resp_STK_OK 0x10 // ' ' +#define Resp_STK_FAILED 0x11 // ' ' +#define Resp_STK_UNKNOWN 0x12 // ' ' +#define Resp_STK_NODEVICE 0x13 // ' ' +#define Resp_STK_INSYNC 0x14 // ' ' +#define Resp_STK_NOSYNC 0x15 // ' ' + +#define Resp_ADC_CHANNEL_ERROR 0x16 // ' ' +#define Resp_ADC_MEASURE_OK 0x17 // ' ' +#define Resp_PWM_CHANNEL_ERROR 0x18 // ' ' +#define Resp_PWM_ADJUST_OK 0x19 // ' ' + +// *****************[ STK Special constants ]*************************** + +#define Sync_CRC_EOP 0x20 // 'SPACE' + +// *****************[ STK Command constants ]*************************** + +#define Cmnd_STK_GET_SYNC 0x30 // ' ' +#define Cmnd_STK_GET_SIGN_ON 0x31 // ' ' + +#define Cmnd_STK_SET_PARAMETER 0x40 // ' ' +#define Cmnd_STK_GET_PARAMETER 0x41 // ' ' +#define Cmnd_STK_SET_DEVICE 0x42 // ' ' +#define Cmnd_STK_SET_DEVICE_EXT 0x45 // ' ' + +#define Cmnd_STK_ENTER_PROGMODE 0x50 // ' ' +#define Cmnd_STK_LEAVE_PROGMODE 0x51 // ' ' +#define Cmnd_STK_CHIP_ERASE 0x52 // ' ' +#define Cmnd_STK_CHECK_AUTOINC 0x53 // ' ' +#define Cmnd_STK_LOAD_ADDRESS 0x55 // ' ' +#define Cmnd_STK_UNIVERSAL 0x56 // ' ' +#define Cmnd_STK_UNIVERSAL_MULTI 0x57 // ' ' + +#define Cmnd_STK_PROG_FLASH 0x60 // ' ' +#define Cmnd_STK_PROG_DATA 0x61 // ' ' +#define Cmnd_STK_PROG_FUSE 0x62 // ' ' +#define Cmnd_STK_PROG_LOCK 0x63 // ' ' +#define Cmnd_STK_PROG_PAGE 0x64 // ' ' +#define Cmnd_STK_PROG_FUSE_EXT 0x65 // ' ' + +#define Cmnd_STK_READ_FLASH 0x70 // ' ' +#define Cmnd_STK_READ_DATA 0x71 // ' ' +#define Cmnd_STK_READ_FUSE 0x72 // ' ' +#define Cmnd_STK_READ_LOCK 0x73 // ' ' +#define Cmnd_STK_READ_PAGE 0x74 // ' ' +#define Cmnd_STK_READ_SIGN 0x75 // ' ' +#define Cmnd_STK_READ_OSCCAL 0x76 // ' ' +#define Cmnd_STK_READ_FUSE_EXT 0x77 // ' ' +#define Cmnd_STK_READ_OSCCAL_EXT 0x78 // ' ' + +// *****************[ STK Parameter constants ]*************************** + +#define Parm_STK_HW_VER 0x80 // ' ' - R +#define Parm_STK_SW_MAJOR 0x81 // ' ' - R +#define Parm_STK_SW_MINOR 0x82 // ' ' - R +#define Parm_STK_LEDS 0x83 // ' ' - R/W +#define Parm_STK_VTARGET 0x84 // ' ' - R/W +#define Parm_STK_VADJUST 0x85 // ' ' - R/W +#define Parm_STK_OSC_PSCALE 0x86 // ' ' - R/W +#define Parm_STK_OSC_CMATCH 0x87 // ' ' - R/W +#define Parm_STK_RESET_DURATION 0x88 // ' ' - R/W +#define Parm_STK_SCK_DURATION 0x89 // ' ' - R/W + +#define Parm_STK_BUFSIZEL 0x90 // ' ' - R/W, Range {0..255} +#define Parm_STK_BUFSIZEH 0x91 // ' ' - R/W, Range {0..255} +#define Parm_STK_DEVICE 0x92 // ' ' - R/W, Range {0..255} +#define Parm_STK_PROGMODE 0x93 // ' ' - 'P' or 'S' +#define Parm_STK_PARAMODE 0x94 // ' ' - TRUE or FALSE +#define Parm_STK_POLLING 0x95 // ' ' - TRUE or FALSE +#define Parm_STK_SELFTIMED 0x96 // ' ' - TRUE or FALSE + + +// *****************[ STK status bit definitions ]*************************** + +#define Stat_STK_INSYNC 0x01 // INSYNC status bit, '1' - INSYNC +#define Stat_STK_PROGMODE 0x02 // Programming mode, '1' - PROGMODE +#define Stat_STK_STANDALONE 0x04 // Standalone mode, '1' - SM mode +#define Stat_STK_RESET 0x08 // RESET button, '1' - Pushed +#define Stat_STK_PROGRAM 0x10 // Program button, ' 1' - Pushed +#define Stat_STK_LEDG 0x20 // Green LED status, '1' - Lit +#define Stat_STK_LEDR 0x40 // Red LED status, '1' - Lit +#define Stat_STK_LEDBLINK 0x80 // LED blink ON/OFF, '1' - Blink + + +// *****************************[ End Of COMMAND.H ]**************************