IAP program sample to write sector28 (address 0x00070000)

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IAP.cpp Source File

IAP.cpp

00001 /**    IAP : internal Flash memory access library
00002  *
00003  *        The internal Flash memory access is described in the LPC1768 usermanual. 
00004  *            http://www.nxp.com/documents/user_manual/UM10360.pdf
00005  *
00006  *            Chapter  2: "LPC17xx Memory map"
00007  *            Chapter 32: "LPC17xx Flash memory interface and programming"
00008  *                refering Rev. 01 - 4 January 2010
00009  *
00010  *        Released under the MIT License: http://mbed.org/license/mit
00011  *
00012  *        revision 1.0  09-Mar-2010   1st release
00013  *        revision 1.1  12-Mar-2010   chaged: to make possible to reserve flash area for user
00014  *                                            it can be set by USER_FLASH_AREA_START and USER_FLASH_AREA_SIZE in IAP.h
00015  */
00016 
00017 #include    "mbed.h"
00018 #include    "IAP.h"
00019 
00020 #define     USER_FLASH_AREA_START_STR( x )      STR( x )
00021 #define     STR( x )                            #x
00022 
00023 unsigned char user_area[ USER_FLASH_AREA_SIZE ] __attribute__((section( ".ARM.__at_" USER_FLASH_AREA_START_STR( USER_FLASH_AREA_START ) ), zero_init));
00024 
00025 
00026 /*
00027  *  Reserve of flash area is explained by Igor. Please refer next URL
00028  *    http://mbed.org/users/okano/notebook/iap-in-application-programming-internal-flash-eras/?page=1#comment-271
00029  */
00030  
00031 //unsigned char user_area[ size ] __attribute__((section(".ARM.__at_0x78000"), zero_init));
00032 
00033 /*
00034  *  IAP command codes
00035  *  Table 589. "IAP Command Summary", Chapter 8. "IAP commands", usermanual
00036  */
00037 
00038 enum command_code
00039         {
00040             IAPCommand_Prepare_sector_for_write_operation    = 50,
00041             IAPCommand_Copy_RAM_to_Flash,
00042             IAPCommand_Erase_sector,
00043             IAPCommand_Blank_check_sector,
00044             IAPCommand_Read_part_ID,
00045             IAPCommand_Read_Boot_Code_version,
00046             IAPCommand_Compare,
00047             IAPCommand_Reinvoke_ISP,
00048             IAPCommand_Read_device_serial_number
00049         };
00050 
00051 
00052 /** Read part identification number
00053  *
00054  *  @return    device ID
00055  *  @see       read_serial()
00056  */
00057 
00058 int IAP::read_ID( void ) {
00059     IAP_command[ 0 ]    = IAPCommand_Read_part_ID;
00060     
00061     iap_entry( IAP_command, IAP_result );
00062     
00063     //  return ( (int)IAP_result[ 0 ] );
00064     return ( (int)IAP_result[ 1 ] );    //  to return the number itself (this command always returns CMD_SUCCESS)
00065 }
00066 
00067 
00068 /** Read device serial number
00069  *
00070  *  @return    device serial number
00071  *  @see       read_ID()
00072  */
00073 
00074 int IAP::read_serial( void ) {
00075     IAP_command[ 0 ]    = IAPCommand_Read_device_serial_number;
00076     
00077     iap_entry( IAP_command, IAP_result );
00078     
00079     //  return ( (int)IAP_result[ 0 ] );
00080     return ( (int)IAP_result[ 1 ] );    //  to return the number itself (this command always returns CMD_SUCCESS)
00081 }
00082 
00083 
00084 /** Blank check sector(s)
00085  *  
00086  *  @param    start    a Start Sector Number
00087  *  @param    end      an End Sector Number (should be greater than or equal to start sector number).
00088  *  @return error code: CMD_SUCCESS | BUSY | SECTOR_NOT_BLANK | INVALID_SECTOR
00089  */
00090 
00091 int IAP::blank_check( int start, int end ) {
00092     IAP_command[ 0 ]    = IAPCommand_Blank_check_sector;
00093     IAP_command[ 1 ]    = (unsigned int)start;  //  Start Sector Number
00094     IAP_command[ 2 ]    = (unsigned int)end;    //  End Sector Number (should be greater than or equal to start sector number)
00095 
00096     iap_entry( IAP_command, IAP_result );
00097 
00098     return ( (int)IAP_result[ 0 ] );
00099 }
00100 
00101 
00102 /** Erase Sector(s)
00103  *  
00104  *  @param    start    a Start Sector Number
00105  *  @param    end      an End Sector Number (should be greater than or equal to start sector number).
00106  *  @return   error code: CMD_SUCCESS | BUSY | SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION | INVALID_SECTOR
00107  */
00108 
00109 int IAP::erase( int start, int end ) {
00110     IAP_command[ 0 ]    = IAPCommand_Erase_sector;
00111     IAP_command[ 1 ]    = (unsigned int)start;  //  Start Sector Number
00112     IAP_command[ 2 ]    = (unsigned int)end;    //  End Sector Number (should be greater than or equal to start sector number)
00113     IAP_command[ 3 ]    = cclk_kHz;             //  CPU Clock Frequency (CCLK) in kHz
00114 
00115     iap_entry( IAP_command, IAP_result );
00116 
00117     return ( (int)IAP_result[ 0 ] );
00118 }
00119 
00120 
00121 /** Prepare sector(s) for write operation
00122  *  
00123  *  @param    start    a Start Sector Number
00124  *  @param    end      an End Sector Number (should be greater than or equal to start sector number).
00125  *  @return   error code: CMD_SUCCESS | BUSY | INVALID_SECTOR
00126  */
00127 
00128 int IAP::prepare( int start, int end ) {
00129     IAP_command[ 0 ]    = IAPCommand_Prepare_sector_for_write_operation;
00130     IAP_command[ 1 ]    = (unsigned int)start;  //  Start Sector Number
00131     IAP_command[ 2 ]    = (unsigned int)end;    //  End Sector Number (should be greater than or equal to start sector number).
00132     
00133     iap_entry( IAP_command, IAP_result );
00134     
00135     return ( (int)IAP_result[ 0 ] );
00136 }
00137 
00138 
00139 /** Copy RAM to Flash
00140  *  
00141  *  @param    source_addr    Source RAM address from which data bytes are to be read. This address should be a word boundary.
00142  *  @param    target_addr    Destination flash address where data bytes are to be written. This address should be a 256 byte boundary.
00143  *  @param    size           Number of bytes to be written. Should be 256 | 512 | 1024 | 4096.
00144  *  @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
00145  */
00146 
00147 int IAP::write( char *source_addr, char *target_addr, int size ) {
00148     IAP_command[ 0 ]    = IAPCommand_Copy_RAM_to_Flash;
00149     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.
00150     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.
00151     IAP_command[ 3 ]    = size;                         //  Number of bytes to be written. Should be 256 | 512 | 1024 | 4096.
00152     IAP_command[ 4 ]    = cclk_kHz;                     //  CPU Clock Frequency (CCLK) in kHz.
00153 
00154     iap_entry( IAP_command, IAP_result );
00155 
00156     return ( (int)IAP_result[ 0 ] );
00157 }
00158 
00159 
00160 /** Compare <address1> <address2> <no of bytes>
00161  *  
00162  *  @param    source_addr Starting flash or RAM address of data bytes to be compared. This address should be a word boundary.
00163  *  @param    target_addr Starting flash or RAM address of data bytes to be compared. This address should be a word boundary.
00164  *  @param    size         Number of bytes to be compared; should be a multiple of 4.
00165  *  @return   error code: CMD_SUCCESS | COMPARE_ERROR | COUNT_ERROR (Byte count is not a multiple of 4) | ADDR_ERROR | ADDR_NOT_MAPPED     
00166  */
00167 
00168 int IAP::compare( char *source_addr, char *target_addr, int size ) {
00169     IAP_command[ 0 ]    = IAPCommand_Compare;
00170     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.
00171     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.
00172     IAP_command[ 3 ]    = size;                         //  Number of bytes to be compared; should be a multiple of 4.
00173 
00174     iap_entry( IAP_command, IAP_result );
00175 
00176     return ( (int)IAP_result[ 0 ] );
00177 }
00178 
00179 
00180 /** Get user reserved flash start address
00181  *
00182  *  @return    start address of user reserved flash memory
00183  *  @see       reserved_flash_area_size()
00184  */
00185 
00186 char * IAP::reserved_flash_area_start( void )
00187 {
00188     return ( (char *)USER_FLASH_AREA_START );
00189 }
00190 
00191 
00192 /** Get user reserved flash size
00193  *
00194  *  @return    size of user reserved flash memory
00195  *  @see       reserved_flash_area_start()
00196  */
00197 
00198 int IAP::reserved_flash_area_size( void )
00199 {
00200     return ( USER_FLASH_AREA_SIZE );
00201 }
00202