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