📄 avrdummy.c
字号:
m163: 1 1 1 1 1 BTSZ1 BTSZ0 BTRST m323: OCDEN JTGEN 1 1 EESAV BTSZ1 BTSZ0 BTRST m16,m64,m128: OCDEN JTGEN x CKOPT EESAV BTSZ1 BTSZ0 BTRST m8: RSTDI WDTON x CKOPT EESAV BTSZ1 BTSZ0 BTRST tn26: 1 1 1 RSTDI SPIEN EESAV BODLV BODEN */voidTAvrDummy::WriteFuseHighBits(TByte val){ TByte fusehigh[4] = { 0xAC, 0xA8, 0, val }; Send(fusehigh, 4);}/* Write Extended Fuse Bits: 7 6 5 4 3 2 1 0 m64,m128: x x x x x x M103C WDTON */voidTAvrDummy::WriteFuseExtBits(TByte val){ TByte fuseext[4] = { 0xAC, 0xA4, 0, val }; Send(fuseext, 4);}voidTAvrDummy::WriteByte(TAddr addr, TByte byte, bool flush_buffer){ struct timeval t_start_wr, t_start_poll, t_wait, t_timeout, t_end, t_write; TByte rbyte=0; bool device_not_erased=false; /* Poll data if use_data_polling is enabled and if page mode is enabled, flash is not selected */ bool poll_data = ((segment==SEG_FLASH && !page_size) || segment==SEG_EEPROM) && use_data_polling && TestFeatures(AVR_BYTE_POLL); CheckMemoryRange(addr); /* For speed, don't program a byte that is already there (such as 0xFF after chip erase). */ if (poll_data){ rbyte=ReadByte(addr); if (rbyte == byte){return;} if (rbyte != 0xff){device_not_erased=true;} } t_wait.tv_sec = 0; t_wait.tv_usec = 500000; gettimeofday(&t_start_wr, NULL); if (segment==SEG_FLASH){ /* PAGE MODE PROGRAMMING: If page mode is enabled cache page address. When current address is out of the page address flush page buffer and continue programming. */ if (page_size) { Info(4, "Loading data to address: %d (page_addr_fetched=%s)\n", addr, page_addr_fetched?"Yes":"No"); if (page_addr_fetched && page_addr != (addr & ~(page_size - 1))){ WriteProgramMemoryPage(); } if (page_addr_fetched==false){ page_addr=addr & ~(page_size - 1); page_addr_fetched=true; } if (flush_buffer){WriteProgramMemoryPage();} } TByte hl = (addr & 1) ? 0x48 : 0x40; TByte flash [4] = { hl, (TByte)((addr >> 9) & 0xff), (TByte)((addr >> 1) & 0xff), byte }; Send(flash, 4); /* Remember the last non-0xFF byte written, for page write polling. */ if (byte != 0xFF) { page_poll_addr = addr; page_poll_byte = byte; } /* We do not need to wait for each byte in page mode programming */ if (page_size){return;} t_wait.tv_usec = Get_t_wd_flash(); } else if (segment==SEG_EEPROM){ TByte eeprom [4] = { 0xC0, (TByte)((addr>>8)&0xff), (TByte)(addr&0xff), byte }; Send(eeprom, 4); t_wait.tv_usec = Get_t_wd_eeprom(); } else if (segment==SEG_FUSE) { Info(3, "Write fuse/lock: byte %d = 0x%02X\n", (int) addr, (int) byte); switch (addr) { case AVR_FUSE_LOW_ADDR: if (TestFeatures(AVR_FUSE_NEWWR)) WriteFuseLowBits(byte); else if (TestFeatures(AVR_FUSE_OLDWR)) WriteOldFuseBits(byte); break; case AVR_FUSE_HIGH_ADDR: if (TestFeatures(AVR_FUSE_HIGH)) WriteFuseHighBits(byte); break; case AVR_CAL_ADDR: /* calibration byte (addr == 2) is read only */ break; case AVR_LOCK_ADDR: WriteLockBits(byte); break; case AVR_FUSE_EXT_ADDR: if (TestFeatures(AVR_FUSE_EXT)) WriteFuseExtBits(byte); } t_wait.tv_usec = Get_t_wd_eeprom(); } gettimeofday(&t_start_poll, NULL); /* t_timeout = now + t_wd_prog */ timeradd(&t_start_poll, &t_wait, &t_timeout); do { /* Data Polling: if the programmed value reads correctly, and is not equal to any of the possible P1, P2 read back values, it is done; else wait until tWD_PROG time has elapsed. The busy loop here is to avoid rounding up the programming wait time to 10ms timer ticks (for Linux/x86). Programming is not really "hard real time" but 10ms instead of ~4ms for every byte makes it slow. gettimeofday() reads the 8254 timer registers (or Pentium cycle counter if available), so it has much better (microsecond) resolution. */ gettimeofday(&t_end, NULL); if (poll_data){ if ((byte == (rbyte = ReadByte(addr))) && (byte != 0) && (byte != 0x7F) && (byte != 0x80) && (byte != 0xFF)){ break; } } } while (timercmp(&t_end, &t_timeout, <)); /* Write Statistics */ timersub(&t_end, &t_start_wr, &t_write); /* t_write = t_end - t_start_wr */ if (poll_data) { float write_time = 1.0e-6 * t_write.tv_usec + t_write.tv_sec; total_poll_time += write_time; if (max_poll_time < write_time) max_poll_time = write_time; if (min_poll_time > write_time) min_poll_time = write_time; total_poll_cnt++; } if (poll_data && byte != rbyte){ if (device_not_erased){ Info(0, "Warning: It seems that device is not erased.\n" " Erase it with the --erase option.\n"); } Info(0, "Error: Data polling readback status: write=0x%02x read=0x%02x\n", byte, rbyte); throw Error_Device("If device was erased disable polling with the " "-dno-poll option."); }}voidTAvrDummy::FlushWriteBuffer(){ if (page_addr_fetched){ WriteProgramMemoryPage(); }}voidTAvrDummy::ChipErase(){ TByte init[4] = { 0xAC, 0x53, 0x00, 0x00 }; TByte chip_erase [4] = { 0xAC, 0x80, 0x00, 0x00 }; Info(1, "Erasing device ...\n"); Send(init, 4); Send(chip_erase, 4); Delay_usec(Get_t_wd_erase()); Delay_usec(Get_t_wd_erase()); Delay_usec(9000); Delay_usec(9000); PulseReset(); Delay_usec(9000); Info(1, "Reinitializing device\n"); EnableAvr();}/* 0 = program (clear bit), 1 = leave unchanged bit 0 = LB1 bit 1 = LB2 bit 2 = BLB01 bit 3 = BLB02 bit 4 = BLB11 bit 5 = BLB12 bit 6 = 1 (reserved) bit 7 = 1 (reserved) */voidTAvrDummy::WriteLockBits(TByte bits){ /* This handles both old (byte 2, bits 1-2) and new (byte 4, bits 0-5) devices. */ TByte lock[4] = { 0xAC, 0xF9 | ((bits << 1) & 0x06), 0xFF, bits }; TByte rbits; Info(1, "Writing lock bits ...\n"); Send(lock, 4); Delay_usec(Get_t_wd_erase()); PulseReset(); Info(1, "Reinitializing device\n"); EnableAvr(); rbits = ReadLockBits(); if (rbits & ~bits) Info(0, "Warning: lock bits write=0x%02X read=0x%02X\n", (int) bits, (int) rbits);}TByteTAvrDummy::ReadLockBits(){ TByte rbits = 0xFF; if (TestFeatures(AVR_LOCK_BOOT)) { /* x x BLB12 BLB11 BLB02 BLB01 LB2 LB1 */ rbits = ReadLockFuseBits(); } else if (TestFeatures(AVR_LOCK_RD76)) { rbits = ReadLockFuseBits(); /* LB1 LB2 x x x x x x -> 1 1 1 1 1 1 LB2 LB1 */ rbits = ((rbits >> 7) & 1) | ((rbits >> 5) & 1) | 0xFC; } else if (TestFeatures(AVR_LOCK_RD12)) { rbits = ReadLockFuseBits(); /* x x x x x LB2 LB1 x -> 1 1 1 1 1 1 LB2 LB1 */ rbits = ((rbits >> 1) & 3) | 0xFC; } else if (GetPartInfo(0) == 0 && GetPartInfo(1) == 1 && GetPartInfo(2) == 2) { rbits = 0xFC; } else throw Error_Device ("ReadLockBits failed: are you sure this device has lock bits?"); return rbits;}unsigned intTAvrDummy::GetPollCount(){ return total_poll_cnt;}floatTAvrDummy::GetMinPollTime(){ return min_poll_time;}floatTAvrDummy::GetMaxPollTime(){ return max_poll_time;}floatTAvrDummy::GetTotPollTime(){ return total_poll_time;}voidTAvrDummy::ResetMinMax(){ min_poll_time = 1.0; max_poll_time = 0.0; total_poll_time = 0.0; total_poll_cnt = 0;}/* Constructor*/TAvrDummy::TAvrDummy(): use_data_polling(true){ ResetMinMax(); /* Device Command line options ... */ if (GetCmdParam("-dno-poll", false)){use_data_polling=false;} EnableAvr();}#endif/* eof */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -