LPC812 sector size flash write sample code

Dependencies:   IAP mbed

Fork of IAP_internal_flash_write by Tedd OKANO

Committer:
okano
Date:
Wed Aug 15 05:33:27 2018 +0000
Revision:
9:d5a8fd977fbc
Parent:
8:2592da51b781
fixed: string in last line was not printed in last "memdump" function.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okano 7:5ee0af975589 1 /**
okano 7:5ee0af975589 2 * ISP sector size writing demo
okano 0:b802bd2f4cc9 3 */
okano 0:b802bd2f4cc9 4
okano 0:b802bd2f4cc9 5 #include "mbed.h"
okano 0:b802bd2f4cc9 6 #include "IAP.h"
okano 0:b802bd2f4cc9 7
okano 7:5ee0af975589 8 #define MEM_SIZE 1024
okano 1:a85b51eeb446 9
okano 7:5ee0af975589 10 IAP iap;
okano 0:b802bd2f4cc9 11
okano 0:b802bd2f4cc9 12 void memdump( char *p, int n );
okano 0:b802bd2f4cc9 13
okano 0:b802bd2f4cc9 14
okano 3:63a0993315e5 15 int main()
okano 3:63a0993315e5 16 {
okano 0:b802bd2f4cc9 17 char mem[ MEM_SIZE ]; // memory, it should be aligned to word boundary
okano 6:2357a04a16ff 18 int *serial_number;
okano 0:b802bd2f4cc9 19 int r;
okano 0:b802bd2f4cc9 20
okano 3:63a0993315e5 21 printf( "\r\n\r\n=== IAP: Flash memory writing test ===\r\n" );
okano 6:2357a04a16ff 22 printf( " device-ID = 0x%08X\r\n", iap.read_ID() );
okano 6:2357a04a16ff 23
okano 6:2357a04a16ff 24 serial_number = iap.read_serial();
okano 6:2357a04a16ff 25
okano 6:2357a04a16ff 26 printf( " serial# =" );
okano 6:2357a04a16ff 27 for ( int i = 0; i < 4; i++ )
okano 6:2357a04a16ff 28 printf( " %08X", *(serial_number + i) );
okano 6:2357a04a16ff 29 printf( "\r\n" );
okano 6:2357a04a16ff 30
okano 6:2357a04a16ff 31 printf( " CPU running %dkHz\r\n", SystemCoreClock / 1000 );
okano 1:a85b51eeb446 32 printf( " user reserved flash area: start_address=0x%08X, size=%d bytes\r\n", iap.reserved_flash_area_start(), iap.reserved_flash_area_size() );
okano 1:a85b51eeb446 33 printf( " read_BootVer=0x%08X\r\r\n", iap.read_BootVer() );
okano 0:b802bd2f4cc9 34
okano 0:b802bd2f4cc9 35
okano 0:b802bd2f4cc9 36 // blank check: The mbed will erase all flash contents after downloading new executable
okano 0:b802bd2f4cc9 37
okano 7:5ee0af975589 38 r = iap.blank_check( 12, 15 );
okano 1:a85b51eeb446 39 printf( "blank check result = 0x%08X\r\n", r );
okano 0:b802bd2f4cc9 40
okano 0:b802bd2f4cc9 41 // erase sector, if required
okano 3:63a0993315e5 42
okano 0:b802bd2f4cc9 43 if ( r == SECTOR_NOT_BLANK ) {
okano 7:5ee0af975589 44 iap.prepare( 12, 15 );
okano 7:5ee0af975589 45 r = iap.erase( 12, 15 );
okano 1:a85b51eeb446 46 printf( "erase result = 0x%08X\r\n", r );
okano 0:b802bd2f4cc9 47 }
okano 3:63a0993315e5 48
okano 0:b802bd2f4cc9 49 // copy RAM to Flash
okano 0:b802bd2f4cc9 50
okano 7:5ee0af975589 51 for ( int target_sector = 12; target_sector < 16; target_sector++ ) {
okano 7:5ee0af975589 52 for ( int i = 0; i < MEM_SIZE; i += 16 )
okano 7:5ee0af975589 53 sprintf( mem + i, "sctr%02d Osft=%04d", target_sector, i );
okano 0:b802bd2f4cc9 54
okano 7:5ee0af975589 55 iap.prepare( target_sector, target_sector );
okano 7:5ee0af975589 56 r = iap.write( mem, sector_start_adress[ target_sector ], MEM_SIZE );
okano 7:5ee0af975589 57 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 );
okano 0:b802bd2f4cc9 58
okano 7:5ee0af975589 59 // compare
okano 3:63a0993315e5 60
okano 7:5ee0af975589 61 r = iap.compare( mem, sector_start_adress[ target_sector ], MEM_SIZE );
okano 7:5ee0af975589 62 printf( "compare result = \"%s\"\r\n", r ? "FAILED" : "OK" );
okano 7:5ee0af975589 63 }
okano 3:63a0993315e5 64
okano 7:5ee0af975589 65 for ( int target_sector = 12; target_sector < 16; target_sector++ ) {
okano 7:5ee0af975589 66 printf( "showing the flash contents (sector %02d)...\r\n", target_sector );
okano 7:5ee0af975589 67 memdump( sector_start_adress[ target_sector ], MEM_SIZE );
okano 7:5ee0af975589 68 }
okano 3:63a0993315e5 69
okano 7:5ee0af975589 70 printf( "Done! (=== IAP: Flash memory writing test ===)\r\n\r\n" );
okano 0:b802bd2f4cc9 71 }
okano 0:b802bd2f4cc9 72
okano 7:5ee0af975589 73 #include <ctype.h>
okano 0:b802bd2f4cc9 74
okano 3:63a0993315e5 75 void memdump( char *base, int n )
okano 3:63a0993315e5 76 {
okano 0:b802bd2f4cc9 77 unsigned int *p;
okano 7:5ee0af975589 78 char s[17] = { '\x0' };
okano 0:b802bd2f4cc9 79
okano 0:b802bd2f4cc9 80 printf( " memdump from 0x%08X for %d bytes", (unsigned long)base, n );
okano 0:b802bd2f4cc9 81
okano 0:b802bd2f4cc9 82 p = (unsigned int *)((unsigned int)base & ~(unsigned int)0x3);
okano 0:b802bd2f4cc9 83
okano 0:b802bd2f4cc9 84 for ( int i = 0; i < (n >> 2); i++, p++ ) {
okano 7:5ee0af975589 85 if ( !(i % 4) ) {
okano 7:5ee0af975589 86 printf( " : %s\r\n 0x%08X :", s, (unsigned int)p );
okano 7:5ee0af975589 87
okano 7:5ee0af975589 88 for ( int j = 0; j < 16; j++)
okano 7:5ee0af975589 89 s[ j ] = isgraph( (int)(*((char *)p + j)) ) ? (*((char *)p + j)) : ' ';
okano 7:5ee0af975589 90 }
okano 0:b802bd2f4cc9 91
okano 0:b802bd2f4cc9 92 printf( " 0x%08X", *p );
okano 0:b802bd2f4cc9 93 }
okano 0:b802bd2f4cc9 94
okano 9:d5a8fd977fbc 95 printf( " : %s\r\n", s );
okano 0:b802bd2f4cc9 96 }
okano 7:5ee0af975589 97
okano 7:5ee0af975589 98
okano 7:5ee0af975589 99