/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V1.8.0
 * @date     31-Dec-2021
 *
 * @note
 * Copyright (C) 2021 Spintrol Limited Corporation. All rights reserved.
 *
 * @attention
 * THIS SOFTWARE JUST PROVIDES CUSTOMERS WITH CODING INFORMATION REGARDING
 * THEIR PRODUCTS, WHICH AIMS AT SAVING TIME FOR THEM. SPINTROL SHALL NOT BE
 * LIABLE FOR THE USE OF THE SOFTWARE. SPINTROL DOES NOT GUARANTEE THE
 * CORRECTNESS OF THIS SOFTWARE AND RESERVES THE RIGHT TO MODIFY THE SOFTWARE
 * WITHOUT NOTIFICATION.
 *
 ******************************************************************************/

#include"spc1168.h"
#include<stdio.h>
#include<string.h>


#define             ExpectResult             0xA77

char                sCRCTestStr[100]         = "CRC calculate access done!";
int16_t             u16status                = -1;
uint32_t            u32Result;


 /*************************************************************************************************************************
 *
 * @brief      This data(string 'CRC calculate access done!') is calculated by a online CRC algorithm firstly, and save the
 *             result as 'ExpectResult', then compare the 'u32Result'(the result calculated by CRC mode) with 'ExpectResult',
 *             if not equal, there may be something wrong with CRC operation.
 *
 *************************************************************************************************************************/


int main()
{
  FLASH_WALLOW();
  FLASH_SetTiming(200000000);
  /* Disable flash write access after flash operation had done */
  FLASH_WDIS();

  CLOCK_InitWithRCO(CLOCK_HCLK_200MHZ);

  Delay_Init();

  /*
   * Init the UART
   *
   * 1.Set the GPIO34/35 as UART FUNC
   *
   * 2.Enable the UART CLK
   *
   * 3.Set the rest para
   */
  GPIO_SetPinChannel(GPIO_34,GPIO34_UART_TXD);
  GPIO_SetPinChannel(GPIO_35,GPIO35_UART_RXD);
  CLOCK_EnableModule(UART_MODULE);
  UART_Init(UART,38400);

  /* Disable CRC */
  CRC_Disable(CRC);

  /* Set CRC work as CCITT model */
  CRC_SetMode(CRC,CRC_MODE_16_CCITT);

  /* Enable CRC to work */
  CRC_Enable(CRC);

  /* Clear CRC INT */
  CRC_ClearInt(CRC);

  /* Enable Done Int */
  CRC_EnableInt(CRC);

  u32Result = CRC_Calculate(CRC,(uint8_t *)sCRCTestStr, strlen(sCRCTestStr));

  /* 
   * Enable Global Int(Be carefull : the global Int must be after the 'CRC_Calculate()' function,
   * other wise, the interruput will happend before the caculate done.)
   */
  NVIC_EnableIRQ(CRC_IRQn);

  while(1)
  {

  }
}

void CRC_IRQHandler(void)
{
  u16status = u32Result == ExpectResult ? TRUE : FALSE;
  if(u16status == TRUE)
    printf("The CRC caculating test is success\n");
  else
    printf("The CRC caculating test is fail, data is [%x] ,but the expecting [%x]\n",u32Result,ExpectResult);

  /* Clear CRC INT */
  CRC_ClearInt(CRC);

  /* Disable CRC Done INT */
  CRC_DisableInt(CRC);
}
