00001 /** IAP demo : demo code for internal Flash memory access library00002 *00003 * The internal Flash memory access is described in the LPC1768 usermanual. 00004 * http://www.nxp.com/documents/user_manual/UM10360.pdf00005 *00006 * Chapter 2: "LPC17xx Memory map"00007 * Chapter 32: "LPC17xx Flash memory interface and programming"00008 * refering Rev. 01 - 4 January 201000009 *00010 * This main.cpp demonstrates how the flash can be erased and wrote.00011 *00012 * This program tries to...00013 * 0. read device ID and serial#00014 * 1. check if the targat sector blank00015 * 2. erase the sector if it was not blank00016 * 3. write into the flash (prepare before write)00017 * 4. verify the data by IAP command00018 * 5. show the content of the flash00019 *00020 * The Flash must be erased as sectors. No overwrite can be done like SRAM. 00021 * So erase should be done in size of 4K or 32K. 00022 *00023 * Writing sector can be done with size of 256, 512, 1024 or 4096. 00024 * If other size is used, the IAP returns an error. 00025 * The SRAM memory should be allocated in 00026 *00027 *00028 * Released under the MIT License: http://mbed.org/license/mit00029 *00030 * revision 1.0 09-Mar-2010 1st release00031 * revision 1.1 12-Mar-2010 chaged: to make possible to reserve flash area for user00032 * it can be set by USER_FLASH_AREA_START and USER_FLASH_AREA_SIZE in IAP.h00033 */00034
00035 #include "mbed.h"00036 #include "IAP.h"00037
00038 #define MEM_SIZE 25600039 #define TARGET_SECTOR 2900040
00041 void memdump( char *p, int n );
00042 int isprint( int c );
00043
00044 IAP iap;
00045
00046
00047 int main() {
00048 char mem[ MEM_SIZE ]; // memory, it should be aligned to word boundary00049 int r;
00050
00051 printf( "IAP: Flash memory writing test\n" );
00052 printf( " device-ID = 0x%08X, serial# = 0x%08X, CPU running %dkHz\n", iap.read_ID(), iap.read_serial(), SystemCoreClock / 1000 );
00053 printf( " user reserved flash area: start_address=0x%08X, size=%d bytes\n", iap.reserved_flash_area_start(), iap.reserved_flash_area_size() );
00054
00055 for ( int i = 0; i < MEM_SIZE; i++ )
00056 mem[ i ] = i & 0xFF;
00057
00058 // blank check: The mbed will erase all flash contents after downloading new executable00059
00060 r = iap.blank_check( TARGET_SECTOR, TARGET_SECTOR );
00061 printf( "blank check result = 0x%08X\n", r );
00062
00063 // erase sector, if required00064
00065 if ( r == SECTOR_NOT_BLANK ) {
00066 iap.prepare( TARGET_SECTOR, TARGET_SECTOR );
00067 r = iap.erase( TARGET_SECTOR, TARGET_SECTOR );
00068 printf( "erase result = 0x%08X\n", r );
00069 }
00070
00071 // copy RAM to Flash00072
00073 iap.prepare( TARGET_SECTOR, TARGET_SECTOR );
00074 r = iap.write( mem, sector_start_adress[ TARGET_SECTOR ], MEM_SIZE );
00075 printf( "copied: SRAM(0x%08X)->Flash(0x%08X) for %d bytes. (result=0x%08X)\n", mem, sector_start_adress[ TARGET_SECTOR ], MEM_SIZE, r );
00076
00077 // compare00078
00079 r = iap.compare( mem, sector_start_adress[ TARGET_SECTOR ], MEM_SIZE );
00080 printf( "compare result = \"%s\"\n", r ? "FAILED" : "OK" );
00081
00082 //#define WRITE_NEXT_BLOCK00083 #ifdef WRITE_NEXT_BLOCK00084
00085 // copy RAM to Flash00086
00087 iap.prepare( TARGET_SECTOR, TARGET_SECTOR );
00088 r = iap.write( mem, sector_start_adress[ TARGET_SECTOR ] + 256, MEM_SIZE );
00089 printf( "copied: SRAM(0x%08X)->Flash(0x%08X) for %d bytes. (result=0x%08X)\n", mem, sector_start_adress[ TARGET_SECTOR ], MEM_SIZE, r );
00090
00091 // compare00092
00093 r = iap.compare( mem, sector_start_adress[ TARGET_SECTOR ] + 256, MEM_SIZE );
00094 printf( "compare result = \"%s\"\n", r ? "FAILED" : "OK" );
00095
00096 #endif00097
00098 printf( "showing the flash contents...\n" );
00099 memdump( sector_start_adress[ TARGET_SECTOR ], MEM_SIZE * 3 );
00100 }
00101
00102
00103 void memdump( char *base, int n ) {
00104 unsignedint *p;
00105
00106 printf( " memdump from 0x%08X for %d bytes", (unsignedlong)base, n );
00107
00108 p = (unsignedint *)((unsignedint)base & ~(unsigned int)0x3);
00109
00110 for ( int i = 0; i < (n >> 2); i++, p++ ) {
00111 if ( !(i % 4) )
00112 printf( "\n 0x%08X :", (unsignedint)p );
00113
00114 printf( " 0x%08X", *p );
00115 }
00116
00117 printf( "\n" );
00118 }