sample of erasing sector 7 on LPC1768

Dependencies:   IAP mbed

Fork of IAP_internal_flash_write by Tedd OKANO

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include    "mbed.h"
00002 #include    "IAP.h"
00003 
00004 #define     MEM_SIZE        256
00005 
00006 #define     TARGET_SECTOR    7      //  use sector 7 as target sector if it is on LPC1768
00007 
00008 void    memdump( char *p, int n );
00009 int     isprint( int c );
00010 
00011 IAP     iap;
00012 
00013 void write_dummy_data( void );
00014 
00015 
00016 main()
00017 {
00018     int     r;
00019     
00020     //  Write dummy data into Flash sector7
00021     write_dummy_data();
00022 
00023     printf( "\r\n==== dummy data written ====\r\n" );
00024 
00025 
00026     printf( "\r\nShowing flash contents of sector %d\r\n", TARGET_SECTOR );
00027     memdump( sector_start_adress[ TARGET_SECTOR ], MEM_SIZE );
00028 
00029 
00030     //  Try blank check
00031     r   = iap.blank_check( TARGET_SECTOR, TARGET_SECTOR );
00032     printf( "Try blank check ==> blank check result = 0x%08X\r\n", r );
00033     
00034     //  Try erase
00035     iap.prepare( TARGET_SECTOR, TARGET_SECTOR );
00036     r   = iap.erase( TARGET_SECTOR, TARGET_SECTOR );
00037     printf( "Try erase       ==> erase result       = 0x%08X\r\n", r );
00038     
00039     printf( "\r\nShowing flash contents of sector %d\r\n", TARGET_SECTOR );
00040     memdump( sector_start_adress[ TARGET_SECTOR ], MEM_SIZE );
00041 
00042 }
00043 
00044 void write_dummy_data( void )
00045 {
00046     char    mem[ MEM_SIZE ];    //  memory, it should be aligned to word boundary
00047     int     r;
00048 
00049     printf( "\r\n\r\n=== IAP: Flash memory writing test ===\r\n" );
00050     printf( "  device-ID = 0x%08X\r\n", iap.read_ID() );
00051 
00052     printf( "  CPU running %dkHz\r\n", SystemCoreClock / 1000 );
00053     printf( "  user reserved flash area: start_address=0x%08X, size=%d bytes\r\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\r\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\r\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)\r\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\"\r\n", r ? "FAILED" : "OK" );
00081 }
00082 
00083 
00084 void memdump( char *base, int n )
00085 {
00086     unsigned int    *p;
00087 
00088     printf( "  memdump from 0x%08X for %d bytes", (unsigned long)base, n );
00089 
00090     p   = (unsigned int *)((unsigned int)base & ~(unsigned int)0x3);
00091 
00092     for ( int i = 0; i < (n >> 2); i++, p++ ) {
00093         if ( !(i % 4) )
00094             printf( "\r\n  0x%08X :", (unsigned int)p );
00095 
00096         printf( " 0x%08X", *p );
00097     }
00098 
00099     printf( "\r\n" );
00100 }