//************************************************************
//  Copyright (c) 深圳市赛元微电子股份有限公司
//	文件名称	: main.c
//	模块功能	: 
//  最后更正日期: 2026/6/18
// 	版本	  	: V1.0
//************************************************************
#include "IapOperation.h"
#include "SC92F5X5X_C.h"
#include "uart.h"
#include "stdio.h"

/**************************************************
*函数名称：void IAP_Write_NByte(unsigned long Sadd,unsigned long Eadd, unsigned char Byte)
*函数功能：写N个Byte
*入口参数：Sadd 写入的起始地址,Eadd 写入的结束地址，操作对象 EEPROM
*出口参数：void
**************************************************/
void IAP_Write_NByte(unsigned long Sadd,unsigned long Eadd, unsigned char Byte)
{
	for(; Sadd <= Eadd; Sadd ++)
	{
		IAP_EEPROMProgramByteOption(Sadd,Byte);
	}
}

/**************************************************
*函数名称：void IAP_Read_NByte(unsigned long Sadd,unsigned long Eadd,unsigned char Iapade)
*函数功能：读N个Byte
*入口参数：Sadd 读取的起始地址,Eadd 读取的结束地址，Iapade:操作对象 CODE为00，EEPROM为02
*出口参数：void
**************************************************/
void IAP_Read_NByte(unsigned long Sadd,unsigned long Eadd,unsigned char Iapade)
{

	for(; Sadd <= Eadd; Sadd ++)
	{
		if(Sadd % 16 == 0)
		{
			 printf("\r\n");
		}
        printf("0X%0.2x ",(int)IAP_Read(Sadd,Iapade));
	}
     printf("\r\n");
}


void main()
{ 
	unsigned char  KeyFree = 0;
	unsigned char  i = 0;
	P2CON &= ~0x20;
	P2PH  |= 0x20;
	
  //UART0初始化
  UartInit();
	printf("\r\n\r\n*******IAP 功能测试*******\r\n");

	while(1)
	{
		if(P25 == 0)      //IAP操作触发条件有效
		{
			for(i=0;i<100;i++)
			{
				WDTCON |=0x10;
			}
			
			if((P25 == 0) && (KeyFree == 0)) //有效一次
			{
				IapWriteFlag = IAP_ENABLE;  //解锁IAP(注意：解锁IAP和操作IAP不能在同一流程分支)
			}
		}
		else
		{
				KeyFree=0;
		}			

		if((P25==0)&&(KeyFree==0))  //IPA操作触发条件有效，操作IAP
		{
			KeyFree=1;
			
			printf("\r\n\r\n*******IAP 功能测试*******\r\n");
			
			EEPROM_WriteTime_Config(EEPROM_WriteTime_2ms);   //EEPROM写入时间设为2ms
			
			printf("*******清除EEPROM ：地址 0X00~0X7F*******\r\n");
			IAP_Write_NByte(0x0,0X7F, 0X00);
			
			printf("*******写EEPROM ：地址 0X00~0X7F*******\r\n");
			IAP_Write_NByte(0x0,0X3F, 0X01);
			IAP_Write_NByte(0x40,0X7F, 0X02);
			
			printf("*******读取EEPROM数据 ：地址 0X00~0X7F*******\r\n");
			IAP_Read_NByte(0x0,0x7F,0X02);
			
		}	
		IapWriteFlag = IAP_DISABLE;  //加锁IAP

	}		
}

