/*!
 * @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 "CDD_Usart.h"
#include "Mcu.h"
#include "TestCommon.h"
#include "Port.h"
#include "CDD_Pdl.h"
#include "TestCommon.h"
#include <string.h>

/** @addtogroup Examples
  @{
  */
static volatile uint8_t s_dmaCh1ComFlg = 0;
static volatile uint8_t s_dmaCh5ComFlg = 0;
static volatile uint8_t s_dmaCh5ErrFlg = 0;

static uint8_t s_srcBuff[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
static uint8_t s_destBuff[8] = {0};
static uint8_t s_destBuff1[8] = {0};

/* Function declaration */
void Dma_Channel1CompletionNotificaiton(void)
{
    s_dmaCh1ComFlg = 1;
    printf("Dma channel1 completed notification\r\n");
}

void Dma_Channel5CompletionNotificaiton(void)
{
    s_dmaCh5ComFlg = 1;
    printf("Dma channel5 completed notification\r\n");
}

void Dma_Channel5ErrNotificaiton(void)
{
    s_dmaCh5ErrFlg = 1;
    printf("Dma channel5 error notification\r\n");
}

/**
* @brief  DMA memory to memory.
*/
static TestResult Pdl_DmaMemoryToMemory(void)
{
    char *l_p_name_test_case = "Pdl_Dma_MemoryToMemory Example";
    TestResult result = TEST_FAILED;
    uint8_t l_step1 = 0;
    uint8_t l_step2 = 0;
    uint8_t l_step3 = 0;

    Pdl_DmaChHwConfigType dmaChHwConfig = {
        .periphAddr = (uint32)s_destBuff,
        .memAddr = (uint32)s_srcBuff,
        .dir = DMA_DIR_MEMORY,
        .bufSize = 8,
        .periphInc = DMA_PERIPHERAL_INC_ENABLE,
        .memInc = DMA_MEMORY_INC_ENABLE,
        .periphDataWidth = DMA_PERIPHERAL_DATASIZE_BYTE,
        .memDataWidth = DMA_MEMORY_DATASIZE_BYTE,
        .circularMode = DMA_CIRCULAR_DISABLE,
        .priority = DMA_PRIORITY_LEVEL_VERYHIGH,
        .mem2Mem = DMA_M2M_ENABLE
    };
    
    Pdl_DmaChHwConfigType dmaChHwConfig1 = {
        .periphAddr = (uint32)s_destBuff1,
        .memAddr = (uint32)s_srcBuff,
        .dir = DMA_DIR_MEMORY,
        .bufSize = 8,
        .periphInc = DMA_PERIPHERAL_INC_ENABLE,
        .memInc = DMA_MEMORY_INC_ENABLE,
        .periphDataWidth = DMA_PERIPHERAL_DATASIZE_BYTE,
        .memDataWidth = DMA_MEMORY_DATASIZE_BYTE,
        .circularMode = DMA_CIRCULAR_DISABLE,
        .priority = DMA_PRIORITY_LEVEL_VERYHIGH,
        .mem2Mem = DMA_M2M_ENABLE
    };

    Pdl_DmaChHwConfigType dmaChHwConfig2 = {
        .periphAddr = (uint32)0xffffffff,
        .memAddr = (uint32)0xffffffff,
        .dir = DMA_DIR_MEMORY,
        .bufSize = 8,
        .periphInc = DMA_PERIPHERAL_INC_DISABLE,
        .memInc = DMA_MEMORY_INC_ENABLE,
        .periphDataWidth = DMA_PERIPHERAL_DATASIZE_BYTE,
        .memDataWidth = DMA_MEMORY_DATASIZE_BYTE,
        .circularMode = DMA_CIRCULAR_DISABLE,
        .priority = DMA_PRIORITY_LEVEL_VERYHIGH,
        .mem2Mem = DMA_M2M_ENABLE
    };

    print_test_start((uint8_t*)l_p_name_test_case);
    
    print_test_step(TEST_STEP_1);

    Pdl_Init(&g_pdlConfig);

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

    NVIC_SetPriority(DMA_CH4_5_IRQn, 0x0f);
    NVIC_EnableIRQ(DMA_CH4_5_IRQn);
    
    Pdl_TopDmaChConfig(DMA_HW_CH1, &dmaChHwConfig);
    Pdl_TopDmaChEn(DMA_HW_CH1);

    sys_delay(10000);

    if((1 == s_dmaCh1ComFlg) && (0 == memcmp(s_srcBuff, s_destBuff, 8)))
    {
        l_step1 = 1;
        printf("Step 1 SUCCESSED\r\n");
    }
    else
    {
        printf("Step 1 FAILED\r\n");
    }
		
    print_test_step(TEST_STEP_2);
    Pdl_TopDmaChConfig(DMA_HW_CH5, &dmaChHwConfig1);
    Pdl_TopDmaChEn(DMA_HW_CH5);
    sys_delay(10000);
    if((1 == s_dmaCh5ComFlg) && (0 == memcmp(s_srcBuff, s_destBuff1, 8)))
    {
        l_step2 = 1;
        printf("Step 2 SUCCESSED\r\n");
    }
    else
    {
        printf("Step 2 FAILED\r\n");
    }

    print_test_step(TEST_STEP_3);
    memset(s_destBuff1, 0, 8);
    Pdl_TopDmaChConfig(DMA_HW_CH5, &dmaChHwConfig2);
    Pdl_TopDmaChEn(DMA_HW_CH5);
    sys_delay(10000);
    if(1 == s_dmaCh5ErrFlg)
    {
        l_step3 = 1;
        printf("Step 3 SUCCESSED\r\n");
    }
    else
    {
        printf("Step 3 FAILED\r\n");
    }

    if((1 == l_step1) && (1 == l_step2) && (1 == l_step3))
    {
        result = TEST_SUCCESS;
    }
    else
    {
        result = TEST_FAILED;
    }

    print_test_end((uint8_t*)l_p_name_test_case);

    return result;
}

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

    Port_Init(&g_portConfig);
    Usart_Init(g_usartConfg);  //TX PIN:PA9

    Pdl_DmaMemoryToMemory();

    while(1)
    {

    }
}

#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 */
