In Application Programming with support for both LPC1768 and LPC2368. Original library here http://mbed.org/users/okano/notebook/iap-in-application-programming-internal-flash-eras/
Revision 0:406ffaf4d93c, committed 2011-07-11
- Comitter:
- tecnosys
- Date:
- Mon Jul 11 01:53:18 2011 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r 406ffaf4d93c IAP.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/IAP.cpp Mon Jul 11 01:53:18 2011 +0000 @@ -0,0 +1,202 @@ +/** IAP : internal Flash memory access library + * + * The internal Flash memory access is described in the LPC1768 usermanual. + * http://www.nxp.com/documents/user_manual/UM10360.pdf + * + * Chapter 2: "LPC17xx Memory map" + * Chapter 32: "LPC17xx Flash memory interface and programming" + * refering Rev. 01 - 4 January 2010 + * + * Released under the MIT License: http://mbed.org/license/mit + * + * revision 1.0 09-Mar-2010 1st release + * revision 1.1 12-Mar-2010 chaged: to make possible to reserve flash area for user + * it can be set by USER_FLASH_AREA_START and USER_FLASH_AREA_SIZE in IAP.h + */ + +#include "mbed.h" +#include "IAP.h" + +#define USER_FLASH_AREA_START_STR( x ) STR( x ) +#define STR( x ) #x + +unsigned char user_area[ USER_FLASH_AREA_SIZE ] __attribute__((section( ".ARM.__at_" USER_FLASH_AREA_START_STR( USER_FLASH_AREA_START ) ), zero_init)); + + +/* + * Reserve of flash area is explained by Igor. Please refer next URL + * http://mbed.org/users/okano/notebook/iap-in-application-programming-internal-flash-eras/?page=1#comment-271 + */ + +//unsigned char user_area[ size ] __attribute__((section(".ARM.__at_0x78000"), zero_init)); + +/* + * IAP command codes + * Table 589. "IAP Command Summary", Chapter 8. "IAP commands", usermanual + */ + +enum command_code + { + IAPCommand_Prepare_sector_for_write_operation = 50, + IAPCommand_Copy_RAM_to_Flash, + IAPCommand_Erase_sector, + IAPCommand_Blank_check_sector, + IAPCommand_Read_part_ID, + IAPCommand_Read_Boot_Code_version, + IAPCommand_Compare, + IAPCommand_Reinvoke_ISP, + IAPCommand_Read_device_serial_number + }; + + +/** Read part identification number + * + * @return device ID + * @see read_serial() + */ + +int IAP::read_ID( void ) { + IAP_command[ 0 ] = IAPCommand_Read_part_ID; + + iap_entry( IAP_command, IAP_result ); + + // return ( (int)IAP_result[ 0 ] ); + return ( (int)IAP_result[ 1 ] ); // to return the number itself (this command always returns CMD_SUCCESS) +} + + +/** Read device serial number + * + * @return device serial number + * @see read_ID() + */ + +int IAP::read_serial( void ) { + IAP_command[ 0 ] = IAPCommand_Read_device_serial_number; + + iap_entry( IAP_command, IAP_result ); + + // return ( (int)IAP_result[ 0 ] ); + return ( (int)IAP_result[ 1 ] ); // to return the number itself (this command always returns CMD_SUCCESS) +} + + +/** Blank check sector(s) + * + * @param start a Start Sector Number + * @param end an End Sector Number (should be greater than or equal to start sector number). + * @return error code: CMD_SUCCESS | BUSY | SECTOR_NOT_BLANK | INVALID_SECTOR + */ + +int IAP::blank_check( int start, int end ) { + IAP_command[ 0 ] = IAPCommand_Blank_check_sector; + IAP_command[ 1 ] = (unsigned int)start; // Start Sector Number + IAP_command[ 2 ] = (unsigned int)end; // End Sector Number (should be greater than or equal to start sector number) + + iap_entry( IAP_command, IAP_result ); + + return ( (int)IAP_result[ 0 ] ); +} + + +/** Erase Sector(s) + * + * @param start a Start Sector Number + * @param end an End Sector Number (should be greater than or equal to start sector number). + * @return error code: CMD_SUCCESS | BUSY | SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION | INVALID_SECTOR + */ + +int IAP::erase( int start, int end ) { + IAP_command[ 0 ] = IAPCommand_Erase_sector; + IAP_command[ 1 ] = (unsigned int)start; // Start Sector Number + IAP_command[ 2 ] = (unsigned int)end; // End Sector Number (should be greater than or equal to start sector number) + IAP_command[ 3 ] = cclk_kHz; // CPU Clock Frequency (CCLK) in kHz + + iap_entry( IAP_command, IAP_result ); + + return ( (int)IAP_result[ 0 ] ); +} + + +/** Prepare sector(s) for write operation + * + * @param start a Start Sector Number + * @param end an End Sector Number (should be greater than or equal to start sector number). + * @return error code: CMD_SUCCESS | BUSY | INVALID_SECTOR + */ + +int IAP::prepare( int start, int end ) { + IAP_command[ 0 ] = IAPCommand_Prepare_sector_for_write_operation; + IAP_command[ 1 ] = (unsigned int)start; // Start Sector Number + IAP_command[ 2 ] = (unsigned int)end; // End Sector Number (should be greater than or equal to start sector number). + + iap_entry( IAP_command, IAP_result ); + + return ( (int)IAP_result[ 0 ] ); +} + + +/** Copy RAM to Flash + * + * @param source_addr Source RAM address from which data bytes are to be read. This address should be a word boundary. + * @param target_addr Destination flash address where data bytes are to be written. This address should be a 256 byte boundary. + * @param size Number of bytes to be written. Should be 256 | 512 | 1024 | 4096. + * @return error code: CMD_SUCCESS | SRC_ADDR_ERROR (Address not a word boundary) | DST_ADDR_ERROR (Address not on correct boundary) | SRC_ADDR_NOT_MAPPED | DST_ADDR_NOT_MAPPED | COUNT_ERROR (Byte count is not 256 | 512 | 1024 | 4096) | SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION | BUSY + */ + +int IAP::write( char *source_addr, char *target_addr, int size ) { + IAP_command[ 0 ] = IAPCommand_Copy_RAM_to_Flash; + IAP_command[ 1 ] = (unsigned int)target_addr; // Destination flash address where data bytes are to be written. This address should be a 256 byte boundary. + IAP_command[ 2 ] = (unsigned int)source_addr; // Source RAM address from which data bytes are to be read. This address should be a word boundary. + IAP_command[ 3 ] = size; // Number of bytes to be written. Should be 256 | 512 | 1024 | 4096. + IAP_command[ 4 ] = cclk_kHz; // CPU Clock Frequency (CCLK) in kHz. + + iap_entry( IAP_command, IAP_result ); + + return ( (int)IAP_result[ 0 ] ); +} + + +/** Compare <address1> <address2> <no of bytes> + * + * @param source_addr Starting flash or RAM address of data bytes to be compared. This address should be a word boundary. + * @param target_addr Starting flash or RAM address of data bytes to be compared. This address should be a word boundary. + * @param size Number of bytes to be compared; should be a multiple of 4. + * @return error code: CMD_SUCCESS | COMPARE_ERROR | COUNT_ERROR (Byte count is not a multiple of 4) | ADDR_ERROR | ADDR_NOT_MAPPED + */ + +int IAP::compare( char *source_addr, char *target_addr, int size ) { + IAP_command[ 0 ] = IAPCommand_Compare; + IAP_command[ 1 ] = (unsigned int)target_addr; // Starting flash or RAM address of data bytes to be compared. This address should be a word boundary. + IAP_command[ 2 ] = (unsigned int)source_addr; // Starting flash or RAM address of data bytes to be compared. This address should be a word boundary. + IAP_command[ 3 ] = size; // Number of bytes to be compared; should be a multiple of 4. + + iap_entry( IAP_command, IAP_result ); + + return ( (int)IAP_result[ 0 ] ); +} + + +/** Get user reserved flash start address + * + * @return start address of user reserved flash memory + * @see reserved_flash_area_size() + */ + +char * IAP::reserved_flash_area_start( void ) +{ + return ( (char *)USER_FLASH_AREA_START ); +} + + +/** Get user reserved flash size + * + * @return size of user reserved flash memory + * @see reserved_flash_area_start() + */ + +int IAP::reserved_flash_area_size( void ) +{ + return ( USER_FLASH_AREA_SIZE ); +} +
diff -r 000000000000 -r 406ffaf4d93c IAP.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/IAP.h Mon Jul 11 01:53:18 2011 +0000 @@ -0,0 +1,263 @@ +/** IAP : internal Flash memory access library + * + * The internal Flash memory access is described in the LPC1768 usermanual. + * http://www.nxp.com/documents/user_manual/UM10360.pdf + * + * Chapter 2: "LPC17xx Memory map" + * Chapter 32: "LPC17xx Flash memory interface and programming" + * refering Rev. 01 - 4 January 2010 + * + * Released under the MIT License: http://mbed.org/license/mit + * + * revision 1.0 09-Mar-2010 1st release + * revision 1.1 12-Mar-2010 chaged: to make possible to reserve flash area for user + * it can be set by USER_FLASH_AREA_START and USER_FLASH_AREA_SIZE in IAP.h + */ + +#ifndef MBED_IAP +#define MBED_IAP + +#include "mbed.h" + +/* + * memory map information is available in next URL also. + * http://mbed.org/projects/libraries/svn/mbed/trunk/LPC1768/LPC17xx.h + */ + +#ifdef TARGET_LPC1768 + +#define IAP_LOCATION 0x1fff1ff1 + +#define FLASH_SECTOR_0 0x00000000 +#define FLASH_SECTOR_1 0x00001000 +#define FLASH_SECTOR_2 0x00002000 +#define FLASH_SECTOR_3 0x00003000 +#define FLASH_SECTOR_4 0x00004000 +#define FLASH_SECTOR_5 0x00005000 +#define FLASH_SECTOR_6 0x00006000 +#define FLASH_SECTOR_7 0x00007000 +#define FLASH_SECTOR_8 0x00008000 +#define FLASH_SECTOR_9 0x00009000 +#define FLASH_SECTOR_10 0x0000A000 +#define FLASH_SECTOR_11 0x0000B000 +#define FLASH_SECTOR_12 0x0000C000 +#define FLASH_SECTOR_13 0x0000D000 +#define FLASH_SECTOR_14 0x0000E000 +#define FLASH_SECTOR_15 0x0000F000 +#define FLASH_SECTOR_16 0x00010000 +#define FLASH_SECTOR_17 0x00018000 +#define FLASH_SECTOR_18 0x00020000 +#define FLASH_SECTOR_19 0x00028000 +#define FLASH_SECTOR_20 0x00030000 +#define FLASH_SECTOR_21 0x00038000 +#define FLASH_SECTOR_22 0x00040000 +#define FLASH_SECTOR_23 0x00048000 +#define FLASH_SECTOR_24 0x00050000 +#define FLASH_SECTOR_25 0x00058000 +#define FLASH_SECTOR_26 0x00060000 +#define FLASH_SECTOR_27 0x00068000 +#define FLASH_SECTOR_28 0x00070000 +#define FLASH_SECTOR_29 0x00078000 + +#define FLASH_SECTOR_SIZE_0_TO_15 ( 4 * 1024) +#define FLASH_SECTOR_SIZE_16_TO_29 (32 * 1024) + +#define USER_FLASH_AREA_START FLASH_SECTOR_29 +#define USER_FLASH_AREA_SIZE (FLASH_SECTOR_SIZE_16_TO_29 * 1) + +static char * sector_start_adress[] = { + (char *)FLASH_SECTOR_0, + (char *)FLASH_SECTOR_1, + (char *)FLASH_SECTOR_2, + (char *)FLASH_SECTOR_3, + (char *)FLASH_SECTOR_4, + (char *)FLASH_SECTOR_5, + (char *)FLASH_SECTOR_6, + (char *)FLASH_SECTOR_7, + (char *)FLASH_SECTOR_8, + (char *)FLASH_SECTOR_9, + (char *)FLASH_SECTOR_10, + (char *)FLASH_SECTOR_11, + (char *)FLASH_SECTOR_12, + (char *)FLASH_SECTOR_13, + (char *)FLASH_SECTOR_14, + (char *)FLASH_SECTOR_15, + (char *)FLASH_SECTOR_16, + (char *)FLASH_SECTOR_17, + (char *)FLASH_SECTOR_18, + (char *)FLASH_SECTOR_19, + (char *)FLASH_SECTOR_20, + (char *)FLASH_SECTOR_21, + (char *)FLASH_SECTOR_22, + (char *)FLASH_SECTOR_23, + (char *)FLASH_SECTOR_24, + (char *)FLASH_SECTOR_25, + (char *)FLASH_SECTOR_26, + (char *)FLASH_SECTOR_27, + (char *)FLASH_SECTOR_28, + (char *)FLASH_SECTOR_29 +}; + + +#endif + +#ifdef TARGET_LPC2368 +#define IAP_LOCATION 0x7FFFFFF1 + +#define FLASH_SECTOR_0 0x00000000 +#define FLASH_SECTOR_1 0x00001000 +#define FLASH_SECTOR_2 0x00002000 +#define FLASH_SECTOR_3 0x00003000 +#define FLASH_SECTOR_4 0x00004000 +#define FLASH_SECTOR_5 0x00005000 +#define FLASH_SECTOR_6 0x00006000 +#define FLASH_SECTOR_7 0x00007000 +#define FLASH_SECTOR_8 0x00008000 +#define FLASH_SECTOR_9 0x00010000 +#define FLASH_SECTOR_10 0x00018000 +#define FLASH_SECTOR_11 0x00020000 +#define FLASH_SECTOR_12 0x00028000 +#define FLASH_SECTOR_13 0x00030000 +#define FLASH_SECTOR_14 0x00038000 +#define FLASH_SECTOR_15 0x00040000 +#define FLASH_SECTOR_16 0x00048000 +#define FLASH_SECTOR_17 0x00050000 +#define FLASH_SECTOR_18 0x00058000 +#define FLASH_SECTOR_19 0x00060000 +#define FLASH_SECTOR_20 0x00068000 +#define FLASH_SECTOR_21 0x00070000 +#define FLASH_SECTOR_22 0x00078000 +#define FLASH_SECTOR_23 0x00079000 +#define FLASH_SECTOR_24 0x0007A000 +#define FLASH_SECTOR_25 0x0007B000 +#define FLASH_SECTOR_26 0x0007C000 +#define FLASH_SECTOR_27 0x0007D000 + +#define FLASH_SECTOR_SIZE_0_TO_7 ( 4 * 1024) +#define FLASH_SECTOR_SIZE_8_TO_21 (32 * 1024) +#define FLASH_SECTOR_SIZE_22_TO_27 ( 4 * 1024) + + +#define USER_FLASH_AREA_START FLASH_SECTOR_21 +#define USER_FLASH_AREA_SIZE (FLASH_SECTOR_SIZE_8_TO_21 * 1) + +static char * sector_start_adress[] = { + (char *)FLASH_SECTOR_0, + (char *)FLASH_SECTOR_1, + (char *)FLASH_SECTOR_2, + (char *)FLASH_SECTOR_3, + (char *)FLASH_SECTOR_4, + (char *)FLASH_SECTOR_5, + (char *)FLASH_SECTOR_6, + (char *)FLASH_SECTOR_7, + (char *)FLASH_SECTOR_8, + (char *)FLASH_SECTOR_9, + (char *)FLASH_SECTOR_10, + (char *)FLASH_SECTOR_11, + (char *)FLASH_SECTOR_12, + (char *)FLASH_SECTOR_13, + (char *)FLASH_SECTOR_14, + (char *)FLASH_SECTOR_15, + (char *)FLASH_SECTOR_16, + (char *)FLASH_SECTOR_17, + (char *)FLASH_SECTOR_18, + (char *)FLASH_SECTOR_19, + (char *)FLASH_SECTOR_20, + (char *)FLASH_SECTOR_21, + (char *)FLASH_SECTOR_22, + (char *)FLASH_SECTOR_23, + (char *)FLASH_SECTOR_24, + (char *)FLASH_SECTOR_25, + (char *)FLASH_SECTOR_26, + (char *)FLASH_SECTOR_27 +}; + + +#endif + + +/** Error code by IAP routine + * + * Table 588 "ISP Return Codes Summary", Section 7.15 "ISP Return Codes", usermanual + */ + +enum error_code { + CMD_SUCCESS, + INVALID_COMMAND, + SRC_ADDR_ERROR, + DST_ADDR_ERROR, + SRC_ADDR_NOT_MAPPED, + DST_ADDR_NOT_MAPPED, + COUNT_ERROR, + INVALID_SECTOR, + SECTOR_NOT_BLANK, + SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION, + COMPARE_ERROR, + BUSY, + PARAM_ERROR, + ADDR_ERROR, + ADDR_NOT_MAPPED, + CMD_LOCKED, + INVALID_CODE, + INVALID_BAUD_RATE, + INVALID_STOP_BIT, + CODE_READ_PROTECTION_ENABLED +}; + + + +/* + * IAP routine entry + * + * Chapter 8. "IAP commands", usermanual + */ + + +//#define IAP_LOCATION 0x1fff1ff1 + + +typedef void (*IAP_call)(unsigned int [], unsigned int []); + +/** IAP class + * + * Interface for internal flash memory access + */ + + +class IAP { +public: + + /* + * SystemCoreClock ??? : + * http://mbed.org/forum/mbed/topic/229/ + * http://mbed.org/users/simon/programs/SystemCoreClock/16mhsh/ + */ + + + /** Constructor for IAP + * + */ + + IAP() : iap_entry( reinterpret_cast<IAP_call>(IAP_LOCATION) ), cclk_kHz( SystemCoreClock / 1000 ) {} + int read_ID( void ); + int read_serial( void ); + int blank_check( int start, int end ); + int erase( int start, int end ); + int prepare( int start, int end ); + int write( char *source_addr, char *target_addr, int size ); + int compare( char *source_addr, char *target_addr, int size ); + + char *reserved_flash_area_start( void ); + int reserved_flash_area_size( void ); + +private: + IAP_call iap_entry; + unsigned int IAP_command[ 5 ]; + unsigned int IAP_result[ 5 ]; + int cclk_kHz; + + //int cpu_clock( void ); +} +; + +#endif // #ifndef MBED_IAP
diff -r 000000000000 -r 406ffaf4d93c main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Jul 11 01:53:18 2011 +0000 @@ -0,0 +1,118 @@ +/** IAP demo : demo code for internal Flash memory access library + * + * The internal Flash memory access is described in the LPC1768 usermanual. + * http://www.nxp.com/documents/user_manual/UM10360.pdf + * + * Chapter 2: "LPC17xx Memory map" + * Chapter 32: "LPC17xx Flash memory interface and programming" + * refering Rev. 01 - 4 January 2010 + * + * This main.cpp demonstrates how the flash can be erased and wrote. + * + * This program tries to... + * 0. read device ID and serial# + * 1. check if the targat sector blank + * 2. erase the sector if it was not blank + * 3. write into the flash (prepare before write) + * 4. verify the data by IAP command + * 5. show the content of the flash + * + * The Flash must be erased as sectors. No overwrite can be done like SRAM. + * So erase should be done in size of 4K or 32K. + * + * Writing sector can be done with size of 256, 512, 1024 or 4096. + * If other size is used, the IAP returns an error. + * The SRAM memory should be allocated in + * + * + * Released under the MIT License: http://mbed.org/license/mit + * + * revision 1.0 09-Mar-2010 1st release + * revision 1.1 12-Mar-2010 chaged: to make possible to reserve flash area for user + * it can be set by USER_FLASH_AREA_START and USER_FLASH_AREA_SIZE in IAP.h + */ + +#include "mbed.h" +#include "IAP.h" + +#define MEM_SIZE 256 +#define TARGET_SECTOR 26 + +void memdump( char *p, int n ); +int isprint( int c ); + +IAP iap; + + +int main() { + char mem[ MEM_SIZE ]; // memory, it should be aligned to word boundary + int r; + + printf( "IAP: Flash memory writing test\n" ); + printf( " device-ID = 0x%08X, serial# = 0x%08X, CPU running %dkHz\n", iap.read_ID(), iap.read_serial(), SystemCoreClock / 1000 ); + printf( " user reserved flash area: start_address=0x%08X, size=%d bytes\n", iap.reserved_flash_area_start(), iap.reserved_flash_area_size() ); + + for ( int i = 0; i < MEM_SIZE; i++ ) + mem[ i ] = i & 0xFF; + + // blank check: The mbed will erase all flash contents after downloading new executable + + r = iap.blank_check( TARGET_SECTOR, TARGET_SECTOR ); + printf( "blank check result = 0x%08X\n", r ); + + // erase sector, if required + + // if ( r == SECTOR_NOT_BLANK ) { + iap.prepare( TARGET_SECTOR, TARGET_SECTOR ); + r = iap.erase( TARGET_SECTOR, TARGET_SECTOR ); + printf( "erase result = 0x%08X\n", r ); + //} + + // copy RAM to Flash + + iap.prepare( TARGET_SECTOR, TARGET_SECTOR ); + r = iap.write( mem, sector_start_adress[ TARGET_SECTOR ], MEM_SIZE ); + printf( "copied: SRAM(0x%08X)->Flash(0x%08X) for %d bytes. (result=0x%08X)\n", mem, sector_start_adress[ TARGET_SECTOR ], MEM_SIZE, r ); + + // compare + + r = iap.compare( mem, sector_start_adress[ TARGET_SECTOR ], MEM_SIZE ); + printf( "compare result = \"%s\"\n", r ? "FAILED" : "OK" ); + +//#define WRITE_NEXT_BLOCK +#ifdef WRITE_NEXT_BLOCK + + // copy RAM to Flash + + iap.prepare( TARGET_SECTOR, TARGET_SECTOR ); + r = iap.write( mem, sector_start_adress[ TARGET_SECTOR ] + 256, MEM_SIZE ); + printf( "copied: SRAM(0x%08X)->Flash(0x%08X) for %d bytes. (result=0x%08X)\n", mem, sector_start_adress[ TARGET_SECTOR ], MEM_SIZE, r ); + + // compare + + r = iap.compare( mem, sector_start_adress[ TARGET_SECTOR ] + 256, MEM_SIZE ); + printf( "compare result = \"%s\"\n", r ? "FAILED" : "OK" ); + +#endif + + printf( "showing the flash contents...\n" ); + memdump( sector_start_adress[ TARGET_SECTOR ], MEM_SIZE * 3 ); +} + + +void memdump( char *base, int n ) { + unsigned int *p; + + printf( " memdump from 0x%08X for %d bytes", (unsigned long)base, n ); + + p = (unsigned int *)((unsigned int)base & ~(unsigned int)0x3); + + for ( int i = 0; i < (n >> 2); i++, p++ ) { + if ( !(i % 4) ) + printf( "\n 0x%08X :", (unsigned int)p ); + + printf( " 0x%08X", *p ); + } + + printf( "\n" ); +}
diff -r 000000000000 -r 406ffaf4d93c mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Jul 11 01:53:18 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/49a220cc26e0