LPC812 sector size flash write sample code
Fork of IAP_internal_flash_write by
main.cpp@3:63a0993315e5, 2015-01-09 (annotated)
- Committer:
- okano
- Date:
- Fri Jan 09 06:37:02 2015 +0000
- Revision:
- 3:63a0993315e5
- Parent:
- 2:c22f0c87fee6
- Child:
- 4:5705a494169a
ver3 : LPC812, LPC824 support
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:63a0993315e5 | 43 | * revision 3.0 09-Jan-2014 LPC812 and LPC824 support added |
okano | 0:b802bd2f4cc9 | 44 | */ |
okano | 0:b802bd2f4cc9 | 45 | |
okano | 0:b802bd2f4cc9 | 46 | #include "mbed.h" |
okano | 0:b802bd2f4cc9 | 47 | #include "IAP.h" |
okano | 0:b802bd2f4cc9 | 48 | |
okano | 0:b802bd2f4cc9 | 49 | #define MEM_SIZE 256 |
okano | 1:a85b51eeb446 | 50 | |
okano | 1:a85b51eeb446 | 51 | #if defined(TARGET_LPC1768) |
okano | 1:a85b51eeb446 | 52 | #define TARGET_SECTOR 29 // use sector 29 as target sector if it is on LPC1768 |
okano | 1:a85b51eeb446 | 53 | #elif defined(TARGET_LPC11U24) |
okano | 1:a85b51eeb446 | 54 | #define TARGET_SECTOR 7 // use sector 7 as target sector if it is on LPC11U24 |
okano | 2:c22f0c87fee6 | 55 | #define TARGET_EEPROM_ADDRESS 64 |
okano | 3:63a0993315e5 | 56 | #define TARGET_EEPROM_ADDRESS 64 |
okano | 3:63a0993315e5 | 57 | #elif defined(TARGET_LPC812) || defined(TARGET_LPC824) |
okano | 3:63a0993315e5 | 58 | #define TARGET_SECTOR 15 // use sector 15 as target sector if it is on LPC812 |
okano | 1:a85b51eeb446 | 59 | #endif |
okano | 0:b802bd2f4cc9 | 60 | |
okano | 0:b802bd2f4cc9 | 61 | void memdump( char *p, int n ); |
okano | 0:b802bd2f4cc9 | 62 | int isprint( int c ); |
okano | 0:b802bd2f4cc9 | 63 | |
okano | 0:b802bd2f4cc9 | 64 | IAP iap; |
okano | 0:b802bd2f4cc9 | 65 | |
okano | 0:b802bd2f4cc9 | 66 | |
okano | 3:63a0993315e5 | 67 | int main() |
okano | 3:63a0993315e5 | 68 | { |
okano | 0:b802bd2f4cc9 | 69 | char mem[ MEM_SIZE ]; // memory, it should be aligned to word boundary |
okano | 0:b802bd2f4cc9 | 70 | int r; |
okano | 0:b802bd2f4cc9 | 71 | |
okano | 3:63a0993315e5 | 72 | printf( "\r\n\r\n=== IAP: Flash memory writing test ===\r\n" ); |
okano | 1:a85b51eeb446 | 73 | printf( " device-ID = 0x%08X, serial# = 0x%08X, CPU running %dkHz\r\n", iap.read_ID(), iap.read_serial(), SystemCoreClock / 1000 ); |
okano | 1:a85b51eeb446 | 74 | 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 | 75 | printf( " read_BootVer=0x%08X\r\r\n", iap.read_BootVer() ); |
okano | 0:b802bd2f4cc9 | 76 | |
okano | 0:b802bd2f4cc9 | 77 | for ( int i = 0; i < MEM_SIZE; i++ ) |
okano | 0:b802bd2f4cc9 | 78 | mem[ i ] = i & 0xFF; |
okano | 0:b802bd2f4cc9 | 79 | |
okano | 0:b802bd2f4cc9 | 80 | // blank check: The mbed will erase all flash contents after downloading new executable |
okano | 0:b802bd2f4cc9 | 81 | |
okano | 0:b802bd2f4cc9 | 82 | r = iap.blank_check( TARGET_SECTOR, TARGET_SECTOR ); |
okano | 1:a85b51eeb446 | 83 | printf( "blank check result = 0x%08X\r\n", r ); |
okano | 0:b802bd2f4cc9 | 84 | |
okano | 0:b802bd2f4cc9 | 85 | // erase sector, if required |
okano | 3:63a0993315e5 | 86 | |
okano | 0:b802bd2f4cc9 | 87 | if ( r == SECTOR_NOT_BLANK ) { |
okano | 0:b802bd2f4cc9 | 88 | iap.prepare( TARGET_SECTOR, TARGET_SECTOR ); |
okano | 0:b802bd2f4cc9 | 89 | r = iap.erase( TARGET_SECTOR, TARGET_SECTOR ); |
okano | 1:a85b51eeb446 | 90 | printf( "erase result = 0x%08X\r\n", r ); |
okano | 0:b802bd2f4cc9 | 91 | } |
okano | 3:63a0993315e5 | 92 | |
okano | 0:b802bd2f4cc9 | 93 | // copy RAM to Flash |
okano | 0:b802bd2f4cc9 | 94 | |
okano | 0:b802bd2f4cc9 | 95 | iap.prepare( TARGET_SECTOR, TARGET_SECTOR ); |
okano | 0:b802bd2f4cc9 | 96 | r = iap.write( mem, sector_start_adress[ TARGET_SECTOR ], MEM_SIZE ); |
okano | 1:a85b51eeb446 | 97 | 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 | 98 | |
okano | 0:b802bd2f4cc9 | 99 | // compare |
okano | 0:b802bd2f4cc9 | 100 | |
okano | 0:b802bd2f4cc9 | 101 | r = iap.compare( mem, sector_start_adress[ TARGET_SECTOR ], MEM_SIZE ); |
okano | 1:a85b51eeb446 | 102 | printf( "compare result = \"%s\"\r\n", r ? "FAILED" : "OK" ); |
okano | 0:b802bd2f4cc9 | 103 | |
okano | 0:b802bd2f4cc9 | 104 | //#define WRITE_NEXT_BLOCK |
okano | 0:b802bd2f4cc9 | 105 | #ifdef WRITE_NEXT_BLOCK |
okano | 0:b802bd2f4cc9 | 106 | |
okano | 0:b802bd2f4cc9 | 107 | // copy RAM to Flash |
okano | 0:b802bd2f4cc9 | 108 | |
okano | 0:b802bd2f4cc9 | 109 | iap.prepare( TARGET_SECTOR, TARGET_SECTOR ); |
okano | 0:b802bd2f4cc9 | 110 | r = iap.write( mem, sector_start_adress[ TARGET_SECTOR ] + 256, MEM_SIZE ); |
okano | 1:a85b51eeb446 | 111 | 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 | 112 | |
okano | 0:b802bd2f4cc9 | 113 | // compare |
okano | 0:b802bd2f4cc9 | 114 | |
okano | 0:b802bd2f4cc9 | 115 | r = iap.compare( mem, sector_start_adress[ TARGET_SECTOR ] + 256, MEM_SIZE ); |
okano | 1:a85b51eeb446 | 116 | printf( "compare result = \"%s\"\r\n", r ? "FAILED" : "OK" ); |
okano | 0:b802bd2f4cc9 | 117 | |
okano | 0:b802bd2f4cc9 | 118 | #endif |
okano | 0:b802bd2f4cc9 | 119 | |
okano | 1:a85b51eeb446 | 120 | printf( "showing the flash contents...\r\n" ); |
okano | 0:b802bd2f4cc9 | 121 | memdump( sector_start_adress[ TARGET_SECTOR ], MEM_SIZE * 3 ); |
okano | 3:63a0993315e5 | 122 | |
okano | 3:63a0993315e5 | 123 | |
okano | 3:63a0993315e5 | 124 | #if defined(TARGET_LPC812) || defined(TARGET_LPC824) |
okano | 3:63a0993315e5 | 125 | iap.prepare( TARGET_SECTOR, TARGET_SECTOR ); |
okano | 3:63a0993315e5 | 126 | r = iap.erase_page( 241, 241 ); // 241 is page number for sector 7 with 64 byte offset |
okano | 3:63a0993315e5 | 127 | |
okano | 3:63a0993315e5 | 128 | printf( "\r\nerase page test\r\n" ); |
okano | 3:63a0993315e5 | 129 | printf( "erase page result = \"%s\"\r\n", r ? "FAILED" : "OK" ); |
okano | 3:63a0993315e5 | 130 | printf( "showing memory dump to confirm 0x00003C40 to 0x00003C7F are erased (should be changed to 0xFF)" ); |
okano | 3:63a0993315e5 | 131 | |
okano | 3:63a0993315e5 | 132 | memdump( sector_start_adress[ TARGET_SECTOR ], MEM_SIZE ); |
okano | 3:63a0993315e5 | 133 | #endif |
okano | 3:63a0993315e5 | 134 | |
okano | 3:63a0993315e5 | 135 | |
okano | 2:c22f0c87fee6 | 136 | #if defined(TARGET_LPC11U24) // SAMPLE OF EEPROM ACCESS (LPC11U24 only) |
okano | 2:c22f0c87fee6 | 137 | printf( "IAP: EEPROM writing test\r\n" ); |
okano | 2:c22f0c87fee6 | 138 | char mem2[ MEM_SIZE ]; |
okano | 2:c22f0c87fee6 | 139 | |
okano | 2:c22f0c87fee6 | 140 | r = iap.write_eeprom( mem, (char*)TARGET_EEPROM_ADDRESS, MEM_SIZE ); |
okano | 2:c22f0c87fee6 | 141 | 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 | 142 | |
okano | 2:c22f0c87fee6 | 143 | r = iap.read_eeprom( (char*)TARGET_EEPROM_ADDRESS, mem2, MEM_SIZE ); |
okano | 2:c22f0c87fee6 | 144 | 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 | 145 | |
okano | 2:c22f0c87fee6 | 146 | // compare |
okano | 2:c22f0c87fee6 | 147 | r = memcmp(mem, mem2, MEM_SIZE); |
okano | 2:c22f0c87fee6 | 148 | printf( "compare result = \"%s\"\r\n", r ? "FAILED" : "OK" ); |
okano | 3:63a0993315e5 | 149 | |
okano | 2:c22f0c87fee6 | 150 | printf( "showing the EEPROM contents...\r\n" ); |
okano | 2:c22f0c87fee6 | 151 | memdump( mem2, MEM_SIZE ); |
okano | 2:c22f0c87fee6 | 152 | #endif |
okano | 2:c22f0c87fee6 | 153 | |
okano | 0:b802bd2f4cc9 | 154 | } |
okano | 0:b802bd2f4cc9 | 155 | |
okano | 0:b802bd2f4cc9 | 156 | |
okano | 3:63a0993315e5 | 157 | void memdump( char *base, int n ) |
okano | 3:63a0993315e5 | 158 | { |
okano | 0:b802bd2f4cc9 | 159 | unsigned int *p; |
okano | 0:b802bd2f4cc9 | 160 | |
okano | 0:b802bd2f4cc9 | 161 | printf( " memdump from 0x%08X for %d bytes", (unsigned long)base, n ); |
okano | 0:b802bd2f4cc9 | 162 | |
okano | 0:b802bd2f4cc9 | 163 | p = (unsigned int *)((unsigned int)base & ~(unsigned int)0x3); |
okano | 0:b802bd2f4cc9 | 164 | |
okano | 0:b802bd2f4cc9 | 165 | for ( int i = 0; i < (n >> 2); i++, p++ ) { |
okano | 0:b802bd2f4cc9 | 166 | if ( !(i % 4) ) |
okano | 1:a85b51eeb446 | 167 | printf( "\r\n 0x%08X :", (unsigned int)p ); |
okano | 0:b802bd2f4cc9 | 168 | |
okano | 0:b802bd2f4cc9 | 169 | printf( " 0x%08X", *p ); |
okano | 0:b802bd2f4cc9 | 170 | } |
okano | 0:b802bd2f4cc9 | 171 | |
okano | 1:a85b51eeb446 | 172 | printf( "\r\n" ); |
okano | 0:b802bd2f4cc9 | 173 | } |