Example #1
0
int writeHex(char *nomeArq, int totMem) {
	int cnt, totP;
	unsigned char dados[totMem];
	unsigned char check[totMem];
	
	if(readHex(nomeArq, dados, totMem) < 0)
		return 10;
		
	chipErase();
	totP = writeChip(totMem, dados);
	
	readChip(totMem, check);
	for(cnt=0; cnt < totMem; cnt++)
		if(dados[cnt] != check[cnt]) {
			printf("Erro! Endereço %d leu 0x%.2X, mas era para ser 0x%.2X\n", cnt, check[cnt], dados[cnt]);
			return 0;
		}
	
	return totP;
}
Example #2
0
__attribute__((noreturn)) void main(void){
	cli();
	//set-up serial communications
	DDRD=28;//3 serial pins on d.2 to d.4 d.5 to d.7 contain msbs for flash data d.0 to d.4 is in port C
	DDRB=62;//serial clocks (B.1 SHCP) (B.2 Latch) (B.3 CE#) (B.4 OE#) (B.5 WE#)
	UBRR0H=0;
	UBRR0L=3;//set to 0.5M baud
	UCSR0A|=2;//double speed aysnc
	UCSR0B = (1<<RXEN0)|(1<<TXEN0);//Enable receiver and transmitter
	UCSR0C=6;//async 1 stop bit 8bit char no parity bits
	_delay_ms(50);
	StringPgm(PSTR("RDY"));
	USART_Receive();//wait for handshake
	char mode = USART_Receive();//wait for mode
	StringPgm(PSTR("RDY"));
	serialWrB(readId(0));
	uint8_t cap=readId(1);
	serialWrB(cap);
	__uint24 capacity=524288L;
	switch(cap){
		case 0xB5:
			capacity=131072L;
		case 0xB6:
			capacity=262144L;
		break;
	}
	if(mode=='W'){
		chipErase();
		serialWrB('D');
		verifyF(capacity);
		__uint24 x;
		for (x=0;x<capacity;++x){
			pgmB(x,USART_Receive());
			serialWrB(readB(x));
		}
	}else if(mode=='R')
		ReadChip(capacity);
	while(1);
}
Example #3
0
int main(int argc,char** argv){

	FILE* inputHex;
	int fileSize;
	int byteCount;
	int addr;
	int highLowToggle = LOW_BYTE;
	int ndx;
	uint16_t data;
	char hexData[128];

	inputHex = fopen(argv[1],"r");

	setATmega(ATMEGA_328P);

	initializeSPI();

	if(programmingEnable() != 0x53){
		printf("Programmer Out of Sync!!!\n");
		return -1;
	} else {
		printf("In sync!\n");
	}


	//printf("Erasing chip\n");
	if(argv[2][0] == 'e'){
		chipErase();
		return 1;
	}

	

	//writeProgramMemoryByte(0,0xFF,LOW_BYTE);

	//chipErase();

	usleep(25000);

	//printf("Fuse check: %hhX\n",readFuseBits());
	
	//return 1;

	/*if(programmingEnable() != 0x53){
		printf("Out of Sync!\n");
		return -1;
	} else {
		printf("In sync!\n");
	}*/

	while(feof(inputHex) == 0){
		fgets(hexData,128,inputHex);
		//printf("parsing %s\n",hexData);
		if(hexData[0] != ':'){
			printf("Error in hex file!\n");
		}
		byteCount = hexCharToInt(hexData[1]) << 4;
		byteCount |= hexCharToInt(hexData[2]);
	
		addr = hexCharToInt(hexData[3]) << 12;
		addr |= hexCharToInt(hexData[4]) << 8;
		addr |= hexCharToInt(hexData[5]) << 4;
		addr |= hexCharToInt(hexData[6]);

		addr /= 2;

		if(hexData[8] == '1'){
			break;
		} else if(hexData[8] != '0'){
			printf("Unsupported Hex file\n");
			break;
		}

		ndx = 9;
		while(byteCount){
			data = hexCharToInt(hexData[ndx]) << 4;
			ndx++;
			data |= hexCharToInt(hexData[ndx]);
			ndx++;
			data |= hexCharToInt(hexData[ndx]) << 12;
			ndx++;
			data |= hexCharToInt(hexData[ndx]) << 8;
		
			//printf("Writing byte %hX to address %hX\n",data,addr);

			writeProgramMemory(addr,data);
			usleep(4000);
			ndx++;
			addr++;
			byteCount -= 2;
		}
	}

	finishProgramming();

	byteCount = 0;
	while(byteCount < 0x88){
		printf("%X: %hX\n",byteCount,readProgramMemory(byteCount));

		//printf("%X: %hhX %hhX\n",byteCount,readProgramMemoryByte(byteCount,HIGH_BYTE),readProgramMemoryByte(byteCount,LOW_BYTE));
		byteCount++;
	}

	fclose(inputHex);
}	
Example #4
0
void processCommand( unsigned char cmd ) {
	
	if ( cmd == CMD_SELECT_DEVICE ) {
		selectedDevice = uart_getc( );
		uart_putc( BYTE_CR );
	} else if ( cmd == CMD_SHOW_ID ) {
		uart_puts( AVRISP_IDENTIFIER );
	} else if ( cmd == CMD_SOFTWARE_VERSION ) {
		uart_puts( AVRISP_SWVER );
	} else if ( cmd == CMD_HARDWARE_VERSION ) {
		uart_puts( AVRISP_HWVER );
	} else if ( cmd == CMD_LIST_SUPPORTED_DEVICES ) {
		uart_puts( AVRISP_SUPPORTED_DEVICES );
		uart_putc( 0 ); // terminating null character needs to be sent
	} else if ( cmd == CMD_PROGRAMMER_TYPE ) {
		uart_putc( AVRISP_PROGRAMMER_TYPE );
	} else if ( cmd == CMD_SUPPORTS_AUTOINCREMENT ) {
		uart_putc( AVRISP_RESP_YES );
	} else if ( cmd == CMD_SET_LED ) {
		LED_on( );
		uart_putc( BYTE_CR );
	} else if ( cmd == CMD_CLEAR_LED ) {
		LED_off( );
		uart_putc( BYTE_CR );
	} else if ( selectedDevice == 0 ) {
		// require a device for the rest of the commands
		uart_putc( AVRISP_RESP_UNKNOWN );
		return;
	}
	
	// command not specified above
	
	if ( cmd == CMD_ENTER_PROGRAM_MODE ) {
		enableProgramMode( );
	} else if ( cmd == CMD_LEAVE_PROGRAM_MODE ) {
		leaveProgramMode( );
	} else if ( cmd == CMD_WRITE_PROG_H ) {
		writeProgH( );
	} else if ( cmd == CMD_WRITE_PROG_L ) {
		writeProgL( );
	} else if ( cmd == CMD_READ_PROG ) {
		readProg( );
	} else if ( cmd == CMD_LOAD_ADDRESS ) {
		loadAddress( );
	} else if ( cmd == CMD_WRITE_DATA ) {
		writeData( );
	} else if ( cmd == CMD_READ_DATA ) {
		readData( );
	} else if ( cmd == CMD_CHIP_ERASE ) {
		chipErase( );
	} else if ( cmd == CMD_WRITE_LOCK_BITS ) {
		writeLockBits( );
	} else if ( cmd == CMD_READ_SIGNATURE ) {
		readSignature( );
	} else if ( cmd == CMD_WRITE_PROGRAM_PAGE ) {
		writeProgramPage( );
	} else if ( cmd == CMD_UNIVERSAL_3 ) {
		universalWrite3( );
	} else if ( cmd == CMD_UNIVERSAL_4 ) {
		universalWrite4( );
	} else if ( cmd == CMD_LOAD_ADDRESS_0 ) {
		currentAddress = 0;
		uart_putc( BYTE_CR );
	}
}
Example #5
0
void TTerminal::Run()
{
#if 0
  enableAvr ();
  if (isDeviceLocked ()) {
    char q[20];
    printf ("Do you want to clear it and enter normal mode now "
            "(enter y for yes): ");
    if (fgets (q,sizeof(q),stdin))
    {
        if (q[0]=='y') { chipErase (); }
        else { return; }
    }
  }
#endif

  printf ("Entering the AVR Terminal. ?-help, q-quit.\n");
  char cmd[32];
  TAddr addr = 0;
  do {
    try {
      printf("avr> "); 
      scanf("%s",cmd);
      if (!strcmp(cmd,"?")){
	printf ("AVR Terminal supports the following commands:\n"
	  "ul fileName        - uploads data from Motorola/Intel format.\n"
	  "vf fileName        - verify file with memory\n"
/*	  
	  "dl fileName[%segs] - downloads data to Micro Output File\n"
*/	  
          "ls                 - list segments\n"
	  "ss seg_name        - set segment\n"
	  "ce                 - perform chip erase\n"
	  "rd addr            - read a byte from a segment\n"
	  "wr addr byte       - write a 'byte' to a segment at address 'addr'\n"
	  "du addr            - dump segment starting at address 'addr'\n"
	  ",                  - continue segment dump\n"
	  "\n"
	  "Written by Uros Platise (c) 1997-1999, [email protected]\n");
      }
      else if (!strcmp(cmd,"ul")) {
	char inputFileName [64]; scanf ("%s", inputFileName);
	try{
	  motintl.Read(inputFileName, true, false);
	}
	catch (Error_Device& errDev) { errDev.print (); }
	catch (Error_C) { perror ("Error"); }
      }
      else if (!strcmp(cmd,"vf")) {
	char inputFileName [64]; scanf ("%s", inputFileName);
	try{
	  motintl.Read(inputFileName, false, true);
	}
	catch (Error_Device& errDev) { errDev.print (); }
	catch (Error_C) { perror ("Error"); }
      }      
/*      
      else if (cmd=="dl") {
	char outputFileName [64]; scanf ("%s", outputFileName);
	try { 
	  TAout outAout (outputFileName, "wt");
	  download (&outAout); 
	}
	catch (Error_Device& errDev) { errDev.print (); }
	catch (Error_C) { perror ("Error"); }
      }
*/     
      else if (!strcmp(cmd,"ls")){
        printf("Available segments: ");
        const char* seg_name;
        for (unsigned i=0; (seg_name=device->ListSegment(i))!=NULL; i++){
	  if (i>0){printf(", ");}
	  printf("%s", seg_name);
	}
	putchar('\n');
      } 
      else if (!strcmp(cmd,"ss")){
        char seg_name [32];
	scanf("%s", seg_name);
	if (!device->SetSegment(seg_name)){
	  printf("Invalid segment: `%s'\n", seg_name);
	} else {addr=0;}
      }       
      else if (!strcmp(cmd,"ce")){ 
	device->ChipErase();
      }
/*      
      else if (cmd=="rsb") {
	unsigned char byte = readLockBits ();
	printf ("Lock and Fuse bits status: %.2x\n", byte );
      }
      else if (cmd=="wlb") {
	string mode; cin >> mode;
	if (mode=="wr") { writeLockBits (lckPrg);
	} else if (mode=="rdwr") { writeLockBits (lckPrgRd); 
	} else { printf ("Invalid parameter: %s\n", mode); }
      }
*/      
      else if (!strcmp(cmd,"rd")){
	scanf ("%x", &addr);
	printf("%s: $%.2x\n", 
	  device->TellActiveSegment(), device->ReadByte(addr));
      }
      else if (!strcmp(cmd,"wr")){
        unsigned x;
	scanf("%x%x", &addr, &x);
	device->WriteByte(addr, TByte(x));
      }
      else if (!strcmp(cmd,"du")){
	scanf ("%x", &addr);
	goto list_contents;
      }
      else if (!strcmp(cmd,",")){
list_contents:
	int i,l=0;
	while (l<4) {
	  printf ("%s $%.5x: ", device->TellActiveSegment(), addr); 
	  for (i=0; i<0x8; addr++,i++) 
	    printf ("%.2x ", device->ReadByte(addr));
	  printf ("\n");
	  l++;
	}  
      }
      else printf ("Ouch.\n");
      
    } catch (Error_MemoryRange){Info(0,"Out of memory range!\n");putchar('\n');}
  } while (strcmp(cmd,"q"));
}