Order this document MOTOROLA as ANAS4/D ea SEMICONDUCTOR =o APPLICATION NOTE AN434 Serial bootstrap for the RAM and EEPROM1 of the MC68HCO5B6 By Jeff Wright, Motorola Ltd., East Kilbride INTRODUCTION The MC68HCO5B6 has 256 bytes of onchip EEPROM, called EEPROM'1, which can be used to store variable data in a non-volatile manner. In many applications this EEPROM1 will be used to hold a look-up table or system set up variables. In these cases it is usually a requirement that the EEPROM1 be initialised during the manufacture of the application. In addition, loading small programs into RAM and executing them is an easy way of trying out new software routines. This application note describes one method for serially loading (bootstrapping) the EEPROM1 via a program executing in the RAM of the MC68HCO5B6. BUILT IN BOOTSTRAP The MC68HCO5B6 has a built in RAM serial bootstrap program contained in the mask ROM of the device that uses the SCI. It would therefore seem a simple task to load programs into RAM; however, as ROM space on the device is obviously critical, a very simple protocol has been implemented. This means that the boot- loader on the B6 does notaccept S-records which are the normal output from an assembler: instead, the protocol expects pure binary data preceded by a count byte that holds the size of the program to be down- loaded. No address information is contained in the download; instead, the bootloader always starts the program load at address $50 in RAM. The first byte (the count byte} is stored here and then as the subse- quent bytes are received via the SCI they are stored at incrementing RAM locations and the count byte is MOTOROLA LTD., 1990 decremented for each byte received. When the count byte reaches zero the bootstrap program jumps to address $51 and starts to execute the program that has just been loaded. No built in bootstrap routine is provided for the EEPROM 1 array. These restrictions present two problems: i) Howto convert assembler output to the format ac- cepted by the 68HCO5B6 RAM bootstrap routine? i) How to bootstrap the EEPROM1 of the 68HCO5B6? This application note provides a solution for each of these problems. (MA) MOTOROLA WE AN434/D1) CONVERTING S-RECORDS FOR RAM BOOTSTRAP To use the built in RAM bootstrap program on the MC68HCO5B6 the device must be configured as shown in Figure 1. If these conditions are met when the reset pin is released, then the serial bootstrap program de- scribed above will start to execute anda program can be downloaded via a 9600 baud RS-232 source. Personal computers usually have one or more RS-232 ports referred to as COM ports. To overcome the format difference between S-records and that accepted by the bootloader, a conversion program is required. There is also an additional problem when using a PC - whenafile is copied to a COM port to transfer it, it is the ascii characters that are transmitted, not the binary data. This means for example that if a file containing the typed data byte $A5 was copied via the COM port to the B6, the B& would in fact receive two bytes: $41 and $35, which represent the ascii characters A and 5 respectively. This means that the conversion program has to strip out the S-record format and convert the resultant data to binary format for transfer to the HCO5B6. It must also insert the count byte at the beginning of the output file. The pascal program BINCONV performs these three tasks; a listing of the source code is given at the end of this application note. A flow diagram of BINCONV can be seen in Figure 2. The inclusion of the count byte has been left as an option to increase the flexibility of the program, but it could easily be standardised to include the count byte for the B6 RAM bootloader. When BINCONV is invoked it prompts for the name of the S- record input file and the name required for the binary output file. After this each S-record in the input file is read and converted to binary data and stored in a temporary file. As each S-record is read it is echoed to the screen; when they have all been processed a message prompts the user and asks if a count byte is required. When used with the 68HCO5B6 RAM boot loader the answer will always be yes, in which case the count value is written to the output file before the rest of the data is copied from the temporary file to the output file. Finally the value of the count byte is dis- played for user confirmation remember that the count byte is equal to the number of bytes in the program being converted plus 1 for the count byte itself. The program will only accept standard S-record format and will trap and abort if any non-valid character or format is detected. With the PC COM port set for 9600 baud and the 68HCOS5B6 configured as in Figure 1 the binary file can be transferred and executed as follows: i) Release Reset on the HCO5B6 ii) Enterthe command COPY XXXX.YYY COM1\B on the PC. The program will then be transferred to the B6 and execution started automatically. Note that the \B option is used to denote a binary file transfer so that the copy procedure does not abort if it finds an end of file (EOF) character in the middle of the file. MOTOROLA AN434/D 2+9V +5V 10KQ 10KQ RESET o~o_4 19 10 iRQ 0.01pF ved pie 18 ~ | RESET 15 ~ 16 10Ka 10MQ 17 ose! TCAP1 22 ++ ___}- osc2 11 19k p [ , PD3 ii _l CRI | Le ppa (2 2apF | 4 MHz 22pF | ToKa ~ ; ~ 52 | TDO RS 232 level | psoa0 Rp {80 MC14E407 os cimaar }- 9800 baud 20 PLMA or similar 21) pimB 1 TCMP2 souk [S1- 2} TOMP1 33 PDO/ANOL!4__ | TCAPe = PD1/AN1L 13 391 ppo Po2 L12__ 38} pay 2 pps |S _37| pR2 Q PD6 }+ 361 pag wo pp7 3 35| Pea $ vALLZ 34] PBS > {8 _ 34 VRH Connect / 33] pBE connect i as ire! as desired \ 32 PB? VPP1 {40 nc [S. 31] pao pc7 -42- 30} Pat pce }-43- 291 pag PCS 44 _28| pas pos | 45_ 27} PA4 pc [46 26] pas Pc2, 47. 251 PAG Pct {48_ 241 paz Pco | 49_ VSS Figure 1. RAM bootstrap schematic AN434/D MOTOROLA 32) BOOTSTRAPPING THE EEPROM1 To bootstrap the EEPROM1 on the MC68HCOS5B6 in the absence of a built in loader program, use must be made of the RAM bootloader described above. The idea is that an EEPROM1 loader can be written to the users exact requirements then assembled and down- loaded into the RAM of the HCO5B6 where it will execute and in turn download data and program it into the EEPROM1. The 6K EEPROM emulation part, the MC68HC805B6, does havea builtin EEPROM bootloader in place of the RAM bootloader and there is an accompanying PC program available from Motorola called E2B6 that downloads S-records to the device for programming. The following is an explanation of an example EEPROM1 bootstrap program for the B6 that has been written to be compatible with the 805B6 PC program E2B6 thus eliminating the need to develop another PC program. Alisting of this program (EE1BOOT) is given at the end of this application note. The MC68HCO5B6 has 176 bytes of RAM that can be used for the EEPROM1 bootstrap program, so the protocol must be kept simple and the code written efficiently. The format of the E2B6 program is a transfer of 2 address bytes followed by the data byte that is to be programmed at that location. At the same time the B6 returns the data from the previously programmed location for verifica- tion by E2B6. The program EE1BOOT has 4 main sections: a main loop, an erase routine, a program routine andan SCI service routine. The core of both the erase and program subroutines is the extended ad- dressing subroutine EXTSUB which is used to access the EEPROM 1 array. This subroutine is builtin RAM by the main loop as the address information for the next byte to be programmed is received from the SCI. E2B6 always sends a null character during initialisation which could throw the EE1BOOT program out of synchronisation, as it is already executing before E2B6 is invoked. For this reason EE1 BOOT ignores the first character received and treats the second as the first address byte. The EXTSUB routine is first called as an LDA $aaaa to retrieve the last byte programmed for verification. Then the address in the routine is modified as the next address to be programmedis received. When the data byte is received the opcode of EXTSUB is incremented so that it becomes STA bbbb before the erase and program routines are called. After programming the opcode is decremented back to LDA before the main loop is repeated. Note that the EEPROM1 location is always erased before programming. The timer output compare func- tion is used to provide a 10ms delay for erasing and programming and the programming step is skipped to save time if the data presented to that location is $FF. The sequence of events to bootstrap the EEPROM1 of the 68HCO5B6 is therefore as follows: 1) Configure the 68HCO5B6 as in Figure 1. 2) Assembie the program EE1BOOT and convertit to binary using BINCONV as described in section 1. 3) Set up PC COM port to 9600 baud then release Reset on the HCO5B6. 4) Use the command COPY EE1BOOT.BIN COM1/ B to download EE1BOOT into the RAM of the HCO5B6. EE1BOOT will now start to execute. 5) Start the program E2B6 on the PC and follow the instructions to download the desired S-records to the EEPROM1 of the G8HC05B6. Note: i) Only the download procedure of E2B6 will work in conjunction with EE1BOOT. ii) Once the EEPROM 1 security bit has been set, the RAM bootloader on the 68HCO5B6 will no longer op- erate. This means that after the device has been reset it will be impossible to download any more data into the EEPROM1 until selfcheck has been executed - selfcheck performs an erase of the entire EEPROM1 array. This means that if the EEPROM1 is to be pro- grammed in several steps, the one that will set the security bit should be done last. FURTHER POSSIBILITIES This application note has shown a method for initialis- ing the EEPROM1 on the 68HCO5B6 by using the RAM bootloader. It would of course be much simpler to incorporate a EEPROM? bootloader in the ROM space of the user program, but often there is not enough space. !f enough space is available (117 bytes), then EE1BOOT could be incorporated in the application software, thus saving steps 2, 3 &4 in the procedure above. MOTOROLA AN434/D 4PROCEDURE BINWRITE ADJUST LENGTH FOR ADDRESS & CHECKSUM N MAIN PROGRAM Y Y GET NEXT (BEGIN) 2 CHARS INPUT FILE ss LENGTH = LENGTH-2 NAMES L PROCEDURE CALC-HEX OPEN FILES & INITIALISE: (CONVERT THESE 2 TO HEX QUIT=FALSE, COUNTBYT=1 I WRITE HEX TO TEMPF Y READ NEXT S-REC & ECHO TO SCREEN QUIT = TRUE WRITE ERROR MESSAGET? NS WRITE BEGIN \ OR END MESSAGE ? QUIT = TRUE . WRITE ERROR MESSAGE READ 2 RECORD LENGTH CHARS & CONVERT TO HEX PROCEDURE BINWRITE' - CONVERT REST OF S-REC L L TO BINARY & STORE IN TEMPF WRITE COUNTBYTE TO ) OUTPUT FILE & ECHO N74 TO SCREEN COPY BINARY DATA FROM TEMPF TO OUTPUT FILE ERASE TEMPF & CLOSE INPUT & OUTPUT FILES Figure 2. Flow diagram of BINCONV AN434/D MOTOROLA 5ooo TIO TOR TORT RR RIT OTC RRO RIO TOI SOTTO OI ROI RI ROTOR OTTO TR TTT IO TTT ARTE 0002 *TELETTELELEETEEE EES ESTEELEELELEEELELETEEESSELETEEESELELEEESELEESEEEEE EEE TESS 0003 *% %* 0004 *% EE1BOOT 68HCOSB6 EEPROM] Serial bootloader %* 0005 *% %* 0006 *% - This prog. is loaded into the RAM of the HCO5B6 via the RAM %* 0007 *% bootloader. The program will then start to execute. The format %* 0008 *% has been selected to be the same as that on the 805B6 se that $* 0003 *& the program E2B6 can be used to program the EEPROM1. &* 0010 *% Note: E2B6 sends a null character during initialisaton so this prog %* 0011 *% ignores the first character received on the SCI. %* 0012 *% $* 0013 *% $* 0014 *% Jeff Wright Last Updated 10/5/90 +* 0015 *% $* 0016 XEELETELEEETLEGEFEBEVEFEELEETELETEEEELTELELEEEEEEGEEEEBEVELEETETEETEERGEEEEESE 0017 FGI IIIS IG IOI I IDIIG GI IUIOCIUICIOITIOIGIOIOIIIGIIIOOIOIG III IO IO IIR it 0018 0019 0020 wuaxwnawewe T/O and INTERNAL registers definition *****xeeexrennke ener 0021 * 0022 * 0023 * I/O registers 0024 * 0025 0000 PORTA EQU $00 port A. 0026 0001 PORTB EQU $01 port B. 0027 0002 PORTC EQU $02 port C. 0028 0003 PORTD EQU $03 port D. 0029 0004 DDRA EQU $04 port A DDR. 0030 0005 DDRB EQU $05 port B DDR. 0031 C006 DDRC EQU $06 port C DDR. 0032 0033 0007 EECONT EQU $0? 0034 0002 ELERA EQu 2 0035 0001 ELLAT EQuU 1 0036 0000 E1PGM EQU 0 0037 0038 000d BAUD EQU $OD 0039 000e SCCR1 EQU SOE 0040 0004 MBIT EQU 4 0041 000 SCCR2 EQU SOF 0042 0010 SCSR EQU $10 0043 0005 RDRF EQU 5 0044 0011 SCDAT EQu $11 0045 * 0046 * TIMER registers 0047 * 0048 0012 TCR EQU $12 Timer control register. 0049 0005 TOIE EQU 5 Timer overflow interrupt enable. 0050 0006 OCIE EQU 6 Timer output compares interrupt enable. 0051 0007 ICIE EQU 7 Timer input captures interrupt enable. 0052 MOTOROLA 6 AN434/D0053 0013 TSR EQU $13 Timer status register. 0054 0003 OCF2 EQU 3 Timer output compare 2 flag. 0055 0004 ICF2 EQU 4 Timer input capture 2 flag. 6056 0005 TOF EQU 5 Timer overflow flag. 0057 0006 OCF. EQu 6 Timer output compare 1 flag. 0058 0007 ICF1l EQU 7 Timer input capture 1 flag. 0059 o060 0016 TOCIHI EQU $16 Timer output compare register 1 (16-bit). 0062 0017 TOCILO EQU $17 0062 0018 TIMHI EQU $18 Timer free running counter (16-bit). 0063 0019 TIMLO EQU $19 0064 0065 xuexe* MISC DEFINITIONS 0066 0067 00c6 LDAEXT EQU $C OP-Code for LDA extended. 0068 0014 MS10 EQU $14 10mS delay constant. 0069 0070 * 0072 0072 HK RK ERK EERE EERE KER KERR KK KREREKEEKREKKEREARKEN EE KEK 0073 * * 0074 * START OF CODE * 0075 * * 0076 FR RII IIIT IORI ICICI IOI IOR ITT TOIT IOI TOSI KIT TIS TI RR RIK 0077 0078 0051 ORG $51 0079 0080 0051 a6 00 RESET LDA #$00 0081 0053 b? 04 STA DDRA All Ports inputs. 0082 0055 b? 05 STA DDRB 0083 0057 b? 06 : STA DDRC 0084 0085 0059 19 Oe SCIINT BCLR MBIT, SCCR1 Initialise SCI - 8 data bits. 0086 005b a6 cO LDA #SCc0 0087 005d b7 Od STA BAUD 9600 baud at 4MHz. 0088 O0Sf a Oc LDA #S0C Enable transmit and receive. 00893 0061 b? Of STA SCCR2 0090 0063 b?7 10 STA SCSR Clear pending flags. 0091 0065 a6 c6 LDA #LDAEXT Init extended addressing subroutine to LDA. 0092 0067 c7 00 Bf STA OPCDE 0093 006a ad id BSR SCREAD Wait here and ignore lst char (E2B6 init). 0094 0095 006c ad 21 LOOP BSR EXTSUB Load Acc with data from last programmed addr 0096 006e b? 11 STA SCDAT Send it back for host to verify. 0097 0070 ad 17 BSR SCREAD Get high address 0098 0072 c7 00 90 STA ADDHI - and store it. 6099 0075 ad 12 BSR SCREAD Get low address 0100 0077 c7 00 31 STA ADDLO - and store it. 0101 007a ad Od BSR SCREAD Get the data to be programmed 0102 007c c7 00 93 STA DATA Store it temporarily. 0103 O07f 3c Bf INC OPCDE Change the ext addr subroutine to STA aaaa. 0104 0081 ad 11 BSR ERASEE Erase the selected address for 10ms. 0105 0083 ad 27 BSR PROGEE Now prog the data for 10mS. 0106 0085 3a 8f DEC OPCDE Restote ext addr subroutine to LDA aaaa. 0107 0087 20 e3 BRA LOOP 0108 AN434/D MOTOROLA 70109 KRkRAKAAKK RRR RE RERR ER ERE SUBROUTINE TO SERVICE SCI 888 8 RRR AERER REAR RENEE EEK 0110 0111 0089 Ob 10 fd SCREAD BRCLR RORF,SCSR, * 0112 008c b6 11 LDA SCDAT 0113 008e 81 RTS 0114 0115 0116 aakaneata eee EXTENDED ADDRESSING SUBROUTINE TO ACCESS FULL MEMORY MAP ***** eek Re ee 0117 0118 oo8sf EXTSUB EOU * 0119 oO8f 00 OPCDE FCB 0 0120 00390 00 ADDHI FCB 0 0121 0091 00 ADDLO FCB 0 0122 0092 81 RTS 0123 0124 0093 00 DATA FCB 6 Reserved Byte for data during erasing. 0125 0126 RETAKE RAEN AEKERERE EE EEl ERASING SUBROUTINE WERKE HARKER AR AKER KEKE 0127 0128 0094 12 07 ERASEE BSET E1LAT, EECONT 0129 0096 14 O07 BSET ELERA, EECONT 0130 0098 ad 5 BSR EXTSUB 0131 009a a6 14 LDA #MS10 0132 009c 10 07 DELI BSET E1PGM, EECONT 0133 009e b7 19 STA TIMLO Set up timer for a 10ms count 0134 00a0 b?7 16 STA TOC1HI 0135 00a2 b7 13 STA TSR - using output compare 1 function. 0136 00a4 b? 17 STA TOC1LO 0137 00a6 Od 13 fd BRCLR OCF1,TSR,* Wait here for end of erase time 0138 00a9 3 07 CLR EECONT - erase finsished. 0139 OCab 81 RTS 0140 0141 EKER KERR EK EEL PROGRAMMING SUBROUTINE WEEK KEE KEKE EE HKHKEREKEAKAKKRE 0142 0143 O0ac 12 07 PROGEE BSET E1LLAT, EECONT 0144 O0ae b 93 LDA DATA 0145 00b0 ad dd BSR EXTSUB 0146 00b2 4c INCA 0147 OOb3 27 Of BEQ SKIP Skip programming if data = SFF 0148 OObS a6 14 LDA #MS10 0149 00b7 10 07 DEL BSET E1PGM, EECONT 0150 90b9 b7? 19 STA TIMLO Set-up timer for 10mS count 0151 00bb b7 16 STA TOCIHI 0152 00bd b7? 13 STA TSR - using output compare 1 function. 0153 O0bf b7 17 STA TOCLLO 0154 00cl Od 13 fd BRCLR OCF1,TSR, * Wait here for programming to finish. 0155 00c4 3f 07 SKIP CLR EECONT 0156 00c6 81 RTS MOTOROLA AN434/D 8[RH KA KR HH KKK RK KEK RIK RIK RIK KR EK KE KKK RK KEK RHE RIK KHIR KE KKK EKER KEKE KKK EK Y program BINCONV; { Programto convert Motorola S-record files to binary format. Optional inclusion of a count byte for HCOSB6 RAM bootloader etc} { Programmer - Jeff Wright, MCU applications Motorola East Kilbride} { Last Updated 10/5/90} {BRR RRR RIK RR RIK KK KK KE KKK KKK RRR RRR KK IK EK KKK IK KIRKE KIRK KEK KEKE KHER AKKKK KE } var SrecFile : text; BinFile : file; Tempf : file; srec : string(100]; Transfer : array[1..20000J of char; numread, numwritten : word; answer : char; fnamei : string[15]; fnameo : string[15]; bytout : char; countbyt : integer; datent : integer; datval : integer; point : integer; entl : integer; ecnt2 : integer; quit : boolean; Count : boolean; { } Procedure Calc_hex(chrl,chr2 : integer); {Combines 2 characters into a single byte value i.e A5->165, error signaled if non hex character detected} Begin Case chrl of 48..57 : chrl := chrl - 48; 65..70 : chrl :=chrl - 55; { Is this a valid hex character?} else begin writeln (invalid data ~- conversion aborted); quit := true end end; Case chr2 of 48..57 : chr2 := chr2 - 48; 65..70 : chr2 := chr2 - 55; else begin writeln (invalid data - conversion aborted); quit := true end AN434/D MOTOROLA 9end; datval := chrl1*16 + chr2; {Convert to single byte} end; Procedure Binwrite (length, dpoint : integer); {Converts an S-record line to hex and stores it in a temporary file} begin length := length-3; {Allow for address and checksum bytes} countbyt := countbytt+length; {Update running byte total} length := length*2; {Twice as many characters as bytes} while length > 0 do begin entl := Ord(srec[dpoint]); {Get the next two characters} ent2 := Ord(srec[dpoint+1]); dpoint := dpoint+2; {Update pointer and length} length := length-2; Calc_hex(cnt1,cnt2); {Convert two characters into single byte} bytout := Chr (datval); {- now convert that single byte into a } blockwrite (tempf,bytout,1) {character and save it in temporary file} end end; {[*e RAR AKA R RE MATN PROGRAM STARTS BELOW #4 #4 8K KARA REAAEKERKKKA EE} begin writeln (S-record to Binary conversion utility); writeln; writeln; write(Input S-record file name? > ); readin (fnamei) ; assign (SrecFile, fnamei); write(* Binary output file name? > ); readin (fnameo) ; assign (BinFile, fnameo); assign(tempf, temp.tmp); quit := false; countbyt := 1; Reset (SrecFile) ; {open the two } Rewrite (BinFile,1); { -selected files} Rewrite (tempf,1); { + a temporary file} MOTOROLA AN434/D 10while not Eof(SrecFile) and not quit do begin readln(SrecFile, srec); {read S-rec into char string srec} writeln (srec) ; If srec[1]=Sthen {If string does not start with S then quit} begin CASE srec[2] of 1: {If not Sl record then loop back} begin entl := Ord(srec[3])}; {get the 2 record length} ent2 := Ord(srec[4]); {characters} calc _ hex(cnt1,cnt2); {func to produce hex in datent from cntl & 2} datent := datval; point := 9; {point to first data character} binwrite (datcnt, point) { convert the data in this s-rec line to binary and store in temp file} end; 0 : writeln (Conversion started); 9 : writeln (last S-record done); else , begin {If not S0,SlorS9 record then abort} quit := true; writeln (Non standard S-record detected - Conversion aborted) end end end else begin {If lst char not an S then abort} quit := true; writeln (Non standard S-record detected - Conversion aborted } end end; If quit = false then {If no errors then copy the temporary file to the output file and add in a count byte if required} begin Reset (tempf,1); writeln; write (Do you want a count byte added to start of output file? -> ); readin (answer); If upcase (answer) = Y then Begin writeln (Total size including count byte = ,countbyt); bytout := chr(countbyt); blockwrite (binfile,bytout,1) end; repeat blockread (tempf,transfer, sizeof (transfer) ,numread); blockwrite (binfile,transfer,numread,numwritten) ; until (numread=0) or (numwritten <> numread} end; close (tempf) ; erase (tempf) ; {Finished with temporary file so erase it} close (SrecFile); close (BinFile) {Close files before quiting} end. AN434/D MOTOROLA 11Motorola reserves the right to make changes without further notice to any products herein to improve reliability, function or design. Motorola does not assume any liability arising out of the application or use of any product or circuit described herein; neither does it convey any license under its patent rights nor the rights of others. Motorola products are not designed, intended, or authorized for use as components in systems intended for surgical implant into the body, or other applications intended to support or sustain fife, or for any other application in which the failure of the Motorola product could create a situation where personal injury or death may occur. Should Buyer purchase or use Motorola products for any such unintended or unauthorized application, Buyer shall indemnify and hold Motorola and its officers, employees, subsidiaries, affiliates, and distributors harmless against all claims, costs, damages, and expenses, and reasonable attorney fees arising out of, directly or indirectly, any claim of personal injury or death associated with such unintended or unauthorized use, even if such claim alleges that Motorola was negligent regarding the design or manufacture of the part. Motorola and m are registered trademarks of Motorola, Inc. Motorola, Inc. is an Equal Opportunity/Affirmative Action Employer. Literature Distribution Centers: USA: Motorola Literature Distribution; P.O. Box 20912; Phoenix, Arizona 85036. EUROPE: Motorola Ltd.; European Literature Center; 88 Tanners Drive, Blakelands, Milton Keynes, MK14 5BP, England. JAPAN: Nippon Motorola Ltd.; 4-32-1, Nishi-Gotanda, Shinagawa-ku, Tokyo 141 Japan. ASIA-PACIFIC: Motorola Semiconductors H.K. Ltd.; Silicon Harbour Center, No. 2 Dai King Street, Tai Po Industrial Estate, Tai Po, N.T., Hong Kong. = (M) MOTOROLA AS30051 PRINTED IN USA 3/91 IMPERIAL LITHO 77977 5,000 ASIC YNAROQ AN434/D