SPI Library for 240x320 TFT LCD with ILI9320, ILI9325 and ILI9328 chip

Dependencies:   BurstSPI

Dependents:   KL25Z_ILI9320_Demo Mini-DK

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 and LPC11U24 usermanual. 
00004  *            http://www.nxp.com/documents/user_manual/UM10360.pdf
00005  *            http://www.nxp.com/documents/user_manual/UM10462.pdf
00006  *
00007  *               LPC1768 --
00008  *                    Chapter  2: "LPC17xx Memory map"
00009  *                    Chapter 32: "LPC17xx Flash memory interface and programming"
00010  *                    refering Rev. 01 - 4 January 2010
00011  * 
00012  *               LPC11U24 --
00013  *                    Chapter  2: "LPC11Uxx Memory mapping"
00014  *                    Chapter 20: "LPC11Uxx Flash programming firmware"
00015  *                    refering Rev. 03 - 16 July 2012
00016  * 
00017  *        Released under the MIT License: http://mbed.org/license/mit
00018  *
00019  *        revision 1.0  09-Mar-2010   1st release
00020  *        revision 1.1  12-Mar-2010   chaged: to make possible to reserve flash area for user
00021  *                                            it can be set by USER_FLASH_AREA_START and USER_FLASH_AREA_SIZE in IAP.h
00022  *        revision 2.0  26-Nov-2012   LPC11U24 code added
00023  *        revision 2.1  26-Nov-2012   EEPROM access code imported from Suga koubou san's (http://mbed.org/users/okini3939/) library
00024  *                                            http://mbed.org/users/okini3939/code/M0_EEPROM_test/
00025  */
00026 #if defined(TARGET_LPC11U24) || defined(TARGET_LPC1768)
00027 
00028 #include    "mbed.h"
00029 #include    "IAP.h"
00030 
00031 #define     USER_FLASH_AREA_START_STR( x )      STR( x )
00032 #define     STR( x )                            #x
00033 
00034 unsigned char user_area[ USER_FLASH_AREA_SIZE ] __attribute__((section( ".ARM.__at_" USER_FLASH_AREA_START_STR( USER_FLASH_AREA_START ) ), zero_init));
00035 
00036 
00037 /*
00038  *  Reserve of flash area is explained by Igor. Please refer next URL
00039  *    http://mbed.org/users/okano/notebook/iap-in-application-programming-internal-flash-eras/?page=1#comment-271
00040  */
00041  
00042 //unsigned char user_area[ size ] __attribute__((section(".ARM.__at_0x78000"), zero_init));
00043 
00044 /*
00045  *  IAP command codes
00046  *  Table 589. "IAP Command Summary", Chapter 8. "IAP commands", usermanual
00047  */
00048 
00049 enum command_code
00050         {
00051             IAPCommand_Prepare_sector_for_write_operation    = 50,
00052             IAPCommand_Copy_RAM_to_Flash,
00053             IAPCommand_Erase_sector,
00054             IAPCommand_Blank_check_sector,
00055             IAPCommand_Read_part_ID,
00056             IAPCommand_Read_Boot_Code_version,
00057             IAPCommand_Compare,
00058             IAPCommand_Reinvoke_ISP,
00059             IAPCommand_Read_device_serial_number,
00060 #if defined(TARGET_LPC11U24)
00061             IAPCommand_EEPROM_Write = 61,
00062             IAPCommand_EEPROM_Read,
00063 #endif
00064         };
00065 
00066 
00067 /** Read part identification number
00068  *
00069  *  @return    device ID
00070  *  @see       read_serial()
00071  */
00072 
00073 int IAP::read_ID( void ) {
00074     IAP_command[ 0 ]    = IAPCommand_Read_part_ID;
00075     
00076     iap_entry( IAP_command, IAP_result );
00077     
00078     //  return ( (int)IAP_result[ 0 ] );
00079     return ( (int)IAP_result[ 1 ] );    //  to return the number itself (this command always returns CMD_SUCCESS)
00080 }
00081 
00082 
00083 /** Read device serial number
00084  *
00085  *  @return    device serial number
00086  *  @see       read_ID()
00087  */
00088 
00089 int IAP::read_serial( void ) {
00090     IAP_command[ 0 ]    = IAPCommand_Read_device_serial_number;
00091     
00092     iap_entry( IAP_command, IAP_result );
00093     
00094     //  return ( (int)IAP_result[ 0 ] );
00095     return ( (int)IAP_result[ 1 ] );    //  to return the number itself (this command always returns CMD_SUCCESS)
00096 }
00097 
00098 
00099 /** Blank check sector(s)
00100  *  
00101  *  @param    start    a Start Sector Number
00102  *  @param    end      an End Sector Number (should be greater than or equal to start sector number).
00103  *  @return error code: CMD_SUCCESS | BUSY | SECTOR_NOT_BLANK | INVALID_SECTOR
00104  */
00105 
00106 int IAP::blank_check( int start, int end ) {
00107     IAP_command[ 0 ]    = IAPCommand_Blank_check_sector;
00108     IAP_command[ 1 ]    = (unsigned int)start;  //  Start Sector Number
00109     IAP_command[ 2 ]    = (unsigned int)end;    //  End Sector Number (should be greater than or equal to start sector number)
00110 
00111     iap_entry( IAP_command, IAP_result );
00112 
00113     return ( (int)IAP_result[ 0 ] );
00114 }
00115 
00116 
00117 /** Erase Sector(s)
00118  *  
00119  *  @param    start    a Start Sector Number
00120  *  @param    end      an End Sector Number (should be greater than or equal to start sector number).
00121  *  @return   error code: CMD_SUCCESS | BUSY | SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION | INVALID_SECTOR
00122  */
00123 
00124 int IAP::erase( int start, int end ) {
00125     IAP_command[ 0 ]    = IAPCommand_Erase_sector;
00126     IAP_command[ 1 ]    = (unsigned int)start;  //  Start Sector Number
00127     IAP_command[ 2 ]    = (unsigned int)end;    //  End Sector Number (should be greater than or equal to start sector number)
00128     IAP_command[ 3 ]    = cclk_kHz;             //  CPU Clock Frequency (CCLK) in kHz
00129 
00130     iap_entry( IAP_command, IAP_result );
00131 
00132     return ( (int)IAP_result[ 0 ] );
00133 }
00134 
00135 
00136 /** Prepare sector(s) for write operation
00137  *  
00138  *  @param    start    a Start Sector Number
00139  *  @param    end      an End Sector Number (should be greater than or equal to start sector number).
00140  *  @return   error code: CMD_SUCCESS | BUSY | INVALID_SECTOR
00141  */
00142 
00143 int IAP::prepare( int start, int end ) {
00144     IAP_command[ 0 ]    = IAPCommand_Prepare_sector_for_write_operation;
00145     IAP_command[ 1 ]    = (unsigned int)start;  //  Start Sector Number
00146     IAP_command[ 2 ]    = (unsigned int)end;    //  End Sector Number (should be greater than or equal to start sector number).
00147     
00148     iap_entry( IAP_command, IAP_result );
00149     
00150     return ( (int)IAP_result[ 0 ] );
00151 }
00152 
00153 
00154 /** Copy RAM to Flash
00155  *  
00156  *  @param    source_addr    Source RAM address from which data bytes are to be read. This address should be a word boundary.
00157  *  @param    target_addr    Destination flash address where data bytes are to be written. This address should be a 256 byte boundary.
00158  *  @param    size           Number of bytes to be written. Should be 256 | 512 | 1024 | 4096.
00159  *  @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
00160  */
00161 
00162 int IAP::write( char *source_addr, char *target_addr, int size ) {
00163     IAP_command[ 0 ]    = IAPCommand_Copy_RAM_to_Flash;
00164     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.
00165     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.
00166     IAP_command[ 3 ]    = size;                         //  Number of bytes to be written. Should be 256 | 512 | 1024 | 4096.
00167     IAP_command[ 4 ]    = cclk_kHz;                     //  CPU Clock Frequency (CCLK) in kHz.
00168 
00169     iap_entry( IAP_command, IAP_result );
00170 
00171     return ( (int)IAP_result[ 0 ] );
00172 }
00173 
00174 
00175 /** Compare <address1> <address2> <no of bytes>
00176  *  
00177  *  @param    source_addr Starting flash or RAM address of data bytes to be compared. This address should be a word boundary.
00178  *  @param    target_addr Starting flash or RAM address of data bytes to be compared. This address should be a word boundary.
00179  *  @param    size         Number of bytes to be compared; should be a multiple of 4.
00180  *  @return   error code: CMD_SUCCESS | COMPARE_ERROR | COUNT_ERROR (Byte count is not a multiple of 4) | ADDR_ERROR | ADDR_NOT_MAPPED     
00181  */
00182 
00183 int IAP::compare( char *source_addr, char *target_addr, int size ) {
00184     IAP_command[ 0 ]    = IAPCommand_Compare;
00185     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.
00186     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.
00187     IAP_command[ 3 ]    = size;                         //  Number of bytes to be compared; should be a multiple of 4.
00188 
00189     iap_entry( IAP_command, IAP_result );
00190 
00191     return ( (int)IAP_result[ 0 ] );
00192 }
00193 
00194 /** Compare <address1> <address2> <no of bytes>
00195  *  
00196  *  @param    source_addr Starting flash or RAM address of data bytes to be compared. This address should be a word boundary.
00197  *  @param    target_addr Starting flash or RAM address of data bytes to be compared. This address should be a word boundary.
00198  *  @param    size         Number of bytes to be compared; should be a multiple of 4.
00199  *  @return   error code: CMD_SUCCESS | COMPARE_ERROR | COUNT_ERROR (Byte count is not a multiple of 4) | ADDR_ERROR | ADDR_NOT_MAPPED     
00200  */
00201 
00202 int IAP::read_BootVer(void) {
00203     IAP_command[0] = IAPCommand_Read_Boot_Code_version;
00204     IAP_result[1] = 0; // not sure if in high or low bits.
00205     iap_entry(IAP_command, IAP_result);
00206     return ((int)IAP_result[1]);
00207 }
00208 
00209 /** Get user reserved flash start address
00210  *
00211  *  @return    start address of user reserved flash memory
00212  *  @see       reserved_flash_area_size()
00213  */
00214 
00215 char * IAP::reserved_flash_area_start( void )
00216 {
00217     return ( (char *)USER_FLASH_AREA_START );
00218 }
00219 
00220 
00221 /** Get user reserved flash size
00222  *
00223  *  @return    size of user reserved flash memory
00224  *  @see       reserved_flash_area_start()
00225  */
00226 
00227 int IAP::reserved_flash_area_size( void )
00228 {
00229     return ( USER_FLASH_AREA_SIZE );
00230 }
00231 
00232 #if defined(TARGET_LPC11U24)
00233 /** Copy RAM to EEPROM (LPC11U24)
00234  *  
00235  *  @param    source_addr    Source RAM address from which data bytes are to be read.
00236  *  @param    target_addr    Destination EEPROM address where data bytes are to be written.
00237  *  @param    size           Number of bytes to be written.
00238  *  @return   error code: CMD_SUCCESS | SRC_ADDR_NOT_MAPPED | DST_ADDR_NOT_MAPPED
00239  *  Remark: The top 64 bytes of the EEPROM memory are reserved and cannot be written to.
00240  */
00241 int IAP::write_eeprom( char *source_addr, char *target_addr, int size ) {
00242     IAP_command[ 0 ]    = IAPCommand_EEPROM_Write;
00243     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.
00244     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.
00245     IAP_command[ 3 ]    = size;                         //  Number of bytes to be written. Should be 256 | 512 | 1024 | 4096.
00246     IAP_command[ 4 ]    = cclk_kHz;                     //  CPU Clock Frequency (CCLK) in kHz.
00247  
00248     iap_entry( IAP_command, IAP_result );
00249  
00250     return ( (int)IAP_result[ 0 ] );
00251 }
00252  
00253 /** Copy EEPROM to RAM (LPC11U24)
00254  *  
00255  *  @param    source_addr    Source EEPROM address from which data bytes are to be read.
00256  *  @param    target_addr    Destination RAM address where data bytes are to be written.
00257  *  @param    size           Number of bytes to be written.
00258  *  @return   error code: CMD_SUCCESS | SRC_ADDR_NOT_MAPPED | DST_ADDR_NOT_MAPPED
00259  *  Remark: The top 64 bytes of the EEPROM memory are reserved and cannot be written to.
00260  */
00261 int IAP::read_eeprom( char *source_addr, char *target_addr, int size ) {
00262     IAP_command[ 0 ]    = IAPCommand_EEPROM_Read;
00263     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.
00264     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.
00265     IAP_command[ 3 ]    = size;                         //  Number of bytes to be written. Should be 256 | 512 | 1024 | 4096.
00266     IAP_command[ 4 ]    = cclk_kHz;                     //  CPU Clock Frequency (CCLK) in kHz.
00267  
00268     iap_entry( IAP_command, IAP_result );
00269  
00270     return ( (int)IAP_result[ 0 ] );
00271 }
00272 #endif
00273 #endif // TARGET_LPC1768 - TARGET_LPC11U24
00274