/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.3
 * @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>

#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(SPD1148)
    #define                 SDA_GPIO                   GPIO_31
    #define                 SCL_GPIO                   GPIO_30
    #define                 SDA_GPIO_FUNC              GPIO31_I2C_SDA
    #define                 SCL_GPIO_FUNC              GPIO30_I2C_SCL
#elif defined(SPD1163)
    #define                 SDA_GPIO                   GPIO_38
    #define                 SCL_GPIO                   GPIO_39
    #define                 SDA_GPIO_FUNC              GPIO38_I2C_SDA
    #define                 SCL_GPIO_FUNC              GPIO39_I2C_SCL
#else
    #define                 SDA_GPIO                   GPIO_13
    #define                 SCL_GPIO                   GPIO_12
    #define                 SDA_GPIO_FUNC              GPIO13_I2C_SDA
    #define                 SCL_GPIO_FUNC              GPIO12_I2C_SCL
#endif

#define                 DEBUG_INFO                 1
#define                 SLAVE_TRANSMIT             0                       /* 1: slave transmit 0: slave receive */

#define                 Rx_FIFO_INT_TH             0xf                    /* Rx threshold as 1 entry */
#define                 Tx_FIFO_INT_TH             0x0                    /* Tx threshold as 1 entry */
#define                 TxRx_DATA_LEN              16                     /* Send receive 16 Byte */
#define                 T_BUFFER_SIZE              128
#define                 I2C_SPEED                  400000
#define                 I2C_Slave_ADDR             0x9                    /* IIC Slave ADDR */

uint32_t                gu32BuffSize                = T_BUFFER_SIZE;
uint8_t                 gau8TxBuf[T_BUFFER_SIZE];
uint8_t                 gau8RxBuf[T_BUFFER_SIZE]; 
uint32_t                u32IsrCnt = 0;
ErrorStatus             estatus;
uint32_t                num;

static ErrorStatus Check_Receive_Data(void)
{
    int i;
    uint8_t u8Data = 0;

    /*To check the datum sent and received are the same*/
    for(i = 0; i < gu32BuffSize; i++)
    {
        if (gau8RxBuf[i] != u8Data)
        {
            #if DEBUG_INFO
            printf("[Error]@%4d: TX(0x%02X) != RX(0x%02X)\n", __LINE__, u8Data, gau8RxBuf[i]);
            #endif

            return ERROR;
        }
        else
        {
            #if DEBUG_INFO
            printf("%3d: TX(0x%02X) == RX(0x%02X)\n", i, u8Data, gau8RxBuf[i]);
            #endif
        }

        u8Data++;
    }
    
    return SUCCESS;
}

void I2C_Slave_TxRX_data(I2C_REGS* I2Cx)
{
    int i;
    uint8_t u8Data = 0;

    if (SLAVE_TRANSMIT)
    {
        /* Generate random datum to transmit */
        for(i = 0; i < gu32BuffSize; i++)
        {
            gau8TxBuf[i] = u8Data++;

            #if DEBUG_INFO
            printf("gau8TxBuf[%d] = 0x%x\n", i, gau8TxBuf[i]);
            #endif
        }

        printf("Slave Tx data...\n");

        I2C_SetTxFIFOThreshold(I2Cx, Tx_FIFO_INT_TH);

        I2C_EnableTxDataRequestInt(I2Cx);
    }
    else
    {
        printf("Slave Rx data...\n");

        I2C_SetRxFIFOThreshold(I2Cx, Rx_FIFO_INT_TH);

        I2C_EnableRxDataAvailableInt(I2Cx);
    }
}

/*************************************************************************************************************************
 *
 * @brief      In this case, the IIC controller is used as master and sent datum to the slave and read these datum back to
 *             compare, if there is a item not equal with each other, the test FAIL.
 *
 * @note
 * The slave device runs first, the master device runs later
 *
 *************************************************************************************************************************/


int main()
{
    FLASH_WALLOW();

#if defined(SPC1158)
    FLASH_SetTiming(100000000);
    /* Disable flash write access after flash operation had done */
    FLASH_WDIS();

    CLOCK_InitWithRCO(100000000UL);
#else
    FLASH_SetTiming(200000000);
    /* Disable flash write access after flash operation had done */
    FLASH_WDIS();

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

    /* Config GPIO as I2C function */
    GPIO_SetPinChannel(SDA_GPIO, SDA_GPIO_FUNC);
    GPIO_SetPinChannel(SCL_GPIO, SCL_GPIO_FUNC);

    /*
     * Set Output Strength as 20mA, in case of many more
     * slaves are linking to the master.
     */
    GPIO_SetOutStrength(SDA_GPIO, GPIO_OUT_STRENGTH_20MA);
    GPIO_SetOutStrength(SCL_GPIO, GPIO_OUT_STRENGTH_20MA);

    /* Disable All INT */
    I2C_DisableAllInt(I2C);

    /* Clear All INT */
    I2C_ClearInt(I2C, I2C_INT_ALL);

    /* Init I2C as Slave and set the speed as 400K */
    estatus = I2C_SlaveInit(I2C, I2C_ADDR_7BIT, I2C_Slave_ADDR, I2C_SPEED, I2C_SPEED_FAST);
    if (estatus == ERROR)
    {
        #if DEBUG_INFO
        printf("[IIC slave init FAIL] I2C clock is not fast enough to support the speed\n");
        #endif

        return 0;
    }

    /* Enable CM4 INT request */
    NVIC_EnableIRQ(I2C_IRQn);

    /* Slave start to transmit and receive data */
    I2C_Slave_TxRX_data(I2C);

    while (1)
    {

    }
}

void I2C_IRQHandler(void)
{
    int i;

    if (SLAVE_TRANSMIT && (u32IsrCnt < T_BUFFER_SIZE))
    {
        if (I2C_IsTxEmpty(I2C))
        {
            for(i = 0; i < TxRx_DATA_LEN; i++)
            {
                /* Wait Until Detect Read Request */
                while(!I2C_GetReadRequestIntRawFlag(I2C)) { }

                /* Clear Event Flag */
                I2C_ClearInt(I2C, I2C_INT_RD_REQ);
                /* Write TxFIFO, Send Data */
                I2C_WriteByte(I2C, gau8TxBuf[u32IsrCnt]);

                u32IsrCnt++;
            }

        }
    }
    else if (!SLAVE_TRANSMIT && (u32IsrCnt < T_BUFFER_SIZE))
    {
        while(I2C_IsRxNotEmpty(I2C))
        {
            gau8RxBuf[u32IsrCnt] = I2C_ReadByte(I2C);
            u32IsrCnt++;
        }
    }

    if (u32IsrCnt == T_BUFFER_SIZE)
    {
        if (SLAVE_TRANSMIT)
        {
            I2C_DisableTxDataRequestInt(I2C);
        }
        else
        {
            I2C_DisableRxDataAvailableInt(I2C);
            estatus = Check_Receive_Data();
            if (estatus == ERROR)
            {
                return;
            }
        }

        u32IsrCnt = 0;

        printf("Success\n");
    }

    I2C_ClearInt(I2C, I2C_INT_ALL);
}
