/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.1
 *
 * @date        2025-04-31
 *
 * @attention
 *
 *  Copyright (C) 2026 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the Geehy Semiconductor Software License Agreement for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "stdio.h"
#include "Port.h"
#include "Dio.h"
#include "CDD_Usart.h"
#include "Mcu.h"
#include "Adc.h"
#include "Lin.h"
#include "Gpt.h"
#include "Wdg.h"
#include "Icu.h"
#include "Fee.h"
#include "Fls.h"
#include "TestCommon.h"
#include "Det.h"

//#define LIN_USED
//#define ICU_USED
//#define GPT_USED
#define FEE_USED

#ifdef LIN_USED
uint8 txData[8] = {0x85,0x95,0xA4,0xB0,0xC3,0xD1,0xE6,0xF2};
uint8 txData1[1] = {0x55};
uint16 rxData[10] = {0};
uint8 testTxData[8]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}; 
uint8 *testRxData = NULL; 

/*!
 * @brief       send one frame data
 *
 * @param       channel: lin channel
 *              pid:frame pid data
 *              cs: select checksum
 *              drc: select master or slave
 *              dl: data length
 *              sdu: data ptr
 * 
 * @retval      0: send correctly
 *              -1: channel status error
 *              -2: channel status error
 *              -3: send data error
 *              -4: tx data busy
 *              -5: go to sleep fail
 *              -6: channel status error
 *
 * @note        Note
 */
int Lin_SendOneFrame(uint8 channel,
                     uint8 pid,
                     Lin_FrameCsModelType cs,
                     Lin_FrameResponseType drc,
                     uint8 dl,
                     uint8* sdu)
{
    uint32 timeOut = 0;
    Lin_PduType Pdu;
    Pdu.Pid = pid;
    Pdu.Cs = cs;
    Pdu.Drc = drc;
    Pdu.Dl = dl;
    Pdu.SduPtr = sdu;
    
    /* Enter wake-up detection state after initialization */
    if ((uint8)LIN_CH_SLEEP_STATE != g_LinChannelStatus[LIN_CONFIG_CHANNEL_0])
    {
        /* state error */
        return -1;
    }
    
    /* wake-up the lin master */
    //Lin_WakeupInternal(LIN_CONFIG_CHANNEL_0);
    Lin_Wakeup(LIN_CONFIG_CHANNEL_0);
    
    /* lin channel initialized success,entering communication */
    if ((uint8)LIN_CH_OPERATIONAL != g_LinChannelStatus[LIN_CONFIG_CHANNEL_0])
    {
        /* state error */
        return -2;
    }
    
    /* normal transmission of host data */
    if (E_OK != Lin_SendFrame(channel,&Pdu))
    {
        /* send error */
        printf("send data error!\r\n");
        return -3;
    }
    
    /* get the status of the LIN bus and check if the transmission was successful */
    while((LIN_TX_OK != Lin_GetStatus(LIN_CONFIG_CHANNEL_0,&testRxData)) && (timeOut < LIN_OPERATION_TIMEOUT))
    {
        timeOut++;
    }
    if (timeOut > LIN_OPERATION_TIMEOUT)
    {
        /* send error */
        printf("send data busy!\r\n");
        return -4;
    }
    
    printf("send data ok!\r\n");
    printf("send data is: ");
    for (int tempSize = 0; tempSize < 8; tempSize++)
    {
        printf(" %02X", sdu[tempSize]);
    }
    printf("\r\n");
    /* enter sleep mode directly after successfully sendling data */
    if (E_OK != Lin_GoToSleepInternal(LIN_CONFIG_CHANNEL_0))
    {
        /* api error */
        return -5;
    }
    
    /* set the status to sleep */
    if ((uint8)LIN_CH_SLEEP_STATE != g_LinChannelStatus[LIN_CONFIG_CHANNEL_0])
    {
        /* state error */
        return -6;
    }
    
    /* send ok */
    return 0;
}
#endif


Adc_ValueGroupType ResultBufferGroup0[ADC_CONFIG_GROUP0_CHANNEL_NUM * ADC_GROUP0_NUMBER_OF_SAMPLES];

void Adc_Notification_Group0(void)
{
    printf("group0 = %dmV  %dmV\r\n",(ResultBufferGroup0[0]* 3300) / 4095,
                                     (ResultBufferGroup0[3]* 3300) / 4095);
}

#ifdef ICU_USED
static void Icu_Tmr_SignalPeriodMeasure_CH0(void)
{
    char *l_p_name_test_case = "Icu_Tmr_SignalPeriodMeasure Example";
    uint32 l_circleCnt = 0xFF;

    print_test_start((uint8_t*)l_p_name_test_case);

    Icu_Init(&g_IcuConfig);

    NVIC_SetPriority(TMR3_IRQn, 0x0c);
    NVIC_EnableIRQ(TMR3_IRQn);

    Icu_StartSignalMeasurement(IcuChannel_0);

    printf("Give GPIOB7 Pwm signal:\r\n");

    while(l_circleCnt--)
    {
        /* Wait for the signal measure complete,the Delay only for 14KHz signal
         * if the peroid is longer,please adjust the delay time */
        sys_delay(100000);
        printf("IcuChannel_0 current signal peroid time is:0x%x\r\n", (int)Icu_GetTimeElapsed(IcuChannel_0)); 
    }

    Icu_StopSignalMeasurement(IcuChannel_0);
    print_test_end((uint8_t*)l_p_name_test_case);

    Icu_DeInit();
}

static void Icu_Tmr_SignalPeriodMeasure_CH1(void)
{
    char *l_p_name_test_case = "Icu_Tmr_SignalPeriodMeasure Example";
    uint32 l_circleCnt = 0xFF;

    print_test_start((uint8_t*)l_p_name_test_case);

    Icu_Init(&g_IcuConfig);

    NVIC_SetPriority(TMR3_IRQn, 0x0c);
    NVIC_EnableIRQ(TMR3_IRQn);

    Icu_StartSignalMeasurement(IcuChannel_1);

    printf("Give GPIOB6 Pwm signal:\r\n");

    while(l_circleCnt--)
    {
        /* Wait for the signal measure complete,the Delay only for 14KHz signal
         * if the peroid is longer,please adjust the delay time */
        sys_delay(100000);
        printf("IcuChannel_1 current signal peroid time is:0x%x\r\n", (int)Icu_GetTimeElapsed(IcuChannel_1)); 
    }

    Icu_StopSignalMeasurement(IcuChannel_1);
    print_test_end((uint8_t*)l_p_name_test_case);

    Icu_DeInit();
}
#endif

#ifdef FEE_USED

#define FEETEST_BLOCK0_SIZE               0x100
#define FEETEST_FIRST_SECTOR_ADDR         0x40000
#define FEETEST_FIRST_SECTOR_SIZE         0x400
#define FEETEST_FIRST_USED_SECTOR_NUM_MAX 32

/***************************************************************************
                        GLOBAL VARIABLES                                    
***************************************************************************/ 
uint8   FeeTest_WriteBuffer1[FEETEST_BLOCK0_SIZE] = {
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
}; 

uint8 FeeTest_WriteBuffer10[FEETEST_BLOCK0_SIZE] = {
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
};


uint8   FeeTest_WriteBuffer3[FEETEST_BLOCK0_SIZE] = {
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
}; 


uint8   FeeTest_WriteBuffer7[FEETEST_BLOCK0_SIZE] = {
0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
}; 


uint8   FeeTest_WriteBuffer0[FEETEST_BLOCK0_SIZE] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
}; 


uint8   FeeTest_WriteBuffer8[FEETEST_BLOCK0_SIZE] = {
0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
}; 

uint8   FeeTest_WriteBuffer6[FEETEST_BLOCK0_SIZE] = {
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 
}; 

uint8   FeeTest_WriteBuffer9[FEETEST_BLOCK0_SIZE] = {
0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 
}; 

/**************************************************************************
                      GLOBAL FUNCTION
**************************************************************************/
/*!
 * @brief       erase flash
 *
 * @param       stctorNum: stctor number
 *
 * @retval      None
 *
 * @note        Note
 */
static void Fee_FlsEraseSector(uint8 stctorNum)
{
    Fls_Erase(FEETEST_FIRST_SECTOR_ADDR + (FEETEST_FIRST_SECTOR_SIZE * stctorNum), FEETEST_FIRST_SECTOR_SIZE);

    while(Fls_GetStatus() == MEMIF_BUSY)
    {
        Fls_MainFunction();
    }
}

/*!
 * @brief       erase all flash
 *
 * @param       None
 *
 * @retval      None
 *
 * @note        Note
 */
void Fee_EraseAllUsedFlash(void)
{
    uint8 i;
    
    /* erase all lash in cycles */
    for(i = 0; i < FEETEST_FIRST_USED_SECTOR_NUM_MAX; i++)
    {
        Fee_FlsEraseSector(i);
    }
}


/*!
 * @brief       wait for the task to be completed
 *
 * @param       job : Result of the last job
 *
 * @retval      None
 *
 * @note        Note
 */
void Fee_WaitTask(MemIf_JobResultType job)
{
    while((Fee_GetJobResult() == job) &&
      (0 == g_detEventErrNum)&&
      (0 == g_detRuntimeEventErrNum))
    {
        Fee_MainFunction();
        Fls_MainFunction();
    }
}

/*!
 * @brief       get the block data from cluster
 *
 * @param       ch   : flash addr
 *              addr : Source address in flash memory
 *              buf  : Pointer to target data buffer
 *
 * @retval      None
 *
 * @note        Note
 */
Std_ReturnType getFalshData(char * ch,uint32_t addr,uint8_t * buf)
{
    Std_ReturnType u8RetVal = (Std_ReturnType)E_NOT_OK;
    
    u8RetVal = Fls_Compare(addr,buf, FEETEST_BLOCK0_SIZE);
    
    if (E_OK == u8RetVal)
    {
        while(Fls_GetStatus() != MEMIF_IDLE)
        {
            Fls_MainFunction();
        }
        if ((0 == g_detEventErrNum) && (0 == g_detRuntimeEventErrNum))
        {
            printf("\r\n\r\nCluster1 %s addr data ok!\r\n",ch);
            u8RetVal = E_OK;
        }
    }
    else
    {
        printf("\r\n\r\nCluster1 %s addr data error!\r\n",ch);
    }
    return u8RetVal;
}

#endif

volatile uint16 g_Count = 0;

void Gpt_NotificationChannel_Timer6_Channel1(void)
{
    g_Count++;
    printf("%d millisecond have passed\r\n", 10 * g_Count);
    Gpt_DisableNotification(Gpt_ConfigRef_GptChannelConfiguration_1);
    Gpt_StopTimer(Gpt_ConfigRef_GptChannelConfiguration_1);
}

void Mcu_LvdNotification()
{
    if(BIT_SET == Mcu_Eint_ReadIntFlag())
    {
        Mcu_Eint_ClearIntFlag();
        printf("LVD notification\r\n");
    }
    else
    {
        
    }
}

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    Mcu_Init(&g_mcuConfig);
    Mcu_InitClock(McuClockSettingConfig_0);

    NVIC_SetPriority(PVD_IRQn, 0x0f);
    NVIC_EnableIRQ(PVD_IRQn);

    Port_Init(&g_portConfig);
    Usart_Init(g_usartConfg);

    NVIC_EnableIRQ(USART2_IRQn);
    NVIC_EnableIRQ(USART1_IRQn);
    
#ifdef LIN_USED
    Lin_Init(&g_linConfig);
  #ifdef LIN_MASTER_NODECFG
    Lin_SendOneFrame(LIN_CONFIG_CHANNEL_0, /* channel 0 */
                   0x10,                                     /* master request frame */
                   LIN_CLASSIC_CS,                           /* classic checksum */
                   LIN_MASTER_RESPONSE,                      /* send using master */
                   8U,                                       /* 8 bytes sent */
                   testTxData) ;                             /* send data */
  #endif
    
    
#endif
    
#ifdef ADC_USED
    NVIC_EnableIRQ(ADC_IRQn);

    Adc_Init(&g_adcConfig);
    
    Adc_SetupResultBuffer(ADC_GROUP_0,ResultBufferGroup0);
    
    Adc_EnableGroupNotification(ADC_GROUP_0);

    Adc_StartGroupConversion(ADC_GROUP_0);
#endif

#if defined(WDG_USED) || defined(GPT_USED)
    Gpt_Init(&g_gptConfg);
#endif

#ifdef GPT_USED
    Gpt_Init(&g_gptConfg);
    NVIC_EnableIRQ(TMR6_IRQn);
    Gpt_EnableNotification(Gpt_ConfigRef_GptChannelConfiguration_1);
    Gpt_StartTimer(Gpt_ConfigRef_GptChannelConfiguration_1,39999);
#endif

#ifdef WDG_USED
    NVIC_EnableIRQ(TMR1_CC_IRQn);
    Wdg_Init(&g_wdgConfig);
    Wdg_SetTriggerCondition(300); //feed dog
#endif

#ifdef ICU_USED
    Icu_Tmr_SignalPeriodMeasure_CH0();
    Icu_Tmr_SignalPeriodMeasure_CH1();
#endif

#ifdef FEE_USED
    uint8 flag = 0;
    Std_ReturnType apiReturn;
    
    uint8 testReadBuffer0[FEETEST_BLOCK0_SIZE] = {0};
    uint8 testReadBuffer1[FEETEST_BLOCK0_SIZE] = {0};
    uint8 testReadBuffer2[FEETEST_BLOCK0_SIZE] = {0};
    
    Fls_Init(&g_flsConfig);
    Fee_Init(NULL_PTR);

    /* wait for the task to be completed */
    Fee_WaitTask(MEMIF_JOB_PENDING);
    
    /* erase flash */
    Fee_EraseAllUsedFlash();
    
    /* wait for the task to be completed */
    Fee_WaitTask(MEMIF_JOB_PENDING);
    
    /* check if the status of the fee is IDLE */
    if (MEMIF_IDLE != Fee_GetStatus())
    {
        flag = 1;
    };
    
    /* write data to the address */
    apiReturn = Fee_Write(FeeConf_FeeBlockConfiguration_FeeBlockConfiguration_0, FeeTest_WriteBuffer3);
    
    /* wait for the task to be completed */
    if(apiReturn == E_OK)
    {
        Fee_WaitTask(MEMIF_JOB_PENDING);
    }
    else
    {
        flag = 1;
    }
    
    /* write data to the address */
    apiReturn = Fee_Write(FeeConf_FeeBlockConfiguration_FeeBlockConfiguration_1, FeeTest_WriteBuffer7);
    
    /* wait for the task to be completed */
    if(apiReturn == E_OK)
    {
        Fee_WaitTask(MEMIF_JOB_PENDING);
    }
    else
    {
        flag = 1;
    }
    
    /* filled with cluster0 and cluster1,trigger paging mechanism */
    apiReturn = Fee_Write(FeeConf_FeeBlockConfiguration_FeeBlockConfiguration_2, FeeTest_WriteBuffer0);

    if(apiReturn == E_OK)
    {
        Fee_WaitTask(MEMIF_JOB_PENDING);
    }
    else
    {
        flag = 1;
    }
    
    if (MEMIF_IDLE != Fee_GetStatus())
    {
        flag = 1;
    };
    
    apiReturn = Fee_Write(FeeConf_FeeBlockConfiguration_FeeBlockConfiguration_1, FeeTest_WriteBuffer8);

    if(apiReturn == E_OK)
    {
        Fee_WaitTask(MEMIF_JOB_PENDING);
    }
    else
    {
        flag = 1;
    }
    
    if (MEMIF_IDLE != Fee_GetStatus())
    {
        flag = 1;
    };
    
    apiReturn = Fee_Write(FeeConf_FeeBlockConfiguration_FeeBlockConfiguration_1, FeeTest_WriteBuffer7);

    if(apiReturn == E_OK)
    {
        Fee_WaitTask(MEMIF_JOB_PENDING);
    }
    else
    {
        flag = 1;
    }
    
    if (MEMIF_IDLE != Fee_GetStatus())
    {
        flag = 1;
    };
    
    apiReturn = Fee_Write(FeeConf_FeeBlockConfiguration_FeeBlockConfiguration_2, FeeTest_WriteBuffer6);

    if(apiReturn == E_OK)
    {
        Fee_WaitTask(MEMIF_JOB_PENDING);
    }
    else
    {
        flag = 1;
    }
    
    if (MEMIF_IDLE != Fee_GetStatus())
    {
        flag = 1;
    };
    
    apiReturn = Fee_Write(FeeConf_FeeBlockConfiguration_FeeBlockConfiguration_0, FeeTest_WriteBuffer9);

    if(apiReturn == E_OK)
    {
        Fee_WaitTask(MEMIF_JOB_PENDING);
    }
    else
    {
        flag = 1;
    }
    
    if (MEMIF_IDLE != Fee_GetStatus())
    {
        flag = 1;
    };
    
    apiReturn = Fee_Write(FeeConf_FeeBlockConfiguration_FeeBlockConfiguration_1, FeeTest_WriteBuffer1);

    if(apiReturn == E_OK)
    {
        Fee_WaitTask(MEMIF_JOB_PENDING);
    }
    else
    {
        flag = 1;
    }
    
    if (MEMIF_IDLE != Fee_GetStatus())
    {
        flag = 1;
    };
    

    apiReturn = Fee_Read(FeeConf_FeeBlockConfiguration_FeeBlockConfiguration_0, 0, testReadBuffer0, FEETEST_BLOCK0_SIZE);

    if(apiReturn == E_OK)
    {
        Fee_WaitTask(MEMIF_JOB_PENDING);
    }
    else
    {
        flag = 1;
    }
    
    if (MEMIF_IDLE != Fee_GetStatus())
    {
        flag = 1;
    };
        
    apiReturn = Fee_Read(FeeConf_FeeBlockConfiguration_FeeBlockConfiguration_1, 0, testReadBuffer1, FEETEST_BLOCK0_SIZE);

    if(apiReturn == E_OK)
    {
        Fee_WaitTask(MEMIF_JOB_PENDING);
    }
    else
    {
        flag = 1;
    }
    
    if (MEMIF_IDLE != Fee_GetStatus())
    {
        flag = 1;
    };
    
    apiReturn = Fee_Read(FeeConf_FeeBlockConfiguration_FeeBlockConfiguration_2, 0, testReadBuffer2, FEETEST_BLOCK0_SIZE);

    if(apiReturn == E_OK)
    {
        Fee_WaitTask(MEMIF_JOB_PENDING);
    }
    else
    {
        flag = 1;
    }
    
    if (MEMIF_IDLE != Fee_GetStatus())
    {
        flag = 1;
    };

    /* erase flash */
    Fee_EraseAllUsedFlash();
    
    /* wait for the task to be completed */
    Fee_WaitTask(MEMIF_JOB_PENDING);
    
    /* check if the status of the fee is IDLE */
    if (MEMIF_IDLE != Fee_GetStatus())
    {
        flag = 1;
    };
    
    if (flag == 0)
    {
        printf("demo ok!");
    }
    else
    {
        printf("demo error!");
    }
    
    
#endif

    while(1)
    {
        #if defined LIN_USED && defined LIN_SLAVE_NODECFG
            if(0 != g_receiveOnceFlg)
            {
                g_receiveOnceFlg = 0;
                printf("Slave mode received data: ");
                for(int i = 0;i < 8; i++)
                {
                    printf("0x%x ",g_responseBuf[i]);
                }
                printf("\r\n");
            }
            else
            {
                /* do nothing */
            }
        #endif
    }
}

#if defined (__CC_ARM) || defined (__ICCARM__) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @param       *f:  pointer to a FILE that can recording all information
*              needed to control a stream
*
* @retval      The characters that need to be send.
*
* @note
*/
int fputc(int ch, FILE* f)
{
    int reVal = 0;
    reVal = Usart_PutRedefinition(USART1_ID,&ch);
    
    return reVal;
}

/* If configuring the full library using versions after IAR 9.30,
   it is necessary to implement the _write function for printf redirection.*/
size_t __write(int handle,const unsigned char *buf,size_t bufSize)
{
    int32 tempCh = 0;
    for(int i=0; i<bufSize; i++)
    {
        Usart_PutRedefinition(USART1_ID,&tempCh);
    }
    return bufSize;
}

#elif defined (__GNUC__)

/* For Arm Cross GCC: Eclipse */
int _write(int fd,const unsigned char *buf,int len)
{
    int32 tempCh = 0;
    for(int i=0; i<len; i++)
    {
        int32 tempCh = (int32)buf[i];
        Usart_PutRedefinition(USART1_ID,&tempCh);
    }
    return len;
}

#endif
/**@} end of group Template_Functions */
/**@} end of group Template */
/**@} end of group Examples */
