===========IAP.C=============
#include "IAP.h"
#define IAP_CLK Fcclk
#define IAP_LOCATION 0x7FFFFFF1
#define iap_entry(a,

((void (*)())(IAP_LOCATION))(a,
unsigned long command[5] = {0,0,0,0,0};
unsigned long result[3]= {0,0,0};
/*************************************************************************
* Function Name: IAP_PrepareSec
* Parameters: unsigned long StartSecNum -- Start Sector Number
* unsigned long EndSecNum -- End Sector Number
* Return: unsigned long -- Status Code
*
* Description: This command must be executed before executing "Copy RAM to Flash" or
* "Erase Sector(s)" command.
*
*************************************************************************/
unsigned long IAP_PrepareSec (unsigned long StartSecNum, unsigned long EndSecNum)
{
if (EndSecNum < StartSecNum)
return IAP_STA_INVALD_PARAM;
command[0] = IAP_CMD_PrepareSec;
command[1] = StartSecNum;
command[2] = EndSecNum;
iap_entry(command, result);
return result[0];
}
/*************************************************************************
* Function Name: IAP_CopyRAMToFlash
* Parameters: unsigned long dst -- Destination Flash address, should be a 256 byte boundary.
* unsigned long src -- Source RAM address, should be a word boundary
* unsigned long number -- 256 | 512 |1024 |4096
* Return: unsigned long -- Status Code
*
* Description: This command is used to program the flash memory.
*
*************************************************************************/
unsigned long IAP_CopyRAMToFlash (unsigned long dst, unsigned long src,
unsigned long number)
{
command[0] = IAP_CMD_CopyRAMToFlash;
command[1] = dst;
command[2] = src;
command[3] = number;
command[4] = IAP_CLK / 1000; // Fcclk in KHz
iap_entry(command, result);
return result[0];
}
/*************************************************************************
* Function Name: IAP_EraseSec
* Parameters: unsigned long StartSecNum -- Start Sector Number
* unsigned long EndSecNum -- End Sector Number
* Return: unsigned long -- Status Code
*
* Description: This command is used to erase a sector or multiple sectors of on-chip Flash
* memory.
*
*************************************************************************/
unsigned long IAP_EraseSec (unsigned long StartSecNum, unsigned long EndSecNum)
{
if (EndSecNum < StartSecNum)
return IAP_STA_INVALD_PARAM;
command[0] = IAP_CMD_EraseSec;
command[1] = StartSecNum;
command[2] = EndSecNum;
command[3] = IAP_CLK / 1000;
iap_entry(command, result);
return result[0];
}
/*************************************************************************
* Function Name: IAP_BlankChkSec
* Parameters: unsigned long StartSecNum -- Start Sector Number
* unsigned long EndSecNum -- End Sector Number
* Return: unsigned long -- Status Code
*
* Description: This command is used to blank check a sector or mutilple sectors of on-chip
* Flash memory.
*
*************************************************************************/
unsigned long IAP_BlankChkSec (unsigned long StartSecNum, unsigned long EndSecNum,
unsigned long * pResult)
{
if (EndSecNum < StartSecNum)
return IAP_STA_INVALD_PARAM;
command[0] = IAP_CMD_BlankChkSec;
command[1] = StartSecNum;
command[2] = EndSecNum;
iap_entry(command, result);
if (result[0] == IAP_STA_SECTOR_NOT_BLANK)
{
*pResult = result[0];
*(pResult+1) = result[1];
}
return result[0];
}
/*************************************************************************
* Function Name: IAP_ReadParID
* Parameters: unsigned long * PartID
* Return: unsigned long -- Status Code
*
* Description: This command is used to read the part identification number.
*
*************************************************************************/
unsigned long IAP_ReadParID (unsigned long * PartID)
{
command[0] = IAP_CMD_ReadParID;
iap_entry(command, result);
*PartID = result[1];
return result[0];
}
/*************************************************************************
* Function Name: IAP_ReadBootVer
* Parameters: char * MajorVer
* char * MinorVer
* Return: unsigned long -- Status Code
*
* Description: This command is used to read the boot code version number.
*
*************************************************************************/
unsigned long IAP_ReadBootVer (unsigned long * MajorVer, unsigned long * MinorVer)
{
command[0] = IAP_CMD_ReadBootVer;
iap_entry(command, result);
*MajorVer = (result[1] & 0xff00)>>8;
*MinorVer = result[1] & 0xff;
return result[0];
}
/*************************************************************************
* Function Name: IAP_Compare
* Parameters: unsigned long dst -- Destination Flash address
* unsigned long src -- Source RAM address
* unsigned long number -- Should be in mutilple of 4
* Return: unsigned long -- Status Code
*
* Description: This command is used to compary the memory contents at two locations.
*
* NOTE: Compary result may not be correct when source or destination address contains
* any of the first 64 bytes starting from address zero. First 64 bytes can be re-mapped
* to RAM.
*
*************************************************************************/
unsigned long IAP_Compare (unsigned long dst, unsigned long src,
unsigned long number, unsigned long *offset)
{
command[0] = IAP_CMD_Compare;
command[1] = dst;
command[2] = src;
command[3] = number;
iap_entry(command, result);
if (result[0] == IAP_STA_COMPARE_ERROR)
*offset = result[1];
return result[0];
}
void IAP_ReinvokeISP(void)
{
command[0] = IAP_CMD_REINVOKEISP;
iap_entry(command, result);;
}
=====================================
===========IAP.H=============
#ifndef __IAP_H
#define __IAP_H
/* IAP Command */
#define IAP_CMD_PrepareSec 50 //select sector
#define IAP_CMD_CopyRAMToFlash 51 //copy data from ram to flash
#define IAP_CMD_EraseSec 52 //erase sector
#define IAP_CMD_BlankChkSec 53 //check if sector is blank
#define IAP_CMD_ReadParID 54 //read chip ID
#define IAP_CMD_ReadBootVer 55 //read BOOT version
#define IAP_CMD_Compare 56 //compare
#define IAP_CMD_REINVOKEISP 57 //reinvoke ISP command
/* IAP Status Codes */
#define IAP_STA_CMD_SUCCESS 0
#define IAP_STA_INVALID_COMMAND 1
#define IAP_STA_SRC_ADDR_ERROR 2
#define IAP_STA_DST_ADDR_ERROR 3
#define IAP_STA_SRC_ADDR_NOT_MAPPED 4
#define IAP_STA_DST_ADDR_NOT_MAPPED 5
#define IAP_STA_COUNT_ERROR 6
#define IAP_STA_INVALID_SECTOR 7
#define IAP_STA_SECTOR_NOT_BLANK 8
#define IAP_STA_SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION 9
#define IAP_STA_COMPARE_ERROR 10
#define IAP_STA_BUSY 11
#define IAP_STA_INVALD_PARAM 12
extern unsigned long IAP_PrepareSec (unsigned long, unsigned long);
extern unsigned long IAP_CopyRAMToFlash (unsigned long dst, unsigned long src,
unsigned long number);
extern unsigned long IAP_EraseSec (unsigned long StartSecNum, unsigned long EndSecNum);
extern unsigned long IAP_BlankChkSec (unsigned long StartSecNum, unsigned long EndSecNum,
unsigned long * pResult);
extern unsigned long IAP_ReadParID (unsigned long * PartID);
extern unsigned long IAP_ReadBootVer (unsigned long * MajorVer, unsigned long * MinorVer);
extern unsigned long IAP_Compare (unsigned long dst, unsigned long src,
unsigned long number, unsigned long *offset);
extern void IAP_ReinvokeISP(void);
#endif