Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /** IAP demo : demo code for internal Flash memory access library 00002 * 00003 * The internal Flash memory access is described in the LPC1768 usermanual. 00004 * http://www.nxp.com/documents/user_manual/UM10360.pdf 00005 * 00006 * Chapter 2: "LPC17xx Memory map" 00007 * Chapter 32: "LPC17xx Flash memory interface and programming" 00008 * refering Rev. 01 - 4 January 2010 00009 * 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 blank 00015 * 2. erase the sector if it was not blank 00016 * 3. write into the flash (prepare before write) 00017 * 4. verify the data by IAP command 00018 * 5. show the content of the flash 00019 * 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/mit 00029 * 00030 * revision 1.0 09-Mar-2010 1st release 00031 * revision 1.1 12-Mar-2010 chaged: to make possible to reserve flash area for user 00032 * it can be set by USER_FLASH_AREA_START and USER_FLASH_AREA_SIZE in IAP.h 00033 */ 00034 00035 #include "mbed.h" 00036 #include "IAP.h" 00037 00038 #define MEM_SIZE 256 00039 #define TARGET_SECTOR 26 00040 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 boundary 00049 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 executable 00059 00060 r = iap.blank_check( TARGET_SECTOR, TARGET_SECTOR ); 00061 printf( "blank check result = 0x%08X\n", r ); 00062 00063 // erase sector, if required 00064 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 Flash 00072 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 // compare 00078 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_BLOCK 00083 #ifdef WRITE_NEXT_BLOCK 00084 00085 // copy RAM to Flash 00086 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 // compare 00092 00093 r = iap.compare( mem, sector_start_adress[ TARGET_SECTOR ] + 256, MEM_SIZE ); 00094 printf( "compare result = \"%s\"\n", r ? "FAILED" : "OK" ); 00095 00096 #endif 00097 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 unsigned int *p; 00105 00106 printf( " memdump from 0x%08X for %d bytes", (unsigned long)base, n ); 00107 00108 p = (unsigned int *)((unsigned int)base & ~(unsigned int)0x3); 00109 00110 for ( int i = 0; i < (n >> 2); i++, p++ ) { 00111 if ( !(i % 4) ) 00112 printf( "\n 0x%08X :", (unsigned int)p ); 00113 00114 printf( " 0x%08X", *p ); 00115 } 00116 00117 printf( "\n" ); 00118 }
Generated on Wed Jul 20 2022 12:22:50 by
1.7.2