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

#include "data_manage.h"

int i;
char    *ret = "Test SUCCESS!\0";
struct  UserData_t  TestUserData;
struct  UserData_t  SaveUserData;
ErrorStatus status;


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

    memset(&TestUserData, 0x0, sizeof(struct UserData_t));
    memset(&SaveUserData, 0x0, sizeof(struct UserData_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((int)(rand() % 1024));
    }

    TestUserData.test2 = abs((int8_t)(rand() % 255));

    for (i = 0; i < 2; i++)
    {
        TestUserData.test3[i] = abs((int)(rand() % 1024));
    }

    /* Init eeprom struct */
    status = EEPROM_Init(EMULATION_START_ADDR, EMULATION_END_ADDR, sizeof(struct UserData_t));
    if (status == ERROR)
    {
        ret = "Init eeprom FAIL!\0";
        goto Error;
    }
    /* Write user data into eeprom */
    status = EEPROM_Write(&TestUserData);
    if (status == ERROR)
    {
        ret = "Write user data into eeprom FAIL!\0";
        goto Error;
    }
    /* Read user data from eeprom */
    EEPROM_Read(&SaveUserData);

    /* check the read back user data */
    for (i = 0; i < sizeof(TestUserData.myData) / sizeof(int8_t); i++)
    {
        if (TestUserData.myData[i] != SaveUserData.myData[i])
        {
            ret = "Test FAIL!\0";
            printf("myData[%d] : %d - %d\n", i, TestUserData.myData[i], SaveUserData.myData[i]);
            break;
        }
    }

    for (i = 0; i < sizeof(TestUserData.myData) / sizeof(uint32_t); i++)
    {
        if (TestUserData.test[i] != SaveUserData.test[i])
        {
            ret = "Test FAIL!\0";
            printf("test[%d] : %d - %d\n", i, TestUserData.test[i], SaveUserData.test[i]);
            break;
        }
    }

    if (TestUserData.test2 != SaveUserData.test2)
    {
        ret = "Test FAIL!\0";
        printf("test2 : %d - %d\n", TestUserData.test2, SaveUserData.test2);
    }

    for (i = 0; i < sizeof(TestUserData.myData) / sizeof(uint16_t); i++)
    {
        if (TestUserData.test3[i] != SaveUserData.test3[i])
        {
            ret = "Test FAIL!\0";
            printf("test3[%d] : %d - %d\n", i, TestUserData.test3[i], SaveUserData.test3[i]);
            break;
        }
    }

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

    printf("%s\n", ret);

    while (1)
    {

    }
}
