//----------------------------------------------------------------------------- //----------------------------------------------------------------------------- int sync_wait_for ( unsigned char *patt, unsigned int len ) { unsigned int ra,rb; // unsigned int rblast; //printf("-- %u\n",len); // rblast=0; while(1) { rb=ser_copystring(rdata); //if(rb!=rblast) //{ //for(ra=rblast;ra<rb;ra++) printf("0x%02X\n",rdata[ra]); //rblast=rb; //} if(rb>=len) { for(ra=0;ra<len;ra++) if(patt[ra]!=rdata[ra]) break; if(ra<len) { printf("fail\n"); for(ra=0;ra<len;ra++) printf("0x%02X 0x%02X %c\n",rdata[ra],patt[ra],rdata[ra]); ser_close(); return(1); } break; } } ser_dump(len); return(0); }
//----------------------------------------------------------------------------- unsigned int wait_for_packet ( void ) { unsigned int ra,rb; while(1) { rb=ser_copystring(rdata); if(rb) { if(rdata[0]!=0x0A) { for(ra=0;ra<rb;ra++) { if(rdata[ra]==0x0A) break; printf("discarding 0x%02X ",rdata[ra]); if((rdata[ra]>=0x20)&&(rdata[ra]<127)) printf("[%c]",rdata[ra]); printf("\n"); ser_dump(1); } continue; } for(ra=0;ra<rb;ra++) if(rdata[ra]==0x0D) break; if(ra<rb) break; } } ra++; ser_dump(ra); rdata[ra]=0; return(ra); }
//----------------------------------------------------------------------------- int main ( int argc, char *argv[] ) { unsigned int ra; unsigned int rb; if(argc<2) { fprintf(stderr,"progpluto /dev/ttySOMETHING\n"); return(1); } if(ser_open(argv[1])) return(1); while(1) { rb=ser_copystring(rdata); for(ra=0;ra<rb;ra++) { printf("0x%02X ",rdata[ra]); if((rdata[ra]>=0x20)&&(rdata[ra]<127)) printf("[%c]",rdata[ra]); printf("\n"); } ser_dump(rb); } ser_close(); return(0); }
//----------------------------------------------------------------------------- int watch_for ( void ) { unsigned int ra,rb; unsigned int rblast; printf("xx\n"); rblast=0; while(1) { rb=ser_copystring(rdata); if(rb!=rblast) { for(ra=rblast;ra<rb;ra++) printf("0x%02X\n",rdata[ra]); rblast=rb; } } return(0); }
//----------------------------------------------------------------------------- int get_sync ( void ) { int ra,rb; for(ra=0;ra<10;ra++) { sdata[0]=0x30; sdata[1]=0x20; ser_senddata(sdata,2); while(1) { rb=ser_copystring(data); if(rb==2) break; } ser_dump(rb); showstring(rb); if((data[0]==0x14)&&(data[1]==0x10)) { printf("got_sync\n"); return(0); } } printf("no_sync\n"); return(1); }
//----------------------------------------------------------------------------- int main ( int argc, char *argv[] ) { //struct termios newsettings; if(argc<2) { printf(".hex file not specified\n"); return(1); } fp=fopen(argv[1],"rt"); if(fp==NULL) { printf("error opening file %s\n",argv[1]); return(1); } if(readhex()) return(1); fclose(fp); pages=maxadd>>8; printf("pages %u maxadd %u\n",pages,maxadd); if(ser_open()) { printf("ser_open() failed\n"); return(1); } printf("port opened\n"); if(get_sync()) { ser_close(); printf("\n\n"); return(1); } sdata[0]=0x41; sdata[1]=0x81; sdata[2]=0x20; ser_senddata(sdata,3); while(1) { rb=ser_copystring(data); if(rb==3) break; } ser_dump(rb); showstring(rb); sdata[0]=0x41; sdata[1]=0x82; sdata[2]=0x20; ser_senddata(sdata,3); while(1) { rb=ser_copystring(data); if(rb==3) break; } ser_dump(rb); showstring(rb); sdata[0]=0x50; sdata[1]=0x20; ser_senddata(sdata,2); while(1) { rb=ser_copystring(data); if(rb==2) break; } ser_dump(rb); showstring(rb); if((data[0]==0x14)&&(data[1]==0x10)) { } else { ser_close(); printf("\n\n"); return(1); } for(page=0;page<=maxadd;page+=256) { printf("set address 0x%04X\n",page); sdata[0]=0x55; sdata[1]=(page>>1)&0xFF; sdata[2]=(page>>9)&0xFF; sdata[3]=0x20; ser_senddata(sdata,4); while(1) { rb=ser_copystring(data); if(rb==2) break; } ser_dump(rb); showstring(rb); printf("write page\n"); sdata[0]=0x64; sdata[1]=0x01; sdata[2]=0x00; sdata[3]='F'; for(ra=0;ra<256;ra++) sdata[4+ra]=memory[ra+page]; sdata[4+ra]=0x20; ser_senddata(sdata,ra+5); while(1) { rb=ser_copystring(data); if(rb==2) break; } ser_dump(rb); showstring(rb); } sdata[0]=0x51; sdata[1]=0x20; ser_senddata(sdata,2); while(1) { rb=ser_copystring(data); if(rb==2) break; } ser_dump(rb); showstring(rb); if((data[0]==0x14)&&(data[1]==0x10)) { } else { ser_close(); printf("\n\n"); return(1); } ser_close(); printf("\n\n"); return(0); }
//----------------------------------------------------------------------------- int do_lpc_stuff ( int erase_all ) { unsigned int ra,rb,rc; unsigned int ramadd; unsigned int romadd; unsigned int romsec; if(lpc_isp_synchronize()) return(1); //ra=0; //sdata[ra++]='J'; //sdata[ra++]=0x0D; //ser_senddata(sdata,ra); //watch_for(); //ra=0; //sdata[ra++]='K'; //sdata[ra++]=0x0D; //ser_senddata(sdata,ra); //watch_for(); //Turn off echo ra=0; sdata[ra++]='A'; sdata[ra++]=' '; sdata[ra++]='0'; sdata[ra++]=0x0D; sdata[ra++]=0x0A; ser_senddata(sdata,ra); if(sync_wait_for(sdata,ra-1)) return(1); //---- //the ra-1 is so that everything from the chip is 0x0A...stuff...0x0D //---- if(get_return_code()) return(1); //Unlock ra=0; sdata[ra++]='U'; sdata[ra++]=' '; sdata[ra++]='2'; sdata[ra++]='3'; sdata[ra++]='1'; sdata[ra++]='3'; sdata[ra++]='0'; sdata[ra++]=0x0D; ser_senddata(sdata,ra); if(get_return_code()) return(1); ////write program to ram and run from ram //rb=0x40000200; //for(ra=0;ra<bin_length;ra++) //{ //if(write_data_word(rb,bin_data[ra])) return(1); //rb+=4; //} //sprintf((char *)sdata,"G %u A\r",0x40000200); //ra=strlen((char *)sdata); //ser_senddata(sdata,ra); ////if(get_return_code()) return(1); if(erase_all) { printf("Erase All\n"); if(flash_erase_all()) { printf("flash_erase_all() failed\n"); return(1); } } //write program to flash printf("Programming 0x%04X bytes to flash\n",fdatalen<<2); //patch vector table, first 0x20 bytes worth of words need to add to zero fdata[0x14>>2]=0x0; rc=0; for(ra=0;ra<0x20;ra+=4) rc+=fdata[ra>>2]; rc=-rc; fdata[0x14>>2]=rc; printf("0x%08X\n",fdata[0]); romadd=0x00000000; //ramadd=0x40000200; for(ra=0;ra<fdatalen;ra+=64) { romsec=romadd>>12; //armmite uses 4KB sectors if((romadd&0xFFF)==0) //armmite uses 4KB sectors { printf("\nErasing 0x%04X\n",romadd); sprintf((char *)sdata,"P %u %u\r",romsec,romsec); ser_senddata(sdata,strlen((char *)sdata)); if(get_return_code()) return(1); sprintf((char *)sdata,"E %u %u\r",romsec,romsec); ser_senddata(sdata,strlen((char *)sdata)); if(get_return_code()) return(1); ramadd=0x40000200; } printf("\nProgramming 0x%04X\n",romadd); rc=ramadd; for(rb=0;rb<64;rb++) //256 bytes per copy { if(write_data_word(rc,fdata[ra+rb])) return(1); rc+=4; } sprintf((char *)sdata,"P %u %u\r",romsec,romsec); ser_senddata(sdata,strlen((char *)sdata)); if(get_return_code()) return(1); sprintf((char *)sdata,"C %u %u 256\r",romadd,ramadd); ser_senddata(sdata,strlen((char *)sdata)); if(get_return_code()) return(1); romadd+=256; ramadd+=256; } printf("\n====\n"); while(1) { rb=ser_copystring(rdata); for(ra=0;ra<rb;ra++) { printf("0x%02X ",rdata[ra]); if((rdata[ra]>=0x20)&&(rdata[ra]<127)) printf("[%c]",rdata[ra]); printf("\n"); } ser_dump(rb); } return(0); }