/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.0
 * @date     30-July-2026
 *
 * @note
 * Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd.. 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<stdio.h>

#if defined(SPC1158)
#include "spc1158.h"
#elif defined(SPD1148)
#include "spd1148.h"
#elif defined(SPD1178)
#include "spd1178.h"
#elif defined(SPD1188)
#include "spd1188.h"
#elif defined(SPD1163)
#include "spd1163.h"
#else
#include "spc1168.h"
#endif

#if defined(SPC1158)
#define                 OffsetADDRInFlash         0xC000                             /*Offset in entire flash memory*/
#else
#define                 OffsetADDRInFlash         0x10000                             /*Offset in entire flash memory*/
#endif

#define                 FlashBaseADDR             0x10000000                          /*Flash ADDR in SYS memory layout*/
#define                 OperationADDR             FlashBaseADDR + OffsetADDRInFlash
#define                 OperationLEN              0x400                               /*1kB data*/


ErrorStatus             status;
uint32_t                pu32Buf[OperationLEN] = {0};
uint32_t                i;

void Flash_Operation(void)
{
    ErrorStatus es;

    /* Erase flash sector */
    es = FLASH_EraseSector(OperationADDR);
    if (es == ERROR)
    {
        printf("flash erase fail\n");
        return;
    }

    /* Read from the flash, to check if the flash erased success or not */
    es = FLASH_Read(pu32Buf, OperationADDR, OperationLEN);
    if (es == ERROR)
    {
        printf("flash erase fail\n");
        return;
    }

    for (i = 0; i < OperationLEN; i++)
    {
        if (pu32Buf[i] == 0xFFFFFFFF)
        {
            goto Fail;
        }
    }

    /* 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("[%s:%d]Flash Sector Protection Test Success\n", __func__, __LINE__);
        return;
    }

Fail:
    printf("[%s:%d]Flash Sector Protection Test ERROR\n", __func__, __LINE__);
}


/*************************************************************************************************************************
*
* @brief      In this case, the partition 0x10000~0x10400 of flash had been set as the protected part, as a result, any
*             write and erase to this part will be fail. 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 stopped).
*
*************************************************************************************************************************/


int main()
{
    FLASH_WALLOW();

#if defined(SPC1158)
    FLASH_SetTiming(100000000);

    CLOCK_InitWithRCO(100000000UL);
#else
    FLASH_SetTiming(200000000);

    CLOCK_InitWithRCO(200000000UL);
#endif


    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);

    /* Write some datum into the sector be going to be protected */
    for (i = 0; i < OperationLEN; i++)
    {
        pu32Buf[i] = (i % 55);
    }
    /* Write data to flash */
    status = FLASH_Program(pu32Buf, OperationADDR, OperationLEN);
    if (status == ERROR)
    {
        printf("[%s:%d]Write to flash ERROR\n", __func__, __LINE__);
        return 0;
    }
    else
    {
        printf("[%s:%d]Write flash success\n", __func__, __LINE__);
    }

    /* Set 0x10000~0x10400 as the write protected area */
    FLASH_WriteProtect(OperationADDR, OperationADDR + OperationLEN);

    Flash_Operation();

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

    while (1)
    {

    }
}
