From c4b7c46a3892905e50d97e3a1c5cf7664591d7d2 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sun, 21 May 2006 19:42:31 +0200 Subject: [PATCH] unknown version --- Makefile | 7 + isp_down.c | 446 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 453 insertions(+) create mode 100644 Makefile create mode 100644 isp_down.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e1de030 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +all: isp_down + +isp_down: isp_down.c + gcc -Wall isp_down.c -o isp_down + +clean: + rm -f isp_down \ No newline at end of file diff --git a/isp_down.c b/isp_down.c new file mode 100644 index 0000000..a73a2b7 --- /dev/null +++ b/isp_down.c @@ -0,0 +1,446 @@ +#include +#include +#include +#include + +#define BAUDRATE B38400 + +struct termios oldtio, newtio; +int ser; + + +char sign[6][3]= +{0x1E,0x90,0x01, /* AT90S1200 */ + 0x1E,0x91,0x01, /* AT90S2313 */ + 0x1E,0x91,0x02, /* AT90S2323 */ + 0x1E,0x91,0x03, /* AT90S2343 */ + 0x1E,0x92,0x01, /* AT90S4414 */ + 0x1E,0x93,0x01}; /* AT90S8515 */ + +int mem_size[6][2]= +{0x1FF,0x3F, /* AT90S1200 */ + 0x3FF,0x7F, /* AT90S2313 */ + 0x3FF,0x7F, /* AT90S2323 */ + 0x3FF,0x7F, /* AT90S2343 */ + 0x7FF,0xFF, /* AT90S4414 */ + 0xFFF,0x1FF}; /* AT90S8515 */ + +void end_prg(char level); +void show_usage(); + +main(int argc,char *argv[]) +{ + int avr_device=5, verify=0, flash=0, eeprom=0; + char arg, ser_device[64]="/dev/ttyS0", flash_file[64]="", eeprom_file[64]=""; + + unsigned char inbuf[8], outbuf[8], databuf[8]; + int fdat, eedat; + unsigned int t; + + printf("\n***************************************\n"); + printf("* In-System-Programmer for Atmel AVRs *\n"); + printf("***************************************\n\n"); + if (argc <= 1) + { + show_usage(); + end_prg(0); + } + for (arg=1; arg ", ser_device); + ser = open(ser_device, O_RDWR | O_NOCTTY ); + if (ser <0) + { + printf("ERROR: Wrong (?) RS232-device\n"); + end_prg(0); + } + printf("OK\n"); + + /* save rs232 settings */ + tcgetattr(ser,&oldtio); /* save current port settings */ + bzero(&newtio, sizeof(newtio)); + newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD; + newtio.c_iflag = IGNPAR; + newtio.c_oflag = 0; + newtio.c_lflag = 0; /* set input mode (non-canonical, no echo,...) */ + newtio.c_cc[VTIME] = 0; /* inter-character timer unused */ + newtio.c_cc[VMIN] = 1; /* blocking read until 5 chars received */ + tcflush(ser, TCIFLUSH); + tcsetattr(ser,TCSANOW,&newtio); + + /* detecting avr-isp adapter */ + printf(" Detecting ISP ----------> "); + write (ser,"S",1); + read (ser,inbuf,7); + inbuf[7]=0x00; + if (!strcmp(inbuf, "AVR ISP")) + printf ("OK\n"); + else + { + printf("ERROR: No response\n"); + end_prg(1); + } + + /* set avr-device in avr-isp adapter */ + switch (avr_device) + { + case 0: printf(" Set device AT90S1200 ---> "); + outbuf[1]=0x12; + break; + + case 1: printf(" Set device AT90S2313 ---> "); + outbuf[1]=0x00; + break; + + case 2: printf(" Set device AT90S2323 ---> "); + outbuf[1]=0x00; + break; + + case 3: printf(" Set device AT90S2343 ---> "); + outbuf[1]=0x00; + break; + + case 4: printf(" Set device AT90S4414 ---> "); + outbuf[1]=0x00; + break; + + case 5: printf(" Set device AT90S8515 ---> "); + outbuf[1]=0x38; + break; + } + outbuf[0]=0x54; + write (ser,outbuf,2); + read (ser,inbuf,1); + if (inbuf[0]==0x0D) + printf("OK\n"); + else + { + printf("ERROR: No response\n"); + end_prg(1); + } + + /* enter programming mode */ + printf(" Set Prg Mode -----------> "); + write (ser,"P",1); + read (ser,inbuf,1); + if (inbuf[0]==0x0D) + printf("OK\n"); + else + { + printf("Error: No Response\n"); + end_prg(1); + } + + /* switch on red LED */ + write (ser,"x",1); + read (ser,inbuf,1); + + /* check the signature */ + printf(" Checking Signature -----> "); + write (ser,"s",1); + read (ser,inbuf,3); + if (sign[avr_device][0] == inbuf[0] && sign[avr_device][1] == inbuf[1] && sign[avr_device][2] == inbuf[2]) + printf ("OK\n"); + else + { + printf("Wrong Signature "); + printf ("%2.2X %2.2X %2.2X\n",inbuf[0], inbuf[1], inbuf[2]); + end_prg(2); + } + + /* clear the chip */ + printf(" Erase FLASH & EEPROM ---> "); + write (ser,"e",1); + read (ser,inbuf,1); + if (inbuf[0]==0x0D) + printf("OK\n"); + else + { + printf("Error: No Response\n"); + end_prg(2); + } + + printf(" Reseting AVR-Device ----> "); + write (ser,"L",1); + read (ser,inbuf,1); + if (inbuf[0]!=0x0D) + { + printf("Error: No Response\n"); + write (ser,"y",1); + read (ser,inbuf,1); + end_prg(2); + } + + write (ser,"P",1); + read (ser,inbuf,1); + if (inbuf[0]==0x0D) + printf("OK\n"); + else + { + printf("Error: No Response\n"); + write (ser,"y",1); + read (ser,inbuf,1); + end_prg(2); + } + + + if (flash == 1) + { + printf(" Writing FLASH DATA -----> "); + fdat = open(flash_file, O_RDWR); + if (fdat <0) + { + printf("<%s> not found\n", flash_file); + } + else + { + t=0; + while (read (fdat,databuf,2) == 2 && t < 4096) + { + outbuf[0]=0x41; + outbuf[1]=(unsigned char)(t>>8) & 0xFF; + outbuf[2]=(unsigned char)t & 0xFF; + write (ser,outbuf,3); + read (ser,inbuf,1); + + outbuf[0]=0x63; //Lowbyte + outbuf[1]=databuf[0]; + write (ser,outbuf,2); + read (ser,inbuf,1); + + outbuf[0]=0x43; //Highbyte + outbuf[1]=databuf[1]; + write (ser,outbuf,2); + read (ser,inbuf,1); + t++; + } + close (fdat); + printf("%4.4Xh Bytes\n",t*2); + } + } + if (verify == 1 && flash == 1) + { + printf(" Verify FLASH DATA ------> "); + fdat = open(flash_file, O_RDWR); + if (fdat <0) + { + printf("<%s> not found\n", flash_file); + } + else + { + t=0; + while (read (fdat,databuf,2) == 2 && t < 4096) + { + outbuf[0]=0x41; + outbuf[1]=(unsigned char)(t>>8) & 0xFF; + outbuf[2]=(unsigned char)t & 0xFF; + write (ser,outbuf,3); + read (ser,inbuf,1); + + write (ser,"R",1); + read (ser,inbuf,2); + if (inbuf[0]!=databuf[1] || inbuf[1]!=databuf[0]) + printf("Verify Error %4.4X: %2.2X.%2.2X - %2.2X.%2.2X\n", t, databuf[1], databuf[0], inbuf[0], inbuf[1]); + t++; + } + close (fdat); + printf("%4.4Xh Bytes\n",t*2); + } + } + if (eeprom == 1) + { + printf(" Writing EEPROM DATA ----> "); + eedat = open(eeprom_file, O_RDWR); + if (eedat <0) + { + printf("<%s> not found\n", eeprom_file); + } + else + { + t=0; + while (read (eedat,databuf,1) == 1 && t < 512) + { + outbuf[0]=0x41; + outbuf[1]=(unsigned char)(t>>8) & 0xFF; + outbuf[2]=(unsigned char)t & 0xFF; + write (ser,outbuf,3); + read (ser,inbuf,1); + + outbuf[0]=0x44; + outbuf[1]=databuf[0]; + write (ser,outbuf,2); + read (ser,inbuf,1); + t++; + } + close (eedat); + printf("%4.4Xh Bytes\n",t); + } + } + if (verify == 1 && eeprom == 1) + { + printf(" Verify EEPROM DATA -----> "); + eedat = open(eeprom_file, O_RDWR); + if (eedat <0) + { + printf("<%s> not found\n", eeprom_file); + } + else + { + t=0; + while (read (eedat,databuf,1) == 1 && t < 512) + { + outbuf[0]=0x41; + outbuf[1]=(unsigned char)(t>>8) & 0xFF; + outbuf[2]=(unsigned char)t & 0xFF; + write (ser,outbuf,3); + read (ser,inbuf,1); + + outbuf[0]=0x64; + write (ser,outbuf,1); + read (ser,inbuf,1); + if (inbuf[0]!=databuf[0]) + printf("Verify Error %4.4X: %2.2X - %2.2X\n", t, databuf[0], inbuf[0]); + t++; + } + close (eedat); + printf("%4.4Xh Bytes\n",t); + } + } + printf(" Leave Prg Mode ---------> "); + write (ser,"L",1); + read (ser,inbuf,1); + if (inbuf[0]==0x0D) + printf("OK\n"); + else + { + printf("Error: No Response\n"); + write (ser,"y",1); + read (ser,inbuf,1); + end_prg(2); + } + + end_prg(2); +} + + +void end_prg(char level) +{ + switch (level) + { + case 2: write (ser,"y",1); + read (ser,0,1); + + case 1: tcsetattr(ser,TCSANOW,&oldtio); + + case 0: printf("\n***************************************\n\n"); + } + exit (-1); +} + +void show_usage() +{ + printf("USAGE:\n"); + printf(" isp_down [OPTIONS] \n\n"); + printf("OPTIONS:\n"); + printf(" -avr selects AVR-device\n"); + printf(" 1200 -> AT90S1200 1kB FLASH / 64 bytes EEPROM\n"); + printf(" 2313 -> AT90S2313 2kB FLASH / 128 bytes EEPROM\n"); + printf(" 2323 -> AT90S2323 2kB FLASH / 128 bytes EEPROM\n"); + printf(" 2343 -> AT90S2343 2kB FLASH / 128 bytes EEPROM\n"); + printf(" 4414 -> AT90S4414 4kB FLASH / 256 bytes EEPROM\n"); + printf(" 8515 -> AT90S8515 8kB FLASH / 512 bytes EEPROM (default)\n\n"); + printf(" -dev selects RS232-device (default: /dev/ttyS0)\n"); + printf(" -flash Write (1st) file to FLASH\n"); + printf(" -eeprom Write (2nd) file to EEPROM\n"); + printf(" -verify Verify writing\n"); + end_prg(0); +} +