;
; bakdoor added to Joe's no touch
; Purpose: 
; Restore the boot vector and boot stat value with a clip in TXD pin
; Based in Erik Malund's documents called backdoor and Joseph Goldburg documents called isp.a51. Please
; read these documents first
;
; Description:
; If you have any trouble with ISP and you loose the boot vector, you must to shut down your system and
; put a clip from TXD pin to GND. Then power up your system, remove the clip, and your controller will be
; in ISP state. This controller will be in "fesh state" I mean, when you power it up it will be in ISP mode.
; 
; 
; Roberto Muņoz 21/05/05
;
;
;
;
;
;
;   *****  LPC900 NoTouch  - ISP Bootloader  *****
;
; Executive Summary: How to activate the ISP Bootloader from within your appication code.
;                    without specific tricky hardware.
;
; Written by Joseph Goldburg    5th Feb 2007
;
; Hardware:  Keil  MCB900 Board
;
; Purpose:
; To demonstrate how to activate the P89LPC932A1   ISP Bootloader Based on ErikM ealier work
;
; Concept:  LPC900  ISP Bootloader Entry:
; ISP hardware entry via 3 pulses jumps to 1F00h  (for LPC932A1 and LPC935).
; SO... why not directly call 1F00h (as per the example below)
;
; Note:  Other LPC900 Micro's may have a different ISP bootloader address
;
; Application Program:
; Turn on ALL LEDs on Port 2
; Wait for swich  P0_1 to go high
; Turn all LEDs off
; Turn on  one LED Port2_1 to indicate jumping to boot loader
; Jump to bootloader...
;
; Additonal Notes for using in your own program
; *************************************************
; a) Once this program has been loaded in to the LPC932A1 successfully - the hardware entry method 
; can be turned off (under Advanced Options --> Hardware Config --> Don't use DTR for RST).
;
;  Before Jumping to the bootloader
; -----------------------------------
; b) Turn off any watchdogs that may reset the LPC932A1 during flash programming
;     (or have an "extended" watchdog delay which is longer than the flash loading process)
; 
; c) Reset all registers back to their power-on defaults, just incase the bootloader is expecting
;    registers to be at factory default values.

	org 0000
	
P2M1	equ	  0a4h	    ; Port2 cong registers
P2M2	equ	  0a5h	    ;   "     "     "

Port2	equ	   0a0h    ;  8 LEDs on port2
Port2_1	equ	   0a1h    ;  Just 1 on port2 (bit 1)
Port2_5 equ        0a5h    ;  another LED

Port0_1	equ	   81h     ; Boot loader test swtich bit Port 0  bit 1

boot	equ	  01f00h	  ; Boot loader jump address for 8k P89LPC932A1
; boot	equ	  xxxxx   	  ; Boot loader jump address for for othe LPC900 devices	
;

PGM_MTP equ 0FF03H			; IAP ENTRY POINT




ASK_BACK_DOOR:	
	JB TXD,BEGIN			; ASK FOR THE CLIP
BACK_DOOR_IN:
	JNB TXD,BACK_DOOR_IN		; WAIT CLIP OUT
BACK_DOOR:
	MOV R0,#0FFH			; AUTHORIZATION KEY 
	MOV @R0,#96H			; 
	MOV A,#02H			; IAP MSC WRITE
	MOV R5,#HIGH(boot)		; BOOTVEC VALUE
	MOV R7,#02H			; IAP BOOTVEC ADDR
	CALL PGM_MTP			; IAP CALL
	MOV R0,#0FFH			; AUTHORIZATION KEY 
	MOV @R0,#96H			; 
	MOV A,#02H			; IAP MSC WRITE
	MOV R5,#01H			; BOOT STAT VALUE
	MOV R7,#03H			; IAP BOOT STAT ADDR
	CALL PGM_MTP			; IAP CALL
	MOV AUXR1,#08H			; SRST = 1 SOFTWARE RESET




; ----------------  User  Code here ---------------	
BEGIN:

	mov     P2M1,#00     ; 
	mov	P2M2,#0ffh   ;  Set Port 2 to output push_pull
	
	mov	Port2,#0ffh   ; Set Port 2 (turn all 8 LEDs on)

; --- User code needs to test swicth on P0.1
loop: 	
	jnb    Port0_1,loop    ;  loop if P0.1 is low
; 
; -----------------------------------------------------

; Port_0_1 is high  - switch is high  ----
; Handoff to bootloader starts here
	mov    Port2,#00h    ; 
	setb   Port2_1       ;  trun 1 LED on to indicate we are jumping to the boot loader

	jmp 	boot	     ; jump tp boot loader

; shouldn't get here
        setb    Port2_5      ; signal LED we have dropped through the code.
	jmp $

end
