demo code for internal EEPROM memory access

Dependencies:   mbed

LPC11U24 の内臓EEPROMアクセス

LPC11U24 は EEPROM を 4KB 内蔵している。

うち、ユーザーが使えるのは64バイト目以降。

ROM内の In-Application Programming (IAP) を経由してアクセスできる。

Committer:
okini3939
Date:
Fri May 11 00:40:02 2012 +0000
Revision:
0:09bfe87fd66a

        

Who changed what in which revision?

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