/******************** (C) COPYRIGHT 2021 SONiX *******************************
* COMPANY:	SONiX
* DATE:		2022/04/18
* AUTHOR:		SONiX
* IC:			SN8F5858
*____________________________________________________________________________
* REVISION		Date				User		Description
* 1.0					2022/04/18	SONiX		1. First release
*____________________________________________________________________________
*****************************************************************************/

/*_____ I N C L U D E S _________________________________*/
#include <SN8F5762.h>
#include "ISP.h"
/*_____ F U N C T I O N S _______________________________*/

/*_____ D E F I N I T I O N S ___________________________*/

/*_____ D E C L A R A T I O N S _________________________*/

/*_____ M A C R O S _____________________________________*/

/**
 *	@Brief		isp_page_write: ISP Page Write Function
 *	@param		None
 *	@retval		None
 */
uint8_t	ISP_WritePage_ChecksumVerify(uint8_t code* bRomAdress, uint8_t* bRamBufAddress)
{
	uint8_t i;
	uint16_t	dbRamChecksum = 0;
	uint16_t	dbRomResult = 0; 
	uint8_t code* ptrRom = (uint16_t*) ((uint16_t) bRomAdress & 0xFFC0);  
	
	/* Step1: Calculation RAM checksum	*/
	for(i=0; i<64 ;i++)
	{
		dbRamChecksum += *(bRamBufAddress+i);
	}
	
	/* Step2: (1)	Setting ISP	RAM Address 
						(2)	Setting ISP ROM Address	*/
	PERAM =(uint8_t) 	bRamBufAddress;
	PEROM =(uint16_t) bRomAdress;		
	PERAMCNT = 0x3F;                    /*  ISP programming length = PERAMCNT[5:0]+1 */
	PECMD = mskPECMD_PAGE_ERASE; 
	PECMD = mskPECMD_PAGE_WRITE;	
	
	/* Step3: Calculation ROM checksum	*/	
	for(i=0; i<64 ;i++)
	{
		dbRomResult += *(ptrRom+i);
	}	
	
	/* Step4: Compare RAM/ROM Checksum and return compare result */
	if(dbRomResult == dbRamChecksum)
	{
		return 1;	/*	ISP Success	*/
	}
	else
	{
		return 0;	/* ISP Failed	*/
	}
}

