Sample code for how to erase/write LPC1768, LPC11U24, LPC1114, LPC812 and LPC824 internal flash memory. This program uses IAP call of MCU's ROM routines. The IAP library also supports read/write of EEPROM in LPC11U24.

Dependencies:   mbed IAP

Sample code for how to erase/write LPC1768, LPC11U24, LPC1114, LPC812 and LPC824 internal flash memory. This program uses IAP call of MCU's ROM routines.

No filesystem interface available. This program is just an interface to flash erasing and writing. User need manage where to store the data in the flash area.

This IAP library supports read/write of EEPROM in LPC11U24.

More information available in
http://mbed.org/users/okano/notebook/iap-in-application-programming-internal-flash-eras/

Revision:
3:63a0993315e5
Parent:
2:c22f0c87fee6
Child:
4:5705a494169a
diff -r c22f0c87fee6 -r 63a0993315e5 main.cpp
--- a/main.cpp	Mon Nov 26 06:03:39 2012 +0000
+++ b/main.cpp	Fri Jan 09 06:37:02 2015 +0000
@@ -1,6 +1,6 @@
 /**    IAP demo : demo code for internal Flash memory access library
  *
- *        The internal Flash memory access is described in the LPC1768 and LPC11U24 usermanual. 
+ *        The internal Flash memory access is described in the LPC1768 and LPC11U24 usermanual.
  *            http://www.nxp.com/documents/user_manual/UM10360.pdf
  *            http://www.nxp.com/documents/user_manual/UM10462.pdf
  *
@@ -8,12 +8,12 @@
  *                    Chapter  2: "LPC17xx Memory map"
  *                    Chapter 32: "LPC17xx Flash memory interface and programming"
  *                    refering Rev. 01 - 4 January 2010
- * 
+ *
  *               LPC11U24 --
  *                    Chapter  2: "LPC11Uxx Memory mapping"
  *                    Chapter 20: "LPC11Uxx Flash programming firmware"
  *                    refering Rev. 03 - 16 July 2012
- * 
+ *
  *  This main.cpp demonstrates how the flash can be erased and wrote.
  *
  *  This program tries to...
@@ -24,12 +24,12 @@
  *    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. 
+ *  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 
+ *  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
@@ -40,6 +40,7 @@
  *        revision 2.0  26-Nov.2012   LPC11U24 code added
  *        revision 2.1  26-Nov-2012   EEPROM access code imported from Suga koubou san's (http://mbed.org/users/okini3939/) library
  *                                            http://mbed.org/users/okini3939/code/M0_EEPROM_test/
+ *        revision 3.0  09-Jan-2014   LPC812 and LPC824 support added
  */
 
 #include    "mbed.h"
@@ -52,6 +53,9 @@
 #elif defined(TARGET_LPC11U24)
 #define     TARGET_SECTOR    7      //  use sector  7 as target sector if it is on LPC11U24
 #define     TARGET_EEPROM_ADDRESS   64
+#define     TARGET_EEPROM_ADDRESS   64
+#elif defined(TARGET_LPC812) || defined(TARGET_LPC824)
+#define     TARGET_SECTOR    15      //  use sector  15 as target sector if it is on LPC812
 #endif
 
 void    memdump( char *p, int n );
@@ -60,11 +64,12 @@
 IAP     iap;
 
 
-int main() {
+int main()
+{
     char    mem[ MEM_SIZE ];    //  memory, it should be aligned to word boundary
     int     r;
 
-    printf( "IAP: Flash memory writing test\r\n" );
+    printf( "\r\n\r\n=== IAP: Flash memory writing test ===\r\n" );
     printf( "  device-ID = 0x%08X, serial# = 0x%08X, CPU running %dkHz\r\n", iap.read_ID(), iap.read_serial(), SystemCoreClock / 1000 );
     printf( "  user reserved flash area: start_address=0x%08X, size=%d bytes\r\n", iap.reserved_flash_area_start(), iap.reserved_flash_area_size() );
     printf( "  read_BootVer=0x%08X\r\r\n", iap.read_BootVer() );
@@ -78,13 +83,13 @@
     printf( "blank check result = 0x%08X\r\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\r\n", r );
     }
-    
+
     // copy RAM to Flash
 
     iap.prepare( TARGET_SECTOR, TARGET_SECTOR );
@@ -114,21 +119,34 @@
 
     printf( "showing the flash contents...\r\n" );
     memdump( sector_start_adress[ TARGET_SECTOR ], MEM_SIZE * 3 );
-    
+
+
+#if defined(TARGET_LPC812) || defined(TARGET_LPC824)
+    iap.prepare( TARGET_SECTOR, TARGET_SECTOR );
+    r   = iap.erase_page( 241, 241 );   //  241 is page number for sector 7 with 64 byte offset
+
+    printf( "\r\nerase page test\r\n" );
+    printf( "erase page result     = \"%s\"\r\n", r ? "FAILED" : "OK" );
+    printf( "showing memory dump to confirm 0x00003C40 to 0x00003C7F are erased (should be changed to 0xFF)" );
+
+    memdump( sector_start_adress[ TARGET_SECTOR ], MEM_SIZE );
+#endif
+
+
 #if defined(TARGET_LPC11U24)    //  SAMPLE OF EEPROM ACCESS (LPC11U24 only)
     printf( "IAP: EEPROM writing test\r\n" );
     char    mem2[ MEM_SIZE ];
 
     r   = iap.write_eeprom( mem, (char*)TARGET_EEPROM_ADDRESS, MEM_SIZE );
     printf( "copied: SRAM(0x%08X)->EEPROM(0x%08X) for %d bytes. (result=0x%08X)\r\n", mem, TARGET_EEPROM_ADDRESS, MEM_SIZE, r );
- 
+
     r   = iap.read_eeprom( (char*)TARGET_EEPROM_ADDRESS, mem2, MEM_SIZE );
     printf( "copied: EEPROM(0x%08X)->SRAM(0x%08X) for %d bytes. (result=0x%08X)\r\n", TARGET_EEPROM_ADDRESS, mem, MEM_SIZE, r );
- 
+
     // compare
     r = memcmp(mem, mem2, MEM_SIZE);
     printf( "compare result     = \"%s\"\r\n", r ? "FAILED" : "OK" );
- 
+
     printf( "showing the EEPROM contents...\r\n" );
     memdump( mem2, MEM_SIZE );
 #endif
@@ -136,7 +154,8 @@
 }
 
 
-void memdump( char *base, int n ) {
+void memdump( char *base, int n )
+{
     unsigned int    *p;
 
     printf( "  memdump from 0x%08X for %d bytes", (unsigned long)base, n );