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

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

ErrorStatus                     status;
uint32_t                        pu32Buf[OperationLEN];
uint32_t                        i;


 /*************************************************************************************************************************
 *
 * @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);

  printf("Start to erase\n");
  /*Erase flash sector*/
  FLASH_EraseSector(OperationADDR);
  printf("Erase flash over\n");

  /*Read from the flash, to check if the flash erased success or not*/
  FLASH_Read(pu32Buf, OperationADDR, OperationLEN);
  for(i = 0; i < OperationLEN; i++)
  {
    if(pu32Buf[i] == 0xFFFFFFFF)
    {
      ;
    }
    else
    {
      printf("Erase flash fail\n");
      return 0;
    }
  }

  printf("Erase flash success\n");

  /*Generate the data need to be write to flash*/
  for(i = 0; i < OperationLEN; i++)
  {
    pu32Buf[i] = (i % 55);
  }
  /*Write data to flash*/
  status = FLASH_Program(pu32Buf, OperationADDR, OperationLEN);
  if(status == ERROR)
  {
    printf("Write to flash ERROR\n");
    return 0;
  }
  else
  {
    printf("Write flash success\n");
  }

  /* Disable flash write access after flash operation had done */
  FLASH_WDIS();
    
  while(1)
  {

  }
}
