/*_____ I N C L U D E S _________________________________*/
#include "crc.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 _________________________*/
uint8_t	code *AddrprtCRCOption;

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


/**
 *	@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
}

/**
 *	@Brief		Initialize CRC status
 *	@param		bMode, bRange
 *	@retval		None
 */
void CRC_Initialization(uint8_t bMode, uint8_t bRange)
{		
  /*	Initializes and configures the CRC function */
  CRCM = bMode | bRange | mskCRCM_ENABLE;
  CRCM |= mskCRCM_RESET;	
  AddrprtCRCOption = (uint8_t	code *)&CRC_CHECK_DATA;
}

/**
 *	@Brief		Start to calculate CRC and return calculation result
 *	@param		None
 *	@retval		dbCrcResult
 */
uint16_t CRC_ExecuteRnageCalculation(void)
{
  uint16_t dbCrcResult;
  
  /*  Seed initialization and re-start calculation  */
  CRCM |= mskCRCM_RESET;													
  CRCM |= mskCRCM_RUN;		
  
  /*	Check CRC calculation done	*/
  while((CRCM & mskCRCM_BUSY)!=0);
  
  dbCrcResult = (CRCDATH << 8) | CRCDATL; 
  return dbCrcResult;
}

/**
 *	@Brief		Accumulate ROM by using one byte CRC calculation
 *            Instruction will be stop as CRC executing
 *	@param		*bptrData, dbLength
 *	@retval		CRCDAT
 */
uint16_t CRC_ExecuteRomByteCalculation(uint8_t	code *bptrData, uint16_t dbLength)
{
  uint16_t i;
  
  CRCM |= mskCRCM_RESET;		
  for(i = 0x0; i < dbLength; i++)
  {
    CRCDATL = *bptrData;
    while((CRCM & mskCRCM_BUSY)!=0);
    bptrData ++;
  }
  return ((CRCDATH << 8) | CRCDATL);
}

/**
 *	@Brief		Accumulate RAM by using one byte CRC calculation
 *            Instruction will be stop as CRC executing
 *	@param		*bDataBuf, dbLength
 *	@retval		CRCDAT
 */
uint16_t CRC_ExecuteRamByteCalculation(uint8_t *bDataBuf, uint16_t dbLength)
{
  uint16_t i;
  
  CRCM |= mskCRCM_RESET;		
  for(i = 0x0; i < dbLength; i++)
  {
    CRCDATL = *bDataBuf;
    while((CRCM & mskCRCM_BUSY)!=0);
    bDataBuf ++;
  }
  return ((CRCDATH << 8) | CRCDATL);
}

/**
 *	@Brief		Get Sonix tool CRC verify data from option file
 *	@param		None
 *	@retval		dbAppResult
 */
uint16_t CRC_GetVerifyData(void)
{
  uint16_t dbAppResult;
  dbAppResult = (*(AddrprtCRCOption+0)<<8) + *(AddrprtCRCOption+1);
  return dbAppResult;
}

