Norimasa Okamoto / Mbed 2 deprecated STM32_IAP_internal_flash_write

Dependencies:   STM32_IAP mbed

Fork of IAP_internal_flash_write by Tedd OKANO

Committer:
va009039
Date:
Mon Apr 11 20:18:58 2016 +0900
Revision:
9:ae7747b119c1
Parent:
8:8273c1727a6a
Child:
10:f3e4dd858adb
add EEPROM test.

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 3:63a0993315e5 3 * The internal Flash memory access is described in the LPC1768 and LPC11U24 usermanual.
okano 0:b802bd2f4cc9 4 * http://www.nxp.com/documents/user_manual/UM10360.pdf
okano 1:a85b51eeb446 5 * http://www.nxp.com/documents/user_manual/UM10462.pdf
okano 0:b802bd2f4cc9 6 *
okano 1:a85b51eeb446 7 * LPC1768 --
okano 1:a85b51eeb446 8 * Chapter 2: "LPC17xx Memory map"
okano 1:a85b51eeb446 9 * Chapter 32: "LPC17xx Flash memory interface and programming"
okano 1:a85b51eeb446 10 * refering Rev. 01 - 4 January 2010
okano 3:63a0993315e5 11 *
okano 1:a85b51eeb446 12 * LPC11U24 --
okano 1:a85b51eeb446 13 * Chapter 2: "LPC11Uxx Memory mapping"
okano 1:a85b51eeb446 14 * Chapter 20: "LPC11Uxx Flash programming firmware"
okano 1:a85b51eeb446 15 * refering Rev. 03 - 16 July 2012
okano 3:63a0993315e5 16 *
okano 0:b802bd2f4cc9 17 * This main.cpp demonstrates how the flash can be erased and wrote.
okano 0:b802bd2f4cc9 18 *
okano 0:b802bd2f4cc9 19 * This program tries to...
okano 0:b802bd2f4cc9 20 * 0. read device ID and serial#
okano 0:b802bd2f4cc9 21 * 1. check if the targat sector blank
okano 0:b802bd2f4cc9 22 * 2. erase the sector if it was not blank
okano 0:b802bd2f4cc9 23 * 3. write into the flash (prepare before write)
okano 0:b802bd2f4cc9 24 * 4. verify the data by IAP command
okano 0:b802bd2f4cc9 25 * 5. show the content of the flash
okano 0:b802bd2f4cc9 26 *
okano 3:63a0993315e5 27 * The Flash must be erased as sectors. No overwrite can be done like SRAM.
okano 3:63a0993315e5 28 * So erase should be done in size of 4K or 32K.
okano 0:b802bd2f4cc9 29 *
okano 3:63a0993315e5 30 * Writing sector can be done with size of 256, 512, 1024 or 4096.
okano 3:63a0993315e5 31 * If other size is used, the IAP returns an error.
okano 3:63a0993315e5 32 * The SRAM memory should be allocated in
okano 0:b802bd2f4cc9 33 *
okano 0:b802bd2f4cc9 34 *
okano 1:a85b51eeb446 35 * Released under the MIT License: http://mbed.org/license/mit
okano 0:b802bd2f4cc9 36 *
okano 0:b802bd2f4cc9 37 * revision 1.0 09-Mar-2010 1st release
okano 0:b802bd2f4cc9 38 * revision 1.1 12-Mar-2010 chaged: to make possible to reserve flash area for user
okano 0:b802bd2f4cc9 39 * it can be set by USER_FLASH_AREA_START and USER_FLASH_AREA_SIZE in IAP.h
okano 1:a85b51eeb446 40 * revision 2.0 26-Nov.2012 LPC11U24 code added
okano 2:c22f0c87fee6 41 * revision 2.1 26-Nov-2012 EEPROM access code imported from Suga koubou san's (http://mbed.org/users/okini3939/) library
okano 2:c22f0c87fee6 42 * http://mbed.org/users/okini3939/code/M0_EEPROM_test/
okano 5:806960ca964e 43 * revision 3.0 09-Jan-2015 LPC812 and LPC824 support added
okano 5:806960ca964e 44 * revision 3.1 13-Jan-2015 LPC1114 support added
okano 5:806960ca964e 45 * revision 3.1.1 16-Jan-2015 Target MCU name changed for better compatibility across the platforms
okano 6:2357a04a16ff 46 * revision 3.1.2 10-Mar-2015 merged with pull requests. reinvoke_isp() added and modified read_serial() to return a pointer.
okano 0:b802bd2f4cc9 47 */
okano 0:b802bd2f4cc9 48
okano 0:b802bd2f4cc9 49 #include "mbed.h"
okano 0:b802bd2f4cc9 50 #include "IAP.h"
okano 0:b802bd2f4cc9 51
okano 0:b802bd2f4cc9 52 #define MEM_SIZE 256
okano 1:a85b51eeb446 53
va009039 8:8273c1727a6a 54 #if defined(TARGET_LPC176X)
okano 1:a85b51eeb446 55 #define TARGET_SECTOR 29 // use sector 29 as target sector if it is on LPC1768
okano 5:806960ca964e 56 #elif defined(TARGET_LPC11UXX) || defined(TARGET_LPC11XX)
okano 1:a85b51eeb446 57 #define TARGET_SECTOR 7 // use sector 7 as target sector if it is on LPC11U24
okano 2:c22f0c87fee6 58 #define TARGET_EEPROM_ADDRESS 64
okano 3:63a0993315e5 59 #define TARGET_EEPROM_ADDRESS 64
okano 5:806960ca964e 60 #elif defined(TARGET_LPC81X) || defined(TARGET_LPC82X)
okano 3:63a0993315e5 61 #define TARGET_SECTOR 15 // use sector 15 as target sector if it is on LPC812
va009039 8:8273c1727a6a 62
va009039 8:8273c1727a6a 63 #elif defined(TARGET_NUCLEO_L152RE)||defined(TARGET_NUCLEO_F042K6)||defined(TARGET_NUCLEO_F103RB)
va009039 8:8273c1727a6a 64 #define TARGET_SECTOR (0x7800/FLASH_PAGE_SIZE)
va009039 9:ae7747b119c1 65 #define TARGET_EEPROM_ADDRESS (FLASH_EEPROM_BASE + 0x200)
va009039 8:8273c1727a6a 66 struct {
va009039 8:8273c1727a6a 67 char* operator[](int sector) const {
va009039 8:8273c1727a6a 68 return reinterpret_cast<char*>(FLASH_BASE + sector * FLASH_PAGE_SIZE);
va009039 8:8273c1727a6a 69 }
va009039 8:8273c1727a6a 70 } sector_start_adress;
va009039 8:8273c1727a6a 71
va009039 8:8273c1727a6a 72 #elif defined(TARGET_NUCLEO_F401RE)||defined(TARGET_NUCLEO_F411RE)||defined(TARGET_NUCLEO_F446RE)
va009039 8:8273c1727a6a 73 #define TARGET_SECTOR 3
va009039 8:8273c1727a6a 74 struct {
va009039 8:8273c1727a6a 75 char* operator[](int sector) const {
va009039 8:8273c1727a6a 76 static const uint32_t offset[8] = {0x00000, 0x04000, 0x08000, 0xc000, 0x10000, 0x20000, 0x40000, 0x60000};
va009039 8:8273c1727a6a 77 return reinterpret_cast<char*>(FLASH_BASE + offset[sector]);
va009039 8:8273c1727a6a 78 }
va009039 8:8273c1727a6a 79 } sector_start_adress;
va009039 8:8273c1727a6a 80
okano 1:a85b51eeb446 81 #endif
okano 0:b802bd2f4cc9 82
okano 0:b802bd2f4cc9 83 void memdump( char *p, int n );
okano 0:b802bd2f4cc9 84 int isprint( int c );
okano 0:b802bd2f4cc9 85
okano 0:b802bd2f4cc9 86 IAP iap;
okano 0:b802bd2f4cc9 87
okano 0:b802bd2f4cc9 88
okano 3:63a0993315e5 89 int main()
okano 3:63a0993315e5 90 {
okano 0:b802bd2f4cc9 91 char mem[ MEM_SIZE ]; // memory, it should be aligned to word boundary
okano 6:2357a04a16ff 92 int *serial_number;
okano 0:b802bd2f4cc9 93 int r;
okano 0:b802bd2f4cc9 94
okano 3:63a0993315e5 95 printf( "\r\n\r\n=== IAP: Flash memory writing test ===\r\n" );
okano 6:2357a04a16ff 96 printf( " device-ID = 0x%08X\r\n", iap.read_ID() );
okano 6:2357a04a16ff 97
okano 6:2357a04a16ff 98 serial_number = iap.read_serial();
okano 6:2357a04a16ff 99
okano 6:2357a04a16ff 100 printf( " serial# =" );
okano 6:2357a04a16ff 101 for ( int i = 0; i < 4; i++ )
okano 6:2357a04a16ff 102 printf( " %08X", *(serial_number + i) );
okano 6:2357a04a16ff 103 printf( "\r\n" );
okano 6:2357a04a16ff 104
okano 6:2357a04a16ff 105 printf( " CPU running %dkHz\r\n", SystemCoreClock / 1000 );
okano 1:a85b51eeb446 106 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 107 printf( " read_BootVer=0x%08X\r\r\n", iap.read_BootVer() );
okano 0:b802bd2f4cc9 108
okano 0:b802bd2f4cc9 109 for ( int i = 0; i < MEM_SIZE; i++ )
okano 0:b802bd2f4cc9 110 mem[ i ] = i & 0xFF;
okano 0:b802bd2f4cc9 111
okano 0:b802bd2f4cc9 112 // blank check: The mbed will erase all flash contents after downloading new executable
okano 0:b802bd2f4cc9 113
okano 0:b802bd2f4cc9 114 r = iap.blank_check( TARGET_SECTOR, TARGET_SECTOR );
okano 1:a85b51eeb446 115 printf( "blank check result = 0x%08X\r\n", r );
okano 0:b802bd2f4cc9 116
okano 0:b802bd2f4cc9 117 // erase sector, if required
okano 3:63a0993315e5 118
okano 0:b802bd2f4cc9 119 if ( r == SECTOR_NOT_BLANK ) {
okano 0:b802bd2f4cc9 120 iap.prepare( TARGET_SECTOR, TARGET_SECTOR );
okano 0:b802bd2f4cc9 121 r = iap.erase( TARGET_SECTOR, TARGET_SECTOR );
okano 1:a85b51eeb446 122 printf( "erase result = 0x%08X\r\n", r );
okano 0:b802bd2f4cc9 123 }
okano 3:63a0993315e5 124
okano 0:b802bd2f4cc9 125 // copy RAM to Flash
okano 0:b802bd2f4cc9 126
okano 0:b802bd2f4cc9 127 iap.prepare( TARGET_SECTOR, TARGET_SECTOR );
okano 0:b802bd2f4cc9 128 r = iap.write( mem, sector_start_adress[ TARGET_SECTOR ], MEM_SIZE );
okano 1:a85b51eeb446 129 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 130
okano 0:b802bd2f4cc9 131 // compare
okano 0:b802bd2f4cc9 132
okano 0:b802bd2f4cc9 133 r = iap.compare( mem, sector_start_adress[ TARGET_SECTOR ], MEM_SIZE );
okano 1:a85b51eeb446 134 printf( "compare result = \"%s\"\r\n", r ? "FAILED" : "OK" );
okano 0:b802bd2f4cc9 135
okano 0:b802bd2f4cc9 136 //#define WRITE_NEXT_BLOCK
okano 0:b802bd2f4cc9 137 #ifdef WRITE_NEXT_BLOCK
okano 0:b802bd2f4cc9 138
okano 0:b802bd2f4cc9 139 // copy RAM to Flash
okano 0:b802bd2f4cc9 140
okano 0:b802bd2f4cc9 141 iap.prepare( TARGET_SECTOR, TARGET_SECTOR );
okano 0:b802bd2f4cc9 142 r = iap.write( mem, sector_start_adress[ TARGET_SECTOR ] + 256, MEM_SIZE );
okano 1:a85b51eeb446 143 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 144
okano 0:b802bd2f4cc9 145 // compare
okano 0:b802bd2f4cc9 146
okano 0:b802bd2f4cc9 147 r = iap.compare( mem, sector_start_adress[ TARGET_SECTOR ] + 256, MEM_SIZE );
okano 1:a85b51eeb446 148 printf( "compare result = \"%s\"\r\n", r ? "FAILED" : "OK" );
okano 0:b802bd2f4cc9 149
okano 0:b802bd2f4cc9 150 #endif
okano 0:b802bd2f4cc9 151
okano 1:a85b51eeb446 152 printf( "showing the flash contents...\r\n" );
okano 0:b802bd2f4cc9 153 memdump( sector_start_adress[ TARGET_SECTOR ], MEM_SIZE * 3 );
okano 3:63a0993315e5 154
okano 3:63a0993315e5 155
okano 5:806960ca964e 156 #if defined(TARGET_LPC81X) || defined(TARGET_LPC82X)
okano 3:63a0993315e5 157 iap.prepare( TARGET_SECTOR, TARGET_SECTOR );
okano 3:63a0993315e5 158 r = iap.erase_page( 241, 241 ); // 241 is page number for sector 7 with 64 byte offset
okano 3:63a0993315e5 159
okano 3:63a0993315e5 160 printf( "\r\nerase page test\r\n" );
okano 3:63a0993315e5 161 printf( "erase page result = \"%s\"\r\n", r ? "FAILED" : "OK" );
okano 3:63a0993315e5 162 printf( "showing memory dump to confirm 0x00003C40 to 0x00003C7F are erased (should be changed to 0xFF)" );
okano 3:63a0993315e5 163
okano 3:63a0993315e5 164 memdump( sector_start_adress[ TARGET_SECTOR ], MEM_SIZE );
okano 3:63a0993315e5 165 #endif
okano 3:63a0993315e5 166
okano 3:63a0993315e5 167
va009039 9:ae7747b119c1 168 #if defined(TARGET_LPC11UXX)||defined(TARGET_STM32L1) // SAMPLE OF EEPROM ACCESS (LPC11U24 only)
okano 2:c22f0c87fee6 169 printf( "IAP: EEPROM writing test\r\n" );
okano 2:c22f0c87fee6 170 char mem2[ MEM_SIZE ];
okano 2:c22f0c87fee6 171
okano 2:c22f0c87fee6 172 r = iap.write_eeprom( mem, (char*)TARGET_EEPROM_ADDRESS, MEM_SIZE );
okano 2:c22f0c87fee6 173 printf( "copied: SRAM(0x%08X)->EEPROM(0x%08X) for %d bytes. (result=0x%08X)\r\n", mem, TARGET_EEPROM_ADDRESS, MEM_SIZE, r );
okano 3:63a0993315e5 174
okano 2:c22f0c87fee6 175 r = iap.read_eeprom( (char*)TARGET_EEPROM_ADDRESS, mem2, MEM_SIZE );
okano 2:c22f0c87fee6 176 printf( "copied: EEPROM(0x%08X)->SRAM(0x%08X) for %d bytes. (result=0x%08X)\r\n", TARGET_EEPROM_ADDRESS, mem, MEM_SIZE, r );
okano 3:63a0993315e5 177
okano 2:c22f0c87fee6 178 // compare
okano 2:c22f0c87fee6 179 r = memcmp(mem, mem2, MEM_SIZE);
okano 2:c22f0c87fee6 180 printf( "compare result = \"%s\"\r\n", r ? "FAILED" : "OK" );
okano 3:63a0993315e5 181
okano 2:c22f0c87fee6 182 printf( "showing the EEPROM contents...\r\n" );
okano 2:c22f0c87fee6 183 memdump( mem2, MEM_SIZE );
okano 2:c22f0c87fee6 184 #endif
okano 2:c22f0c87fee6 185
okano 0:b802bd2f4cc9 186 }
okano 0:b802bd2f4cc9 187
okano 0:b802bd2f4cc9 188
okano 3:63a0993315e5 189 void memdump( char *base, int n )
okano 3:63a0993315e5 190 {
okano 0:b802bd2f4cc9 191 unsigned int *p;
okano 0:b802bd2f4cc9 192
okano 0:b802bd2f4cc9 193 printf( " memdump from 0x%08X for %d bytes", (unsigned long)base, n );
okano 0:b802bd2f4cc9 194
okano 0:b802bd2f4cc9 195 p = (unsigned int *)((unsigned int)base & ~(unsigned int)0x3);
okano 0:b802bd2f4cc9 196
okano 0:b802bd2f4cc9 197 for ( int i = 0; i < (n >> 2); i++, p++ ) {
okano 0:b802bd2f4cc9 198 if ( !(i % 4) )
okano 1:a85b51eeb446 199 printf( "\r\n 0x%08X :", (unsigned int)p );
okano 0:b802bd2f4cc9 200
okano 0:b802bd2f4cc9 201 printf( " 0x%08X", *p );
okano 0:b802bd2f4cc9 202 }
okano 0:b802bd2f4cc9 203
okano 1:a85b51eeb446 204 printf( "\r\n" );
okano 0:b802bd2f4cc9 205 }