; Orange programmer module v3.6 beta
; (c) 2001-2004 CnCLab
; Atmel AVR
; CHIP=ATTiny26,2K
; AREA=FLASH,2K(32),0,0
; AREA=EEPROM,128,0,0
;
; Primary tested


ALLPINS=20
INFO="Tiny26 socket"

PINO=SCK,  0,3
PINO=MOSI, 2,1
PINO=MISO, 1,2
PINO=RESET,3,10
PINI=MISO, 1,2

PINO=XTAL1,12,7
PINO=GND,255,6
PINO=GND2,255,16
PINO=VCC,254,5
PINO=AVCC,254,15


BUSO=CFREQ,128

R8=Fuses,B8
R9=Lock,C2,LB2,LB1
R10=CKSEL,L,"0000: Ext Clk,0001: IntRC 1MHz,0010: IntRC 2MHz,0011: IntRC 4MHz,0100: IntRC 8MHz,0101: ExtRC 0.9MHz,0110: ExtRC 0.9-3.0M,0111: ExtRC 3.0-8.0M,1000: ExtRC 8.0-12.0M,1001: LowFreq Crystal,1010: Crystal/Ceramic,1011: Crystal/Ceramic,1100: Crystal 0.9-3.0,1101: Crystal 0.9-3.0,1110: Crystal 3.0-8.0,1111: Crystal 3.0-8.0"
R11=SUT,L,"00: 0 (BOD rec.),01: 4.1ms,10: 65ms"
R12=CKOPT,L,"0 - On,1 - Off"
R13=PLLCK,L,"0 - On,1 - Off"

R14=BODEN,L,"0 - On,1 - Off"
R15=BODLEVEL,L,"0 - High","1 - Low"
R16=EESAVE,L,"0 - Save,1 - Clear"
R17=SPIEN,L,"0 - Enable,1 - Disable"
R18=RSTDISBL,L,"0: Int.Reset,1: Ext.Reset"
R19=Calibration,H4

CDELAY=10 ; minimum periods for the serial clock (SCK) > 4 XTAL1 clock cycle!


[_DOUT]
R1=0;
LOOP=(7,0){MOSI=R0[I],SCK=1,R1[I]=MISO,SCK=0}

[_DIN]
R0=0
LOOP=(7,0){SCK=1,R0[I]=MISO,SCK=0}


[INIT]
SCK=0
MISO=Z
CFREQ=2000000
P=50
;RESET=1,P=100
RESET=0,P=200

LOOP=(0,31){    ; 32 attempts
RESET=1,P=150
RESET=0,P=20000
R0=0xAC,_DOUT ;Programming Enable
R0=0x53,_DOUT
R0=0x00,_DOUT
R3=R1
R0=0x00,_DOUT
R3?0x53{BREAK}
;PRINT=("ANSWER: %02lX",R1)
}
R3?!0x53{PRINT=("ANSWER ERROR: %02lX",R3),EXIT}

P=10



[READ]
$AREA?0{
R0=0x20            ;program memory area
R1=ADR,R0[3]=R1[0] ;Hi/Lo
_DOUT
LOOP=(16,9){MOSI=ADR[I],SCK=P}
LOOP=(8,1){MOSI=ADR[I],SCK=P}
}
$AREA?1{
R0=0xA0      ;eeprom area
_DOUT
LOOP=(15,8){MOSI=ADR[I],SCK=P}
LOOP=(7,0){MOSI=ADR[I],SCK=P}
}

_DIN		;common
DATA=R0


[WRITE]
$AREA?0{
PRINT=("Only Page Write Supported!")
EXIT
}

$AREA?1{
R0=0xC0 ;eeprom memory area
_DOUT
LOOP=(15,8){MOSI=ADR[I],SCK=P}
LOOP=(7,0){MOSI=ADR[I],SCK=P}
R0=DATA,_DOUT
P=9000 ; 4ms
}


[WRITEBLOCK]
LOOP=(0,15){
R0=0x40 ;program memory
R1=ADR,R0[3]=R1[0],_DOUT 	;Hi/Lo
R0=0,_DOUT
LOOP=(8,1){MOSI=ADR[I],SCK=P}
R0=DATA,_DOUT
P=10
ADR=+1
}
;Write Program memory Page:
ADR=-16
R0=0x4C,_DOUT
LOOP=(16,9){MOSI=ADR[I],SCK=P}
LOOP=(8,5){MOSI=ADR[I],SCK=P}
LOOP=(3,0){MOSI=0,SCK=P}
R0=0x0,_DOUT 		;dummy data
P=5000

[WRITEEND]
P=10000
RESET=1,P=20,
RESET=0,P=20000




["Read Fuses"]
R0=0x50,_DOUT	;Read Fuse bits
R0=0x00,_DOUT
R0=0x00,_DOUT
_DIN
R8=R0


R10=0,R11=0,R12=0,R13=0
R10=R8,R10=&0xF	;R10=CKSEL,L
R11[0]=R8[4]    ;R11=SUT,L,1,2,3,4
R11[1]=R8[5]
R12[0]=R8[6]    ;R12=CKOPT
R13[0]=R8[7]    ;R13=PLLCK

R0=0x58,_DOUT   ;Read Fuse High Bits
R0=0x08,_DOUT
R0=0x00,_DOUT
_DIN
RESET=1,P=10
VCC=0

R8=R0
;GET=("Read Fuses",R8)
R14=0,R15=0,R16=0,R17=0,R18=0
R14[0]=R8[0]	;R14=BODEN
R15[0]=R8[1]	;R15=BODLEVEL
R16[1]=R8[2]	;R16=EESAVE
R17[0]=R8[3]	;R17=SPIEN
R18[0]=R8[4]	;R18=Rstdisbl



GET=("Read Fuses",R10,R11,R12,R13,R14,R15,R16,R17,R18)
;GET=("Read Fuses",R10,R11,R12,R13,R14)


["Write Fuses"]
GET=("Write Fuses",R10,R11,R12,R13,R14,R15,R16,R17,R18)
;PRINT=("%lX",R11)
R18?0{
PRINT=A("This fuse disable serial programming?")
RA?0{EXIT}
}

R17?1{
PRINT=A("This fuse disable serial programming?")
RA?0{EXIT}
}

R8=0

R8=R10,R8=&0xF	;R10=CKSEL,L
R8[4]=R11[0]   ;R11=SUT,L,1,2,3
R8[5]=R11[1]
R8[6]=R12[0]   ;R12=CKOPT
R8[7]=R13[0]   ;R13=PLLCK

R0=0xAC,_DOUT
R0=0xA0,_DOUT
R0=0x00,_DOUT
R0=R8,_DOUT
P=10000

R8=0xFF
R8[0]=R14[0]	;R14=BODEN
R8[1]=R15[0]	;R15=BODLEVEL
R8[2]=R16[0]	;R16=EESAVE
R8[3]=R17[0]	;R17=SPIEN
R8[4]=R18[0]	;R18=Rstdisbl

R0=0xAC,_DOUT   ;Write High Fuse
R0=0xA8,_DOUT
R0=0x00,_DOUT
R0=R8,_DOUT
P=10000

RESET=1,P=20
RESET=0,P=20000


["Read Lock"]
R0=0x58,_DOUT ;Write lock bits
R0=0x00,_DOUT
R0=0x00,_DOUT
_DIN
R9=R0
GET=("Lock",R9)

["Write Lock"]
GET=("Write Lock",R9)
R0=0xAC,_DOUT ;Write lock bits
R0=0xE0,_DOUT
R0=0x00,_DOUT
R0=R9,R0=|0xC0,_DOUT

P=10000
RESET=1,P=20,
RESET=0,P=20000

["Read Calibration"]
_DOUT(0x38)
_DOUT(0x00)
_DOUT(0x00)
_DIN
R19=R0,R19=<<8

_DOUT(0x38)
_DOUT(0x00)
_DOUT(0x01)
_DIN
R19=|R0,R19=<<8

_DOUT(0x38)
_DOUT(0x00)
_DOUT(0x01)
_DIN
R19=|R0,R19=<<8

_DOUT(0x38)
_DOUT(0x00)
_DOUT(0x01)
_DIN
R19=|R0

GET=("Read Calibration",R19)



[READID]
R0=0x30,_DOUT
R0=0x00,_DOUT
R0=0x00,_DOUT
_DIN
R2=R0,R2=<<8

R0=0x30,_DOUT
R0=0x00,_DOUT
R0=0x01,_DOUT
_DIN
R2=|R0,R2=<<8

R0=0x30,_DOUT
R0=0x00,_DOUT
R0=0x02,_DOUT
_DIN
R2=|R0

;R2?0x1E9109{PRINT=("ID: %06lX - ATTiny26",R2),EXIT}
PRINT=S("ID: %06lX",R2)
R0=R2


[Erase]
R0=0xAC,_DOUT ;Chip Erase
R0=0x80,_DOUT
R0=0x00,_DOUT
R0=0x00,_DOUT

P=50000
RESET=1,P=20,
RESET=0,P=20000



[END]
RESET=1
;EO=1,P=10 ;BB off