/******************** (C) COPYRIGHT 2021 SONiX *******************************
* COMPANY:	SONiX
* DATE:		2021/12/21
* AUTHOR:		Roger
* IC:			SN8F5754
*____________________________________________________________________________
* REVISION		Date				User		Description
* 1.0					2021/12/21	Roger		1. First release
*____________________________________________________________________________
*****************************************************************************/

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

/*_____ D E F I N I T I O N S ___________________________*/
#define ONE_PAGE_LENGTH           32
/*_____ 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_CRC16Verify(uint8_t code* bRomAdress, uint8_t* bRamBufAddress)
{
	uint8_t i;
	uint16_t	dbRamCrc = 0;
	uint16_t	dbRomResult = 0;
	uint8_t code* ptrRom = (uint16_t*) ((uint16_t) bRomAdress & 0xFFC0);  
	
	/* Step1: Calculation RAM checksum	*/
	dbRamCrc = CRC_ExecuteRamByteCalculation(bRamBufAddress, ONE_PAGE_LENGTH);
	
	/* Step2: (1)	Setting ISP	RAM Address 
						(2)	Setting ISP ROM Address	*/
	PERAM =(uint8_t) 	bRamBufAddress;
	PEROM =(uint16_t) bRomAdress;		
	PAGE_WRITE_START();
	
	/* Step3: Calculation ROM checksum	*/	
	dbRomResult = CRC_ExecuteRomByteCalculation(bRomAdress, ONE_PAGE_LENGTH);
	
	/* Step4: Compare RAM/ROM Checksum and return compare result */
	if(dbRomResult == dbRamCrc)
	{
		return 1;	/*	ISP Success	*/
	}
	else
	{
		return 0;	/* ISP Failed	*/
	}
}


