LPC812 sector size flash write sample code

Dependencies:   IAP mbed

Fork of IAP_internal_flash_write by Tedd OKANO

main.cpp

Committer:
okano
Date:
2018-08-15
Revision:
9:d5a8fd977fbc
Parent:
8:2592da51b781

File content as of revision 9:d5a8fd977fbc:

/**
 *  ISP sector size writing demo
 */

#include    "mbed.h"
#include    "IAP.h"

#define     MEM_SIZE        1024

IAP     iap;

void    memdump( char *p, int n );


int main()
{
    char    mem[ MEM_SIZE ];    //  memory, it should be aligned to word boundary
    int     *serial_number;
    int     r;

    printf( "\r\n\r\n=== IAP: Flash memory writing test ===\r\n" );
    printf( "  device-ID = 0x%08X\r\n", iap.read_ID() );

    serial_number = iap.read_serial();

    printf( "  serial# =" );
    for ( int i = 0; i < 4; i++ )
        printf( " %08X", *(serial_number + i) );
    printf( "\r\n" );

    printf( "  CPU running %dkHz\r\n", 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() );


    //  blank check: The mbed will erase all flash contents after downloading new executable

    r   = iap.blank_check( 12, 15 );
    printf( "blank check result = 0x%08X\r\n", r );

    //  erase sector, if required

    if ( r == SECTOR_NOT_BLANK ) {
        iap.prepare(  12, 15  );
        r   = iap.erase(  12, 15  );
        printf( "erase result       = 0x%08X\r\n", r );
    }

    // copy RAM to Flash

    for ( int target_sector = 12; target_sector < 16; target_sector++ ) {
        for ( int i = 0; i < MEM_SIZE; i += 16 )
            sprintf( mem + i, "sctr%02d Osft=%04d", target_sector, i );

        iap.prepare( target_sector, target_sector );
        r   = iap.write( mem, sector_start_adress[ target_sector ], MEM_SIZE );
        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 );

        // compare

        r   = iap.compare( mem, sector_start_adress[ target_sector ], MEM_SIZE );
        printf( "compare result     = \"%s\"\r\n", r ? "FAILED" : "OK" );
    }

    for ( int target_sector = 12; target_sector < 16; target_sector++ ) {
        printf( "showing the flash contents (sector %02d)...\r\n", target_sector );
        memdump( sector_start_adress[ target_sector ], MEM_SIZE );
    }

    printf( "Done! (=== IAP: Flash memory writing test ===)\r\n\r\n" );
}

#include    <ctype.h>

void memdump( char *base, int n )
{
    unsigned int    *p;
    char            s[17]   = { '\x0' };

    printf( "  memdump from 0x%08X for %d bytes", (unsigned long)base, n );

    p   = (unsigned int *)((unsigned int)base & ~(unsigned int)0x3);

    for ( int i = 0; i < (n >> 2); i++, p++ ) {
        if ( !(i % 4) ) {
            printf( " : %s\r\n  0x%08X :", s, (unsigned int)p );

            for ( int j = 0; j < 16; j++)
                s[ j ]  = isgraph( (int)(*((char *)p + j)) ) ? (*((char *)p + j)) : ' ';
        }

        printf( " 0x%08X", *p );
    }

    printf( " : %s\r\n", s );
}