fork

Dependencies:   mbed

Fork of LG by igor Apu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IAP.c Source File

IAP.c

00001 /*****************************************************************************
00002  * $Id$
00003  *
00004  * Project:     NXP LPC1700 Secondary Bootloader Example
00005  *
00006  * Description: Provides access to In-Application Programming (IAP) routines
00007  *              contained within the bootROM sector of LPC1100 devices.
00008  *
00009  * Copyright(C) 2010, NXP Semiconductor
00010  * All rights reserved.
00011  *
00012  *****************************************************************************
00013  * Software that is described herein is for illustrative purposes only
00014  * which provides customers with programming information regarding the
00015  * products. This software is supplied "AS IS" without any warranties.
00016  * NXP Semiconductors assumes no responsibility or liability for the
00017  * use of the software, conveys no license or title under any patent,
00018  * copyright, or mask work right to the product. NXP Semiconductors
00019  * reserves the right to make changes in the software without
00020  * notification. NXP Semiconductors also make no representation or
00021  * warranty that such application will be suitable for the specified
00022  * use without further testing or modification.
00023  *****************************************************************************/
00024 #include "IAP.h"
00025 #include <LPC17xx.h>
00026 extern uint32_t SystemCoreClock1 ;
00027 
00028 /* IAP Command Definitions */
00029 #define IAP_CMD_PREPARE_SECTORS         50
00030 #define IAP_CMD_COPY_RAM_TO_FLASH       51
00031 #define IAP_CMD_ERASE_SECTORS           52
00032 #define IAP_CMD_BLANK_CHECK_SECTORS     53
00033 #define IAP_CMD_READ_PART_ID            54
00034 #define IAP_CMD_READ_BOOT_ROM_VERSION   55
00035 #define IAP_CMD_COMPARE                 56
00036 #define IAP_CMD_REINVOKE_ISP            57
00037 #define IAP_CMD_READ_SERIAL_NUMBER      58
00038 
00039 /* IAP boot ROM location and access function */
00040 #define IAP_ROM_LOCATION                0x1FFF1FF1UL
00041 #define IAP_EXECUTE_CMD(a, b)           ((void (*)())(IAP_ROM_LOCATION))(a, b)
00042 
00043 /*****************************************************************************
00044 ** Function name:   u32IAP_PrepareSectors
00045 **
00046 ** Description:     Prepares sector(s) for erasing or write operations. This
00047 **                  command must be executed before executing the "Copy RAM to
00048 **                  Flash" or "Erase Sector(s)" commands.
00049 **
00050 ** Parameters:      u32StartSector - Number of first sector to prepare.
00051 **                  u32EndSector - Number of last sector to prepare.
00052 **
00053 ** Returned value:  Status code returned by IAP ROM function.
00054 **
00055 ******************************************************************************/
00056 uint32_t u32IAP_PrepareSectors(uint32_t u32StartSector, uint32_t u32EndSector)
00057 {
00058     uint32_t u32Status;
00059     uint32_t au32Result[5];
00060     uint32_t au32Command[5];
00061 
00062     if (u32EndSector < u32StartSector)
00063     {
00064         u32Status = IAP_STA_INVALD_PARAM;
00065     }
00066     else
00067     {
00068         au32Command[0] = IAP_CMD_PREPARE_SECTORS;
00069         au32Command[1] = u32StartSector;
00070         au32Command[2] = u32EndSector;
00071 
00072         IAP_EXECUTE_CMD(au32Command, au32Result);
00073 
00074         u32Status = au32Result[0];
00075     }
00076     return u32Status;
00077 }
00078 
00079 /*****************************************************************************
00080 ** Function name:   u32IAP_CopyRAMToFlash
00081 **
00082 ** Description:     Program the flash memory with data stored in RAM.
00083 **
00084 ** Parameters:      u32DstAddr - Destination Flash address, should be a 256
00085 **                               byte boundary.
00086 **                  u32SrcAddr - Source RAM address, should be a word boundary
00087 **                  u32Len     - Number of 8-bit bytes to write, must be 256
00088 **                               512, 1024, or 4096.
00089 *
00090 ** Returned value:  Status code returned by IAP ROM function.
00091 **
00092 ******************************************************************************/
00093 uint32_t u32IAP_CopyRAMToFlash(uint32_t u32DstAddr, uint32_t u32SrcAddr, uint32_t u32Len)
00094 {
00095     uint32_t au32Result[5];
00096     uint32_t au32Command[5];
00097     au32Command[0] = IAP_CMD_COPY_RAM_TO_FLASH;
00098     au32Command[1] = u32DstAddr;
00099     au32Command[2] = u32SrcAddr;
00100     au32Command[3] = u32Len;
00101   //   au32Command[4] = SystemFrequency  / 1000UL;  /* Core clock frequency in kHz */
00102     au32Command[4] = SystemCoreClock1  / 1000UL;  /* Core clock frequency in kHz */
00103 
00104     IAP_EXECUTE_CMD(au32Command, au32Result);
00105 
00106     return au32Result[0];
00107 }
00108 
00109 /*****************************************************************************
00110 ** Function name:   u32IAP_EraseSectors
00111 **
00112 ** Description:     Erase a sector or multiple sectors of on-chip Flash memory.
00113 **
00114 ** Parameters:      u32StartSector - Number of first sector to erase.
00115 **                  u32EndSector - Number of last sector to erase.
00116 *
00117 ** Returned value:  Status code returned by IAP ROM function.
00118 **
00119 ******************************************************************************/
00120 uint32_t u32IAP_EraseSectors(uint32_t u32StartSector, uint32_t u32EndSector)
00121 {
00122     uint32_t u32Status;
00123     uint32_t au32Result[5];
00124     uint32_t au32Command[5];
00125 
00126     if (u32EndSector < u32StartSector)
00127     {
00128         u32Status = IAP_STA_INVALD_PARAM;
00129     }
00130     else
00131     {
00132         au32Command[0] = IAP_CMD_ERASE_SECTORS;
00133         au32Command[1] = u32StartSector;
00134         au32Command[2] = u32EndSector;
00135         au32Command[3] = SystemCoreClock1  / 1000UL;  /* Core clock frequency in kHz */
00136      // au32Command[3] = /*SystemFrequency / 1000UL;  /* Core clock frequency in kHz */
00137 
00138         IAP_EXECUTE_CMD(au32Command, au32Result);
00139 
00140         u32Status = au32Result[0];
00141     }
00142     return u32Status;
00143 }
00144 
00145 /*****************************************************************************
00146 ** Function name:   u32IAP_BlankCheckSectors
00147 **
00148 ** Description:     Blank check a sector or multiple sectors of on-chip flash
00149 **                  memory.
00150 **
00151 ** Parameters:      u32StartSector - Number of first sector to check.
00152 **                  u32EndSector - Number of last sector to check.
00153 **                  pu32Result[0] - Offset of the first non blank word location
00154 **                  if the Status Code is IAP_STA_SECTOR_NOT_BLANK.
00155 **                  pu32Result[1] - Contents of non blank word location.
00156 **
00157 ** Returned value:  Status code returned by IAP ROM function.
00158 **
00159 ******************************************************************************/
00160 uint32_t u32IAP_BlankCheckSectors(uint32_t u32StartSector, uint32_t u32EndSector, uint32_t *pu32Result)
00161 {
00162     uint32_t u32Status;
00163     uint32_t au32Result[5];
00164     uint32_t au32Command[5];
00165 
00166     if (u32EndSector < u32StartSector)
00167     {
00168         u32Status = IAP_STA_INVALD_PARAM;
00169     }
00170     else
00171     {
00172         au32Command[0] = IAP_CMD_BLANK_CHECK_SECTORS;
00173         au32Command[1] = u32StartSector;
00174         au32Command[2] = u32EndSector;
00175 
00176         IAP_EXECUTE_CMD(au32Command, au32Result);
00177 
00178         if (au32Result[0] == IAP_STA_SECTOR_NOT_BLANK)
00179         {
00180             *pu32Result       = au32Result[0];
00181             *(pu32Result + 1) = au32Result[1];
00182         }
00183         u32Status = au32Result[0];
00184     }
00185     return u32Status;
00186 }
00187 
00188 /*****************************************************************************
00189 ** Function name:   u32IAP_ReadPartID
00190 **
00191 ** Description:     Read the part identification number.
00192 **
00193 ** Parameters:      pu32PartID - Pointer to storage for part ID number.
00194 *
00195 ** Returned value:  Status code returned by IAP ROM function.
00196 **
00197 ******************************************************************************/
00198 uint32_t u32IAP_ReadPartID(uint32_t *pu32PartID)
00199 {
00200     uint32_t au32Result[5];
00201     uint32_t au32Command[5];
00202 
00203     au32Command[0] = IAP_CMD_READ_PART_ID;
00204 
00205     IAP_EXECUTE_CMD(au32Command, au32Result);
00206 
00207     *pu32PartID = au32Result[1];
00208 
00209     return au32Result[0];
00210 }
00211 
00212 /*****************************************************************************
00213 ** Function name:   u32IAP_ReadBootVersion
00214 **
00215 ** Description:     Read the boot code version number.
00216 **
00217 ** Parameters:      pu32Major - Major version number in ASCII format.
00218 **                  pu32Minor - Minor version number in ASCII format.
00219 **
00220 ** Returned value:  Status code returned by IAP ROM function.
00221 **
00222 ******************************************************************************/
00223 uint32_t u32IAP_ReadBootVersion(uint32_t *pu32Major, uint32_t *pu32Minor)
00224 {
00225     uint32_t au32Result[5];
00226     uint32_t au32Command[5];
00227 
00228     au32Command[0] = IAP_CMD_READ_BOOT_ROM_VERSION;
00229 
00230     IAP_EXECUTE_CMD(au32Command, au32Result);
00231 
00232     *pu32Major = (au32Result[1] & 0x0000FF00UL) >> 8;
00233     *pu32Minor = au32Result[1] & 0x000000FFUL;
00234 
00235     return au32Result[0];
00236 }
00237 
00238 /*****************************************************************************
00239 ** Function name:   u32IAP_ReadBootVersion
00240 **
00241 ** Description:     Read the boot code version number.
00242 **
00243 ** Parameters:      pu32Major - Major version number in ASCII format.
00244 **                  pu32Minor - Minor version number in ASCII format.
00245 **
00246 ** Returned value:  Status code returned by IAP ROM function.
00247 **
00248 ******************************************************************************/
00249 void u32IAP_ReadSerialNumber(uint32_t *pu32byte0, uint32_t *pu32byte1,
00250                                  uint32_t *pu32byte2, uint32_t *pu32byte3)
00251 {
00252     uint32_t au32Result[5];
00253     uint32_t au32Command[5];
00254 
00255     au32Command[0] = IAP_CMD_READ_SERIAL_NUMBER;
00256 
00257     IAP_EXECUTE_CMD(au32Command, au32Result);
00258 
00259     *pu32byte0 = au32Result[0];
00260     *pu32byte1 = au32Result[1];
00261     *pu32byte2 = au32Result[2];
00262     *pu32byte3 = au32Result[3];
00263 
00264     return;
00265 }
00266 
00267 /*****************************************************************************
00268 ** Function name:   u32IAP_Compare
00269 **
00270 ** Description:     Compares the memory contents at two locations.
00271 **
00272 ** Parameters:      u32Len - Number of bytes to compare, must be a multiple of 4.
00273 **                  pu32Offset - Offset of the first mismatch if the Status Code is COMPARE_ERROR
00274 **
00275 ** Returned value:  Status code returned by IAP ROM function.
00276 **
00277 ******************************************************************************/
00278 uint32_t u32IAP_Compare(uint32_t u32DstAddr, uint32_t u32SrcAddr, uint32_t u32Len, uint32_t *pu32Offset)
00279 {
00280     uint32_t au32Result[5];
00281     uint32_t au32Command[5];
00282 
00283     au32Command[0] = IAP_CMD_COMPARE;
00284     au32Command[1] = u32DstAddr;
00285     au32Command[2] = u32SrcAddr;
00286     au32Command[3] = u32Len;
00287 
00288     IAP_EXECUTE_CMD(au32Command, au32Result);
00289 
00290     if (au32Result[0] == IAP_STA_COMPARE_ERROR)
00291     {
00292         if (pu32Offset != 0)
00293         {
00294             *pu32Offset = au32Result[1];
00295         }
00296     }
00297     return au32Result[0];
00298 }
00299 
00300 /*****************************************************************************
00301 ** Function name:   vIAP_ReinvokeISP
00302 **
00303 ** Description:     Invoke the bootloader in ISP mode.
00304 **
00305 ** Parameters:      None.
00306 *
00307 ** Returned value:  None.
00308 **
00309 ******************************************************************************/
00310 void vIAP_ReinvokeISP(void)
00311 {
00312     uint32_t au32Result[5];
00313     uint32_t au32Command[5];
00314 
00315     au32Command[0] = IAP_CMD_REINVOKE_ISP;
00316 
00317     IAP_EXECUTE_CMD(au32Command, au32Result);
00318 }
00319 
00320 /*****************************************************************************
00321  **                            End Of File
00322  *****************************************************************************/