/******************************************************************************
 * @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 "lin.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



uint8_t gau8ScheduleTable_A[] = { 0x02, 0x03, 0x02, 0x04, 0x02 };
uint8_t au8DataFieldLengthTable[64] =
{
    8, 8, 8, 1, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8
};


uint8_t au8Data[8] = { 0 };

const uint32_t gu32BaudRate = 9600;




int main(void)
{
    /* Set Flash timing */
    FLASH_WALLOW();
    FLASH_SetTiming(200000000);
    FLASH_WDIS();

    /* Clock init */
    CLOCK_InitWithRCO(200000000UL);

    /* Delay init */
    Delay_Init();


    /* LED Init */
    GPIO_SetPinChannel(GPIO_30, GPIO30_GPIO30);
    GPIO_SetPinDir(GPIO_30, GPIO_OUTPUT);
    GPIO_WritePin(GPIO_30, GPIO_LEVEL_HIGH);


    /* UART init */
    GPIO_SetPinChannel(GPIO_34, GPIO34_UART_TXD);
    GPIO_SetPinChannel(GPIO_35, GPIO35_UART_RXD);

    UART_Init(UART, gu32BaudRate);


#if (LIN_NODE == 1)     /* LIN Master */
    {
        int i, j;
        int i32TabelSize = sizeof(gau8ScheduleTable_A) / sizeof(gau8ScheduleTable_A[0]);

        uint32_t status;
        LIN_ReadWriteEnum RnW = LIN_WRITE;
        uint8_t u8ID = LIN_NODE;
        uint8_t u8Length;
        uint32_t u32Timeout = 3000 * 3000;

        for (i = 0; i < i32TabelSize; i++)
        {
            /* Get ID from table */
            u8ID = gau8ScheduleTable_A[i];

            /* Get data length for the selected ID */
            u8Length = au8DataFieldLengthTable[u8ID];


            if (RnW == LIN_WRITE)
            {
                /* Data to be sent */
                for (j = 0; j < u8Length; j++)
                {
                    au8Data[j] = 0x10 + i + j;
                }
            }

            /* Master frame control */
            status = LIN_MasterControl(RnW, gu32BaudRate, u8ID, au8Data, u8Length, u32Timeout);
            if (status != LIN_OK)
            {
                /* Close LED */
                GPIO_WritePin(GPIO_30, GPIO_LEVEL_LOW);
            }

            if (RnW == LIN_READ)
            {
                /* Check received data */
                for (j = 0; j < u8Length; j++)
                {

                }

            }
        }
    }
#elif (LIN_NODE == 2)   /* LIN Slave  */
    {
        int i, j;
        int i32TabelSize = sizeof(gau8ScheduleTable_A) / sizeof(gau8ScheduleTable_A[0]);

        uint32_t status;
        LIN_ReadWriteEnum RnW = LIN_WRITE;
        uint8_t u8ID;
        uint8_t u8ID_Tar;
        uint8_t u8Length;

        uint32_t u32Timeout = 3000 * 3000;

        for (i = 0; i < i32TabelSize; i++)
        {
            /* Get ID from table */
            u8ID_Tar = gau8ScheduleTable_A[i];

            /* Get data length for the selected ID */
            u8Length = au8DataFieldLengthTable[u8ID_Tar];

            if (RnW == LIN_WRITE)
            {
                /* Data to be sent */
                for (j = 0; j < u8Length; j++)
                {
                    au8Data[j] = 0x20 + i + j;
                }
            }

            status = LIN_SlaveControl(RnW, &u8ID, au8Data, u8Length, u32Timeout);
            if (status != LIN_OK)
            {
                /* Close LED */
                GPIO_WritePin(GPIO_30, GPIO_LEVEL_LOW);
            }

            if (RnW == LIN_READ)
            {
                /* Check received data */
                for (j = 0; j < u8Length; j++)
                {

                }
            }
        }
    }

#endif

    while (1)
    {

    }
}

