IAP class library for LPC1768, LPC11U24, LPC1114, LPC1347, LPC1549, LPC812 and LPC824

Fork of IAP by Tedd OKANO

Committer:
jaerts
Date:
Tue Oct 22 17:23:53 2013 +0000
Revision:
5:7484398d50ea
Parent:
1:ff906ad52cf9
Child:
6:f794a51897b8
Implement reinvoke isp function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okano 0:ada7fb504504 1 /** IAP : internal Flash memory access library
okano 0:ada7fb504504 2 *
okano 0:ada7fb504504 3 * The internal Flash memory access is described in the LPC1768 and LPC11U24 usermanual.
okano 0:ada7fb504504 4 * http://www.nxp.com/documents/user_manual/UM10360.pdf
okano 0:ada7fb504504 5 * http://www.nxp.com/documents/user_manual/UM10462.pdf
okano 0:ada7fb504504 6 *
okano 0:ada7fb504504 7 * LPC1768 --
okano 0:ada7fb504504 8 * Chapter 2: "LPC17xx Memory map"
okano 0:ada7fb504504 9 * Chapter 32: "LPC17xx Flash memory interface and programming"
okano 0:ada7fb504504 10 * refering Rev. 01 - 4 January 2010
okano 0:ada7fb504504 11 *
okano 0:ada7fb504504 12 * LPC11U24 --
okano 0:ada7fb504504 13 * Chapter 2: "LPC11Uxx Memory mapping"
okano 0:ada7fb504504 14 * Chapter 20: "LPC11Uxx Flash programming firmware"
okano 0:ada7fb504504 15 * refering Rev. 03 - 16 July 2012
okano 0:ada7fb504504 16 *
okano 0:ada7fb504504 17 * Released under the MIT License: http://mbed.org/license/mit
okano 0:ada7fb504504 18 *
okano 0:ada7fb504504 19 * revision 1.0 09-Mar-2010 1st release
okano 0:ada7fb504504 20 * revision 1.1 12-Mar-2010 chaged: to make possible to reserve flash area for user
okano 0:ada7fb504504 21 * it can be set by USER_FLASH_AREA_START and USER_FLASH_AREA_SIZE in IAP.h
okano 0:ada7fb504504 22 * revision 2.0 26-Nov.2012 LPC11U24 code added
okano 1:ff906ad52cf9 23 * revision 2.1 26-Nov-2012 EEPROM access code imported from Suga koubou san's (http://mbed.org/users/okini3939/) library
okano 1:ff906ad52cf9 24 * http://mbed.org/users/okini3939/code/M0_EEPROM_test/
okano 0:ada7fb504504 25 */
okano 0:ada7fb504504 26
okano 0:ada7fb504504 27 #ifndef MBED_IAP
okano 0:ada7fb504504 28 #define MBED_IAP
okano 0:ada7fb504504 29
okano 0:ada7fb504504 30 #include "mbed.h"
okano 0:ada7fb504504 31
okano 0:ada7fb504504 32 #if defined(TARGET_LPC1768)
okano 0:ada7fb504504 33
okano 0:ada7fb504504 34 #define USER_FLASH_AREA_START FLASH_SECTOR_29
okano 0:ada7fb504504 35 #define USER_FLASH_AREA_SIZE (FLASH_SECTOR_SIZE_16_TO_29 * 1)
okano 0:ada7fb504504 36
okano 0:ada7fb504504 37 /*
okano 0:ada7fb504504 38 * memory map information is available in next URL also.
okano 0:ada7fb504504 39 * http://mbed.org/projects/libraries/svn/mbed/trunk/LPC1768/LPC17xx.h
okano 0:ada7fb504504 40 */
okano 0:ada7fb504504 41
okano 0:ada7fb504504 42 /** Table for start adress of sectors
okano 0:ada7fb504504 43 *
okano 0:ada7fb504504 44 * LPC1768 internal flash memory sector numbers and addresses
okano 0:ada7fb504504 45 *
okano 0:ada7fb504504 46 * LPC1768 flash memory are and sector number/size
okano 0:ada7fb504504 47 * Table 568 "Sectors in a LPC17xx device", Section 5. "Sector numbers", usermanual
okano 0:ada7fb504504 48 *
okano 0:ada7fb504504 49 * 0x00000000 - 0x0007FFFF flash (29 sectors)
okano 0:ada7fb504504 50 *
okano 0:ada7fb504504 51 * Sector0: 0x00000000 - 0x00000FFF 4K
okano 0:ada7fb504504 52 * Sector1: 0x00001000 - 0x00001FFF 4K
okano 0:ada7fb504504 53 * Sector2: 0x00002000 - 0x00002FFF 4K
okano 0:ada7fb504504 54 * Sector3: 0x00003000 - 0x00003FFF 4K
okano 0:ada7fb504504 55 * Sector4: 0x00004000 - 0x00004FFF 4K
okano 0:ada7fb504504 56 * Sector5: 0x00005000 - 0x00005FFF 4K
okano 0:ada7fb504504 57 * Sector6: 0x00006000 - 0x00006FFF 4K
okano 0:ada7fb504504 58 * Sector7: 0x00007000 - 0x00007FFF 4K
okano 0:ada7fb504504 59 * Sector8: 0x00008000 - 0x00008FFF 4K
okano 0:ada7fb504504 60 * Sector9: 0x00009000 - 0x00009FFF 4K
okano 0:ada7fb504504 61 * Sector10: 0x0000A000 - 0x0000AFFF 4K
okano 0:ada7fb504504 62 * Sector11: 0x0000B000 - 0x0000BFFF 4K
okano 0:ada7fb504504 63 * Sector12: 0x0000C000 - 0x0000CFFF 4K
okano 0:ada7fb504504 64 * Sector13: 0x0000D000 - 0x0000DFFF 4K
okano 0:ada7fb504504 65 * Sector14: 0x0000E000 - 0x0000EFFF 4K
okano 0:ada7fb504504 66 * Sector15: 0x0000F000 - 0x0000FFFF 4K
okano 0:ada7fb504504 67 *
okano 0:ada7fb504504 68 * Sector16: 0x00010000 - 0x00017FFF 32K
okano 0:ada7fb504504 69 * Sector17: 0x00018000 - 0x0001FFFF 32K
okano 0:ada7fb504504 70 * Sector18: 0x00020000 - 0x00027FFF 32K
okano 0:ada7fb504504 71 * Sector19: 0x00028000 - 0x0002FFFF 32K
okano 0:ada7fb504504 72 * Sector20: 0x00030000 - 0x00037FFF 32K
okano 0:ada7fb504504 73 * Sector21: 0x00038000 - 0x0003FFFF 32K
okano 0:ada7fb504504 74 * Sector22: 0x00040000 - 0x00047FFF 32K
okano 0:ada7fb504504 75 * Sector23: 0x00048000 - 0x0004FFFF 32K
okano 0:ada7fb504504 76 * Sector24: 0x00050000 - 0x00057FFF 32K
okano 0:ada7fb504504 77 * Sector25: 0x00058000 - 0x0005FFFF 32K
okano 0:ada7fb504504 78 * Sector26: 0x00060000 - 0x00067FFF 32K
okano 0:ada7fb504504 79 * Sector27: 0x00068000 - 0x0006FFFF 32K
okano 0:ada7fb504504 80 * Sector28: 0x00070000 - 0x00077FFF 32K
okano 0:ada7fb504504 81 * Sector29: 0x00078000 - 0x0007FFFF 32K
okano 0:ada7fb504504 82 */
okano 0:ada7fb504504 83
okano 0:ada7fb504504 84 #define FLASH_SECTOR_0 0x00000000
okano 0:ada7fb504504 85 #define FLASH_SECTOR_1 0x00001000
okano 0:ada7fb504504 86 #define FLASH_SECTOR_2 0x00002000
okano 0:ada7fb504504 87 #define FLASH_SECTOR_3 0x00003000
okano 0:ada7fb504504 88 #define FLASH_SECTOR_4 0x00004000
okano 0:ada7fb504504 89 #define FLASH_SECTOR_5 0x00005000
okano 0:ada7fb504504 90 #define FLASH_SECTOR_6 0x00006000
okano 0:ada7fb504504 91 #define FLASH_SECTOR_7 0x00007000
okano 0:ada7fb504504 92 #define FLASH_SECTOR_8 0x00008000
okano 0:ada7fb504504 93 #define FLASH_SECTOR_9 0x00009000
okano 0:ada7fb504504 94 #define FLASH_SECTOR_10 0x0000A000
okano 0:ada7fb504504 95 #define FLASH_SECTOR_11 0x0000B000
okano 0:ada7fb504504 96 #define FLASH_SECTOR_12 0x0000C000
okano 0:ada7fb504504 97 #define FLASH_SECTOR_13 0x0000D000
okano 0:ada7fb504504 98 #define FLASH_SECTOR_14 0x0000E000
okano 0:ada7fb504504 99 #define FLASH_SECTOR_15 0x0000F000
okano 0:ada7fb504504 100 #define FLASH_SECTOR_16 0x00010000
okano 0:ada7fb504504 101 #define FLASH_SECTOR_17 0x00018000
okano 0:ada7fb504504 102 #define FLASH_SECTOR_18 0x00020000
okano 0:ada7fb504504 103 #define FLASH_SECTOR_19 0x00028000
okano 0:ada7fb504504 104 #define FLASH_SECTOR_20 0x00030000
okano 0:ada7fb504504 105 #define FLASH_SECTOR_21 0x00038000
okano 0:ada7fb504504 106 #define FLASH_SECTOR_22 0x00040000
okano 0:ada7fb504504 107 #define FLASH_SECTOR_23 0x00048000
okano 0:ada7fb504504 108 #define FLASH_SECTOR_24 0x00050000
okano 0:ada7fb504504 109 #define FLASH_SECTOR_25 0x00058000
okano 0:ada7fb504504 110 #define FLASH_SECTOR_26 0x00060000
okano 0:ada7fb504504 111 #define FLASH_SECTOR_27 0x00068000
okano 0:ada7fb504504 112 #define FLASH_SECTOR_28 0x00070000
okano 0:ada7fb504504 113 #define FLASH_SECTOR_29 0x00078000
okano 0:ada7fb504504 114 #define FLASH_SECTOR_SIZE_0_TO_15 ( 4 * 1024)
okano 0:ada7fb504504 115 #define FLASH_SECTOR_SIZE_16_TO_29 (32 * 1024)
okano 0:ada7fb504504 116
okano 0:ada7fb504504 117 static char * sector_start_adress[] = {
okano 0:ada7fb504504 118 (char *)FLASH_SECTOR_0,
okano 0:ada7fb504504 119 (char *)FLASH_SECTOR_1,
okano 0:ada7fb504504 120 (char *)FLASH_SECTOR_2,
okano 0:ada7fb504504 121 (char *)FLASH_SECTOR_3,
okano 0:ada7fb504504 122 (char *)FLASH_SECTOR_4,
okano 0:ada7fb504504 123 (char *)FLASH_SECTOR_5,
okano 0:ada7fb504504 124 (char *)FLASH_SECTOR_6,
okano 0:ada7fb504504 125 (char *)FLASH_SECTOR_7,
okano 0:ada7fb504504 126 (char *)FLASH_SECTOR_8,
okano 0:ada7fb504504 127 (char *)FLASH_SECTOR_9,
okano 0:ada7fb504504 128 (char *)FLASH_SECTOR_10,
okano 0:ada7fb504504 129 (char *)FLASH_SECTOR_11,
okano 0:ada7fb504504 130 (char *)FLASH_SECTOR_12,
okano 0:ada7fb504504 131 (char *)FLASH_SECTOR_13,
okano 0:ada7fb504504 132 (char *)FLASH_SECTOR_14,
okano 0:ada7fb504504 133 (char *)FLASH_SECTOR_15,
okano 0:ada7fb504504 134 (char *)FLASH_SECTOR_16,
okano 0:ada7fb504504 135 (char *)FLASH_SECTOR_17,
okano 0:ada7fb504504 136 (char *)FLASH_SECTOR_18,
okano 0:ada7fb504504 137 (char *)FLASH_SECTOR_19,
okano 0:ada7fb504504 138 (char *)FLASH_SECTOR_20,
okano 0:ada7fb504504 139 (char *)FLASH_SECTOR_21,
okano 0:ada7fb504504 140 (char *)FLASH_SECTOR_22,
okano 0:ada7fb504504 141 (char *)FLASH_SECTOR_23,
okano 0:ada7fb504504 142 (char *)FLASH_SECTOR_24,
okano 0:ada7fb504504 143 (char *)FLASH_SECTOR_25,
okano 0:ada7fb504504 144 (char *)FLASH_SECTOR_26,
okano 0:ada7fb504504 145 (char *)FLASH_SECTOR_27,
okano 0:ada7fb504504 146 (char *)FLASH_SECTOR_28,
okano 0:ada7fb504504 147 (char *)FLASH_SECTOR_29
okano 0:ada7fb504504 148 };
okano 0:ada7fb504504 149
okano 0:ada7fb504504 150 #elif defined(TARGET_LPC11U24)
okano 0:ada7fb504504 151
okano 0:ada7fb504504 152 #define USER_FLASH_AREA_START FLASH_SECTOR_7
okano 0:ada7fb504504 153 #define USER_FLASH_AREA_SIZE (FLASH_SECTOR_SIZE * 1)
okano 0:ada7fb504504 154
okano 0:ada7fb504504 155 /** Table for start adress of sectors
okano 0:ada7fb504504 156 *
okano 0:ada7fb504504 157 * LPC11U24 internal flash memory sector numbers and addresses
okano 0:ada7fb504504 158 *
okano 0:ada7fb504504 159 * LPC11U24 flash memory are and sector number/size
okano 0:ada7fb504504 160 * Table 334 "LPC11U1x/2x flash sectors", Section 20. "Sector numbers", usermanual
okano 0:ada7fb504504 161 *
okano 0:ada7fb504504 162 * 0x00000000 - 0x00007FFF flash (8 sectors)
okano 0:ada7fb504504 163 *
okano 0:ada7fb504504 164 * Sector0: 0x00000000 - 0x00000FFF 4K
okano 0:ada7fb504504 165 * Sector1: 0x00001000 - 0x00001FFF 4K
okano 0:ada7fb504504 166 * Sector2: 0x00002000 - 0x00002FFF 4K
okano 0:ada7fb504504 167 * Sector3: 0x00003000 - 0x00003FFF 4K
okano 0:ada7fb504504 168 * Sector4: 0x00004000 - 0x00004FFF 4K
okano 0:ada7fb504504 169 * Sector5: 0x00005000 - 0x00005FFF 4K
okano 0:ada7fb504504 170 * Sector6: 0x00006000 - 0x00006FFF 4K
okano 0:ada7fb504504 171 * Sector7: 0x00007000 - 0x00007FFF 4K
okano 0:ada7fb504504 172 */
okano 0:ada7fb504504 173
okano 0:ada7fb504504 174 #define FLASH_SECTOR_0 0x00000000
okano 0:ada7fb504504 175 #define FLASH_SECTOR_1 0x00001000
okano 0:ada7fb504504 176 #define FLASH_SECTOR_2 0x00002000
okano 0:ada7fb504504 177 #define FLASH_SECTOR_3 0x00003000
okano 0:ada7fb504504 178 #define FLASH_SECTOR_4 0x00004000
okano 0:ada7fb504504 179 #define FLASH_SECTOR_5 0x00005000
okano 0:ada7fb504504 180 #define FLASH_SECTOR_6 0x00006000
okano 0:ada7fb504504 181 #define FLASH_SECTOR_7 0x00007000
okano 0:ada7fb504504 182 #define FLASH_SECTOR_SIZE (4 * 1024)
okano 0:ada7fb504504 183
okano 0:ada7fb504504 184 static char * sector_start_adress[] = {
okano 0:ada7fb504504 185 (char *)FLASH_SECTOR_0,
okano 0:ada7fb504504 186 (char *)FLASH_SECTOR_1,
okano 0:ada7fb504504 187 (char *)FLASH_SECTOR_2,
okano 0:ada7fb504504 188 (char *)FLASH_SECTOR_3,
okano 0:ada7fb504504 189 (char *)FLASH_SECTOR_4,
okano 0:ada7fb504504 190 (char *)FLASH_SECTOR_5,
okano 0:ada7fb504504 191 (char *)FLASH_SECTOR_6,
okano 0:ada7fb504504 192 (char *)FLASH_SECTOR_7,
okano 0:ada7fb504504 193 };
okano 0:ada7fb504504 194
okano 0:ada7fb504504 195 #endif
okano 0:ada7fb504504 196
okano 0:ada7fb504504 197
okano 0:ada7fb504504 198 /** Error code by IAP routine
okano 0:ada7fb504504 199 *
okano 0:ada7fb504504 200 * Table 588 "ISP Return Codes Summary", Section 7.15 "ISP Return Codes", usermanual
okano 0:ada7fb504504 201 */
okano 0:ada7fb504504 202
okano 0:ada7fb504504 203 enum error_code
okano 0:ada7fb504504 204 {
okano 0:ada7fb504504 205 CMD_SUCCESS,
okano 0:ada7fb504504 206 INVALID_COMMAND,
okano 0:ada7fb504504 207 SRC_ADDR_ERROR,
okano 0:ada7fb504504 208 DST_ADDR_ERROR,
okano 0:ada7fb504504 209 SRC_ADDR_NOT_MAPPED,
okano 0:ada7fb504504 210 DST_ADDR_NOT_MAPPED,
okano 0:ada7fb504504 211 COUNT_ERROR,
okano 0:ada7fb504504 212 INVALID_SECTOR,
okano 0:ada7fb504504 213 SECTOR_NOT_BLANK,
okano 0:ada7fb504504 214 SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION,
okano 0:ada7fb504504 215 COMPARE_ERROR,
okano 0:ada7fb504504 216 BUSY,
okano 0:ada7fb504504 217 PARAM_ERROR,
okano 0:ada7fb504504 218 ADDR_ERROR,
okano 0:ada7fb504504 219 ADDR_NOT_MAPPED,
okano 0:ada7fb504504 220 CMD_LOCKED,
okano 0:ada7fb504504 221 INVALID_CODE,
okano 0:ada7fb504504 222 INVALID_BAUD_RATE,
okano 0:ada7fb504504 223 INVALID_STOP_BIT,
okano 0:ada7fb504504 224 CODE_READ_PROTECTION_ENABLED
okano 0:ada7fb504504 225 };
okano 0:ada7fb504504 226
okano 0:ada7fb504504 227
okano 0:ada7fb504504 228
okano 0:ada7fb504504 229 /*
okano 0:ada7fb504504 230 * IAP routine entry
okano 0:ada7fb504504 231 *
okano 0:ada7fb504504 232 * "IAP commands"
okano 0:ada7fb504504 233 */
okano 0:ada7fb504504 234
okano 0:ada7fb504504 235 #define IAP_LOCATION 0x1fff1ff1
okano 0:ada7fb504504 236 typedef void (*IAP_call)(unsigned int [], unsigned int []);
okano 0:ada7fb504504 237
okano 0:ada7fb504504 238 /** IAP class
okano 0:ada7fb504504 239 *
okano 0:ada7fb504504 240 * Interface for internal flash memory access
okano 0:ada7fb504504 241 */
okano 0:ada7fb504504 242
okano 0:ada7fb504504 243
okano 0:ada7fb504504 244 class IAP {
okano 0:ada7fb504504 245 public:
okano 0:ada7fb504504 246
okano 0:ada7fb504504 247 /*
okano 0:ada7fb504504 248 * SystemCoreClock ??? :
okano 0:ada7fb504504 249 * http://mbed.org/forum/mbed/topic/229/
okano 0:ada7fb504504 250 * http://mbed.org/users/simon/programs/SystemCoreClock/16mhsh/
okano 0:ada7fb504504 251 */
okano 0:ada7fb504504 252
okano 0:ada7fb504504 253
okano 0:ada7fb504504 254 /** Constructor for IAP
okano 0:ada7fb504504 255 *
okano 0:ada7fb504504 256 */
okano 0:ada7fb504504 257
okano 0:ada7fb504504 258 IAP() : iap_entry( reinterpret_cast<IAP_call>(IAP_LOCATION) ), cclk_kHz( SystemCoreClock / 1000 ) {}
jaerts 5:7484398d50ea 259 int reinvoke_isp( void );
okano 0:ada7fb504504 260 int read_ID( void );
okano 0:ada7fb504504 261 int read_serial( void );
okano 0:ada7fb504504 262 int blank_check( int start, int end );
okano 0:ada7fb504504 263 int erase( int start, int end );
okano 0:ada7fb504504 264 int prepare( int start, int end );
okano 0:ada7fb504504 265 int write( char *source_addr, char *target_addr, int size );
okano 0:ada7fb504504 266 int compare( char *source_addr, char *target_addr, int size );
okano 0:ada7fb504504 267 int read_BootVer( void );
okano 0:ada7fb504504 268
okano 0:ada7fb504504 269 char *reserved_flash_area_start( void );
okano 0:ada7fb504504 270 int reserved_flash_area_size( void );
okano 0:ada7fb504504 271
okano 1:ff906ad52cf9 272 #if defined(TARGET_LPC11U24)
okano 1:ff906ad52cf9 273 int write_eeprom( char *source_addr, char *target_addr, int size );
okano 1:ff906ad52cf9 274 int read_eeprom( char *source_addr, char *target_addr, int size );
okano 1:ff906ad52cf9 275 #endif
okano 1:ff906ad52cf9 276
okano 0:ada7fb504504 277 private:
okano 0:ada7fb504504 278 IAP_call iap_entry;
okano 0:ada7fb504504 279 unsigned int IAP_command[ 5 ];
okano 0:ada7fb504504 280 unsigned int IAP_result[ 5 ];
okano 0:ada7fb504504 281 int cclk_kHz;
okano 0:ada7fb504504 282
okano 0:ada7fb504504 283 //int cpu_clock( void );
okano 0:ada7fb504504 284 }
okano 0:ada7fb504504 285 ;
okano 0:ada7fb504504 286
okano 0:ada7fb504504 287 #endif // #ifndef MBED_IAP