/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2023. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */
/******************************************************************************
 * @version: V1.0.0 demo
 ******************************************************************************/

/*!
* @file uart_dma.c
*
* @brief This file contains the user pwm function.
*/

/* ===========================================  Includes  =========================================== */

#include "uart_dma.h"
#include <string.h>
#include "ac780x_uart_reg.h"
#include "ac780x_uart.h"
#include "ac780x_gpio.h"
#include "ac780x_dma.h"

/* ============================================  Define  ============================================ */
#define UART_DATA_LEN 128

/* ===========================================  Typedef  ============================================ */

/* ==========================================  Variables  =========================================== */
/* DMA memory is set to 32-bit mode, so the buffer for DMA transfer must be aligned to 4-byte boundaries */
uint8_t g_rxBuf[UART_DATA_LEN] __attribute__((aligned(4)));
uint8_t g_txBuf[UART_DATA_LEN] __attribute__((aligned(4)));

/* ====================================  Functions declaration  ===================================== */

/* ======================================  Functions define  ======================================== */

/*!
* @brief   Uart Tx DMA Event Callback
*
* @param[in] device: DMA_Type pointer
* @param[in] wpara: DMA Channel Status
* @param[in] lpara: reserve for user's code
*
* @return    none
*/
void UartTxDMAEventCallback(void *device, uint32_t wpara, uint32_t lpara)
{
    if (wpara & DMA_CHANNEL_STATUS_FINISH_Msk)    /* DMA finish */
    {

    }
    else if (wpara & DMA_CHANNEL_STATUS_HALF_FINISH_Msk)  /* DMA half finish */
    {

    }
    else if (wpara & DMA_CHANNEL_STATUS_TRANS_ERROR_Msk)  /* DMA error */
    {

    }
    else
    {
        /* never be here */
    }
}

/*!
* @brief   Uart Rx DMA Event Callback
*
* @param[in] device: DMA_Type pointer
* @param[in] wpara: DMA Channel Status
* @param[in] lpara: reserve for user's code
*
* @return    none
*/
static void UartRxDMAEventCallback(void *device, uint32_t wpara, uint32_t lpara)
{
    /* Check if DMA Channel Receive Complete */
    if (wpara & DMA_CHANNEL_STATUS_FINISH_Msk)    /* DMA finish */
    {

    }
    else if (wpara & DMA_CHANNEL_STATUS_HALF_FINISH_Msk)  /* DMA half finish */
    {

    }
    else if (wpara & DMA_CHANNEL_STATUS_TRANS_ERROR_Msk)  /* DMA error */
    {

    }
    else
    {
        /* never be here */
    }
}

/*!
* @brief   Uart Rx Idle CallBack
*
* @param[in] device: UART_Type pointer
* @param[in] wpara:  UART lsr0 register
* @param[in] lpara:  UART lsr1 register
*
* @return    none
*/
static void UartRxIdleCallBack(void *device, uint32_t wpara, uint32_t lpara)
{
    if (lpara & UART_LSR1_IDLE_Msk)    /* Check if IDLE interrupt happened */
    {
        uint16_t recLen;
        uint16_t index;

        recLen = DMA0_CHANNEL1->DATA_TRANS_NUM + DMA0_CHANNEL1->FIFO_LEFT_NUM;    /* Received data less than 4 bytes will be buffered in the DMA FIFO */

        DMA_ChannelFlush(DMA0_CHANNEL1);    /* Flush the DMA FIFO data, as memory is set to 32-bit mode, ensuring that the FIFO is also aligned to 4-byte boundaries \
                                               Even if there is only 1 byte in the FIFO, it will still be flushed as 4 bytes, ensuring that the cache redundancy does not result in overflow */
        for (index = 0; index < recLen; index++)
        {
            g_txBuf[index] = g_rxBuf[index];
        }
        UART_TransmitDMA(UART1, DMA0_CHANNEL0, g_txBuf, recLen, UartTxDMAEventCallback);          /* send data with dma */

        UART_ReceiveDMA(UART1, DMA0_CHANNEL1, g_rxBuf, UART_DATA_LEN, UartRxDMAEventCallback);    /* receive data with dma */
    }
}

/*!
 * @brief UART Init function
 *
 * @param[in] none
 *
 * @return none
 */
void Uart_DMA_Init(void)
{
    UART_ConfigType uart_config;

    memset(&uart_config, 0U, sizeof(uart_config));

    GPIO_SetFunc(GPIOC, GPIO_PIN2, GPIO_FUN1);    /* uart tx */
    GPIO_SetFunc(GPIOC, GPIO_PIN3, GPIO_FUN1);    /* uart rx */

    uart_config.baudrate = 115200U;               /* set uart baudrate: 115200 */
    uart_config.dataBits = UART_WORD_LEN_8BIT;    /* select word length: 8bit */
    uart_config.stopBits = UART_STOP_1BIT;        /* select 1bit stop */
    uart_config.parity   = UART_PARI_NO;          /* set UART Parity: no parity  */
    uart_config.fifoByteEn = ENABLE;              /* Enable uart fifo */
    uart_config.dmaEn = UART_DMA_TXRX_EN;         /* set uart tx/rx dma */
    uart_config.sampleCnt = UART_SMP_CNT0;        /* select uart sample counter: SAM_CNT0: based on 16*baud_pulse, baud_rate = APB CLOCK/16/{DLH, DLL} */
    uart_config.callBack = UartRxIdleCallBack;    /* uart callback function */

    UART_Init(UART1, &uart_config);               /* uart init function */

    /* enable uart idle interrupt */
    UART_SetIdleFuncEn(UART1, ENABLE);
    UART_SetIdleInterrupt(UART1, ENABLE);

    /* enable uart1 interrupt */
    NVIC_SetPriority(UART1_IRQn, 3);
    NVIC_ClearPendingIRQ(UART1_IRQn);
    NVIC_EnableIRQ(UART1_IRQn);

    UART_ReceiveDMA(UART1, DMA0_CHANNEL1, g_rxBuf, UART_DATA_LEN, UartRxDMAEventCallback);    /* receive data with dma */
}

/* =============================================  EOF  ============================================== */
