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/

Dependencies:   mbed

Committer:
tecnosys
Date:
Mon Jul 11 01:53:18 2011 +0000
Revision:
0:406ffaf4d93c

        

Who changed what in which revision?

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