/* 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 */
/* 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 CDDUART_MSG1 "Hi, Welcome to use this demo to test uart!\r\n"
#define CDDUART_MSG2 "Please input 5 data and we will send them back!\r\n"
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint8 CddUart_TestedRxBuffer[20];
uint8 RxBuffIndex = 0U;
boolean RxBufferIndex_0 = FALSE;
boolean RxBufferIndex_5 = FALSE;
/* USER CODE END PV */

/* Private function declare --------------------------------------------------*/
/* USER CODE BEGIN PFDC */
/* USER CODE END PFDC */
static void Board_Init(void);

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void Test_CddUart_CallBack(uint8 Channel, CddUart_General_EventType Event)
{
    if (UART_EVENT_RX_FULL == Event)
    {
        RxBuffIndex += 5U;
        if (RxBuffIndex >= 10U)
        {
            RxBuffIndex = 0U;
        }
        if (RxBuffIndex == 5U)
        {
            RxBufferIndex_0 = TRUE;
        }
        else
        {
            RxBufferIndex_5 = TRUE;
        }
        /* Set the buffer to receive data */
        CddUart_SetBuffer(CddUartConf_UartChannel_UartChannel_0, &CddUart_TestedRxBuffer[RxBuffIndex], 5, CDDUART_RECEIVE);
    }
}
/* 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())
    {
        /*Busy wait until the System Pll is locked*/
    }
    Mcu_DistributePllClock();
#endif
    /* USER CODE END 1 */ 
    Board_Init();
    /* USER CODE BEGIN 2 */
    CddUart_SyncSend(CddUartConf_UartChannel_UartChannel_0, (uint8 *)CDDUART_MSG1, sizeof(CDDUART_MSG1), 0xFFFF);

    Dio_FlipChannel(DioConf_DioChannel_PTE16);
    CddUart_AsyncSend(CddUartConf_UartChannel_UartChannel_0, (uint8 *)CDDUART_MSG2, sizeof(CDDUART_MSG2));
    Dio_FlipChannel(DioConf_DioChannel_PTE16);

    CddUart_AsyncReceive(CddUartConf_UartChannel_UartChannel_0, CddUart_TestedRxBuffer, 5);
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        /* USER CODE END WHILE */
        /* USER CODE BEGIN 3 */
        if (RxBufferIndex_0 == TRUE)
        {
            /* The reception is done,send the data back */
            CddUart_SyncSend(CddUartConf_UartChannel_UartChannel_0, &CddUart_TestedRxBuffer[0], 5, 0xFFFF);
            RxBufferIndex_0 = FALSE;
        }
        else if (RxBufferIndex_5 == TRUE)
        {
            CddUart_SyncSend(CddUartConf_UartChannel_UartChannel_0, &CddUart_TestedRxBuffer[5], 5, 0xFFFF);
            RxBufferIndex_5 = FALSE;
        }
        else
        {
            /* Do nothing */
        }
    }
    /* USER CODE END 3 */
}

static void Board_Init(void)
{
    CddDma_Init(&CddDma_Config);
    CddUart_Init(&CddUart_Config);
    Platform_Init(NULL_PTR);
    Port_Init(&Port_Config);
}

/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
