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/

Committer:
okano
Date:
Fri Mar 12 10:24:57 2010 +0000
Revision:
0:b802bd2f4cc9
Child:
1:a85b51eeb446

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okano 0:b802bd2f4cc9 1 /** IAP demo : demo code for internal Flash memory access library
okano 0:b802bd2f4cc9 2 *
okano 0:b802bd2f4cc9 3 * The internal Flash memory access is described in the LPC1768 usermanual.
okano 0:b802bd2f4cc9 4 * http://www.nxp.com/documents/user_manual/UM10360.pdf
okano 0:b802bd2f4cc9 5 *
okano 0:b802bd2f4cc9 6 * Chapter 2: "LPC17xx Memory map"
okano 0:b802bd2f4cc9 7 * Chapter 32: "LPC17xx Flash memory interface and programming"
okano 0:b802bd2f4cc9 8 * refering Rev. 01 - 4 January 2010
okano 0:b802bd2f4cc9 9 *
okano 0:b802bd2f4cc9 10 * This main.cpp demonstrates how the flash can be erased and wrote.
okano 0:b802bd2f4cc9 11 *
okano 0:b802bd2f4cc9 12 * This program tries to...
okano 0:b802bd2f4cc9 13 * 0. read device ID and serial#
okano 0:b802bd2f4cc9 14 * 1. check if the targat sector blank
okano 0:b802bd2f4cc9 15 * 2. erase the sector if it was not blank
okano 0:b802bd2f4cc9 16 * 3. write into the flash (prepare before write)
okano 0:b802bd2f4cc9 17 * 4. verify the data by IAP command
okano 0:b802bd2f4cc9 18 * 5. show the content of the flash
okano 0:b802bd2f4cc9 19 *
okano 0:b802bd2f4cc9 20 * The Flash must be erased as sectors. No overwrite can be done like SRAM.
okano 0:b802bd2f4cc9 21 * So erase should be done in size of 4K or 32K.
okano 0:b802bd2f4cc9 22 *
okano 0:b802bd2f4cc9 23 * Writing sector can be done with size of 256, 512, 1024 or 4096.
okano 0:b802bd2f4cc9 24 * If other size is used, the IAP returns an error.
okano 0:b802bd2f4cc9 25 * The SRAM memory should be allocated in
okano 0:b802bd2f4cc9 26 *
okano 0:b802bd2f4cc9 27 *
okano 0:b802bd2f4cc9 28 * Released under the MIT License: http://mbed.org/license/mit
okano 0:b802bd2f4cc9 29 *
okano 0:b802bd2f4cc9 30 * revision 1.0 09-Mar-2010 1st release
okano 0:b802bd2f4cc9 31 * revision 1.1 12-Mar-2010 chaged: to make possible to reserve flash area for user
okano 0:b802bd2f4cc9 32 * it can be set by USER_FLASH_AREA_START and USER_FLASH_AREA_SIZE in IAP.h
okano 0:b802bd2f4cc9 33 */
okano 0:b802bd2f4cc9 34
okano 0:b802bd2f4cc9 35 #include "mbed.h"
okano 0:b802bd2f4cc9 36 #include "IAP.h"
okano 0:b802bd2f4cc9 37
okano 0:b802bd2f4cc9 38 #define MEM_SIZE 256
okano 0:b802bd2f4cc9 39 #define TARGET_SECTOR 29
okano 0:b802bd2f4cc9 40
okano 0:b802bd2f4cc9 41 void memdump( char *p, int n );
okano 0:b802bd2f4cc9 42 int isprint( int c );
okano 0:b802bd2f4cc9 43
okano 0:b802bd2f4cc9 44 IAP iap;
okano 0:b802bd2f4cc9 45
okano 0:b802bd2f4cc9 46
okano 0:b802bd2f4cc9 47 int main() {
okano 0:b802bd2f4cc9 48 char mem[ MEM_SIZE ]; // memory, it should be aligned to word boundary
okano 0:b802bd2f4cc9 49 int r;
okano 0:b802bd2f4cc9 50
okano 0:b802bd2f4cc9 51 printf( "IAP: Flash memory writing test\n" );
okano 0:b802bd2f4cc9 52 printf( " device-ID = 0x%08X, serial# = 0x%08X, CPU running %dkHz\n", iap.read_ID(), iap.read_serial(), SystemCoreClock / 1000 );
okano 0:b802bd2f4cc9 53 printf( " user reserved flash area: start_address=0x%08X, size=%d bytes\n", iap.reserved_flash_area_start(), iap.reserved_flash_area_size() );
okano 0:b802bd2f4cc9 54
okano 0:b802bd2f4cc9 55 for ( int i = 0; i < MEM_SIZE; i++ )
okano 0:b802bd2f4cc9 56 mem[ i ] = i & 0xFF;
okano 0:b802bd2f4cc9 57
okano 0:b802bd2f4cc9 58 // blank check: The mbed will erase all flash contents after downloading new executable
okano 0:b802bd2f4cc9 59
okano 0:b802bd2f4cc9 60 r = iap.blank_check( TARGET_SECTOR, TARGET_SECTOR );
okano 0:b802bd2f4cc9 61 printf( "blank check result = 0x%08X\n", r );
okano 0:b802bd2f4cc9 62
okano 0:b802bd2f4cc9 63 // erase sector, if required
okano 0:b802bd2f4cc9 64
okano 0:b802bd2f4cc9 65 if ( r == SECTOR_NOT_BLANK ) {
okano 0:b802bd2f4cc9 66 iap.prepare( TARGET_SECTOR, TARGET_SECTOR );
okano 0:b802bd2f4cc9 67 r = iap.erase( TARGET_SECTOR, TARGET_SECTOR );
okano 0:b802bd2f4cc9 68 printf( "erase result = 0x%08X\n", r );
okano 0:b802bd2f4cc9 69 }
okano 0:b802bd2f4cc9 70
okano 0:b802bd2f4cc9 71 // copy RAM to Flash
okano 0:b802bd2f4cc9 72
okano 0:b802bd2f4cc9 73 iap.prepare( TARGET_SECTOR, TARGET_SECTOR );
okano 0:b802bd2f4cc9 74 r = iap.write( mem, sector_start_adress[ TARGET_SECTOR ], MEM_SIZE );
okano 0:b802bd2f4cc9 75 printf( "copied: SRAM(0x%08X)->Flash(0x%08X) for %d bytes. (result=0x%08X)\n", mem, sector_start_adress[ TARGET_SECTOR ], MEM_SIZE, r );
okano 0:b802bd2f4cc9 76
okano 0:b802bd2f4cc9 77 // compare
okano 0:b802bd2f4cc9 78
okano 0:b802bd2f4cc9 79 r = iap.compare( mem, sector_start_adress[ TARGET_SECTOR ], MEM_SIZE );
okano 0:b802bd2f4cc9 80 printf( "compare result = \"%s\"\n", r ? "FAILED" : "OK" );
okano 0:b802bd2f4cc9 81
okano 0:b802bd2f4cc9 82 //#define WRITE_NEXT_BLOCK
okano 0:b802bd2f4cc9 83 #ifdef WRITE_NEXT_BLOCK
okano 0:b802bd2f4cc9 84
okano 0:b802bd2f4cc9 85 // copy RAM to Flash
okano 0:b802bd2f4cc9 86
okano 0:b802bd2f4cc9 87 iap.prepare( TARGET_SECTOR, TARGET_SECTOR );
okano 0:b802bd2f4cc9 88 r = iap.write( mem, sector_start_adress[ TARGET_SECTOR ] + 256, MEM_SIZE );
okano 0:b802bd2f4cc9 89 printf( "copied: SRAM(0x%08X)->Flash(0x%08X) for %d bytes. (result=0x%08X)\n", mem, sector_start_adress[ TARGET_SECTOR ], MEM_SIZE, r );
okano 0:b802bd2f4cc9 90
okano 0:b802bd2f4cc9 91 // compare
okano 0:b802bd2f4cc9 92
okano 0:b802bd2f4cc9 93 r = iap.compare( mem, sector_start_adress[ TARGET_SECTOR ] + 256, MEM_SIZE );
okano 0:b802bd2f4cc9 94 printf( "compare result = \"%s\"\n", r ? "FAILED" : "OK" );
okano 0:b802bd2f4cc9 95
okano 0:b802bd2f4cc9 96 #endif
okano 0:b802bd2f4cc9 97
okano 0:b802bd2f4cc9 98 printf( "showing the flash contents...\n" );
okano 0:b802bd2f4cc9 99 memdump( sector_start_adress[ TARGET_SECTOR ], MEM_SIZE * 3 );
okano 0:b802bd2f4cc9 100 }
okano 0:b802bd2f4cc9 101
okano 0:b802bd2f4cc9 102
okano 0:b802bd2f4cc9 103 void memdump( char *base, int n ) {
okano 0:b802bd2f4cc9 104 unsigned int *p;
okano 0:b802bd2f4cc9 105
okano 0:b802bd2f4cc9 106 printf( " memdump from 0x%08X for %d bytes", (unsigned long)base, n );
okano 0:b802bd2f4cc9 107
okano 0:b802bd2f4cc9 108 p = (unsigned int *)((unsigned int)base & ~(unsigned int)0x3);
okano 0:b802bd2f4cc9 109
okano 0:b802bd2f4cc9 110 for ( int i = 0; i < (n >> 2); i++, p++ ) {
okano 0:b802bd2f4cc9 111 if ( !(i % 4) )
okano 0:b802bd2f4cc9 112 printf( "\n 0x%08X :", (unsigned int)p );
okano 0:b802bd2f4cc9 113
okano 0:b802bd2f4cc9 114 printf( " 0x%08X", *p );
okano 0:b802bd2f4cc9 115 }
okano 0:b802bd2f4cc9 116
okano 0:b802bd2f4cc9 117 printf( "\n" );
okano 0:b802bd2f4cc9 118 }