/* * 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 * Pin 3 -> PB7(SCK) CLOCK IN * Pin 4 -> PB5(MOSI) Instruction input * Pin 5 -> /RESET * Pin 10 <- PB6(MISO) Data out * */ #include #include #include #include #include #include #include #include #include #include #define PARALLEL "/dev/ppi0" char * version = "$Id$"; char * progname; /* * bit definitions for AVR device connections */ #define AVR_POWER 0x01 /* bit 0 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; /* * variable declarations required for getopt() */ char *optarg; int optind; int optopt; int opterr; int optreset; /* * set 'get' and 'set' appropriately for subsequent passage to ioctl() * to get/set the specified PPI registers. */ int ppi_getops ( int reg, unsigned long * get, unsigned long * set ) { switch (reg) { case PPIDATA: *set = PPISDATA; *get = PPIGDATA; break; case PPICTRL: *set = PPISCTRL; *get = PPIGCTRL; break; case PPISTATUS: *set = PPISSTATUS; *get = PPIGSTATUS; break; default: fprintf ( stderr, "%s: avr_set(): invalid register=%d\n", progname, reg ); return -1; break; } return 0; } /* * set the indicated bit of the specified register. */ int ppi_set ( int fd, int reg, int bit ) { unsigned char v; unsigned long get, set; int rc; rc = ppi_getops ( reg, &get, &set ); if (rc) return -1; ioctl(fd, get, &v); v |= bit; ioctl(fd, set, &v); return 0; } /* * clear the indicated bit of the specified register. */ int ppi_clr ( int fd, int reg, int bit ) { unsigned char v; unsigned long get, set; int rc; rc = ppi_getops ( reg, &get, &set ); if (rc) return -1; ioctl(fd, get, &v); v &= ~bit; ioctl(fd, set, &v); return 0; } /* * get the indicated bit of the specified register. */ int ppi_get ( int fd, int reg, int bit ) { unsigned char v; unsigned long get, set; int rc; rc = ppi_getops ( reg, &get, &set ); if (rc) return -1; ioctl(fd, get, &v); v &= bit; return (v == bit); } /* * toggle the indicated bit of the specified register. */ int ppi_toggle ( int fd, int reg, int bit ) { unsigned char v; unsigned long get, set; int rc; rc = ppi_getops ( reg, &get, &set ); if (rc) return -1; ioctl(fd, get, &v); v |= bit; ioctl(fd, set, &v); v &= ~bit; ioctl(fd, set, &v); return 0; } /* * transmit and receive a bit of data to/from the AVR device */ int avr_txrx_bit ( int fd, int bit ) { unsigned char d; int r; ioctl(fd, PPIGDATA, &d); r = ppi_get(fd, PPISTATUS, AVR_DATA); if (bit) ppi_set(fd, PPIDATA, AVR_INSTR); else ppi_clr(fd, PPIDATA, AVR_INSTR); ppi_toggle(fd, PPIDATA, AVR_CLOCK); return r; } /* * transmit and receive a byte of data to/from the AVR device */ unsigned char avr_txrx ( int fd, unsigned char byte ) { int i; unsigned char r, b, rbyte; rbyte = 0; for (i=0; i<8; i++) { b = (byte >> (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; addr &= 0x7f; 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 'n' words of data from the indicated memory region. If the * flash memory is being read, n*2 bytes will be read into 'buf'; if * the eeprom is being read, 'n' bytes will be read into 'buf'. */ int avr_read ( int fd, AVRMEM memtype, unsigned start, unsigned n, unsigned char * buf, int bufsize ) { unsigned char rbyte, memt; unsigned short end, i, bi; switch (memtype) { case AVR_FLASH : memt = AVR_FLASH_LO; break; case AVR_EEPROM : memt = memtype; 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 'bufsize' bytes of data to the indicated memory region. */ int avr_write ( int fd, AVRMEM memtype, unsigned start, unsigned char * buf, int bufsize ) { unsigned char data, memt; unsigned short end, i, bi; int nl; int rc; switch (memtype) { case AVR_FLASH : end = start+bufsize/2; memt = AVR_FLASH_LO; break; case AVR_EEPROM : 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; i