/* 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 Idle interrupt!\r\n"
#define CDDUART_MSG2 "Please input data less than 32 bytes and we will send them back!\r\n"
#define CDDUART_MSG3 "\nIdle interrupt is triggered!\nReceived data num:"
#define CDDUART_MSG4 "\nReceived data:"
#define CDDUART_MSG5 "\nReceived data is full!\nReceived data:"

#define  Max_Receive_Size 32U
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
volatile CddUart_General_EventType CddUart_Event;
uint8 CddUart_TestedRxBuffer[Max_Receive_Size];
uint32 CddUart_ReceiveCnt = 0U; /* The number of bytes received in the last reception operation. */
uint8 Char_Buffer[3]; /* Buffer used to convert the number of received bytes to string. */
/* 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 UintToString(uint32 Value, uint8 *Buffer)
{
    Value = Value % 100U;
    /* Initialize the buffer to hold the string representation of the number. */
    Char_Buffer[0] = '\0';
    Char_Buffer[1] = '\0';
    Char_Buffer[2] = '\0';
    if (Value >= 10U)
    {
        Buffer[0] = (uint8)((Value / 10U) + '0');
        Buffer[1] = (uint8)((Value % 10U) + '0');
        Buffer[2] = '\0';
    }
    else
    {
        Buffer[0] = (uint8)(Value + '0');
        Buffer[1] = '\0';
    }
}


void Test_CddUart_CallBack(uint8 Channel, CddUart_General_EventType Event)
{
    CddUart_Event = Event;
    if (UART_EVENT_IDLE == Event)
    {
        uint32 Num;
        /* The Idle interrupt is triggered, get the number of bytes remaind to calculate received number*/
        CddUart_GetStatus(CddUartConf_UartChannel_UartChannel_0, &Num, CDDUART_RECEIVE);
        CddUart_ReceiveCnt = Max_Receive_Size - Num; /* The number of bytes received in the last reception operation. */
    }
}
void delay(uint32 cnt)
{
    for (uint32 i = 0; i < cnt; i++)
    {
        asm("NOP");
    }
}
/* 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);
    CddUart_SyncSend(CddUartConf_UartChannel_UartChannel_0, (uint8 *)CDDUART_MSG2, sizeof(CDDUART_MSG2), 0xFFFF);
    CddUart_AsyncReceive(CddUartConf_UartChannel_UartChannel_0, CddUart_TestedRxBuffer, Max_Receive_Size);
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        /* USER CODE END WHILE */
        /* USER CODE BEGIN 3 */
        if (CddUart_Event == UART_EVENT_END_RX)
        {
            /* The reception is done,send the data back */
            CddUart_SyncSend(CddUartConf_UartChannel_UartChannel_0, (uint8 *)CDDUART_MSG5, sizeof(CDDUART_MSG5), 0xFFFF);
            CddUart_SyncSend(CddUartConf_UartChannel_UartChannel_0, CddUart_TestedRxBuffer, Max_Receive_Size, 0xFFFF);
            /*Receive data again */
            CddUart_AsyncReceive(CddUartConf_UartChannel_UartChannel_0, CddUart_TestedRxBuffer, Max_Receive_Size);
        }
        else if (CddUart_Event == UART_EVENT_IDLE)
        {
            /* The Idle is triggered,send the data and num back */
            UintToString(CddUart_ReceiveCnt, Char_Buffer);
            CddUart_SyncSend(CddUartConf_UartChannel_UartChannel_0, (uint8 *)CDDUART_MSG3, sizeof(CDDUART_MSG3), 0xFFFF);
            delay(5000);
            CddUart_SyncSend(CddUartConf_UartChannel_UartChannel_0, (uint8 *)Char_Buffer, sizeof(Char_Buffer), 0xFFFF);
            delay(5000);
            CddUart_SyncSend(CddUartConf_UartChannel_UartChannel_0, (uint8 *)CDDUART_MSG4, sizeof(CDDUART_MSG4), 0xFFFF);
            delay(5000);
            CddUart_SyncSend(CddUartConf_UartChannel_UartChannel_0, CddUart_TestedRxBuffer, CddUart_ReceiveCnt, 0xFFFF);
            delay(5000);
            /*Receive data again */
            CddUart_AsyncReceive(CddUartConf_UartChannel_UartChannel_0, CddUart_TestedRxBuffer, Max_Receive_Size);
        }
    }
    /* USER CODE END 3 */
}

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

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