/* USER CODE BEGIN Header */
/* you can remove the copyright */
/*
 * Copyright 2020-2026 Yuntu Microelectronics Co., Ltd.
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * @file main.c
 * @brief
 *
 */

/* USER CODE END Header */
#include "Mcal.h"
/* Includes ------------------------------------------------------------------*/
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define EB_BUFFER_LENGTH 60
#define IB_BUFFER_LENGTH 10
#define MASTERSLAVELENGTH 10
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */

Spi_DataBufferType EbSrcDataBuffers[EB_BUFFER_LENGTH] = {0};
Spi_DataBufferType EbDecDataBuffers[EB_BUFFER_LENGTH] = {0};
Spi_DataBufferType IbSrcDataBuffers[IB_BUFFER_LENGTH] = {0};
Spi_DataBufferType IbDecDataBuffers[IB_BUFFER_LENGTH] = {0};
Spi_DataBufferType MasterSrcDataBuffers[MASTERSLAVELENGTH] = {0};
Spi_DataBufferType MasterDecDataBuffers[MASTERSLAVELENGTH] = {0};
Spi_DataBufferType SlaveSrcDataBuffers[MASTERSLAVELENGTH] = {0};
Spi_DataBufferType SlaveDecDataBuffers[MASTERSLAVELENGTH] = {0};
static uint8 BufferLoop = 0;
/* USER CODE END PV */

/* Private function declare --------------------------------------------------*/
/* USER CODE BEGIN PFDC */
void GptControl(void);

/* spi */
boolean BufferCompare(uint8 *da1, uint8 *da2, uint8 len)
{
    uint16 i = 0;
    for (i = 0; i < len; i++)
    {
        if (da1[i] != da2[i])
        {
            return FALSE;
        }
    }
    return TRUE;
}
/**
 * @brief SPI2主机测试
 *
 */
void Spi2Test(void)
{
    Spi_SetAsyncMode(SPI_INTERRUPT_MODE);
    while (2 == Spi_GetStatus())
        ;
    Spi_AsyncTransmit(SpiConf_SpiSequence_SpiSequence_1);
    Spi_AsyncTransmit(SpiConf_SpiSequence_SpiSequence_0);
    while (Spi_GetSequenceResult(SpiConf_SpiSequence_SpiSequence_1) != SPI_SEQ_OK)
    {
    }
    while (Spi_GetSequenceResult(SpiConf_SpiSequence_SpiSequence_0) != SPI_SEQ_OK)
    {
    }
    while (TRUE != BufferCompare(&MasterSrcDataBuffers[0], &SlaveDecDataBuffers[0], 10))
        // LOG("master ......");
        ;
    while (TRUE != BufferCompare(&SlaveSrcDataBuffers[0], &MasterDecDataBuffers[0], 10))
        // LOG("slave ......");
        ;
    for (BufferLoop = 0; BufferLoop < MASTERSLAVELENGTH; BufferLoop++)
    {
        MasterDecDataBuffers[BufferLoop] = 0;
        SlaveDecDataBuffers[BufferLoop] = 0;
    }
}
/* USER CODE END PFDC */
static void Board_Init(void);

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */


/**
 * @brief  The application entry point.
 * @retval int
 */
int main(void)
{
    /* USER CODE BEGIN 1 */
    Mcu_Init(&Mcu_Config);
    Mcu_InitClock(0);
#if (MCU_NO_PLL == STD_OFF)
    while (MCU_PLL_LOCKED != Mcu_GetPllStatus())
    {
        /* Wait for the configuration PLL lock  */
    }

    while (E_OK != Mcu_DistributePllClock())
    {
    }
#endif
    /* USER CODE END 1 */ 
    Board_Init();
    /* USER CODE BEGIN 2 */
    for (uint8 i = 0; i < GPT_LOGIC_CHANNEL_NUM; i++)
    {
        /*1ms*/
        Gpt_StartTimer(i, 60);
        /*enable*/
        Gpt_EnableNotification(i);
    }
    /* spi2 test */
    /*clear buffer*/
    for (BufferLoop = 0; BufferLoop < EB_BUFFER_LENGTH; BufferLoop++)
    {
        EbSrcDataBuffers[BufferLoop] = BufferLoop;
    }
    for (BufferLoop = 0; BufferLoop < IB_BUFFER_LENGTH; BufferLoop++)
    {
        IbSrcDataBuffers[BufferLoop] = BufferLoop;
    }
    for (BufferLoop = 0; BufferLoop < MASTERSLAVELENGTH; BufferLoop++)
    {
        MasterSrcDataBuffers[BufferLoop] = BufferLoop;
        SlaveSrcDataBuffers[BufferLoop] = (BufferLoop + 0xAU);
    }
    Spi_SetupEB(SpiConf_SpiChannel_SpiChannel_0, &MasterSrcDataBuffers[0], &MasterDecDataBuffers[0], 10);
    Spi_SetupEB(SpiConf_SpiChannel_SpiChannel_1, &SlaveSrcDataBuffers[0], &SlaveDecDataBuffers[0], 10);
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        /* USER CODE END WHILE */
        /* USER CODE BEGIN 3 */
        GptControl();
    }
    /* USER CODE END 3 */
}

static void Board_Init(void)
{
    CddDma_Init(&CddDma_Config);
    Port_Init(&Port_Config);
    Gpt_Init(&Gpt_Config);
    Spi_Init(&Spi_Config);
    Platform_Init(NULL_PTR);
}

/* USER CODE BEGIN 4 */
void AdcContinuousCallBack(void)
{
}
volatile uint32 timeTick = 0;
#define TIMERS 10
void Gpt_Lptmr0Callback(void)
{
    timeTick++;
}
void GptControl(void)
{
    static uint32 CurrentTime = 0;
    if (timeTick >= CurrentTime)
    {
        if (timeTick - CurrentTime >= TIMERS)
        {
            CurrentTime = timeTick;

            Spi2Test();
        }
    }
    else
    {
        if (0xFFFFFFFF - CurrentTime + timeTick >= TIMERS)
        {
            CurrentTime = timeTick;
        }
    }
}
/* USER CODE END 4 */
