/* USER CODE BEGIN Header */
/* you can remove the copyright */

/*
 * Copyright 2020-2025 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 */
#include "pSIP_Dma.h"
/* USER CODE END Includes */

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

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
#define EB_BUFFER_LENGTH 16
#define IB_BUFFER_LENGTH 20
/* 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};
/* USER CODE END PV */

/* Private function declare --------------------------------------------------*/
/* USER CODE BEGIN PFDC */
/*uint8 buffer comapre function*/

/* 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 */
    /*
     * User case description:
     * 1. Sequence_0 -- Job_0 --------- SPI_0 -- Channel_0(IB) & Channel_6(EB) : with DMA, AsyncTransmit
     * 2. Sequence_1 -- Job_1 --------- SPI_1 -- Channel_1(EB)                 : with DMA, AsyncTransmit
     * 3. Sequence_2 -- Job_2 --------- SPI_2 -- Channel_2(EB) & Channel_7(EB) : None DMA, AsyncTransmit
     * 4. Sequence_3 -- Job_3 --------- SPI_3 -- Channel_3(EB)                 : None DMA, AsyncTransmit
     * 5. Sequence_4 -- Job_4 & Job_6-- SPI_4 -- Channel_4(EB)                 : with DMA, AsyncTransmit
     * 6. Sequence_5 -- Job_5 --------- SPI_5 -- Channel_5(EB) & Channel_8(IB) : None DMA, SyncTransmit
     */
    uint32 RunCount = 0;
    uint8 BufferLoop = 0;
    Mcu_Init(NULL_PTR);
    Mcu_InitClock(0);
#if (MCU_NO_PLL == STD_OFF)
    while (MCU_PLL_LOCKED != Mcu_GetPllStatus())
    {
        /* Busy wait until the System PLL is locked */
    }
    Mcu_DistributePllClock();
#endif
    /* USER CODE END 1 */ 
    Board_Init();
    /* USER CODE BEGIN 2 */
    for (BufferLoop = 0; BufferLoop < EB_BUFFER_LENGTH; BufferLoop++)
    {
        EbSrcDataBuffers[BufferLoop] = BufferLoop;
    }
    Spi_SetupEB(SpiConf_SpiChannel_SpiChannel_0, &EbSrcDataBuffers[0], &EbDecDataBuffers[0], 16);   /*Used by Job0*/
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    // while (1)
    {
        /* USER CODE END WHILE */
        /* USER CODE BEGIN 3 */
        /*Test case for Leve2 API Asynctransmit with interrupt mode*/
        Spi_SetAsyncMode(SPI_INTERRUPT_MODE);
        Spi_AsyncTransmit(SpiConf_SpiSequence_SpiSequence_0);
    }
    while (1)
    {

    }
    /* USER CODE END 3 */
}

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

/* USER CODE BEGIN 4 */
void DMA_CTSSetSrcAddr_REG(volatile DMA_Type *Base, uint8 Channel, uint32 Address)
{
    Base->CTS[Channel].SADDR = Address;
}

void DMA_CTSSetDestAddr_REG(volatile DMA_Type *Base, uint8 Channel, uint32 Address)
{
    Base->CTS[Channel].DADDR = Address;
}
void DMA_CTSSetTriggerCount_REG(volatile DMA_Type *Base, uint8 Channel, uint32 Count)
{
    uint16 RegValTemp;
    if (DMA_CTS_TCNT_LKDIS_LKEN_MASK == (Base->CTS[Channel].TCNTRV & DMA_CTS_TCNT_LKDIS_LKEN_MASK))
    {
        RegValTemp = Base->CTS[Channel].TCNTRV;
        RegValTemp &= (uint16) ~DMA_CTS_TCNT_LKEN_TCNT_MASK;
        RegValTemp |= (uint16) DMA_CTS_TCNT_LKEN_TCNT(Count); /*PRQA S 2985*/
        Base->CTS[Channel].TCNTRV = RegValTemp;

        RegValTemp = Base->CTS[Channel].TCNT.LKEN;
        RegValTemp &= (uint16) ~DMA_CTS_TCNT_LKEN_TCNT_MASK;
        RegValTemp |= (uint16) DMA_CTS_TCNT_LKEN_TCNT(Count); /*PRQA S 2985*/
        Base->CTS[Channel].TCNT.LKEN = RegValTemp;
    }
    else
    {
        RegValTemp = Base->CTS[Channel].TCNTRV;
        RegValTemp &= (uint16) ~DMA_CTS_TCNT_LKDIS_TCNT_MASK;
        RegValTemp |= (uint16) DMA_CTS_TCNT_LKDIS_TCNT(Count); /*PRQA S 2985*/
        Base->CTS[Channel].TCNTRV = RegValTemp;

        RegValTemp = Base->CTS[Channel].TCNT.LKDIS;
        RegValTemp &= (uint16) ~DMA_CTS_TCNT_LKDIS_TCNT_MASK;
        RegValTemp |= (uint16) DMA_CTS_TCNT_LKDIS_TCNT(Count); /*PRQA S 2985*/
        Base->CTS[Channel].TCNT.LKDIS = RegValTemp;
    }
}

/* USER CODE END 4 */
