/******************************************************************************
 * @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"data_manage.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define                         OffsetADDRInFlash                      0x10000
#define                         FlashBaseADDR                          0x10000000
#define                         OperationADDR                          FlashBaseADDR + OffsetADDRInFlash


int                             i;
int                             status = 0;
struct                          DataPack_t                             MyDataPackWriteTest;
struct                          DataPack_t                             MyDataPackReadTest;
struct                          UserData_t                             TestUserData;
#define                         OperationLEN                           0x800
uint32_t                        pu32Buf[OperationLEN];


 /*************************************************************************************************************************
 *
 * @brief      In this case, we need to put the object file flash.o(include all flash operation program code) into ram, and
 *             the other part code into flash, so that, the sys can still get the program data about flash operation from
 *             ram when flash is under processed (XIP was stoped).
 *
 *             The key point is when we decrease the FLASH CLK, the function 'FLASH_SetTiming' must be clled after HCLK had
 *             already been set.
 *
 *************************************************************************************************************************/


int main()
{
  FLASH_WALLOW();
  FLASH_SetTiming(200000000);

  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);
  
  memset(&TestUserData, 0x0, sizeof(struct UserData_t));
  memset(&MyDataPackReadTest, 0x0, sizeof(struct DataPack_t));
  memset(&MyDataPackWriteTest, 0x0, sizeof(struct DataPack_t));
  
  /* Init user struct data */
  for(i = 0; i < 6; i++)
    TestUserData.myData[i] = abs((int8_t)(rand() % 255));
  
  for(i = 0; i < 10; i++)
    TestUserData.test[i] = abs((uint32_t)(rand() % 1024));
  
  TestUserData.test2 = abs((int8_t)(rand() % 255));
  
  for(i = 0; i < 2; i++)
    TestUserData.test3[i] = abs((uint16_t)(rand() % 1024));
  
  MyDataPackWriteTest.sUserData = TestUserData;

  /* Init the control structure of emulation algorithm */
  status = DATA_AccessInit(&MyDataPackWriteTest, OperationADDR);
  if(!status)
    printf("DATA Write Access Init FAIL\n");
  
  /* Write the data into flash */
  status = DATA_Save(&MyDataPackWriteTest);
  if(!status)
    printf("DATA Write Save FAIL\n");
  
  /* Init the control structure of emulation algorithm */
  status = DATA_AccessInit(&MyDataPackReadTest, OperationADDR);
  if(!status)
    printf("DATA Read Access Init FAIL\n");
  
  /* Read out the data */
  DATA_Read(&MyDataPackReadTest);
  
  /* Do checking */
  for(i = 0; i < 6; i++)
  {
    if(MyDataPackReadTest.sUserData.myData[i] != MyDataPackWriteTest.sUserData.myData[i])
    {
      printf("@@Check myData FAIL R[%d]=%d    W[%d]=%d\n", i, MyDataPackReadTest.sUserData.myData[i], i, MyDataPackWriteTest.sUserData.myData[i]);
      return 0;
    }
  }
  
  for(i = 0; i < 10; i++)
  {
    if(MyDataPackReadTest.sUserData.test[i] != MyDataPackWriteTest.sUserData.test[i])
    {
      printf("Check test FAIL R[%d]=%d    W[%d]=%d\n", i, MyDataPackReadTest.sUserData.test[i], i, MyDataPackWriteTest.sUserData.test[i]);
      return 0;
    }
  }
  
  if(MyDataPackReadTest.sUserData.test2 != MyDataPackWriteTest.sUserData.test2)
  {
    printf("Check test2 FAIL R[%d]=%d    W[%d]=%d\n", i, MyDataPackReadTest.sUserData.test2, i, MyDataPackWriteTest.sUserData.test2);
    return 0;
  }
  
  for(i = 0; i < 2; i++)
  {
    if(MyDataPackReadTest.sUserData.test3[i] != MyDataPackWriteTest.sUserData.test3[i])
    {
      printf("Check test3 FAIL R[%d]=%d    W[%d]=%d\n", i, MyDataPackReadTest.sUserData.test3[i], i, MyDataPackWriteTest.sUserData.test3[i]);
      return 0;
    }
  }

  /* Disable flash write access after flash operation had done */
  FLASH_WDIS();
  
  printf("Test SUCCESS!\n");
    
  while(1)
  {

  }
}
