; ATMEL ISP ALGOTIHM FOR VI-LAB PROGRAMMER ; version 1.0 ; CeDeROM 2004 / www.cederom.blok.net.pl ; Global Variables Var CheckSum:Word ;will keep checsum VAR ChipStart : Word ;chip memory start word VAR FlashStop : Word ;chip FLASH memory stop word VAR EEPROMStop : Word ;chip EEPROM memory stop word VAR MemBufferStart: Word ;editor address word that contains first data word Var DeviceLockBits:Byte ;will keep lock bits ; ; ===== GUI ===== ; ;variables that stores GUI values Var CB_ProgramVerify:Byte; Var CB_AutoErase:Byte; Var CB_AutoProgram:Byte; Var CB_AutoVerify:Byte; Var CB_AutoProtect:Byte; Var CB_FHB7:Byte; Var CB_FHB6:Byte; Var CB_FHB5:Byte; Var CB_FHB4:Byte; Var CB_FHB3:Byte; Var CB_FHB2:Byte; Var CB_FHB1:Byte; Var CB_FHB0:Byte; Var CB_FLB7:Byte; Var CB_FLB6:Byte; Var CB_FLB5:Byte; Var CB_FLB4:Byte; Var CB_FLB3:Byte; Var CB_FLB2:Byte; Var CB_FLB1:Byte; Var CB_FLB0:Byte; Var OperatingOnMemoryNr:Byte; Var BlankCheck:Byte; ; operation buttons Frame(10,10,230,395); Button('/Blank' ,20, 20,100,35,'IconDeviceBlank',Callback_BlankCheck); Button('/Erase' ,130, 20,100,35,'IconDeviceErase',Callback_Erase); Button('/Read' , 20, 65,100, 35,'IconDeviceRead',Callback_ReadMemory); Button('/Verify' , 130, 65,100, 35,'IconDeviceVerify',Callback_Verify); Button('/Program', 20, 110, 100, 35,'IconDeviceProgram',Callback_ProgramMemory); RadioGroup('MEMORY:', 130, 103, 100, 45, OperatingOnMemoryNr, 'FLASH', 'EEPROM'); Frame(10,155,230,75) Button('/Auto' ,20 ,165, 100, 55,'IconDeviceAuto',Callback_Auto); CheckBox('Erase' , 140, 160, CB_AutoErase,1); CheckBox('Program' , 140, 175, CB_AutoProgram,1); CheckBox('Verify', 140, 190, CB_AutoVerify,1); CheckBox('LockBits', 140, 205, CB_AutoProtect,0); Button('/write' ,20 ,360 ,100 ,35,'IconDeviceProtect',Callback_WriteLockBits); Button('/read' ,130 ,360 ,100 ,35,'IconDeviceProtectRD',Callback_ReadLockBits); RadioGroup('MEMORY PROTECTION:', 20, 235, 210, 120, DeviceLockBits, 'NO LOCK', 'PROGRAMMING', 'PROGRAMMING / READING'); Frame(250, 185, 345, 220); Label('Fuses configuration (check=program "0"):', 260, 178); Frame(260,197,320,158) CheckBox('FHB 7:', 270, 200, CB_FHB7, 0); CheckBox('FHB 6:', 270, 215, CB_FHB6, 0); CheckBox('FHB 5:', 270, 230, CB_FHB5, 0); CheckBox('FHB 4: RSTDISBL*', 270, 245, CB_FHB4, 0); CheckBox('FHB 3: SPIEN ', 270, 260, CB_FHB3, 1); CheckBox('FHB 2: EESAVE ', 270, 275, CB_FHB2, 0); CheckBox('FHB 1: BODLEVEL', 270, 290, CB_FHB1, 0); CheckBox('FHB 0: BODEN ', 270, 305, CB_FHB0, 0); CheckBox('FLB 7: PLLCK ', 430, 200, CB_FLB7, 0); CheckBox('FLB 6: CKOPT ', 430, 215, CB_FLB6, 0); CheckBox('FLB 5: SUT1 ', 430, 230, CB_FLB5, 0); CheckBox('FLB 4: SUT0 ', 430, 245, CB_FLB4, 1); CheckBox('FLB 3: CKSEL0*', 430, 260, CB_FLB3, 1); CheckBox('FLB 2: CKSEL1', 430, 275, CB_FLB2, 1); CheckBox('FLB 1: CKSEL2', 430, 290, CB_FLB1, 1); CheckBox('FLB 0: CKSEL3*', 430, 305, CB_FLB0, 0); Label('WARNING: "*" FUSES CAN DISABLE PINS', 270, 320); Label('USED BY ISP INTERFACE FOR GOOD!!!', 270, 334); Button('/write' , 260 , 360 ,150 ,35,'IconDeviceProgram',Callback_WriteFuseBits); Button('/read' , 430 , 360 ,150 ,35,'IconDeviceBlank',Callback_ReadFuseBits); Frame(590,10,185,145) EditHEX('Memory Start' ,100,705, 20,55,25,ChipStart,0,0,FlashStop); EditHEX('FLASH Size' ,100,705, 55,55,25,FLASHStop,_GLOBALSIZE-1,0,_GLOBALSIZE-1); EditHEX('EEPROM Size' ,100,705, 90,55,25,EEPROMStop,03Fh,0,_GLOBALSIZE); EditHEX('Buffer Start',100,705, 125,55,25,MemBufferStart,0,0,0); TextFrame(250,10,330,145, TF_1); ProgressRect(250,160,330,15, PB_1); ; ==================== ; ====== PROGRAM ===== ; ==================== BODY ;******************************************************* ; PINOUT DECLARATION Procedure(Device_SetPins_ZIF) EQU PIN_VCC = 15 EQU PIN_AVCC = 33 EQU PIN_GND = 34 EQU PIN_GND2 = 16 EQU PIN_RST = 20 EQU PIN_MOSI = 11 EQU PIN_MISO = 12 EQU PIN_SCK = 13 EQU PIN_XTAL1 = 17 EndProc; ;******************************************************* ;some variables used by procedures EQU FailCount=5; EQU SyncReply=53h; EQU DontCareByte=0fh; EQU FuseDefaultHighByte=11110111b EQU FuseDefaultLowByte=11100001b VAR ADRESChip:Word; VAR ADRESMemory:LongWord; VAR Blanked:Byte; VAR DataTemp:Byte; VAR Verify:Word; VAR Wait:Word; VAR Status:byte; VAR DeviceSignature:LongWord; VAR EEPROMStopByte:Word; Var DifferentWordsCount:Word; Var CurrentBit:byte; Var CurrentByte:byte; Var CurrentWord:byte; Var CurrentPageWord:Byte; Var CurrentPage:Word; Var PagesCount:Word; Var DataOut:Byte; Var DataIn:Byte; Var TempOut:Byte; Var TempIn:Byte; Var Data:Word; Var DataH:Byte; Var DataL:Byte; Var Data_addr_chip:Word; Var Data_Laddr_chip:Byte; Var Data_Haddr_chip:Byte; Var Data_addr_mem:Word; Var wTemp:Word; Var wTemp2:Word; Var x:word; ; power on the device Procedure(PowerON) Device_SetPins_ZIF; RESET LEDON; VCCSET(_GLOBALVCC); if _GLOBALVCC > 401 then PULLUP5V; if _GLOBALVCC < 400 then PULLUP3V; PINL(PIN_GND, PIN_GND2, PIN_SCK, PIN_RST); VCC(PIN_VCC, PIN_AVCC); VCCON PINH(PIN_MISO, PIN_MOSI); Loop(2150); EndProc; ;power off the device Procedure(PowerOFF) Text(TF_1, '#'); PinH(PIN_RST); Progress(PB_1,0); Loop(10); Reset; Loop(100); EndProc; ;asynchronous serial transmision ;(send and receive at the same time) ;parameters: ; DataOut - send this byte ; DataIn - got this byte during transmision Procedure(Device_TalkByte) Let DataIn=0; Let CurrentBit=7; for CurrentBit downto 0 do begin Let TempOut=DataOut; Let DataOut shl 1; Let TempOut AND 10000000b; if TempOut>0 then PinH(PIN_MOSI); if TempOut=0 then PinL(PIN_MOSI); Let TempIn=0; PINH(PIN_MISO) PinIN(PIN_MISO, TempIn); if TempIn=1 then begin Let TempIn Shl CurrentBit; Let DataIn OR TempIn; end PinH(PIN_SCK) Loop(20) PinL(PIN_SCK) end EndProc; ;send "programming enable" command to device ;parameters: ; Status - returned result of execution Procedure(Device_PrgCMD) Let DataOut = 10101100b Device_TalkByte Let DataOut = 01010011b Device_TalkByte Let DataOut = 0F0h Device_TalkByte Let Status=DataIn Let DataOut = 0F0h Device_TalkByte EndProc; ;reads device signature ;stores result in DeviceSignature variable ;prints out the signature Procedure(Device_ReadSignature) Let CurrentByte=0; Let DeviceSignature=0; for CurrentByte to 2 do begin Let DataOut=00110000b Device_TalkByte Let DataOut=0F0h Device_TalkByte Let DataOut=CurrentByte Device_TalkByte Let DataOut=0F0h Device_TalkByte Let DeviceSignature shl 8; Let DeviceSignature OR DataIn; end Text(TF_1,'#Chip signature: '); TextHex(TF_1, DeviceSignature); EndProc; ;initializes programming mode ;retries FailCount times if handshake fails ;abort program if initialization fails Procedure(Device_Init) TEXT(TF_1,'#/Maroon/Initializing...'); Let x=FailCount; For x downto 1 do begin Device_PrgCMD If Status=SyncReply then begin Device_readSignature; exit; end; PowerOFF; TEXT(TF_1,'init_retry'); PowerON; end; TEXT(TF_1,'#/Red/FAILED! Bad chip response!##//'); PowerOFF; HALT; EndProc; ;erases device Procedure(Device_Erase) Let DataOut=10101100b; Device_TalkByte; Let DataOut=10000000b Device_TalkByte; Let DataOut=0F0h Device_TalkByte; Let DataOut=0F0h Device_TalkByte; Loop(250); Text(TF_1,'#ERASED!#'); EndProc; ;read lock bits and store result in variable Procedure(Device_ReadLockBits) Let DataOut=01011000b Device_TalkByte Let DataOut=0 Device_TalkByte Let DataOut=0F0h Device_TalkByte Let DataOut=0F0h Device_TalkByte Let Data=DataIn; Let Data AND 3; EndProc; ;write lock bits to device according to variable Procedure(Device_WriteLockBits) if DeviceLockBits=0 then Let Data=11111111b; if DeviceLockBits=1 then Let Data=11111110b; if DeviceLockBits=2 then Let Data=11111100b; Let DataOut=10101100b Device_TalkByte Let DataOut=11100000b Device_TalkByte Let DataOut=0F0h Device_TalkByte Let DataOut=Byte(Data) Device_TalkByte EndProc; ;print string that chip is not blank and abort program Procedure(ChipNotBlank) TEXT(TF_1,'#/Red/CHIP NOT BLANK!##//'); PowerOFF; HALT; Let BlankCheck=0; EndProc; ;read word from FLASH program memory ;parameters: ;Data_addr_chip - word address in chip-memory ;Data - store result here Procedure(Device_ReadFLASHWord) Let Data=0; Let wTemp=Data_addr_chip; Let Data_Laddr_chip=Byte(wTemp); Swap(wTemp); Let Data_Haddr_chip=Byte(wTemp); Let DataOut=00101000b; Device_TalkByte; Let DataOut=Data_Haddr_chip; Device_TalkByte; Let DataOut=Data_Laddr_chip; Device_TalkByte; Let DataOut=0F0h; Device_TalkByte; Let Data = DataIn; Let DataOut=00100000b; Device_TalkByte; Let DataOut=Data_Haddr_chip; Device_TalkByte; Let DataOut=Data_Laddr_chip; Device_TalkByte; Let DataOut=0F0h; Device_TalkByte; Swap(Data); Let Data OR DataIn; EndProc; ;read FLASH program memory range from till ;and store result in main buffer starting at Procedure(Device_ReadFLASH) Let CheckSum=0; Let Data_addr_mem=MemBufferStart; Let Data_addr_chip=ChipStart; PROGRESSMAX(PB_1,FLASHStop); PROGRESSMIN(PB_1,ChipStart); for Data_addr_chip to FLASHStop do begin Device_ReadFLASHWord; if BlankCheck=0 then MemoryOut(Data_addr_mem, Data); if BlankCheck=1 then begin if Data <> 0FFFFh then ChipNotBlank; end; Inc(Data_addr_mem); Let CheckSum+Data; Progress(PB_1,Data_addr_chip); end Text(TF_1,'#READ OK: Crc='); TextHex(TF_1,CheckSum); Text(TF_1,'h'); EndProc; ;uploads one word stored in variable ;to device, at address Procedure(Device_LoadProgramMemoryPageWord) Let DataOut=01000000b; Device_TalkByte; Let DataOut=0F0h Device_TalkByte; Let DataOut=CurrentWord; Device_TalkByte; Let DataOut=Byte(Data); Device_TalkByte; Let DataOut=01001000b; Device_TalkByte; Let DataOut=0F0h; Device_TalkByte; Let DataOut=CurrentWord; Device_TalkByte; Swap(Data); Let DataOut=Byte(Data); Device_TalkByte; EndProc; ;write loaded page into FLASH ; data_addr_chip keeps number of page to write to Procedure(Device_WriteProgramMemoryPage) Let Data_addr_chip shl 4; Let DataOut=01001100b; Device_TalkByte; Swap(Data_addr_chip); Let DataOut=Byte(Data_addr_chip); Device_TalkByte; Swap(Data_addr_chip); Let DataOut=Byte(Data_addr_chip); Device_TalkByte; Let DataOut=0F0h; Device_TalkByte; Loop(100); EndProc; ;write buffer to chip FLASH memory from 0 till FLASHStop address Procedure(Device_WriteFLASH) PROGRESSMIN(PB_1,0); PROGRESSMAX(PB_1,FLASHStop); Let PagesCount=FLASHStop; Let PagesCount/16; Let CurrentPage=0; for CurrentPage to PagesCount do begin Let CurrentWord=0; for CurrentWord to 15 do begin Let Data_addr_mem=16; Let Data_addr_mem*CurrentPage; Let Data_addr_mem+CurrentWord; MemoryIn(Data_addr_mem, Data); Device_LoadProgramMemoryPageWord; Progress(PB_1,Data_addr_mem); end Let Data_addr_chip=CurrentPage; Device_WriteProgramMemoryPage end Progress(PB_1,0); EndProc; ;read one byte from EEPROM memory, at address ;and leave it in Procedure(Device_ReadEEPROMByte) Let wTemp=Data_addr_chip; Let Data_Laddr_chip=Byte(wTemp); Swap(wTemp); Let Data_Haddr_chip=Byte(wTemp); Let DataOut=10100000b; Device_TalkByte; Let DataOut=Data_Haddr_chip; Device_TalkByte; Let DataOut=Data_Laddr_chip; Device_TalkByte; Let DataOut=0F0h; Device_TalkByte; EndProc; ;read word from EEPROM memory, at address ;and store it in variable Procedure(Device_ReadEEPROMWord) Device_ReadEEPROMByte; Let Data=DataIn; Inc(Data_addr_chip); Device_ReadEEPROMByte; Swap(Data); Let Data OR DataIn; EndProc; ;write EEPROM byte to chip, at address , ;from variable Procedure(Device_WriteEEPROMByte) Let wTemp=Data_addr_chip; Let Data_Laddr_chip=Byte(wTemp); Swap(wTemp); Let Data_Haddr_chip=Byte(wTemp); Let DataOut=11000000b; Device_TalkByte; Let DataOut=Data_Haddr_chip; Device_TalkByte; Let DataOut=Data_Laddr_chip; Device_TalkByte; Let DataOut=DataL; Device_TalkByte; Loop(100); EndProc; ;write word from to EEPROM chip memory, ;at address Procedure(Device_WriteEEPROMWord) Swap(Data); Let DataL=Byte(Data); Device_WriteEEPROMByte; Inc(Data_addr_chip); Swap(Data); Let DataL=Byte(Data); Device_WriteEEPROMByte; EndProc; ;read chip EEPROM memory from till ;and store it in main buffer startin from address Procedure(Device_ReadEEPROM) Let CheckSum=0; Let Data_addr_mem=MemBufferStart; Let Data_addr_chip=ChipStart; Let EEPROMStopByte = EEPROMStop; Let EEPROMStopByte * 2; PROGRESSMIN(PB_1,ChipStart); PROGRESSMAX(PB_1,EEPROMStop); for Data_addr_chip to EEPROMStopByte do begin Device_ReadEEPROMWord; if BlankCheck=0 then MemoryOut(Data_addr_mem, Data); if BlankCheck=1 then begin if Data<>0FFFFh then ChipNotBlank; end; Inc(Data_addr_mem); Let CheckSum+Data; Progress(PB_1,Data_addr_chip); end Text(TF_1,'#READ OK: Crc='); TextHex(TF_1,CheckSum); Text(TF_1,'h'); EndProc; ;Write to chip EEPROM range .. ;data from main buffer starting at Procedure(Device_WriteEEPROM) Let Data_addr_mem=MemBufferStart; Let Data_addr_chip=ChipStart; Let EEPROMStopByte = EEPROMStop; Let EEPROMStopByte * 2; PROGRESSMIN(PB_1,ChipStart); PROGRESSMAX(PB_1,EEPROMStopByte); for Data_addr_chip to EEPROMStopByte do begin Progress(PB_1,Data_addr_chip); MemoryIn(Data_addr_mem, Data); Device_WriteEEPROMWord; Inc(Data_addr_mem); end; EndProc; ;write that verification failed and abort execution Procedure(VerifyFailed) Text(TF_1,'#/Red/WARNING: '); TextHex(TF_1, DifferentWordsCount); Text(TF_1,'h DIFFERENT WORDS!//'); PowerOFF; HALT; EndProc; ;verify FLASH memory ;get one word from chip, store it in comparation buffer ;and compare with data at the same address in main buffer ;On first difference call VerifyFailed procedure Procedure(Device_VerifyFLASH) Let CheckSum=0; Let DifferentWordsCount=0; Let Data_addr_mem=MemBufferStart; Let Data_addr_chip=ChipStart; PROGRESSMAX(PB_1,FLASHStop); PROGRESSMIN(PB_1,ChipStart); for Data_addr_chip to FLASHStop do begin Device_ReadFLASHWord; CompareOut(Data_addr_mem, Data); MemoryIn(Data_addr_mem, wTemp); if Data<>wTemp then Inc(DifferentWordsCount); Inc(Data_addr_mem); Let CheckSum+Data; Progress(PB_1,Data_addr_chip); end; Text(TF_1,'#Chip-Memory CRC='); TextHex(TF_1,CheckSum); Text(TF_1,'h'); if DifferentWordsCount<>0 then VerifyFailed; EndProc; ;verify EEPROM memory ;get one word from chip, store it in comparation buffer ;and compare with data at the same address in main buffer ;On first difference call VerifyFailed procedure Procedure(Device_VerifyEEPROM) Let CheckSum=0; Let DifferentWordsCount=0; Let Data_addr_mem=MemBufferStart; Let Data_addr_chip=ChipStart; Let EEPROMStopByte = EEPROMStop; Let EEPROMStopByte * 2; PROGRESSMIN(PB_1,ChipStart); PROGRESSMAX(PB_1,EEPROMStop); for Data_addr_chip to EEPROMStopByte do begin Device_ReadEEPROMWord; CompareOut(Data_addr_mem, Data); MemoryIn(Data_addr_mem, wTemp); if Data<>wTemp then Inc(DifferentWordsCount); Inc(Data_addr_mem); Let CheckSum+Data; Progress(PB_1,Data_addr_chip); end; Text(TF_1,'#Chip-Memory CRC='); TextHex(TF_1,CheckSum); Text(TF_1,'h'); if DifferentWordsCount<>0 then VerifyFailed; EndProc; ;read fuses bits and store result in variable Procedure(Device_ReadFuseBits) Let DataOut=01010000b; Device_TalkByte; Let DataOut=00000000b; Device_TalkByte; Let DataOut=DontCareByte; Device_TalkByte; Let DataOut=DontCareByte; Device_TalkByte; Let DataL=DataIn; Let DataOut=01011000b; Device_TalkByte; Let DataOut=00001000b; Device_TalkByte; Let DataOut=DontCareByte; Device_TalkByte; Let DataOut=DontCareByte; Device_TalkByte; Let DataH=DataIn; Let Data = DataH; Swap(Data); Let Data OR DataL; Text(TF_1, '#FUSE DATA: '); TextHex(TF_1, Data); EndProc; ;write fuses bits from variable Procedure(Device_WriteFuseBits) Let DataL=0FFh; Let DataH=0FFh; if CB_FLB7=1 then Let DataL AND 01111111b; if CB_FLB6=1 then Let DataL AND 10111111b; if CB_FLB5=1 then Let DataL AND 11011111b; if CB_FLB4=1 then Let DataL AND 11101111b; if CB_FLB3=1 then Let DataL AND 11110111b; if CB_FLB2=1 then Let DataL AND 11111011b; if CB_FLB1=1 then Let DataL AND 11111101b; if CB_FLB0=1 then Let DataL AND 11111110b; if CB_FHB7=1 then Let DataH AND 01111111b; if CB_FHB6=1 then Let DataH AND 10111111b; if CB_FHB5=1 then Let DataH AND 11011111b; if CB_FHB4=1 then Let DataH AND 11101111b; if CB_FHB3=1 then Let DataH AND 11110111b; if CB_FHB2=1 then Let DataH AND 11111011b; if CB_FHB1=1 then Let DataH AND 11111101b; if CB_FHB0=1 then Let DataH AND 11111110b; Let DataOut=10101100b; Device_TalkByte; Let DataOut=10100000b; Device_TalkByte; Let DataOut=DontCareByte; Device_TalkByte; Let DataOut=DataL; Device_TalkByte; Loop(250); Let DataOut=10101100b; Device_TalkByte; Let DataOut=10101000b; Device_TalkByte; Let DataOut=DontCareByte; Device_TalkByte; Let DataOut=DataH; Device_TalkByte; Loop(250); EndProc; ;============================ ; CALLBACKS FOR BUTTON-PRESS ;============================ Procedure(Callback_Erase) Text(TF_1, '#/Green/==ERASING==//'); PowerON Device_Init; Device_Erase; PowerOFF EndProc; Procedure(Callback_BlankCheck) Text(TF_1, '#/Green/==BLANK CHECK==//'); Let BlankCheck=1; PowerON; Device_Init; if OperatingOnMemoryNr=0 then Device_ReadFLASH; if OperatingOnMemoryNr=1 then Device_ReadEEPROM; PowerOFF; Text(TF_1, 'DEVICE BLANK#'); Let BlankCheck=0; EndProc; Procedure(Callback_ReadMemory) Text(TF_1, '#/Green/==READING==//'); PowerON; Device_Init; Device_ReadLockBits; if Data=0 then Text(TF_1, '#/Red/Memory locked against reading!#Defective read almost sure!//'); if OperatingOnMemoryNr=0 then Device_ReadFLASH; if OperatingOnMemoryNr=1 then Device_ReadEEPROM; PowerOFF; EndProc; Procedure(Callback_ProgramMemory) Text(TF_1, '#/Green/==PROGRAMMING==//'); PowerON; Device_init; if OperatingOnMemoryNr=0 then Device_WriteFLASH; if OperatingOnMemoryNr=1 then Device_WriteEEPROM; Text(TF_1, '#OK'); PowerOFF; EndProc; Procedure(Callback_ReadLockBits) Text(TF_1, '#/Green/==READING LOCK BITS==//'); PowerON; Device_Init; Device_ReadLockBits; if Data=3 then Text(TF_1, '#NO MEMORY LOCK'); if Data=2 then Text(TF_1, '#/Red/MEMORY WRITE LOCK!//'); if Data=0 then Text(TF_1, '#/Red/MEMORY READ AND WRITE LOCK!//'); PowerOFF; EndProc; Procedure(Callback_WriteLockBits) Text(TF_1, '#/Green/==PROGRAMMING LOCK BIT(S)==//'); PowerON; Device_Init; Device_ReadLockBits; if Data=0 then begin Text(TF_1, '/Red/#ERROR: Lock bits already programmed!#ChipErase required...'); PowerOFF; Exit; end Device_WriteLockBits; Text(TF_1, '#OK'); PowerOFF; EndProc; Procedure(Callback_ReadFuseBits) Text(TF_1, '#/Green/==READING FUSE BITS==//'); PowerON; Device_Init; Device_ReadFuseBits; PowerOFF; EndProc; Procedure(Callback_WriteFuseBits) Text(TF_1, '#/Green/==PROGRAMMING FUSE BITS==//'); PowerON; Device_Init; Device_ReadLockBits; if Data<3 then begin Text(TF_1, '/Red/#ERROR: Lock bits programmed!#ChipErase required...'); PowerOFF; Exit; end Device_WriteFuseBits; Text(TF_1, '#OK#'); PowerOFF; EndProc; Procedure(Callback_Verify) Text(TF_1, '#/Green/==VERYFYING==//'); PowerON; Device_Init; if OperatingOnMemoryNr=0 then Device_VerifyFLASH; if OperatingOnMemoryNr=1 then Device_VerifyEEPROM; PowerOFF; EndProc; Procedure(Callback_Auto) if CB_AutoErase=1 then Callback_Erase; if CB_AutoProgram=1 then Callback_ProgramMemory; if CB_AutoVerify=1 then Callback_Verify; if CB_AutoProtect=1 then Callback_WriteLockBits; EndProc; End. ; ATMEL ISP ALGOTIHM FOR VI-LAB PROGRAMMER ; version 1.0 ; CeDeROM 2004 / www.cederom.blok.net.pl ; Global Variables Var CheckSum:Word ;will keep checsum VAR ChipStart : Word ;chip memory start word VAR FlashStop : Word ;chip FLASH memory stop word VAR EEPROMStop : Word ;chip EEPROM memory stop word VAR MemBufferStart: Word ;editor address word that contains first data word Var DeviceLockBits:Byte ;will keep lock bits ; ; ===== GUI ===== ; ;variables that stores GUI values Var CB_ProgramVerify:Byte; Var CB_AutoErase:Byte; Var CB_AutoProgram:Byte; Var CB_AutoVerify:Byte; Var CB_AutoProtect:Byte; Var CB_FHB7:Byte; Var CB_FHB6:Byte; Var CB_FHB5:Byte; Var CB_FHB4:Byte; Var CB_FHB3:Byte; Var CB_FHB2:Byte; Var CB_FHB1:Byte; Var CB_FHB0:Byte; Var CB_FLB7:Byte; Var CB_FLB6:Byte; Var CB_FLB5:Byte; Var CB_FLB4:Byte; Var CB_FLB3:Byte; Var CB_FLB2:Byte; Var CB_FLB1:Byte; Var CB_FLB0:Byte; Var OperatingOnMemoryNr:Byte; Var BlankCheck:Byte; ; operation buttons Frame(10,10,230,395); Button('/Blank' ,20, 20,100,35,'IconDeviceBlank',Callback_BlankCheck); Button('/Erase' ,130, 20,100,35,'IconDeviceErase',Callback_Erase); Button('/Read' , 20, 65,100, 35,'IconDeviceRead',Callback_ReadMemory); Button('/Verify' , 130, 65,100, 35,'IconDeviceVerify',Callback_Verify); Button('/Program', 20, 110, 100, 35,'IconDeviceProgram',Callback_ProgramMemory); RadioGroup('MEMORY:', 130, 103, 100, 45, OperatingOnMemoryNr, 'FLASH', 'EEPROM'); Frame(10,155,230,75) Button('/Auto' ,20 ,165, 100, 55,'IconDeviceAuto',Callback_Auto); CheckBox('Erase' , 140, 160, CB_AutoErase,1); CheckBox('Program' , 140, 175, CB_AutoProgram,1); CheckBox('Verify', 140, 190, CB_AutoVerify,1); CheckBox('LockBits', 140, 205, CB_AutoProtect,0); Button('/write' ,20 ,360 ,100 ,35,'IconDeviceProtect',Callback_WriteLockBits); Button('/read' ,130 ,360 ,100 ,35,'IconDeviceProtectRD',Callback_ReadLockBits); RadioGroup('MEMORY PROTECTION:', 20, 235, 210, 120, DeviceLockBits, 'NO LOCK', 'PROGRAMMING', 'PROGRAMMING / READING'); Frame(250, 185, 345, 220); Label('Fuses configuration (check=program "0"):', 260, 178); Frame(260,197,320,158) CheckBox('FHB 7:', 270, 200, CB_FHB7, 0); CheckBox('FHB 6:', 270, 215, CB_FHB6, 0); CheckBox('FHB 5:', 270, 230, CB_FHB5, 0); CheckBox('FHB 4: RSTDISBL*', 270, 245, CB_FHB4, 0); CheckBox('FHB 3: SPIEN ', 270, 260, CB_FHB3, 1); CheckBox('FHB 2: EESAVE ', 270, 275, CB_FHB2, 0); CheckBox('FHB 1: BODLEVEL', 270, 290, CB_FHB1, 0); CheckBox('FHB 0: BODEN ', 270, 305, CB_FHB0, 0); CheckBox('FLB 7: PLLCK ', 430, 200, CB_FLB7, 0); CheckBox('FLB 6: CKOPT ', 430, 215, CB_FLB6, 0); CheckBox('FLB 5: SUT1 ', 430, 230, CB_FLB5, 0); CheckBox('FLB 4: SUT0 ', 430, 245, CB_FLB4, 1); CheckBox('FLB 3: CKSEL0*', 430, 260, CB_FLB3, 1); CheckBox('FLB 2: CKSEL1', 430, 275, CB_FLB2, 1); CheckBox('FLB 1: CKSEL2', 430, 290, CB_FLB1, 1); CheckBox('FLB 0: CKSEL3*', 430, 305, CB_FLB0, 0); Label('WARNING: "*" FUSES CAN DISABLE PINS', 270, 320); Label('USED BY ISP INTERFACE FOR GOOD!!!', 270, 334); Button('/write' , 260 , 360 ,150 ,35,'IconDeviceProgram',Callback_WriteFuseBits); Button('/read' , 430 , 360 ,150 ,35,'IconDeviceBlank',Callback_ReadFuseBits); Frame(590,10,185,145) EditHEX('Memory Start' ,100,705, 20,55,25,ChipStart,0,0,FlashStop); EditHEX('FLASH Size' ,100,705, 55,55,25,FLASHStop,_GLOBALSIZE-1,0,_GLOBALSIZE-1); EditHEX('EEPROM Size' ,100,705, 90,55,25,EEPROMStop,03Fh,0,_GLOBALSIZE); EditHEX('Buffer Start',100,705, 125,55,25,MemBufferStart,0,0,0); TextFrame(250,10,330,145, TF_1); ProgressRect(250,160,330,15, PB_1); ; ==================== ; ====== PROGRAM ===== ; ==================== BODY ;******************************************************* ; PINOUT DECLARATION Procedure(Device_SetPins_ZIF) EQU PIN_VCC = 15 EQU PIN_AVCC = 33 EQU PIN_GND = 34 EQU PIN_GND2 = 16 EQU PIN_RST = 20 EQU PIN_MOSI = 11 EQU PIN_MISO = 12 EQU PIN_SCK = 13 EQU PIN_XTAL1 = 17 EndProc; ;******************************************************* ;some variables used by procedures EQU FailCount=5; EQU SyncReply=53h; EQU DontCareByte=0fh; EQU FuseDefaultHighByte=11110111b EQU FuseDefaultLowByte=11100001b VAR ADRESChip:Word; VAR ADRESMemory:LongWord; VAR Blanked:Byte; VAR DataTemp:Byte; VAR Verify:Word; VAR Wait:Word; VAR Status:byte; VAR DeviceSignature:LongWord; VAR EEPROMStopByte:Word; Var DifferentWordsCount:Word; Var CurrentBit:byte; Var CurrentByte:byte; Var CurrentWord:byte; Var CurrentPageWord:Byte; Var CurrentPage:Word; Var PagesCount:Word; Var DataOut:Byte; Var DataIn:Byte; Var TempOut:Byte; Var TempIn:Byte; Var Data:Word; Var DataH:Byte; Var DataL:Byte; Var Data_addr_chip:Word; Var Data_Laddr_chip:Byte; Var Data_Haddr_chip:Byte; Var Data_addr_mem:Word; Var wTemp:Word; Var wTemp2:Word; Var x:word; ; power on the device Procedure(PowerON) Device_SetPins_ZIF; RESET LEDON; VCCSET(_GLOBALVCC); if _GLOBALVCC > 401 then PULLUP5V; if _GLOBALVCC < 400 then PULLUP3V; PINL(PIN_GND, PIN_GND2, PIN_SCK, PIN_RST); VCC(PIN_VCC, PIN_AVCC); VCCON PINH(PIN_MISO, PIN_MOSI); Loop(2150); EndProc; ;power off the device Procedure(PowerOFF) Text(TF_1, '#'); PinH(PIN_RST); Progress(PB_1,0); Loop(10); Reset; Loop(100); EndProc; ;asynchronous serial transmision ;(send and receive at the same time) ;parameters: ; DataOut - send this byte ; DataIn - got this byte during transmision Procedure(Device_TalkByte) Let DataIn=0; Let CurrentBit=7; for CurrentBit downto 0 do begin Let TempOut=DataOut; Let DataOut shl 1; Let TempOut AND 10000000b; if TempOut>0 then PinH(PIN_MOSI); if TempOut=0 then PinL(PIN_MOSI); Let TempIn=0; PINH(PIN_MISO) PinIN(PIN_MISO, TempIn); if TempIn=1 then begin Let TempIn Shl CurrentBit; Let DataIn OR TempIn; end PinH(PIN_SCK) Loop(20) PinL(PIN_SCK) end EndProc; ;send "programming enable" command to device ;parameters: ; Status - returned result of execution Procedure(Device_PrgCMD) Let DataOut = 10101100b Device_TalkByte Let DataOut = 01010011b Device_TalkByte Let DataOut = 0F0h Device_TalkByte Let Status=DataIn Let DataOut = 0F0h Device_TalkByte EndProc; ;reads device signature ;stores result in DeviceSignature variable ;prints out the signature Procedure(Device_ReadSignature) Let CurrentByte=0; Let DeviceSignature=0; for CurrentByte to 2 do begin Let DataOut=00110000b Device_TalkByte Let DataOut=0F0h Device_TalkByte Let DataOut=CurrentByte Device_TalkByte Let DataOut=0F0h Device_TalkByte Let DeviceSignature shl 8; Let DeviceSignature OR DataIn; end Text(TF_1,'#Chip signature: '); TextHex(TF_1, DeviceSignature); EndProc; ;initializes programming mode ;retries FailCount times if handshake fails ;abort program if initialization fails Procedure(Device_Init) TEXT(TF_1,'#/Maroon/Initializing...'); Let x=FailCount; For x downto 1 do begin Device_PrgCMD If Status=SyncReply then begin Device_readSignature; exit; end; PowerOFF; TEXT(TF_1,'init_retry'); PowerON; end; TEXT(TF_1,'#/Red/FAILED! Bad chip response!##//'); PowerOFF; HALT; EndProc; ;erases device Procedure(Device_Erase) Let DataOut=10101100b; Device_TalkByte; Let DataOut=10000000b Device_TalkByte; Let DataOut=0F0h Device_TalkByte; Let DataOut=0F0h Device_TalkByte; Loop(250); Text(TF_1,'#ERASED!#'); EndProc; ;read lock bits and store result in variable Procedure(Device_ReadLockBits) Let DataOut=01011000b Device_TalkByte Let DataOut=0 Device_TalkByte Let DataOut=0F0h Device_TalkByte Let DataOut=0F0h Device_TalkByte Let Data=DataIn; Let Data AND 3; EndProc; ;write lock bits to device according to variable Procedure(Device_WriteLockBits) if DeviceLockBits=0 then Let Data=11111111b; if DeviceLockBits=1 then Let Data=11111110b; if DeviceLockBits=2 then Let Data=11111100b; Let DataOut=10101100b Device_TalkByte Let DataOut=11100000b Device_TalkByte Let DataOut=0F0h Device_TalkByte Let DataOut=Byte(Data) Device_TalkByte EndProc; ;print string that chip is not blank and abort program Procedure(ChipNotBlank) TEXT(TF_1,'#/Red/CHIP NOT BLANK!##//'); PowerOFF; HALT; Let BlankCheck=0; EndProc; ;read word from FLASH program memory ;parameters: ;Data_addr_chip - word address in chip-memory ;Data - store result here Procedure(Device_ReadFLASHWord) Let Data=0; Let wTemp=Data_addr_chip; Let Data_Laddr_chip=Byte(wTemp); Swap(wTemp); Let Data_Haddr_chip=Byte(wTemp); Let DataOut=00101000b; Device_TalkByte; Let DataOut=Data_Haddr_chip; Device_TalkByte; Let DataOut=Data_Laddr_chip; Device_TalkByte; Let DataOut=0F0h; Device_TalkByte; Let Data = DataIn; Let DataOut=00100000b; Device_TalkByte; Let DataOut=Data_Haddr_chip; Device_TalkByte; Let DataOut=Data_Laddr_chip; Device_TalkByte; Let DataOut=0F0h; Device_TalkByte; Swap(Data); Let Data OR DataIn; EndProc; ;read FLASH program memory range from till ;and store result in main buffer starting at Procedure(Device_ReadFLASH) Let CheckSum=0; Let Data_addr_mem=MemBufferStart; Let Data_addr_chip=ChipStart; PROGRESSMAX(PB_1,FLASHStop); PROGRESSMIN(PB_1,ChipStart); for Data_addr_chip to FLASHStop do begin Device_ReadFLASHWord; if BlankCheck=0 then MemoryOut(Data_addr_mem, Data); if BlankCheck=1 then begin if Data <> 0FFFFh then ChipNotBlank; end; Inc(Data_addr_mem); Let CheckSum+Data; Progress(PB_1,Data_addr_chip); end Text(TF_1,'#READ OK: Crc='); TextHex(TF_1,CheckSum); Text(TF_1,'h'); EndProc; ;uploads one word stored in variable ;to device, at address Procedure(Device_LoadProgramMemoryPageWord) Let DataOut=01000000b; Device_TalkByte; Let DataOut=0F0h Device_TalkByte; Let DataOut=CurrentWord; Device_TalkByte; Let DataOut=Byte(Data); Device_TalkByte; Let DataOut=01001000b; Device_TalkByte; Let DataOut=0F0h; Device_TalkByte; Let DataOut=CurrentWord; Device_TalkByte; Swap(Data); Let DataOut=Byte(Data); Device_TalkByte; EndProc; ;write loaded page into FLASH ; data_addr_chip keeps number of page to write to Procedure(Device_WriteProgramMemoryPage) Let Data_addr_chip shl 4; Let DataOut=01001100b; Device_TalkByte; Swap(Data_addr_chip); Let DataOut=Byte(Data_addr_chip); Device_TalkByte; Swap(Data_addr_chip); Let DataOut=Byte(Data_addr_chip); Device_TalkByte; Let DataOut=0F0h; Device_TalkByte; Loop(100); EndProc; ;write buffer to chip FLASH memory from 0 till FLASHStop address Procedure(Device_WriteFLASH) PROGRESSMIN(PB_1,0); PROGRESSMAX(PB_1,FLASHStop); Let PagesCount=FLASHStop; Let PagesCount/16; Let CurrentPage=0; for CurrentPage to PagesCount do begin Let CurrentWord=0; for CurrentWord to 15 do begin Let Data_addr_mem=16; Let Data_addr_mem*CurrentPage; Let Data_addr_mem+CurrentWord; MemoryIn(Data_addr_mem, Data); Device_LoadProgramMemoryPageWord; Progress(PB_1,Data_addr_mem); end Let Data_addr_chip=CurrentPage; Device_WriteProgramMemoryPage end Progress(PB_1,0); EndProc; ;read one byte from EEPROM memory, at address ;and leave it in Procedure(Device_ReadEEPROMByte) Let wTemp=Data_addr_chip; Let Data_Laddr_chip=Byte(wTemp); Swap(wTemp); Let Data_Haddr_chip=Byte(wTemp); Let DataOut=10100000b; Device_TalkByte; Let DataOut=Data_Haddr_chip; Device_TalkByte; Let DataOut=Data_Laddr_chip; Device_TalkByte; Let DataOut=0F0h; Device_TalkByte; EndProc; ;read word from EEPROM memory, at address ;and store it in variable Procedure(Device_ReadEEPROMWord) Device_ReadEEPROMByte; Let Data=DataIn; Inc(Data_addr_chip); Device_ReadEEPROMByte; Swap(Data); Let Data OR DataIn; EndProc; ;write EEPROM byte to chip, at address , ;from variable Procedure(Device_WriteEEPROMByte) Let wTemp=Data_addr_chip; Let Data_Laddr_chip=Byte(wTemp); Swap(wTemp); Let Data_Haddr_chip=Byte(wTemp); Let DataOut=11000000b; Device_TalkByte; Let DataOut=Data_Haddr_chip; Device_TalkByte; Let DataOut=Data_Laddr_chip; Device_TalkByte; Let DataOut=DataL; Device_TalkByte; Loop(100); EndProc; ;write word from to EEPROM chip memory, ;at address Procedure(Device_WriteEEPROMWord) Swap(Data); Let DataL=Byte(Data); Device_WriteEEPROMByte; Inc(Data_addr_chip); Swap(Data); Let DataL=Byte(Data); Device_WriteEEPROMByte; EndProc; ;read chip EEPROM memory from till ;and store it in main buffer startin from address Procedure(Device_ReadEEPROM) Let CheckSum=0; Let Data_addr_mem=MemBufferStart; Let Data_addr_chip=ChipStart; Let EEPROMStopByte = EEPROMStop; Let EEPROMStopByte * 2; PROGRESSMIN(PB_1,ChipStart); PROGRESSMAX(PB_1,EEPROMStop); for Data_addr_chip to EEPROMStopByte do begin Device_ReadEEPROMWord; if BlankCheck=0 then MemoryOut(Data_addr_mem, Data); if BlankCheck=1 then begin if Data<>0FFFFh then ChipNotBlank; end; Inc(Data_addr_mem); Let CheckSum+Data; Progress(PB_1,Data_addr_chip); end Text(TF_1,'#READ OK: Crc='); TextHex(TF_1,CheckSum); Text(TF_1,'h'); EndProc; ;Write to chip EEPROM range .. ;data from main buffer starting at Procedure(Device_WriteEEPROM) Let Data_addr_mem=MemBufferStart; Let Data_addr_chip=ChipStart; Let EEPROMStopByte = EEPROMStop; Let EEPROMStopByte * 2; PROGRESSMIN(PB_1,ChipStart); PROGRESSMAX(PB_1,EEPROMStopByte); for Data_addr_chip to EEPROMStopByte do begin Progress(PB_1,Data_addr_chip); MemoryIn(Data_addr_mem, Data); Device_WriteEEPROMWord; Inc(Data_addr_mem); end; EndProc; ;write that verification failed and abort execution Procedure(VerifyFailed) Text(TF_1,'#/Red/WARNING: '); TextHex(TF_1, DifferentWordsCount); Text(TF_1,'h DIFFERENT WORDS!//'); PowerOFF; HALT; EndProc; ;verify FLASH memory ;get one word from chip, store it in comparation buffer ;and compare with data at the same address in main buffer ;On first difference call VerifyFailed procedure Procedure(Device_VerifyFLASH) Let CheckSum=0; Let DifferentWordsCount=0; Let Data_addr_mem=MemBufferStart; Let Data_addr_chip=ChipStart; PROGRESSMAX(PB_1,FLASHStop); PROGRESSMIN(PB_1,ChipStart); for Data_addr_chip to FLASHStop do begin Device_ReadFLASHWord; CompareOut(Data_addr_mem, Data); MemoryIn(Data_addr_mem, wTemp); if Data<>wTemp then Inc(DifferentWordsCount); Inc(Data_addr_mem); Let CheckSum+Data; Progress(PB_1,Data_addr_chip); end; Text(TF_1,'#Chip-Memory CRC='); TextHex(TF_1,CheckSum); Text(TF_1,'h'); if DifferentWordsCount<>0 then VerifyFailed; EndProc; ;verify EEPROM memory ;get one word from chip, store it in comparation buffer ;and compare with data at the same address in main buffer ;On first difference call VerifyFailed procedure Procedure(Device_VerifyEEPROM) Let CheckSum=0; Let DifferentWordsCount=0; Let Data_addr_mem=MemBufferStart; Let Data_addr_chip=ChipStart; Let EEPROMStopByte = EEPROMStop; Let EEPROMStopByte * 2; PROGRESSMIN(PB_1,ChipStart); PROGRESSMAX(PB_1,EEPROMStop); for Data_addr_chip to EEPROMStopByte do begin Device_ReadEEPROMWord; CompareOut(Data_addr_mem, Data); MemoryIn(Data_addr_mem, wTemp); if Data<>wTemp then Inc(DifferentWordsCount); Inc(Data_addr_mem); Let CheckSum+Data; Progress(PB_1,Data_addr_chip); end; Text(TF_1,'#Chip-Memory CRC='); TextHex(TF_1,CheckSum); Text(TF_1,'h'); if DifferentWordsCount<>0 then VerifyFailed; EndProc; ;read fuses bits and store result in variable Procedure(Device_ReadFuseBits) Let DataOut=01010000b; Device_TalkByte; Let DataOut=00000000b; Device_TalkByte; Let DataOut=DontCareByte; Device_TalkByte; Let DataOut=DontCareByte; Device_TalkByte; Let DataL=DataIn; Let DataOut=01011000b; Device_TalkByte; Let DataOut=00001000b; Device_TalkByte; Let DataOut=DontCareByte; Device_TalkByte; Let DataOut=DontCareByte; Device_TalkByte; Let DataH=DataIn; Let Data = DataH; Swap(Data); Let Data OR DataL; Text(TF_1, '#FUSE DATA: '); TextHex(TF_1, Data); EndProc; ;write fuses bits from variable Procedure(Device_WriteFuseBits) Let DataL=0FFh; Let DataH=0FFh; if CB_FLB7=1 then Let DataL AND 01111111b; if CB_FLB6=1 then Let DataL AND 10111111b; if CB_FLB5=1 then Let DataL AND 11011111b; if CB_FLB4=1 then Let DataL AND 11101111b; if CB_FLB3=1 then Let DataL AND 11110111b; if CB_FLB2=1 then Let DataL AND 11111011b; if CB_FLB1=1 then Let DataL AND 11111101b; if CB_FLB0=1 then Let DataL AND 11111110b; if CB_FHB7=1 then Let DataH AND 01111111b; if CB_FHB6=1 then Let DataH AND 10111111b; if CB_FHB5=1 then Let DataH AND 11011111b; if CB_FHB4=1 then Let DataH AND 11101111b; if CB_FHB3=1 then Let DataH AND 11110111b; if CB_FHB2=1 then Let DataH AND 11111011b; if CB_FHB1=1 then Let DataH AND 11111101b; if CB_FHB0=1 then Let DataH AND 11111110b; Let DataOut=10101100b; Device_TalkByte; Let DataOut=10100000b; Device_TalkByte; Let DataOut=DontCareByte; Device_TalkByte; Let DataOut=DataL; Device_TalkByte; Loop(250); Let DataOut=10101100b; Device_TalkByte; Let DataOut=10101000b; Device_TalkByte; Let DataOut=DontCareByte; Device_TalkByte; Let DataOut=DataH; Device_TalkByte; Loop(250); EndProc; ;============================ ; CALLBACKS FOR BUTTON-PRESS ;============================ Procedure(Callback_Erase) Text(TF_1, '#/Green/==ERASING==//'); PowerON Device_Init; Device_Erase; PowerOFF EndProc; Procedure(Callback_BlankCheck) Text(TF_1, '#/Green/==BLANK CHECK==//'); Let BlankCheck=1; PowerON; Device_Init; if OperatingOnMemoryNr=0 then Device_ReadFLASH; if OperatingOnMemoryNr=1 then Device_ReadEEPROM; PowerOFF; Text(TF_1, 'DEVICE BLANK#'); Let BlankCheck=0; EndProc; Procedure(Callback_ReadMemory) Text(TF_1, '#/Green/==READING==//'); PowerON; Device_Init; Device_ReadLockBits; if Data=0 then Text(TF_1, '#/Red/Memory locked against reading!#Defective read almost sure!//'); if OperatingOnMemoryNr=0 then Device_ReadFLASH; if OperatingOnMemoryNr=1 then Device_ReadEEPROM; PowerOFF; EndProc; Procedure(Callback_ProgramMemory) Text(TF_1, '#/Green/==PROGRAMMING==//'); PowerON; Device_init; if OperatingOnMemoryNr=0 then Device_WriteFLASH; if OperatingOnMemoryNr=1 then Device_WriteEEPROM; Text(TF_1, '#OK'); PowerOFF; EndProc; Procedure(Callback_ReadLockBits) Text(TF_1, '#/Green/==READING LOCK BITS==//'); PowerON; Device_Init; Device_ReadLockBits; if Data=3 then Text(TF_1, '#NO MEMORY LOCK'); if Data=2 then Text(TF_1, '#/Red/MEMORY WRITE LOCK!//'); if Data=0 then Text(TF_1, '#/Red/MEMORY READ AND WRITE LOCK!//'); PowerOFF; EndProc; Procedure(Callback_WriteLockBits) Text(TF_1, '#/Green/==PROGRAMMING LOCK BIT(S)==//'); PowerON; Device_Init; Device_ReadLockBits; if Data=0 then begin Text(TF_1, '/Red/#ERROR: Lock bits already programmed!#ChipErase required...'); PowerOFF; Exit; end Device_WriteLockBits; Text(TF_1, '#OK'); PowerOFF; EndProc; Procedure(Callback_ReadFuseBits) Text(TF_1, '#/Green/==READING FUSE BITS==//'); PowerON; Device_Init; Device_ReadFuseBits; PowerOFF; EndProc; Procedure(Callback_WriteFuseBits) Text(TF_1, '#/Green/==PROGRAMMING FUSE BITS==//'); PowerON; Device_Init; Device_ReadLockBits; if Data<3 then begin Text(TF_1, '/Red/#ERROR: Lock bits programmed!#ChipErase required...'); PowerOFF; Exit; end Device_WriteFuseBits; Text(TF_1, '#OK#'); PowerOFF; EndProc; Procedure(Callback_Verify) Text(TF_1, '#/Green/==VERYFYING==//'); PowerON; Device_Init; if OperatingOnMemoryNr=0 then Device_VerifyFLASH; if OperatingOnMemoryNr=1 then Device_VerifyEEPROM; PowerOFF; EndProc; Procedure(Callback_Auto) if CB_AutoErase=1 then Callback_Erase; if CB_AutoProgram=1 then Callback_ProgramMemory; if CB_AutoVerify=1 then Callback_Verify; if CB_AutoProtect=1 then Callback_WriteLockBits; EndProc; End.