/* * Copyright 2000 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$ */ /* * Code to program an Atmel AVR AT90S device using the parallel port. * * Make the following connections: * * Parallel Port Atmel AVR * ------------- ---------------------------- * Pin 2 -> Vcc (see NOTE below) * Pin 3 -> SCK CLOCK IN * Pin 4 -> MOSI Instruction input * Pin 5 -> /RESET * Pin 6,7,8,9 -> Vcc (Can be tied together with Schottky diodes) * Pin 10 <- MISO Data out * Pin 18 <- GND * * NOTE on Vcc connection: make sure your parallel port can supply an * adequate amount of current to power your device. 6-10 mA is * common for parallel port signal lines, but is not guaranteed, * especially for notebook computers. Optionally, you can tie pins * 6, 7, 8, and 9 also to Vcc with Schottky diodes to supply * additional current. If in doubt, don't risk damaging your * parallel port, use an external power supply. */ #include #include #include #include #include #include #include #include #include #include #include #define DEFAULT_PARALLEL "/dev/ppi0" char * version = "$Id$"; char * progname; /* * bit definitions for AVR device connections */ #define AVR_POWER 0xf1 /* bit 0 and 4...7 of data register */ #define AVR_CLOCK 0x02 /* bit 1 of data register */ #define AVR_INSTR 0x04 /* bit 2 of data register */ #define AVR_RESET 0x08 /* bit 3 of data register */ #define AVR_DATA 0x40 /* bit 6 of status register */ /* * PPI registers */ enum { PPIDATA, PPICTRL, PPISTATUS }; /* * AVR memory designations */ typedef enum { AVR_EEPROM, AVR_FLASH, AVR_FLASH_LO, AVR_FLASH_HI } AVRMEM; enum { FMT_AUTO, FMT_SREC, FMT_IHEX, FMT_RBIN }; struct avrpart { char * partdesc; char * optiontag; int flash_size; int eeprom_size; unsigned char f_readback; unsigned char e_readback[2]; unsigned char * flash; unsigned char * eeprom; }; struct avrpart parts[] = { { "AT90S8515", "8515", 8192, 512, 0x7f, { 0x80, 0x7f }, NULL, NULL }, { "AT90S2313", "2313", 2048, 128, 0x7f, { 0x80, 0x7f }, NULL, NULL }, { "AT90S1200", "1200", 1024, 64, 0x7f, { 0x80, 0x7f }, NULL, NULL } }; #define N_AVRPARTS (sizeof(parts)/sizeof(struct avrpart)) struct fioparms { int op; char * mode; char * iodesc; char * dir; char * rw; }; enum { FIO_READ, FIO_WRITE }; #define MAX_LINE_LEN 256 /* max line length for ASCII format input files */ char * usage_text = "\n" "Usage: avrprog [options]\n" "\n" " Available Options:\n" "\n" " -m MemType : select memory type for reading or writing\n" " \"e\", \"eeprom\" = EEPROM\n" " \"f\", \"flash\" = FLASH\n" "\n" " -i Filename : select input file\n" "\n" " -o Filename : select output file, \"-\" = stdout\n" "\n" " -f Format : select input / output file format\n" " \"i\" = Intel Hex\n" " \"s\" = Motorola S-Record\n" " \"r\" = Raw binary\n" " \"a\" = Auto detect (valid input only)\n" "\n" " -p Part : select Atmel part number (see below for valid parts)\n" "\n" " -P Parallel : select parallel port device name (default = /dev/ppi0)\n" "\n" " -s : read device signature bytes\n" "\n" " -F : override invalid device signature check\n" "\n" " -c : enter interactive command mode (or read commands\n" " from stdin)\n" "\n" " -e : perform a chip erase (required before programming)\n" "\n"; int list_valid_parts ( FILE * f, char * prefix ) { int i; for (i=0; i> (7-i)) & 0x01; r = avr_txrx_bit ( fd, b ); rbyte = rbyte | (r << (7-i)); } return rbyte; } /* * transmit an AVR device command and return the results; 'cmd' and * 'res' must point to at least a 4 byte data buffer */ int avr_cmd ( int fd, unsigned char cmd[4], unsigned char res[4] ) { int i; for (i=0; i<4; i++) { res[i] = avr_txrx(fd, cmd[i]); } return 0; } /* * read a byte of data from the indicated memory region */ unsigned char avr_read_byte ( int fd, AVRMEM memtype, unsigned short addr ) { unsigned char cmd[4]; unsigned char res[4]; switch (memtype) { case AVR_FLASH_LO: cmd[0] = 0x20; break; case AVR_FLASH_HI: cmd[0] = 0x28; break; case AVR_EEPROM: cmd[0] = 0xa0; break; default: fprintf(stderr, "%s: avr_read_byte(); internal error: invalid memtype=%d\n", progname, memtype); exit(1); break; } cmd[1] = addr >> 8; /* high order bits of address */ cmd[2] = addr & 0x0ff; /* low order bits of address */ cmd[3] = 0; /* don't care */ avr_cmd(fd, cmd, res); return res[3]; } /* * read the entirety of the specified memory type into the * corresponding buffer of the avrpart pointed to by 'p'. */ int avr_read ( int fd, AVRMEM memtype, struct avrpart * p ) { unsigned char rbyte, memt; unsigned short n, start, end, i, bi; unsigned char * buf; int bufsize; switch (memtype) { case AVR_FLASH : memt = AVR_FLASH_LO; buf = p->flash; n = p->flash_size/2; bufsize = p->flash_size; break; case AVR_EEPROM : memt = memtype; buf = p->eeprom; n = p->eeprom_size; bufsize = p->eeprom_size; break; default: fprintf(stderr, "%s: avr_read(); internal error: invalid memtype=%d\n", progname, memtype); exit(1); break; } end = start+n; bi = 0; for (i=start; i> 8; /* high order bits of address */ cmd[2] = addr & 0x0ff; /* low order bits of address */ cmd[3] = data; /* data */ avr_cmd(fd, cmd, res); tries = 0; ready = 0; while (!ready) { usleep(5000); /* flash write delay */ r = avr_read_byte(fd, memtype, addr); if (data == 0x7f) { usleep(20000); /* long delay for 0x7f since polling doesn't work */ ready = 1; } else if (r == data) { ready = 1; } tries++; if (!ready && tries > 10) { /* * we couldn't write the data, indicate our displeasure by * returning an error code */ return -1; } } return 0; } /* * Write the whole memory region (flash or eeprom, specified by * 'memtype') from the corresponding buffer of the avrpart pointed to * by 'p'. All of the memory is updated, however, input data of 0xff * is not actually written out, because empty flash and eeprom * contains 0xff, and you can't actually write 1's, only 0's. */ int avr_write ( int fd, AVRMEM memtype, struct avrpart * p ) { unsigned char data, memt; unsigned short start, end, i, bi; int nl; int rc; unsigned char * buf; int bufsize; start = 0; switch (memtype) { case AVR_FLASH : buf = p->flash; bufsize = p->flash_size; end = start+bufsize/2; memt = AVR_FLASH_LO; break; case AVR_EEPROM : buf = p->eeprom; bufsize = p->eeprom_size; end = start+bufsize; memt = memtype; break; default: fprintf(stderr, "%s: avr_write(); internal error: invalid memtype=%d\n", progname, memtype); exit(1); break; } bi = 0; for (i=start; ipartdesc, "AT90S1200")==0) { avr_program_enable ( fd ); } else { tries = 0; do { rc = avr_program_enable ( fd ); if (rc == 0) break; ppi_pulse(fd, PPIDATA, AVR_CLOCK); tries++; } while (tries < 32); /* * can't sync with the device, maybe it's not attached? */ if (tries == 32) { fprintf ( stderr, "%s: AVR device not responding\n", progname ); return -1; } } return 0; } /* * infinite loop, sensing on the pin that we use to read data out of * the device; this is a debugging aid, you can insert a call to this * function in 'main()' and can use it to determine whether your sense * pin is actually sensing. */ int ppi_sense_test ( int fd ) { unsigned char v, pv; pv = 1; do { usleep(100000); /* check every 100 ms */ v = ppi_get(fd, PPISTATUS, AVR_DATA); if (v != pv) { fprintf ( stderr, "sense bit = %d\n", v ); } pv = v; } while(1); return 0; } /* * usage message */ void usage ( void ) { fprintf ( stderr, "%s", usage_text ); fprintf(stderr, " Valid Parts for the -p option are:\n"); list_valid_parts(stderr, " "); fprintf(stderr, "\n"); } int b2ihex ( unsigned char * inbuf, int bufsize, int recsize, int startaddr, char * outfile, FILE * outf ) { unsigned char * buf; unsigned int nextaddr; int n; int i; unsigned char cksum; if (recsize > 255) { fprintf ( stderr, "%s: recsize=%d, must be < 256\n", progname, recsize ); return -1; } nextaddr = startaddr; buf = inbuf; while (bufsize) { n = recsize; if (n > bufsize) n = bufsize; if (n) { cksum = 0; fprintf ( outf, ":%02X%04X00", n, nextaddr ); cksum += n + ((nextaddr >> 8) & 0x0ff) + (nextaddr & 0x0ff); for (i=0; i> 8) & 0x0ff) + (nextaddr & 0x0ff); cksum = -cksum; fprintf ( outf, "%02X\n", cksum ); return 0; } int ihex2b ( char * infile, FILE * inf, unsigned char * outbuf, int bufsize ) { unsigned char buffer [ MAX_LINE_LEN ]; unsigned char * buf; unsigned int prevaddr, nextaddr; unsigned int b; int n; int i, j; unsigned int cksum, rectype; int lineno; lineno = 0; prevaddr = 0; buf = outbuf; while (fgets((char *)buffer,MAX_LINE_LEN,inf)!=NULL) { lineno++; if (buffer[0] != ':') continue; if (sscanf((char *)&buffer[1], "%02x%04x%02x", &n, &nextaddr, &rectype) != 3) { fprintf(stderr, "%s: invalid record at line %d of \"%s\"\n", progname, lineno, infile); exit(1); } if (rectype != 0) { fprintf(stderr, "%s: don't know how to deal with rectype=%d " "at line %d of \"%s\"\n", progname, rectype, lineno, infile); exit(1); } if (n && ((nextaddr + n) > bufsize)) { fprintf(stderr, "%s: address 0x%04x out of range at line %d of %s\n", progname, nextaddr+n, lineno, infile); return -1; } /* start computing a checksum */ cksum = n + ((nextaddr >> 8 ) & 0x0ff) + (nextaddr & 0x0ff); for (i=0; iop) { case FIO_READ: rc = fread(buf, 1, size, f); break; case FIO_WRITE: rc = fwrite(buf, 1, size, f); break; } if (rc < size) { fprintf(stderr, "%s: %s error %s %s: %s; %s %d of the expected %d bytes\n", progname, fio->iodesc, fio->dir, filename, strerror(errno), fio->rw, rc, size); return -5; } return rc; } int fileio_ihex ( struct fioparms * fio, char * filename, FILE * f, unsigned char * buf, int size ) { int rc; switch (fio->op) { case FIO_WRITE: rc = b2ihex(buf, size, 32, 0, filename, f); if (rc) { return -5; } break; case FIO_READ: rc = ihex2b(filename, f, buf, size); if (rc) return -5; break; default: fprintf(stderr, "%s: invalid Intex Hex file I/O operation=%d\n", progname, fio->op); return -5; break; } return 0; } int fileio_srec ( struct fioparms * fio, char * filename, FILE * f, unsigned char * buf, int size ) { fprintf(stderr, "%s: Motorola S-Record %s format not yet supported\n", progname, fio->iodesc); return -5; } int fileio_setparms ( int op, struct fioparms * fp ) { fp->op = op; switch (op) { case FIO_READ: fp->mode = "r"; fp->iodesc = "input"; fp->dir = "from"; fp->rw = "read"; break; case FIO_WRITE: fp->mode = "w"; fp->iodesc = "output"; fp->dir = "to"; fp->rw = "wrote"; break; default: fprintf(stderr, "%s: invalid I/O operation %d\n", progname, op); return -1; break; } return 0; } int fileio ( int op, char * filename, int format, struct avrpart * p, AVRMEM memtype ) { int rc; FILE * f; char * fname; unsigned char * buf; int size; struct fioparms fio; int i; rc = fileio_setparms(op, &fio); if (rc < 0) return -1; if (strcmp(filename, "-")==0) { if (fio.op == FIO_READ) { fname = ""; f = stdin; } else { fname = ""; f = stdout; } } else { f = fopen(filename, fio.mode); if (f == NULL) { fprintf(stderr, "%s: can't open %s file %s: %s\n", progname, fio.iodesc, filename, strerror(errno)); return -2; } } switch (memtype) { case AVR_EEPROM: buf = p->eeprom; size = p->eeprom_size; break; case AVR_FLASH: buf = p->flash; size = p->flash_size; break; default: fprintf(stderr, "%s: invalid memory type for %s: %d\n", progname, fio.iodesc, memtype); return -3; } if (fio.op == FIO_READ) { /* 0xff fill unspecified memory */ for (i=0; ipartdesc, p->flash_size, p->eeprom_size); fprintf(stderr, "\n"); p->flash = (unsigned char *) malloc(p->flash_size); if (p->flash == NULL) { fprintf(stderr, "%s: can't alloc buffer for flash size of %d bytes\n", progname, p->flash_size); exit(1); } p->eeprom = (unsigned char *) malloc(p->eeprom_size); if (p->eeprom == NULL) { fprintf(stderr, "%s: can't alloc buffer for eeprom size of %d bytes\n", progname, p->eeprom_size); exit(1); } /* * open the parallel port */ fd = open ( DEFAULT_PARALLEL, O_RDWR ); if (fd < 0) { fprintf ( stderr, "%s: can't open device \"%s\": %s\n", progname, DEFAULT_PARALLEL, strerror(errno) ); return 1; } exitrc = 0; /* * initialize the chip in preperation for accepting commands */ rc = avr_initialize(fd,p); if (rc < 0) { fprintf ( stderr, "%s: initialization failed, rc=%d\n", progname, rc ); exitrc = 1; goto main_exit; } fprintf ( stderr, "%s: AVR device initialized and ready to accept instructions\n", progname ); /* * Let's read the signature bytes to make sure there is at least a * chip on the other end that is responding correctly. A check * against 0xffffffff should ensure that the signature bytes are * valid. */ avr_signature(fd, sig); fprintf(stderr, "%s: Device signature = 0x", progname); for (i=0; i<4; i++) fprintf(stderr, "%02x", sig[i]); fprintf(stderr, "\n"); memset(nulldev,0xff,4); if (memcmp(sig,nulldev,4)==0) { len = strlen(progname) + 2; for (i=0; iflash, p->flash_size, 32, 0, "", stdout); else b2ihex ( p->eeprom, p->eeprom_size, 32, 0, "", stdout); #endif if (rc) { fprintf ( stderr, "%s: failed to write flash memory, rc=%d\n", progname, rc ); exitrc = 1; goto main_exit; } } main_exit: /* * program complete */ avr_powerdown(fd); ppi_clr(fd, PPIDATA, 0xff); ppi_clr(fd, PPIDATA, AVR_RESET); close(fd); fprintf(stderr, "\n" ); return exitrc; }