/******************** (C) COPYRIGHT 2021 SONiX *******************************
* COMPANY:	SONiX
* DATE:		2021/12/27
* IC:			SN8F5754
*____________________________________________________________________________
* REVISION		Date				Author		Description
* 1.0					2021/12/27	SONiX			1. First release
*____________________________________________________________________________
*****************************************************************************/
/*_____ I N C L U D E S _________________________________*/
#define __XRAM_SFR_H__
#include <SN8F5754.h>
#include "crc.h"

/*_____ F U N C T I O N S _______________________________*/
void GPIO_Initialization(void);

/*_____ D E F I N I T I O N S ___________________________*/
#define ROM_SIZE 										0x4000
#define PAGE_SIZE 									0x20

/*_____ D E C L A R A T I O N S _________________________*/
extern uint16_t code CRC_CHECK_DATA;

/*_____ M A C R O S _____________________________________*/
#define	IO_PORT0_OUTPUT_LOW		P0M |= 0xFF;	P0 &= 0x00;
#define	IO_PORT1_OUTPUT_LOW		P1M |= 0xFF;	P1 &= 0x00;
#define	IO_PORT2_OUTPUT_LOW		P2M |= 0xFF;	P2 &= 0x00;
#define	IO_PORT3_OUTPUT_LOW		P3M |= 0xFF;	P3 &= 0x00;

#define OB_IO_OUTPUT_LOW   		P00=0;  P0M |=0x01; 
#define OB_IO_OUTPUT_HIGH 		P00=1;  P0M |=0x01;


/**
 *	@Brief		main: CRC Test Demo Function
 *	@param		None
 *	@retval		None
 */

void main(void)
{
  uint16_t dbCRC16_OneByte_Result;
  uint16_t dbCRC16_AppResult;
  uint16_t dbCRC16_RANGE_ADDR =	(ROM_SIZE - PAGE_SIZE - 0x02);
  uint16_t i;
  uint8_t	code *Addrptr;
  uint8_t	code *AddrprtCRCOption;
  
	/*	Initialize GPIO status	*/
	GPIO_Initialization();
	
  /*  Initialize and Configurate CRC data */
	Addrptr = 0x0000;
  AddrprtCRCOption = (uint8_t	code *)&CRC_CHECK_DATA;
	
	while(1)
	{
		/*	For example, one byte calculate with CRC CRC16-CCITT */
		/*	(1)	CRC mode configuration 
        (2) CRC seed initialization	*/
    CRCM = mskCRCM_CRC16 | mskCRCM_RANGE_BYPASS1 | mskCRCM_ENABLE;		/* (1)	*/
    CRCM |= mskCRCM_RESET;																						/* (2)	*/
		
		/* Accumulate ROM to last 1page and 2byte by using for loop one byte CRC calculation */
    for (i = 0x0; i < dbCRC16_RANGE_ADDR; i+= 0x1)
    {
      CRCDATL = *(Addrptr + i);
			while ((CRCM & mskCRCM_BUSY) != 0x00);
			dbCRC16_OneByte_Result = (CRCDATH << 8) | CRCDATL;	
    }
		/*	Compare wtih HW result and App result, the APP results were be set on OPTIONS_SN8F5754.A51	*/
		dbCRC16_AppResult = (*(AddrprtCRCOption+0)<<8) + *(AddrprtCRCOption+1);		
		
		/*	If HW CRC result is right, IO output high
				If HW CRC result is wrong, IO output low	*/
		if(dbCRC16_OneByte_Result==dbCRC16_AppResult)
		{
			OB_IO_OUTPUT_HIGH
		}
		else
		{
			OB_IO_OUTPUT_LOW
		}	
	}
}


/**
 *	@Brief		Initialize GPIO status
 *	@param		None
 *	@retval		None
 */
void GPIO_Initialization(void)
{
	/*	Set whole IOs to output low	*/
	IO_PORT0_OUTPUT_LOW
	IO_PORT1_OUTPUT_LOW
	IO_PORT2_OUTPUT_LOW
	IO_PORT3_OUTPUT_LOW
}

