ANM095 In-System Programming - T89C51RD2 Bootloader Operating Modes 1. Introduction 1.1. Overview This application note describes the use of the embedded T89C51RD2 bootloader for in-system programming (ISP) in different application environments. The utility software to implement in-system programming with a PC or a SUN is available on the Atmel Wireless and Microcontrollers Web Site. 1.2. Definition of Terms API: Application Programming Interface BLJB: Boot Loader Jump Bit BLLB: Boot Loader Lock Bit BSB: Boot Status Byte SBV: Software Boot vector HSB: Hardware Security Byte ISP: In-system Programming Rev. A - 12-Jan-01 1 ANM095 2. Flash Memory Description 2.1. Overview The T89C51RD2 Flash memory is a 64-Kbyte program memory organized in 512 pages of 128 bytes. This memory is both parallel and serial in-system programmable. Parallel programming of the Flash is carried out using a programmer device, whereas serial programming is performed using an embedded bootloader program. The memory is organized in two main areas: * User Flash memory: 63 Kbytes for user program, organized in page of 128 bytes, * Boot Flash memory: 1 Kbytes for Bootloader program and application program interface (API). 2.2. Flash Memory Architecture The Flash memory is composed of two arrays. See Figure 1 XROW FFFFh FC00h (Bootloader program) Kbyte 16/321 Kbytes Flash 63 memory KBytes (customer program array) Flash Memory (User Program Array) 0000h Figure 1. Flash Memory Architecture 1. User Program Array The size of this block is 63 Kbytes organized in 504 pages of 128 bytes. 2. XROW Array The XROW is one extra page (128 bytes). This space contains data needed by the bootloader: * BSB: The boot status byte defines the programming status of the device. If the part is not programmed, the value is FFh, else value is 00h. This status can be modified in ISP or in parallel programming mode. In serial programming mode, this modification is either done automatically by ISP application program or with the corresponding command. * SBV: The software boot vector byte is equal to the most significant byte of the first opcode address of the bootloader firmware. The factory setting is FCh. This value can be modified in ISP or in parallel programming mode to point to a customer Bootloader. 2 Rev. A - 12-Jan-01 ANM095 2.3. Hardware Bit BLJB The Boot Loader Jump Bit (BLJB) is a bit of the hardware byte. Its value can be modified in parallel programming mode only. The values and uses are: - 0: After reset, the bootlader is executed at FC03h. This mode is the most versatile and gets full benefits of the Flash thanks to the Flash Bootloader. 1: After reset, the application program is executed at 0000h. The application program is loaded in parallel mode. This mode is used when the customer application program is stable. Parallel mode is re-used to change the program loaded in Flash. 2.4. Bootloader Flash Program The default bootloader Flash is located in address space from FC00h to FFFFh. As it is in a Flash memory, its value can be changed. The boot loader lock bit (BLLB) is an hardware bit which locks the content of the bootloader Flash program. This prevents any programming access to the upper 1-KByte in parallel mode only. If erased, the Flash bootloader can be restored by parallel programming. The default Atmel bootloader program is available on our website. The Bootloader Flash program can be executed in two ways: * Hardware conditions EA=1, PSEN=0, ALE=1 or no connected. In this case the PC is forced to value FC00h. This permits a boot from the default bootloader in all configurations and thus a return to the original device configuration. * Hardware and software method. The boot loader jump bit (BLJB) is an another bit of the hardware byte. Its value can be modified in parallel programming mode only. The values and uses are: - - 0: After reset, the bootlader is executed at address FC03h. This mode is the most versatile and gets full benefits of the Flash thanks to the Flash Bootloader. Depending on the status of the device, the process will be different. The boot modes are detailed in paragraph 4. 1: After reset, the application program is executed at address 0000h. The application program has been loaded in parallel mode. This mode is used when the customer application program is stable. He will need to reuse the parallel mode to change this configuration. Rev. A - 12-Jan-01 3 ANM095 3. Bootloader Overview The Flash bootloader includes: * a bootloader program that controls * the serial communication protocol * the ISP command decoder * the API routines. These are the low-level functions (program byte, read byte, etc.) called from the bootloader to perform Flash access operations. In order to access specific functions, the user may want to load a custom bootloader. This may be necessary in case of : * another communication interface * different protocol (other data format, crypted datas, etc.) * Flash areas protection * Flash areas checks (CRC, etc.) f a custom bootloader program is required, it is located in Flash memory at the address [SBV]00h. It calls the API routines contained in Flash boot loader memory. The customer bootloader can be programmed in Flash memory by two means: * Parallel mode: The procedure is explained in detail in the installation file which can be downloaded from Atmel Wireless and Microcontrollers web site. The MSB of the bootloader start address must be programmed in SBV. The new bootloader can be loaded in place of the default bootloader at address FC00h. Example: If the customer bootloader begins at address 3B00h (the low nibble must be 00), SBV must be programmed with 3Bh. * Serial mode: The procedure is explained in detail in the following sections of this document. To restore the branch to default Atmel bootloader, three methods can be used: - with the default bootloader activated with hardware conditions on EA, PSEN and ALE with a customer bootloader that provides features for erasing and reprogramming of SBV and BSB through the parallel programming method. The customer bootloader program simply calls the API at the common entry point in Flash bootloader with appropriate parameters to accomplish the desired operation. FFFFh FC00h Bootloader entry point : [SBV]00h API Default Bootloader Optional: Customer Bootloader 0000h Flash Memory 4 Rev. A - 12-Jan-01 ANM095 4. Boot modes What is expected from the Bootloader ? q Load the initial application program (See page 7) At reset, download the application program in Flash memory user array. q Execute the application program (See page 8) Run the application program in Flash from address 0000h. q Update the application program (See page 9) After reset, download in Flash a new application program in place of the previous one. q Load a customer bootloader in a blank device (See page 10) q or in an already programmed device (See page 11) Load a customer Bootloader. q Load the initial application program with a customer Bootloader (See page 12) Load and update operations are handled by the customer Bootloader. q Update the application program with a customer Bootloader (See page 13) After reset, download in Flash a new application program in place of the previous one. The following figures show the different valid flows in accordance with each scenario of the overall algorithm. This flowchart extracted from the Datasheet is summarized in appendix page14. Rev. A - 12-Jan-01 5 ANM095 4.1. Load the Initial Application Program a. Initial Device Configuration BLJB BSB SBV Flash 0b FFh FCh Bootloader at FC00h Blank Application Board T89C51 RD2 Rx UART Tx PC serial port ISP Software program Hardware RESET Hardware Condition? No BLJB =1b? No Software FC03h address BSB = 00h? No SBV = FCh? Yes FC00h ATMEL BOOT LOADER b. Final device configuration BLJB BSB SBV Flash 0b 00h (Note1) FCh Bootloader at FC00h Programmed with Application program Note1: The BSB must be reset when the Flash has been programmed in order to change the device status (included in the "program part" function of ISP software program) 6 Rev. A - 12-Jan-01 ANM095 4.2. Execute the Application Program a. Initial Device Configuration BLJB BSB SBV Flash 0b 00h status of programmed part FCh Bootloader at FC00h Flash is already programmed Application Board T89C51 RD2 Rx Tx Hardware RESET Hardware Condition? No BLJB =1b? No Software FC03h address Yes BSB = 00h? 0000h APPLICATION PROGRAM b. Final Device Configuration No change Rev. A - 12-Jan-01 7 ANM095 4.3. Update the Application Program a. Initial Device Configuration BLJB BSB SBV Flash 0b 00h status of programmed part FCh Bootloader at FC00h Flash is already programmed Application Board PSEN=0 T89C51 RD2 Rx Hardware EA=1 Conditions Tx PC serial port ISP software program ALE=1 Hardware RESET Hardware Condition? Yes Software FC00h ATMEL BOOT LOADER b. Final Device Configuration 8 BLJB BSB SBV Flash 0b 00h status of programmed part FCh Bootloader at FC00h Flash is programmed with the new application program ready to be executed Rev. A - 12-Jan-01 ANM095 4.4. Load a Customer Bootloader in a Blank Device a. Initial Device Configuration BLJB BSB SBV Flash 0b FFh status of unprogrammed part FCh Bootloader at FC00h Flash is blank Application Board T89C51 RD2 Rx UART Tx PC serial port ISP software program Hardware RESET Hardware Condition? No BLJB =1b? No Software FC03h address BSB = 00h? No SBV = FCh? Yes ATMEL BOOT LOADER b. Final device configuration BLJB BSB SBV Flash 0b FFh Note(2) XXh Customer Bootloader at XX00h Flash has been programmed with the customer Bootloader Note(2): As it has been reset by ISP program automatically after program part execution, it must be rewritten to its original value to execute the customer bootloader. Rev. A - 12-Jan-01 9 ANM095 4.5. Load a Customer Bootloader in an Already Programmed Part a. Initial Device Configuration BLJB BSB SBV Flash 0b 00h programmed part status FCh Bootloader at FC00h Flash is already programmed with an application program Application Board PSEN=0 T89C51 RD2 Hardware EA=1 Conditions Rx Tx PC serial port ISP software program ALE=1 Hardware RESET Hardware Condition? Yes Software FC00h address ATMEL BOOT LOADER b. Final Device Configuration BLJB BSB 0b FFh Note(3) SBV Flash XXh An area of the Flash has been programmed Customer Bootloader at XX00h with the customer Bootloader Note(3): As it has been reset by ISP program automatically after program part execution, it must be written to FFh value to execute the customer bootloader. 10 Rev. A - 12-Jan-01 ANM095 4.6. Load the Initial Application Program with a Customer Bootloader a. Initial Device Configuration BLJB 0b BSB SBV Flash FFh XXh customer Bootloader at XX00h Flash space except Bootloader is blank Application Board UART or another communication medium T89C51 RD2 Customer ISP software program Hardware RESET Hardware Condition? No BLJB =1b ? No Software FC03h address BSB = 00h? No SBV = FCh? No CUSTOMER BOOTLOADER b. Final Device Configuration BLJB BSB SBV Flash 0b 00h programmed part status Note(4) XXh Customer Bootloader at XX00h Flash space except Bootloader is programmed with program (Note 5) Note(4): The BSB must be reset when the Flash has been programmed in order to change the device status (included in the "program part" function of ISP software program) Note(5): The customer must load the application program in the available Flash space beside the customer Bootloader. Rev. A - 12-Jan-01 11 ANM095 4.7. Update the Application Program with a Customer Bootloader a. Initial Device Configuration BLJB BSB SBV Flash 0b 00h status of programmed part XXh Customer Bootloader at XX00h Flash is already programmed Application Board RS232 or another communication medium T89C51 RD2 Customer ISP software program Hardware RESET Hardware Condition? No BLJB =1b? No Software FC03h address Yes BSB = 00h? 0000h APPLICATION PROGRAM The Application program must program the BSB at FF (thanks to API). Then a reset will execute the sequence described paragraph 4.6 page 12. b. Final Device Configuration 12 BLJB BSB SBV Flash 0b FFh status of unprogrammed part XXh Customer Bootloader at XX00h Flash program is not changed. A Reset will execute the sequence described paragraph 4.6 page 12. Rev. A - 12-Jan-01 ANM095 Bootloader algorithm Hardware RESET Hardware Condition? BLJB =1b? Software 0000h address FC03h address FC00h address BSB = 00h? No SBV = FCh? Yes APPLICATION PROGRAM CUSTOMER BOOTLOADER PC= [SBV]00h Rev. A - 12-Jan-01 ATMEL BOOTLOADER 13