Monday, February 11, 2008

Erik's NoTouch method ( P89C51RD2 )

“Touchless” ISP/IAP of the Philips 89C51Rx2 and 89C66x

Programming the Philips 89C51Rx2 without access to the circuit board using FlashMagic.

Many 8051 derivatives from several manufacturers have the ability to enter “boot mode” by applying certain voltage levels to certain pins, usually during reset. A study found that for our purpose (“touchless” ISP/IAP) the only solution would be the Philips 89C51Rx2 or the Philips 89C66x. Since we occasionally experience very high levels of EMI a secondary reason to chose the Philips 89C51RD2 was that the built-in bootloader is in ROM and thus can not be destroyed by EMI which may scramble flash memory. While the “touchless” ISP/IAP will not function in case of a scrambled flash the Philips 89C51Rx2 can still be programmed in the circuit. While it is a fact that an EMI caused scramble of a specific flash location ( boot vector ) would disable this the statistical likelihood of being unable to perform ISP is greatly reduced. I do not understand why Philips when they did the right thing by using ROM rather than flash for the bootloader did not hard code the boot vector to avoid this problem.

The built in ROM code for Flash programming in the Philips 89C51Rx2 microcontrollers allow several means of Programming, AN-461 describes them in some detail. AN-461 may leave you with the impression that you need switches or latches on the board to accomplish this, that, however, is not necessarily the case. This document assumes you are familiar with the concepts described in AN-461 and the operation of FlashMagic. Figure 2 and figure 3 in AN-461 show the basis for this process. An absolute condition for achieving “touchless” ISP/IAP is that you do not use external code memory which, in most cases, should not be necessary since the Philips 89C51RD2 gives you 64k of internal code space. Also, if you employ bank switching, FlashMagic will not serve your programming needs. To achieve “touchless” ISP/IAP it is necessary to tie !EA/Vpp directly to the 5V supply, thus no external code memory can be used. It is also a condition that nothing pulls P2.6 and P2.7 down after reset is applied when the application code is not running. I suggest tying these 2 pins to Vcc through each a 10k resistor as an extra safety. A RS-232 port is required as well, but that will be the case whether the ISP/IAP is “touchless” or not. The process is simpler if the application uses the watchdog, if not, an additional power off, power on step will be required.

To initiate “touchless” ISP/IAP some action, such as a signal on the serial port, a special keyboard sequence or a command from the PC mentioned below, must cause the application code to call the GoToBoot assembly code shown below. During program development it may be advisable to also activate this if a specific port pin is held low.

It is suggested that the minimal code shown below is written as an entire program with e.g. p1.0 low as the trigger. This can then be loaded using a few clip leads using “standard logic level based ISP”. Once this is accomplished program development can progress using NoTouch ISP simply adding the desired code to the above.
Application code in “C”, equivalent in assembler will work as well:

if (request_for_program_replacement_detected)

{

GoToBoot();

//above does not return, a reset will follow

}

Assembly code:

Note that the r0 value (20 shown here) must be set per AN-461 pg 9 “The oscillator frequency is an integer rounded down to the nearest megahertz”. The remaining code and values are constant for this application.

AUXR1 DATA 0A2h ;

WDTRST DATA 0A6h ;

PUBLIC GoToBoot

BOOTIT SEGMENT CODE

RSEG BOOTIT

GoToBoot:

mov AUXR1,#020h ;enable boot

mov r0,#20 ;oscillator freq = 20Mhz

mov r1,#6h ;program status byte

mov dptr,#0 ;specify status byte

mov a,#0ffh ;nonzero required

call 0fff0h

jmp $ ;wait for watchdog to reset

end


The process is:

1a) If the chosen method for entering “touchless” ISP/IAP is a command from a PC, connect a PC to the serial port and issue the command.

1b) Else use the chosen means to make the application code execute the above routine, then connect a PC to the serial port.

2) Use FlashMagic or some such program to program the microcontroller.

3) Power off the microcontroller.

4) Power on the microcontroller and the updated application will start.

Simple, isn’t it.

This method may, of course, be used even if the program is not updated through a PC running a program such as FlashMagic, the data stream may be issued by any means as long as it conforms to the record format specifications in the data sheet.

Just a note: With the latest version of FlashMagic and P89C51Rx2 rev G or later FlashMagic in combination with the boot ROM has the ability to use the watchdog to reset the microcontroller after the load is complete steps 3) and 4) can be omitted.

No comments: