Aes encryption code
Dependencies: Crypto USBDevice mbed
Fork of larada by
IAP.cpp@0:aaf22a04f350, 2016-02-04 (annotated)
- Committer:
- stupid
- Date:
- Thu Feb 04 03:42:24 2016 +0000
- Revision:
- 0:aaf22a04f350
cleaned up code, most everything mostly works, except the tip encryption
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 | #include "mbed.h" |
stupid | 0:aaf22a04f350 | 21 | #include "IAP.h" |
stupid | 0:aaf22a04f350 | 22 | |
stupid | 0:aaf22a04f350 | 23 | #define USER_FLASH_AREA_START_STR( x ) STR( x ) |
stupid | 0:aaf22a04f350 | 24 | #define STR( x ) #x |
stupid | 0:aaf22a04f350 | 25 | |
stupid | 0:aaf22a04f350 | 26 | unsigned char user_area[ USER_FLASH_AREA_SIZE ] __attribute__((section( ".ARM.__at_" USER_FLASH_AREA_START_STR( USER_FLASH_AREA_START ) ), zero_init)); |
stupid | 0:aaf22a04f350 | 27 | |
stupid | 0:aaf22a04f350 | 28 | |
stupid | 0:aaf22a04f350 | 29 | /* |
stupid | 0:aaf22a04f350 | 30 | * Reserve of flash area is explained by Igor. Please refer next URL |
stupid | 0:aaf22a04f350 | 31 | * http://mbed.org/users/okano/notebook/iap-in-application-programming-internal-flash-eras/?page=1#comment-271 |
stupid | 0:aaf22a04f350 | 32 | */ |
stupid | 0:aaf22a04f350 | 33 | |
stupid | 0:aaf22a04f350 | 34 | //unsigned char user_area[ size ] __attribute__((section(".ARM.__at_0x78000"), zero_init)); |
stupid | 0:aaf22a04f350 | 35 | |
stupid | 0:aaf22a04f350 | 36 | /* |
stupid | 0:aaf22a04f350 | 37 | * IAP command codes |
stupid | 0:aaf22a04f350 | 38 | * Table 589. "IAP Command Summary", Chapter 8. "IAP commands", usermanual |
stupid | 0:aaf22a04f350 | 39 | */ |
stupid | 0:aaf22a04f350 | 40 | |
stupid | 0:aaf22a04f350 | 41 | enum command_code |
stupid | 0:aaf22a04f350 | 42 | { |
stupid | 0:aaf22a04f350 | 43 | IAPCommand_Prepare_sector_for_write_operation = 50, |
stupid | 0:aaf22a04f350 | 44 | IAPCommand_Copy_RAM_to_Flash, |
stupid | 0:aaf22a04f350 | 45 | IAPCommand_Erase_sector, |
stupid | 0:aaf22a04f350 | 46 | IAPCommand_Blank_check_sector, |
stupid | 0:aaf22a04f350 | 47 | IAPCommand_Read_part_ID, |
stupid | 0:aaf22a04f350 | 48 | IAPCommand_Read_Boot_Code_version, |
stupid | 0:aaf22a04f350 | 49 | IAPCommand_Compare, |
stupid | 0:aaf22a04f350 | 50 | IAPCommand_Reinvoke_ISP, |
stupid | 0:aaf22a04f350 | 51 | IAPCommand_Read_device_serial_number, |
stupid | 0:aaf22a04f350 | 52 | IAPCommand_EEPROM_Write = 61, |
stupid | 0:aaf22a04f350 | 53 | IAPCommand_EEPROM_Read, |
stupid | 0:aaf22a04f350 | 54 | }; |
stupid | 0:aaf22a04f350 | 55 | |
stupid | 0:aaf22a04f350 | 56 | |
stupid | 0:aaf22a04f350 | 57 | /** Read part identification number |
stupid | 0:aaf22a04f350 | 58 | * |
stupid | 0:aaf22a04f350 | 59 | * @return device ID |
stupid | 0:aaf22a04f350 | 60 | * @see read_serial() |
stupid | 0:aaf22a04f350 | 61 | */ |
stupid | 0:aaf22a04f350 | 62 | |
stupid | 0:aaf22a04f350 | 63 | int IAP::read_ID( void ) { |
stupid | 0:aaf22a04f350 | 64 | IAP_command[ 0 ] = IAPCommand_Read_part_ID; |
stupid | 0:aaf22a04f350 | 65 | |
stupid | 0:aaf22a04f350 | 66 | iap_entry( IAP_command, IAP_result ); |
stupid | 0:aaf22a04f350 | 67 | |
stupid | 0:aaf22a04f350 | 68 | // return ( (int)IAP_result[ 0 ] ); |
stupid | 0:aaf22a04f350 | 69 | return ( (int)IAP_result[ 1 ] ); // to return the number itself (this command always returns CMD_SUCCESS) |
stupid | 0:aaf22a04f350 | 70 | } |
stupid | 0:aaf22a04f350 | 71 | |
stupid | 0:aaf22a04f350 | 72 | |
stupid | 0:aaf22a04f350 | 73 | /** Read device serial number |
stupid | 0:aaf22a04f350 | 74 | * |
stupid | 0:aaf22a04f350 | 75 | * @return device serial number |
stupid | 0:aaf22a04f350 | 76 | * @see read_ID() |
stupid | 0:aaf22a04f350 | 77 | */ |
stupid | 0:aaf22a04f350 | 78 | |
stupid | 0:aaf22a04f350 | 79 | int IAP::read_serial( void ) { |
stupid | 0:aaf22a04f350 | 80 | IAP_command[ 0 ] = IAPCommand_Read_device_serial_number; |
stupid | 0:aaf22a04f350 | 81 | |
stupid | 0:aaf22a04f350 | 82 | iap_entry( IAP_command, IAP_result ); |
stupid | 0:aaf22a04f350 | 83 | |
stupid | 0:aaf22a04f350 | 84 | // return ( (int)IAP_result[ 0 ] ); |
stupid | 0:aaf22a04f350 | 85 | return ( (int)IAP_result[ 1 ] ); // to return the number itself (this command always returns CMD_SUCCESS) |
stupid | 0:aaf22a04f350 | 86 | } |
stupid | 0:aaf22a04f350 | 87 | |
stupid | 0:aaf22a04f350 | 88 | |
stupid | 0:aaf22a04f350 | 89 | /** Blank check sector(s) |
stupid | 0:aaf22a04f350 | 90 | * |
stupid | 0:aaf22a04f350 | 91 | * @param start a Start Sector Number |
stupid | 0:aaf22a04f350 | 92 | * @param end an End Sector Number (should be greater than or equal to start sector number). |
stupid | 0:aaf22a04f350 | 93 | * @return error code: CMD_SUCCESS | BUSY | SECTOR_NOT_BLANK | INVALID_SECTOR |
stupid | 0:aaf22a04f350 | 94 | */ |
stupid | 0:aaf22a04f350 | 95 | |
stupid | 0:aaf22a04f350 | 96 | int IAP::blank_check( int start, int end ) { |
stupid | 0:aaf22a04f350 | 97 | IAP_command[ 0 ] = IAPCommand_Blank_check_sector; |
stupid | 0:aaf22a04f350 | 98 | IAP_command[ 1 ] = (unsigned int)start; // Start Sector Number |
stupid | 0:aaf22a04f350 | 99 | IAP_command[ 2 ] = (unsigned int)end; // End Sector Number (should be greater than or equal to start sector number) |
stupid | 0:aaf22a04f350 | 100 | |
stupid | 0:aaf22a04f350 | 101 | iap_entry( IAP_command, IAP_result ); |
stupid | 0:aaf22a04f350 | 102 | |
stupid | 0:aaf22a04f350 | 103 | return ( (int)IAP_result[ 0 ] ); |
stupid | 0:aaf22a04f350 | 104 | } |
stupid | 0:aaf22a04f350 | 105 | |
stupid | 0:aaf22a04f350 | 106 | |
stupid | 0:aaf22a04f350 | 107 | /** Erase Sector(s) |
stupid | 0:aaf22a04f350 | 108 | * |
stupid | 0:aaf22a04f350 | 109 | * @param start a Start Sector Number |
stupid | 0:aaf22a04f350 | 110 | * @param end an End Sector Number (should be greater than or equal to start sector number). |
stupid | 0:aaf22a04f350 | 111 | * @return error code: CMD_SUCCESS | BUSY | SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION | INVALID_SECTOR |
stupid | 0:aaf22a04f350 | 112 | */ |
stupid | 0:aaf22a04f350 | 113 | |
stupid | 0:aaf22a04f350 | 114 | int IAP::erase( int start, int end ) { |
stupid | 0:aaf22a04f350 | 115 | IAP_command[ 0 ] = IAPCommand_Erase_sector; |
stupid | 0:aaf22a04f350 | 116 | IAP_command[ 1 ] = (unsigned int)start; // Start Sector Number |
stupid | 0:aaf22a04f350 | 117 | IAP_command[ 2 ] = (unsigned int)end; // End Sector Number (should be greater than or equal to start sector number) |
stupid | 0:aaf22a04f350 | 118 | IAP_command[ 3 ] = cclk_kHz; // CPU Clock Frequency (CCLK) in kHz |
stupid | 0:aaf22a04f350 | 119 | |
stupid | 0:aaf22a04f350 | 120 | iap_entry( IAP_command, IAP_result ); |
stupid | 0:aaf22a04f350 | 121 | |
stupid | 0:aaf22a04f350 | 122 | return ( (int)IAP_result[ 0 ] ); |
stupid | 0:aaf22a04f350 | 123 | } |
stupid | 0:aaf22a04f350 | 124 | |
stupid | 0:aaf22a04f350 | 125 | |
stupid | 0:aaf22a04f350 | 126 | /** Prepare sector(s) for write operation |
stupid | 0:aaf22a04f350 | 127 | * |
stupid | 0:aaf22a04f350 | 128 | * @param start a Start Sector Number |
stupid | 0:aaf22a04f350 | 129 | * @param end an End Sector Number (should be greater than or equal to start sector number). |
stupid | 0:aaf22a04f350 | 130 | * @return error code: CMD_SUCCESS | BUSY | INVALID_SECTOR |
stupid | 0:aaf22a04f350 | 131 | */ |
stupid | 0:aaf22a04f350 | 132 | |
stupid | 0:aaf22a04f350 | 133 | int IAP::prepare( int start, int end ) { |
stupid | 0:aaf22a04f350 | 134 | IAP_command[ 0 ] = IAPCommand_Prepare_sector_for_write_operation; |
stupid | 0:aaf22a04f350 | 135 | IAP_command[ 1 ] = (unsigned int)start; // Start Sector Number |
stupid | 0:aaf22a04f350 | 136 | IAP_command[ 2 ] = (unsigned int)end; // End Sector Number (should be greater than or equal to start sector number). |
stupid | 0:aaf22a04f350 | 137 | |
stupid | 0:aaf22a04f350 | 138 | iap_entry( IAP_command, IAP_result ); |
stupid | 0:aaf22a04f350 | 139 | |
stupid | 0:aaf22a04f350 | 140 | return ( (int)IAP_result[ 0 ] ); |
stupid | 0:aaf22a04f350 | 141 | } |
stupid | 0:aaf22a04f350 | 142 | |
stupid | 0:aaf22a04f350 | 143 | |
stupid | 0:aaf22a04f350 | 144 | /** Copy RAM to Flash |
stupid | 0:aaf22a04f350 | 145 | * |
stupid | 0:aaf22a04f350 | 146 | * @param source_addr Source RAM address from which data bytes are to be read. This address should be a word boundary. |
stupid | 0:aaf22a04f350 | 147 | * @param target_addr Destination flash address where data bytes are to be written. This address should be a 256 byte boundary. |
stupid | 0:aaf22a04f350 | 148 | * @param size Number of bytes to be written. Should be 256 | 512 | 1024 | 4096. |
stupid | 0:aaf22a04f350 | 149 | * @return error code: CMD_SUCCESS | SRC_ADDR_ERROR (Address not a word boundary) | DST_ADDR_ERROR (Address not on correct boundary) | SRC_ADDR_NOT_MAPPED | DST_ADDR_NOT_MAPPED | COUNT_ERROR (Byte count is not 256 | 512 | 1024 | 4096) | SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION | BUSY |
stupid | 0:aaf22a04f350 | 150 | */ |
stupid | 0:aaf22a04f350 | 151 | |
stupid | 0:aaf22a04f350 | 152 | int IAP::write( char *source_addr, char *target_addr, int size ) { |
stupid | 0:aaf22a04f350 | 153 | IAP_command[ 0 ] = IAPCommand_Copy_RAM_to_Flash; |
stupid | 0:aaf22a04f350 | 154 | IAP_command[ 1 ] = (unsigned int)target_addr; // Destination flash address where data bytes are to be written. This address should be a 256 byte boundary. |
stupid | 0:aaf22a04f350 | 155 | IAP_command[ 2 ] = (unsigned int)source_addr; // Source RAM address from which data bytes are to be read. This address should be a word boundary. |
stupid | 0:aaf22a04f350 | 156 | IAP_command[ 3 ] = size; // Number of bytes to be written. Should be 256 | 512 | 1024 | 4096. |
stupid | 0:aaf22a04f350 | 157 | IAP_command[ 4 ] = cclk_kHz; // CPU Clock Frequency (CCLK) in kHz. |
stupid | 0:aaf22a04f350 | 158 | |
stupid | 0:aaf22a04f350 | 159 | iap_entry( IAP_command, IAP_result ); |
stupid | 0:aaf22a04f350 | 160 | |
stupid | 0:aaf22a04f350 | 161 | return ( (int)IAP_result[ 0 ] ); |
stupid | 0:aaf22a04f350 | 162 | } |
stupid | 0:aaf22a04f350 | 163 | |
stupid | 0:aaf22a04f350 | 164 | |
stupid | 0:aaf22a04f350 | 165 | /** Compare <address1> <address2> <no of bytes> |
stupid | 0:aaf22a04f350 | 166 | * |
stupid | 0:aaf22a04f350 | 167 | * @param source_addr Starting flash or RAM address of data bytes to be compared. This address should be a word boundary. |
stupid | 0:aaf22a04f350 | 168 | * @param target_addr Starting flash or RAM address of data bytes to be compared. This address should be a word boundary. |
stupid | 0:aaf22a04f350 | 169 | * @param size Number of bytes to be compared; should be a multiple of 4. |
stupid | 0:aaf22a04f350 | 170 | * @return error code: CMD_SUCCESS | COMPARE_ERROR | COUNT_ERROR (Byte count is not a multiple of 4) | ADDR_ERROR | ADDR_NOT_MAPPED |
stupid | 0:aaf22a04f350 | 171 | */ |
stupid | 0:aaf22a04f350 | 172 | |
stupid | 0:aaf22a04f350 | 173 | int IAP::compare( char *source_addr, char *target_addr, int size ) { |
stupid | 0:aaf22a04f350 | 174 | IAP_command[ 0 ] = IAPCommand_Compare; |
stupid | 0:aaf22a04f350 | 175 | IAP_command[ 1 ] = (unsigned int)target_addr; // Starting flash or RAM address of data bytes to be compared. This address should be a word boundary. |
stupid | 0:aaf22a04f350 | 176 | IAP_command[ 2 ] = (unsigned int)source_addr; // Starting flash or RAM address of data bytes to be compared. This address should be a word boundary. |
stupid | 0:aaf22a04f350 | 177 | IAP_command[ 3 ] = size; // Number of bytes to be compared; should be a multiple of 4. |
stupid | 0:aaf22a04f350 | 178 | |
stupid | 0:aaf22a04f350 | 179 | iap_entry( IAP_command, IAP_result ); |
stupid | 0:aaf22a04f350 | 180 | |
stupid | 0:aaf22a04f350 | 181 | return ( (int)IAP_result[ 0 ] ); |
stupid | 0:aaf22a04f350 | 182 | } |
stupid | 0:aaf22a04f350 | 183 | |
stupid | 0:aaf22a04f350 | 184 | |
stupid | 0:aaf22a04f350 | 185 | /** Get user reserved flash start address |
stupid | 0:aaf22a04f350 | 186 | * |
stupid | 0:aaf22a04f350 | 187 | * @return start address of user reserved flash memory |
stupid | 0:aaf22a04f350 | 188 | * @see reserved_flash_area_size() |
stupid | 0:aaf22a04f350 | 189 | */ |
stupid | 0:aaf22a04f350 | 190 | |
stupid | 0:aaf22a04f350 | 191 | char * IAP::reserved_flash_area_start( void ) |
stupid | 0:aaf22a04f350 | 192 | { |
stupid | 0:aaf22a04f350 | 193 | return ( (char *)USER_FLASH_AREA_START ); |
stupid | 0:aaf22a04f350 | 194 | } |
stupid | 0:aaf22a04f350 | 195 | |
stupid | 0:aaf22a04f350 | 196 | |
stupid | 0:aaf22a04f350 | 197 | /** Get user reserved flash size |
stupid | 0:aaf22a04f350 | 198 | * |
stupid | 0:aaf22a04f350 | 199 | * @return size of user reserved flash memory |
stupid | 0:aaf22a04f350 | 200 | * @see reserved_flash_area_start() |
stupid | 0:aaf22a04f350 | 201 | */ |
stupid | 0:aaf22a04f350 | 202 | |
stupid | 0:aaf22a04f350 | 203 | int IAP::reserved_flash_area_size( void ) |
stupid | 0:aaf22a04f350 | 204 | { |
stupid | 0:aaf22a04f350 | 205 | return ( USER_FLASH_AREA_SIZE ); |
stupid | 0:aaf22a04f350 | 206 | } |
stupid | 0:aaf22a04f350 | 207 | |
stupid | 0:aaf22a04f350 | 208 | #if defined(TARGET_LPC11U24) |
stupid | 0:aaf22a04f350 | 209 | /** Copy RAM to EEPROM (LPC11U24) |
stupid | 0:aaf22a04f350 | 210 | * |
stupid | 0:aaf22a04f350 | 211 | * @param source_addr Source RAM address from which data bytes are to be read. |
stupid | 0:aaf22a04f350 | 212 | * @param target_addr Destination EEPROM address where data bytes are to be written. |
stupid | 0:aaf22a04f350 | 213 | * @param size Number of bytes to be written. |
stupid | 0:aaf22a04f350 | 214 | * @return error code: CMD_SUCCESS | SRC_ADDR_NOT_MAPPED | DST_ADDR_NOT_MAPPED |
stupid | 0:aaf22a04f350 | 215 | * Remark: The top 64 bytes of the EEPROM memory are reserved and cannot be written to. |
stupid | 0:aaf22a04f350 | 216 | */ |
stupid | 0:aaf22a04f350 | 217 | int IAP::write_eeprom( char *source_addr, char *target_addr, int size ) { |
stupid | 0:aaf22a04f350 | 218 | IAP_command[ 0 ] = IAPCommand_EEPROM_Write; |
stupid | 0:aaf22a04f350 | 219 | IAP_command[ 1 ] = (unsigned int)target_addr; // Destination EEPROM address where data bytes are to be written. This address should be a 256 byte boundary. |
stupid | 0:aaf22a04f350 | 220 | IAP_command[ 2 ] = (unsigned int)source_addr; // Source RAM address from which data bytes are to be read. This address should be a word boundary. |
stupid | 0:aaf22a04f350 | 221 | IAP_command[ 3 ] = size; // Number of bytes to be written. Should be 256 | 512 | 1024 | 4096. |
stupid | 0:aaf22a04f350 | 222 | IAP_command[ 4 ] = cclk_kHz; // CPU Clock Frequency (CCLK) in kHz. |
stupid | 0:aaf22a04f350 | 223 | |
stupid | 0:aaf22a04f350 | 224 | iap_entry( IAP_command, IAP_result ); |
stupid | 0:aaf22a04f350 | 225 | |
stupid | 0:aaf22a04f350 | 226 | return ( (int)IAP_result[ 0 ] ); |
stupid | 0:aaf22a04f350 | 227 | } |
stupid | 0:aaf22a04f350 | 228 | |
stupid | 0:aaf22a04f350 | 229 | /** Copy EEPROM to RAM (LPC11U24) |
stupid | 0:aaf22a04f350 | 230 | * |
stupid | 0:aaf22a04f350 | 231 | * @param source_addr Source EEPROM address from which data bytes are to be read. |
stupid | 0:aaf22a04f350 | 232 | * @param target_addr Destination RAM address where data bytes are to be written. |
stupid | 0:aaf22a04f350 | 233 | * @param size Number of bytes to be written. |
stupid | 0:aaf22a04f350 | 234 | * @return error code: CMD_SUCCESS | SRC_ADDR_NOT_MAPPED | DST_ADDR_NOT_MAPPED |
stupid | 0:aaf22a04f350 | 235 | * Remark: The top 64 bytes of the EEPROM memory are reserved and cannot be written to. |
stupid | 0:aaf22a04f350 | 236 | */ |
stupid | 0:aaf22a04f350 | 237 | int IAP::read_eeprom( char *source_addr, char *target_addr, int size ) { |
stupid | 0:aaf22a04f350 | 238 | IAP_command[ 0 ] = IAPCommand_EEPROM_Read; |
stupid | 0:aaf22a04f350 | 239 | IAP_command[ 1 ] = (unsigned int)source_addr; // Source EEPROM address from which data bytes are to be read. This address should be a word boundary. |
stupid | 0:aaf22a04f350 | 240 | IAP_command[ 2 ] = (unsigned int)target_addr; // Destination RAM address where data bytes are to be written. This address should be a 256 byte boundary. |
stupid | 0:aaf22a04f350 | 241 | IAP_command[ 3 ] = size; // Number of bytes to be written. Should be 256 | 512 | 1024 | 4096. |
stupid | 0:aaf22a04f350 | 242 | IAP_command[ 4 ] = cclk_kHz; // CPU Clock Frequency (CCLK) in kHz. |
stupid | 0:aaf22a04f350 | 243 | |
stupid | 0:aaf22a04f350 | 244 | iap_entry( IAP_command, IAP_result ); |
stupid | 0:aaf22a04f350 | 245 | |
stupid | 0:aaf22a04f350 | 246 | return ( (int)IAP_result[ 0 ] ); |
stupid | 0:aaf22a04f350 | 247 | } |
stupid | 0:aaf22a04f350 | 248 | #endif |