Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2012791
  • 博文数量: 356
  • 博客积分: 8284
  • 博客等级: 中将
  • 技术积分: 4580
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-15 20:25
个人简介

天行健,君子以自强不息

文章分类

全部博文(356)

文章存档

2018年(1)

2016年(4)

2015年(13)

2014年(14)

2013年(2)

2012年(25)

2011年(43)

2010年(65)

2009年(189)

IAP

分类: C/C++

2011-11-20 16:36:56

#include "LPC17xx.h"                                                    /* LPC17xx外设寄存器            */
/*********************************************************************************************************
  变量与宏定义
*********************************************************************************************************/
#define LED        (1 << 2)                                             /* LED灯                        */
#define BEEP       (1 << 11)                                            /* 蜂鸣器                       */
#define KEY1       (1 << 1)                                             /* P2.1连接KEY1                 */
#define BEEPOFF()   LPC_GPIO0->FIODIR |= BEEP;LPC_GPIO0->FIOSET = BEEP  /* 蜂鸣器关                     */
#define BEEPON()    LPC_GPIO0->FIODIR |= BEEP;LPC_GPIO0->FIOCLR = BEEP  /* 蜂鸣器开                     */
#define IAP_ENTER_ADR   0x1FFF1FF1                                      /* IAP入口地址定义              */
/*
 *  定义IAP命令字
 */                                    
#define IAP_Prepare             50                                      /* 选择扇区                     */
                                                                        /* 【起始扇区号、结束扇区号】   */                   
#define IAP_RAMTOFLASH          51                                      /* 拷贝数据 FLASH目标地址       */
                                                                        /* RAM源地址    【、写入字节数  */
                                                                        /* 系统时钟频率】               */
#define IAP_ERASESECTOR         52                                      /* 擦除扇区    【起始扇区号     */
                                                                        /* 结束扇区号、系统时钟频率】   */
#define IAP_BLANKCHK            53                                      /* 查空扇区    【起始扇区号、   */
                                                                        /* 结束扇区号】                 */
#define IAP_READPARTID          54                                      /* 读器件ID    【无】           */
#define IAP_BOOTCODEID          55                                      /* 读Boot版本号【无】           */
#define IAP_COMPARE             56                                      /* 比较命令    【Flash起始地址  */
                                                                        /* RAM起始地址、需要比较的      */
                                                                        /* 字节数】                     */
/*
 *  定义IAP返回状态字
 */
#define CMD_SUCCESS                                0
#define INVALID_COMMAND                            1
#define SRC_ADDR_ERROR                             2
#define DST_ADDR_ERROR                             3
#define SRC_ADDR_NOT_MAPPED                        4
#define DST_ADDR_NOT_MAPPED                        5
#define COUNT_ERROR                                6
#define INVALID_SECTOR                             7
#define SECTOR_NOT_BLANK                           8
#define SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION    9
#define COMPARE_ERROR                              10
#define BUSY                                       11

__align(4) uint8_t GucIapData[512];
/*
 *  定义CCLK值大小,单位为KHz
 */
#define IAP_FCCLK            (96000)
/*
 *  定义函数指针 
 */
void (*IAP_Entry) (uint32_t ulParam_tab[], uint32_t ulPesult_tab[]);
uint32_t  GulParamin[8];                                                  /* IAP入口参数缓冲区            */
uint32_t  GulParamout[8];                                                 /* IAP出口参数缓冲区            */
/*********************************************************************************************************
** Function name:       sectorPrepare
** Descriptions:        IAP操作扇区选择,命令代码50
** input parameters:    ucSec1:           起始扇区
**                      ucSec2:           终止扇区
** output parameters:   GulParamout[0]:    IAP操作状态码,IAP返回值    
** Returned value:      GulParamout[0]:    IAP操作状态码,IAP返回值                    
*********************************************************************************************************/
uint32_t  sectorPrepare (uint8_t ucSec1, uint8_t ucSec2)

    GulParamin[0] = IAP_Prepare;                                        /* 设置命令字                   */
    GulParamin[1] = ucSec1;                                             /* 设置参数                     */
    GulParamin[2] = ucSec2;                           
    (*IAP_Entry)(GulParamin, GulParamout);                              /* 调用IAP服务程序              */
  
    return (GulParamout[0]);                                            /* 返回状态码                   */
}
/*********************************************************************************************************
** Function name:       ramCopy
** Descriptions:        复制RAM的数据到FLASH,命令代码51
** input parameters:    ulDst:             目标地址,即FLASH起始地址。以512字节为分界
**                      ulSrc:             源地址,即RAM地址。地址必须字对齐
**                      ulNo:              复制字节个数,为512/1024/4096/8192
** output parameters:   GulParamout[0]:    IAP操作状态码,IAP返回值    
** Returned value:      GulParamout[0]:    IAP操作状态码,IAP返回值                    
*********************************************************************************************************/
uint32_t  ramCopy (uint32_t ulDst, uint32_t ulSrc, uint32_t ulNo)

    GulParamin[0] = IAP_RAMTOFLASH;                                     /* 设置命令字                   */
    GulParamin[1] = ulDst;                                              /* 设置参数                     */
    GulParamin[2] = ulSrc;
    GulParamin[3] = ulNo;
    GulParamin[4] = IAP_FCCLK;
    (*IAP_Entry)(GulParamin, GulParamout);                              /* 调用IAP服务程序              */
   
    return (GulParamout[0]);                                            /* 返回状态码                   */
}
/*********************************************************************************************************
** Function name:       sectorErase
** Descriptions:        扇区擦除,命令代码52
** input parameters:    ucSec1             起始扇区
**                      ucSec2             终止扇区92
** output parameters:   GulParamout[0]:    IAP操作状态码,IAP返回值
** Returned value:      GulParamout[0]:    IAP操作状态码,IAP返回值                    
*********************************************************************************************************/
uint32_t  sectorErase (uint8_t ucSec1, uint8_t ucSec2)

    GulParamin[0] = IAP_ERASESECTOR;                                    /* 设置命令字                   */
    GulParamin[1] = ucSec1;                                             /* 设置参数                     */
    GulParamin[2] = ucSec2;
    GulParamin[3] = IAP_FCCLK;
    (*IAP_Entry)(GulParamin, GulParamout);                              /* 调用IAP服务程序              */
  
    return (GulParamout[0]);                                            /* 返回状态码                   */
}
/*********************************************************************************************************
** Function name:       blankChk
** Descriptions:        扇区查空,命令代码53
** input parameters:    ucSec1:              起始扇区
**                      ucSec2:              终止扇区92
** output parameters:   GulParamout[0]:    IAP操作状态码,IAP返回值
** Returned value:      GulParamout[0]:    IAP操作状态码,IAP返回值                    
*********************************************************************************************************/
uint32_t  blankChk (uint8_t ucSec1, uint8_t ucSec2)

    GulParamin[0] = IAP_BLANKCHK;                                       /* 设置命令字                   */
    GulParamin[1] = ucSec1;                                             /* 设置参数                     */
    GulParamin[2] = ucSec2;
    (*IAP_Entry)(GulParamin, GulParamout);                              /* 调用IAP服务程序              */
    return (GulParamout[0]);                                            /* 返回状态码                   */
}
/*********************************************************************************************************
** Function name:       parIdRead
** Descriptions:        扇区查空,命令代码54
** input parameters:    无
** output parameters:   GulParamout[0]:    IAP操作状态码,IAP返回值
** Returned value:      GulParamout[0]:    IAP操作状态码,IAP返回值                    
*********************************************************************************************************/
uint32_t  parIdRead (void)

    GulParamin[0] = IAP_READPARTID;                                     /* 设置命令字                   */
    (*IAP_Entry)(GulParamin, GulParamout);                              /* 调用IAP服务程序              */
    return (GulParamout[0]);                                            /* 返回状态码                   */
}
/*********************************************************************************************************
** Function name:       codeIdBoot
** Descriptions:        扇区查空,命令代码55
** input parameters:    无
** output parameters:   GulParamout[0]:    IAP操作状态码,IAP返回值
** Returned value:      GulParamout[0]:    IAP操作状态码,IAP返回值                    
*********************************************************************************************************/
uint32_t  codeIdBoot (void)

    GulParamin[0] = IAP_BOOTCODEID;                                     /* 设置命令字                   */
    (*IAP_Entry)(GulParamin, GulParamout);                              /* 调用IAP服务程序              */
    return (GulParamout[0]);                                            /* 返回状态码                   */
}
/*********************************************************************************************************
** Function name:       dataCompare
** Descriptions:        校验数据,命令代码56
** input parameters:    ulDst:             目标地址,即RAM/FLASH起始地址。地址必须字对齐
**                      ulSrc:             源地址,即FLASH/RAM地址。地址必须字对齐
**                      ulNo:              复制字节个数,必须能被4整除
** output parameters:   GulParamout[0]:    IAP操作状态码,IAP返回值
** Returned value:      GulParamout[0]:    IAP操作状态码,IAP返回值                    
*********************************************************************************************************/
uint32_t  dataCompare (uint32_t ulDst, uint32_t ulSrc, uint32_t ulNo)

    GulParamin[0] = IAP_COMPARE;                                        /* 设置命令字                   */
    GulParamin[1] = ulDst;                                              /* 设置参数                     */
    GulParamin[2] = ulSrc;
    GulParamin[3] = ulNo;
    (*IAP_Entry)(GulParamin, GulParamout);                              /* 调用IAP服务程序              */
    return (GulParamout[0]);                                            /* 返回状态码                   */
}
/*********************************************************************************************************
** Function name:       main
** Descriptions:        通过RTC中断将芯片从深度掉电中唤醒,深度掉电唤醒后复位。短接JP17,全速运行以后,
**                      蜂鸣器会响一声(可通过Memery窗口查看0x1000开始的512字节数据是否被编程为0x55)
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
int main (void)
{  
    uint8_t ucErr;
    uint32_t i;
 SystemInit();                                                       /* 初始化目标板,切勿删除       */
   
    for (i = 0; i < 512; i++) {
       GucIapData[i] = 0x55;
    }  
    IAP_Entry = (void(*)())IAP_ENTER_ADR;                               /* 初始化函数指针IAP_Entry      */
    __disable_irq();                                                     /* 在IAP操作前必须关闭所有中断  */
   
    parIdRead();                                                        /* 读器件ID                     */
    codeIdBoot();                                                       /* 读Boot版本号                 */
  
    sectorPrepare(1,1);                                                 /* 准备扇区1                    */
    sectorErase(1,1);                                                   /* 擦除扇区1                    */
    blankChk(1,1) ;                                                     /* 查空扇区1                    */
  
    sectorPrepare(1,1);                                                 /* 选择扇区1                    */
    ramCopy(0x00001000, (uint32_t)GucIapData, 512);                     /* 写数据到扇区1                */
 ucErr = dataCompare(0x00001000, (uint32_t)GucIapData, 512);         /* 比较数据                     */
  
    __enable_irq();
  
    if (ucErr == CMD_SUCCESS) {                                         /* 成功,蜂鸣一声               */     
        BEEPON();
        for (i=0; i<0x3FFFFF; i++);
        BEEPOFF();
    } else {                                                            /* 失败,一直蜂鸣               */     
        BEEPON();
    }   
    while (1);
}
/*********************************************************************************************************
  End Of File
*********************************************************************************************************/
阅读(2471) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~