LPC2000 Secondary Bootloader
LPC2000 Secondary Bootloader
LPC2000 Secondary Bootloader
Document information Info Keywords Abstract Content LPC2000, Secondary bootloader, IAP, Code update This application note describes the design and implementation of a secondary bootloader which can update the user application code in onchip flash via UART with 1K XMODEM protocol, SD/MMC with file system, EEPROM with I2C interface and CAN interface using IAP (InApplication Programming).
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
Contact information
For additional information, please visit: http://www.nxp.com For sales office addresses, please send an email to: [email protected]
AN10835_1 NXP B.V. 2009. All rights reserved.
Application note
2 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
1. Introduction
In most LPC2000 devices, the primary bootloader is the firmware which resides in the Boot Block and is executed every time the part is powered on or reset. The secondary bootloader in this document refers to a user-defined application that provides the user with an option to update the code or execute the previously programmed code. In Application Programming (IAP executes erase and write operations on the on-chip flash memory, as directed by the end-user application code. Code update is a typical application of IAP. This document describes four secondary bootloaders using different interfaces (UART using the 1K XMODEM protocol, SD/MMC using a FAT file system, I2C interfaced to EEPROM and CAN).
AN10835_1
Application note
3 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
Application note
4 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
containing the command code and its parameters. Register r1 contains the results of the IAP command returned by a pointer (pointing to a table). The user can reuse the command table for the results by passing the same pointers into registers r0 and r1. Ensure that the results table is large enough to store all the results coming from the IAP command issued. Refer to Fig 2.
AN10835_1
Application note
5 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
Select sector
Erase sector
Select sector
Program sector
Verify data
2.2.4.1 Define system parameters Some constants such as system clock, IAP entry point, input and output buffers should be defined before an IAP call.
#define IAP_CLK Fcclk #define IAP_LOCATION 0x7FFFFFF1 #define iap_entry(a, b) ((void (*)())(IAP_LOCATION))(a, b) unsigned long command[5] = {0,0,0,0,0}; unsigned long result[3]= {0,0,0};
2.2.4.2 Select sector The sectors have to be selected before any erase or programming operation. More than one sector can be selected.
AN10835_1
Application note
6 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
/************************************************************************* * Function Name: IAP_PrepareSec * Parameters: unsigned long StartSecNum -- Start Sector Number * unsigned long EndSecNum -- End Sector Number * Return: unsigned long -- Status Code * * Description: This command must be executed before executing "Copy RAM to Flash" or * "Erase Sector(s)" command. * *************************************************************************/ unsigned long IAP_PrepareSec (unsigned long StartSecNum, unsigned long EndSecNum) { if (EndSecNum < StartSecNum) return IAP_STA_INVALD_PARAM; command[0] = IAP_CMD_PrepareSec; command[1] = StartSecNum; command[2] = EndSecNum; iap_entry(command, result); return result[0]; }
2.2.4.3 Erase sector Like other Flash implementations, the LPC2000 on-chip should be erased before programming. However, if the target sector is already erased, it is not necessary to erase the sector again. More than one sector can be erased at a time.
/************************************************************************* * Function Name: IAP_EraseSec * Parameters: unsigned long StartSecNum -- Start Sector Number * unsigned long EndSecNum -- End Sector Number * Return: unsigned long -- Status Code * * Description: This command is used to erase a sector or multiple sectors of on-chip Flash * memory. * *************************************************************************/ unsigned long IAP_EraseSec (unsigned long StartSecNum, unsigned long EndSecNum) { if (EndSecNum < StartSecNum) return IAP_STA_INVALD_PARAM; command[0] = IAP_CMD_EraseSec; command[1] = StartSecNum; command[2] = EndSecNum; command[3] = IAP_CLK / 1000; iap_entry(command, result); return result[0]; }
2.2.4.4 Program sector During this stage, the data will be programmed from on-chip RAM to Flash. Note: 1. Data can only be programmed from on-chip SRAM to on-chip Flash. 2. The address in on-chip Flash should be on a 256 byte boundary.
AN10835_1 NXP B.V. 2009. All rights reserved.
Application note
7 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
3. On-chip RAM should be located on the local bus which means neither USB SRAM nor Ethernet SRAM can be used. 4. Number of programmed bytes each time should be 256, 512, 1024 or 4096.
/************************************************************************* * Function Name: IAP_CopyRAMToFlash * Parameters: unsigned long dst -- Destination Flash address, should be a 256 byte boundary. * unsigned long src -- Source RAM address, should be a word boundary * unsigned long number -- 256 | 512 |1024 |4096 * Return: unsigned long -- Status Code * * Description: This command is used to program the flash memory. * *************************************************************************/ unsigned long IAP_CopyRAMToFlash (unsigned long dst, unsigned long src, unsigned long number) { command[0] = IAP_CMD_CopyRAMToFlash; command[1] = dst; command[2] = src; command[3] = number; command[4] = IAP_CLK / 1000; // Fcclk in KHz iap_entry(command, result); return result[0]; }
2.2.4.5 Verify data With this function, user does not have to write their own code to compare the data in RAM and Flash. Note: Source address, destination address and number of bytes should be a word boundary or a multiple of 4.
/************************************************************************* * Function Name: IAP_Compare * Parameters: unsigned long dst -- Destination Flash address * unsigned long src -- Source RAM address * unsigned long number -- Should be in mutilple of 4 * Return: unsigned long -- Status Code * * Description: This command is used to compary the memory contents at two locations. * * NOTE: Compary result may not be correct when source or destination address contains * any of the first 64 bytes starting from address zero. First 64 bytes can be re-mapped * to RAM. * *************************************************************************/ unsigned long IAP_Compare (unsigned long dst, unsigned long src, unsigned long number, unsigned long *offset) { command[0] = IAP_CMD_Compare; command[1] = dst; command[2] = src; command[3] = number; iap_entry(command, result); if (result[0] == IAP_STA_COMPARE_ERROR) *offset = result[1]; return result[0]; }
2.2.4.6 Interrupts during IAP The on-chip flash memory is not accessible during erase/write operations. When the user application code starts executing, the interrupt vectors from the user flash area are active.
AN10835_1 NXP B.V. 2009. All rights reserved.
Application note
8 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
The user should either disable interrupts, or ensure that the users interrupt vectors reside and are active in RAM, before making a flash erase/write IAP call. The IAP code does not use or disable interrupts.
__swi(0x00) void SwiHandle1(int Handle); #define IRQDisable() SwiHandle1(0) #define IRQEnable() SwiHandle1(1) /* * prepare and erase the sectors with index from "start" to "end" * if successful, return TRUE, elsewise FALSE */ BYTE IAP_PrepareErase_Sector(DWORD start, DWORD end) { DWORD IAP_status; BYTE result = FALSE; IRQDisable(); IAP_status = IAP_PrepareSec(start, end); if (IAP_status == IAP_STA_CMD_SUCCESS) { IAP_status = IAP_EraseSec(start, end); if (IAP_status == IAP_STA_CMD_SUCCESS) { result = TRUE; } } IRQEnable(); return result; }
2.2.4.7 RAM used by IAP command handler Flash programming commands use the top 32 bytes of on-chip RAM. The maximum stack usage in the user allocated stack space is 128 bytes (for LPC2300) and grows downwards. 2.2.4.8 Running the user application After loading the application, the user can execute the application by modifying the PC register by pointing it to the starting address of the application code.
#define AP_ADDR 0x8000 typedef void (*FP)(void); // where the user app located
/* * run the user application from pre-specified address */ void Run_Application() { FP fp; fp = (FP)AP_ADDR; (*fp)(); }
AN10835_1
Application note
9 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
0x40008000-0x20
Boot Loader
0x0
Boot Loader
0x40000000
On-chip Flash
On-chip RAM(LPC2378)
AN10835_1
Application note
10 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
User Application
User Application
0x0
Vector Table
0x40000000+0x40 0x40000000
On-chip Flash
On-chip RAM(LPC2378)
If the user application utilizes interrupts, the first 64 bytes (0x40) at the bottom of the onchip RAM should be used as the interrupt vector table. Refer to section 3.2.2. Fig 14 shows the memory configuration for the user application in Keils uVision 3 IDE. The on-chip RAM is from 0x40000040 instead of 0x40000000.
By changing the IROM1 Start address in uVision, the user can change the starting location at which the application will be stored. Note that it is important not to use a starting address that is located in a sector that contains the secondary bootloader.
AN10835_1
Application note
11 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
The portion of memory that is re-mapped allows for interrupt processing in different modes and includes the interrupt vector area (32 bytes) as well as an additional 32 bytes (a total of 64 bytes) to facilitate branching to interrupt handlers at distant physical addresses. The remapped code locations overlay addresses 0x0000 0000 through 0x0000 003F. The interrupt vectors of the secondary bootloader occupies the physical address from 0x0000 0000 through 0x0000 003F in on-chip flash, thus the interrupt vectors for the user application has to be re-mapped to the bottom of the on-chip SRAM using User RAM Mode. Before entering the main application, the user should copy the 64 bytes (32 bytes interrupt vector and 32 bytes additional bytes) into the bottom of the on-chip RAM and set the mapping mode to User RAM Mode. Fig 17 shows how to copy the 64 bytes of interrupt vectors from on-chip flash to the bottom of on-chip RAM.
AN10835_1
Application note
12 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
AN10835_1
Application note
13 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
AN10835_1
Application note
14 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
4. Demo description
4.1 Demo setup
4.1.1 Hardware setup
This demo is tested on the KEIL MCB2300 evaluation board (version 4.7). For more information about MCB2300, please refer to: http://www.nxp.com/redirect/keil.com/mcb2300/. No other special hardware is needed except a RS-232 cable, Standard USB (A-B) cable (for power), and an EEPROM with I2C interface (if using the I2C bootloader). The RS-232 cable is for the connection between the PC and MCB2300 on COM1. The ULINK-ME JTAG module can be used to program the bootloader onto the board. Optionally, the FlashMagic tool can be used to program the bootloader using the RS-232 cable connected onto UART0 (COM0). UART1 (COM1) Setting: 115200 baud, 8N1 The FlashMagic tool is available for free at: http://www.nxp.com/redirect/flashmagictool.com/
Application note
15 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
Throughout this section <starting address> refers to the actual address which should be entered into the serial terminal. For example, if you want to use address 0x10000 and the command is shown as prog <starting address>, then the user should type: prog 0x10000.
AN10835_1
Application note
16 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
The user application is linked at address of 0x70000 but is programmed at address 0x60000. Afterwards, type prog 0x60000 0x70000. This causes the user application code to be copied from address 0x60000 onto address 0x70000 via the CAN interface.
Application note
17 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
Note: The SDA and SCL bus requires a pull-up resistor to Vcc (typical 10 k for 100 kHz, 2 k for 400 kHz). The application code needs to be already programmed into EEPROM. By typing command prog <starting address>, the user application code will be copied from EEPROM via I2C interface onto internal flash at the address specified in the prog command. Then input run <starting address>, which should jump to the application.
3.3V
A0 A1 A2 Vss
GND
AN10835_1
Application note
18 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
AN10835_1
Application note
19 of 21
NXP Semiconductors
AN10835
LPC2000 secondary boot loader for code update using IAP
5. Legal information
5.1 Definitions
Draft The document is a draft version only. The content is still under internal review and subject to formal approval, which may result in modifications or additions. NXP Semiconductors does not give any representations or warranties as to the accuracy or completeness of information included herein and shall have no liability for the consequences of use of such information. Suitability for use NXP Semiconductors products are not designed, authorized or warranted to be suitable for use in medical, military, aircraft, space or life support equipment, nor in applications where failure or malfunction of a NXP Semiconductors product can reasonably be expected to result in personal injury, death or severe property or environmental damage. NXP Semiconductors accepts no liability for inclusion and/or use of NXP Semiconductors products in such equipment or applications and therefore such inclusion and/or use is for the customers own risk. Applications Applications that are described herein for any of these products are for illustrative purposes only. NXP Semiconductors makes no representation or warranty that such applications will be suitable for the specified use without further testing or modification. Export control This document as well as the item(s) described herein may be subject to export control regulations. Export might require a prior authorization from national authorities.
5.2 Disclaimers
General Information in this document is believed to be accurate and reliable. However, NXP Semiconductors does not give any representations or warranties, expressed or implied, as to the accuracy or completeness of such information and shall have no liability for the consequences of use of such information. Right to make changes NXP Semiconductors reserves the right to make changes to information published in this document, including without limitation specifications and product descriptions, at any time and without notice. This document supersedes and replaces all information supplied prior to the publication hereof.
5.3 Trademarks
Notice: All referenced brands, product names, service names and trademarks are property of their respective owners.
AN10835_1
Application note
20 of 21
NXP Semiconductors
AN10835
LPC2000 secondary bootloader for code update using IAP
6. Contents
1. 2. 2.1 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.4.1 2.2.4.2 2.2.4.3 2.2.4.4 2.2.4.5 2.2.4.6 2.2.4.7 2.2.4.8 3. 3.1 3.1.1 3.2 3.2.1 3.2.2 3.2.3 4. 4.1 4.1.1 4.1.2 4.2 4.2.1 4.2.2 4.2.3 4.2.4 5. 5.1 5.2 5.3 6. Introduction .........................................................3 LPC2000 Flash Programming.............................3 Sector description..................................................3 In Application Programming (IAP) .........................4 IAP introduction .....................................................4 IAP application ......................................................4 IAP commands ......................................................4 Using IAP ..............................................................5 Define system parameters .................................6 Select sector ......................................................6 Erase sector .......................................................7 Program sector...................................................7 Verify data ..........................................................8 Interrupts during IAP ..........................................8 RAM used by IAP command handler .................9 Running the user application..............................9 Secondary bootloader and user application...10 Secondary bootloader .........................................10 Bootloader Features and Constraints..................10 User application...................................................10 Memory map .......................................................10 Interrupt vector table re-mapping ........................11 User application size ...........................................13 Demo description ..............................................15 Demo Setup ........................................................15 Hardware setup ...................................................15 Software setup ....................................................15 Start the demo.....................................................15 Code update via UART .......................................16 Code update via CAN..........................................17 Code update via I2C............................................17 Code update via SD/MMC...................................18 Legal information ..............................................20 Definitions............................................................20 Disclaimers..........................................................20 Trademarks .........................................................20 Contents.............................................................21
Please be aware that important notices concerning this document and the product(s) described herein, have been included in the section 'Legal information'.