89LPC935 - Boot Loader activation by software.

Author : R Raghunathan / 12 July 2009

Thanks to the original idea by Erik Malund and initial assembly implementation by Lex Timmerman.

The FlashMagic is a very useful tool to program your code into the flash memory of the LPC935 chip.

However for this, the on board Boot Loader of the LPC935 chip has to be activated, so that the communication between the chip and FlashMagic is established via the Com port.

The Boot Loader can be activated by holding the RST pin low for a prescribed time after power up and then applying three properly timed low going pulses to it. The User manual of the chip has the details of this process.

The other and easy method is to write a "1" to the Boot Status bit, so that when the chip is reset after that, it will automatically vector to the Boot Loader. The steps are outlined below :

- Since the Boot Status bit cannot be directly written to we need to use the IAP function call. 
- Prior to the actual write of the Boot Status bit, we need to ensure that the internal flash Write Enable flag is set. This WE is forced set internally if the AWE bit ( BOOTSTAT.7) is a logic "0". Else we can force the WE by writing  commands to FMCON and FMDATA.
- Also we need to send a key value of #96H to allow the IAP function call.
- We then load the required registers as required by the IAP function call and finally call it.
- It is essential that during the IAP function call to change the Boot Status byte, no interrupt happens.
- After the Status Bit is set to "1", do a software reset and you are ready to link with FlashMagic.

All of the above steps are bundled together in the following C function written for Keil compiler. How the process is invoked is a matter of choice. I have done it when the Port P2.0 goes low. And placed this function outside of the main while loop. When I want to link to FlashMagic and download my new code,

- Holding the ISP_PB pressed, I reset the chip either by a power cycling or by the Reset pin.
- The setStatusBit() executes and the chip is placed in ISP mode.
- Chip can now communicate via the Com port to Flash Magic.
- After Flash Magic downloads my new code, I perform a reset.
- My new code starts executing !

=======================================

void setStatusBit (void)
{

 if ( ISP_PB == 0)                       // ISP_PB is a push button between P2.0 and VSS
    {
	   FMCON  = 0x08;
	   FMDATA = 0x96;                // Set the internal Write Enable flag ( in Case AWE=1)

	  #pragma asm 
	   PGM_MTP EQU 0FF03H            // Common entry address for IAP routines
	   push IEN0
	   clr EA                        // Stop interrupts
	   mov R0, #0FFH
	   mov @R0, #96H                 // Key to permit Flash write
	   mov A,  #02H                              
	   mov R5, #01H
	   mov R7, #03H                  // Load registers to write 01 to Boot Status byte
	   lcall PGM_MTP
	   pop IEN0                      // Chip should now enter ISP for the next power on reset                
	   #pragma endasm
       
	   AUXR1 = 0x08;                 // Do a software reset
    }
}
=======================================