;******************************************************************************
; This code illustrates how to do APROM and CONFIG IAP from LDROM.
; APROM are re-programmed by the code to output P1 as 55h and P0 as aah.
; The CONFIG2 is also updated to disable BOD reset.
; User needs to configure CONFIG0 = 0x7F, CONFIG1 = 0xFE, CONFIG2 = 0xFF.
;******************************************************************************
PAGE_ERASE_AP EQU 00100010b
BYTE_PROGRAM_AP EQU 00100001b
BYTE_READ_AP EQU 00000000b
ALL_ERASE_CONFIG EQU 11100010b
BYTE_PROGRAM_CONFIG EQU 11100001b
BYTE_READ_CONFIG EQU 11000000b
ORG 0000h
CLR EA ;disable all interrupts
CALL Enable_IAP
CALL Enable_AP_Update
CALL Erase_AP ;erase AP data
CALL Program_AP ;programming AP data
CALL Disable_AP_Update
CALL Program_AP_Verify ;verify Programmed AP data
CALL Read_CONFIG ;read back CONFIG2
CALL Enable_CONFIG_Update
CALL Erase_CONFIG ;erase CONFIG bytes
CALL Program_CONFIG ;programming CONFIG2 with new data
CALL Disable_CONFIG_Update
CALL Program_CONFIG_Verify ;verify Programmed CONFIG2
CALL Disable_IAP
MOV TA,#0Aah ;TA protection
MOV TA,#55h ;
ANL CHPCON,#11111101b ;BS = 0, reset to APROM
MOV TA,#0Aah
MOV TA,#55h
ORL CHPCON,#80h ;software reset and reboot from APROM
SJMP $
;********************************************************************
; IAP Subroutine
;********************************************************************
Enable_IAP:
MOV TA,#0Aah ;CHPCON is TA protected
MOV TA,#55h
ORL CHPCON,#00000001b ;IAPEN = 1, enable IAP mode
RET
Disable_IAP:
MOV TA,#0Aah
MOV TA,#55h
ANL CHPCON,#11111110b ;IAPEN = 0, disable IAP mode
RET
Enable_AP_Update:
MOV TA,#0Aah ;IAPUEN is TA protected
MOV TA,#55h
ORL IAPUEN,#00000001b ;APUEN = 1, enable APROM update
RET
Disable_AP_Update:
MOV TA,#0Aah
MOV TA,#55h
ANL IAPUEN,#11111110b ;APUEN = 0, disable APROM update
RET
Enable_CONFIG_Update:
MOV TA,#0Aah
MOV TA,#55h
ORL IAPUEN,#00000100b ;CFUEN = 1, enable CONFIG update
RET
Disable_CONFIG_Update:
MOV TA,#0Aah
MOV TA,#55h
ANL IAPUEN,#11111011b ;CFUEN = 0, disable CONFIG update
RET
Trigger_IAP:
MOV TA,#0Aah ;IAPTRG is TA protected
MOV TA,#55h
ORL IAPTRG,#00000001b ;write ‘1’ to IAPGO to trigger IAP process
RET
;********************************************************************
; IAP APROM Function
;********************************************************************
Erase_AP:
MOV IAPCN,#PAGE_ERASE_AP
MOV IAPFD,#0FFh
MOV R0,#00h
Erase_AP_Loop:
MOV IAPAH,R0
MOV IAPAL,#00h
CALL Trigger_IAP
MOV IAPAL,#80h
CALL Trigger_IAP
INC R0
CJNE R0,#44h,Erase_AP_Loop
RET
Program_AP:
MOV IAPCN,#BYTE_PROGRAM_AP
MOV IAPAH,#00h
MOV IAPAL,#00h
MOV DPTR,#AP_code
Program_AP_Loop:
CLR A
MOVC A,@A+DPTR
MOV IAPFD,A
CALL Trigger_IAP
INC DPTR
INC IAPAL
MOV A,IAPAL
CJNE A,#14,Program_AP_Loop
RET
Program_AP_Verify:
MOV IAPCN,#BYTE_READ_AP
MOV IAPAH,#00h
MOV IAPAL,#00h
MOV DPTR,#AP_code
Program_AP_Verify_Loop:
CALL Trigger_IAP
CLR A
MOVC A,@A+DPTR
MOV B,A
MOV A,IAPFD
CJNE A,B,Program_AP_Verify_Error
INC DPTR
INC IAPAL
MOV A,IAPAL
CJNE A,#14,Program_AP_Verify_Loop
RET
Program_AP_Verify_Error:
CALL Disable_IAP
MOV P0,#00h
SJMP $
;********************************************************************
; IAP CONFIG Function
;********************************************************************
Erase_CONFIG:
MOV IAPCN,#ALL_ERASE_CONFIG
MOV IAPAH,#00h
MOV IAPAL,#00h
MOV IAPFD,#0FFh
CALL Trigger_IAP
RET
Read_CONFIG:
MOV IAPCN,#BYTE_READ_CONFIG
MOV IAPAH,#00h
MOV IAPAL,#02h
CALL Trigger_IAP
MOV R7,IAPFD
RET
Program_CONFIG:
MOV IAPCN,#BYTE_PROGRAM_CONFIG
MOV IAPAH,#00h
MOV IAPAL,#02h
MOV A,R7
ANL A,#11111011b
MOV IAPFD,A ;disable BOD reset
MOV R6,A ;temp data
CALL Trigger_IAP
RET
Program_CONFIG_Verify:
MOV IAPCN,#BYTE_READ_CONFIG
MOV IAPAH,#00h
MOV IAPAL,#02h
CALL Trigger_IAP
MOV B,R6
MOV A,IAPFD
CJNE A,B,Program_CONFIG_Verify_Error
RET
Program_CONFIG_Verify_Error:
CALL Disable_IAP
MOV P0,#00h
SJMP $
;********************************************************************
; APROM code
;********************************************************************
AP_code:
DB 75h,0B1h, 00h ;OPCODEs of “MOV P0M1,#0”
DB 75h,0B3h, 00h ;OPCODEs of “MOV P1M1,#0”
DB 75h, 90h, 55h ;OPCODEs of “MOV P1,#55h”
DB 75h,080h,0Aah ;OPCODEs of “MOV P0,#0Aah”
DB 80h,0Feh ;OPCODEs of “SJMP $”
END