/* 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) 2024. 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: V3.1.0  AC7840x  Demo
 ******************************************************************************/

#include <string.h>
#include "ac7840x.h"
#include "gpio_drv.h"
#include "gpio_hw.h"
#include "eio_uart_drv.h"
#include "eio_UART_polling_sample.h"



#define EIO_D0_CHANNEL     PORTD,0UL          /* EIO 通道0*/
#define EIO_D1_CHANNEL     PORTD,1UL          /* EIO 通道1*/

#define EIO_D0_FUNC        PORT_MUX_ALT6      /*PINMUX 为功能6*/
#define EIO_D1_FUNC        PORT_MUX_ALT6      /*PINMUX 为功能6*/

#define UART_BUFFER_SIZE   16                 /* 数据传输大小*/



uint8_t g_uartRxBuffer[UART_BUFFER_SIZE];     /* 接收数据BUFFER */



/*!
* @brief EIO_SetChannelFunc, EIO 功能GPIO初始化
* @param[in] param: node
* @return none
*/
static void EIO_SetChannelFunc(void)
{
    GPIO_DRV_SetMuxModeSel(EIO_D0_CHANNEL, EIO_D0_FUNC);  /* PD0复用为EIO*/
    GPIO_DRV_SetMuxModeSel(EIO_D1_CHANNEL, EIO_D1_FUNC);  /* PD1复用为EIO*/
}

/*!
* @brief EIO_UART_Test
*                用于UART 测试，将接收到的数据回复至发送方
* @param[in] none
* @return none
*/
void EIO_UART_Test(void)
{
    eio_uart_state_t          uartStateTX= {0};                /* 发送索引*/
    eio_uart_state_t          uartStateRX= {0};                /* 接收索引*/
    eio_device_state_t        uartEIODeviceState= {0};         /* 模块索引*/

    uint8_t index = 0;

    eio_uart_user_config_t eio_uart_RX_Config0 = {        /* 接收配置*/
        .driverType = EIO_DRIVER_TYPE_POLLING,            /* 采用polling模式*/
        .direction = EIO_UART_DIRECTION_RX,               /* 作为接收方*/
        .baudRate = 115200U,                              /* 波特率为115200*/
        .bitCount = 8U,                                   /* 长度为8*/
        .dataPin = 1U,                                    /* 使用EIO通道1， 即PD1*/
        .callback = NULL,                                 /* 回调 */
        .callbackParam = NULL,                            /* 回调参数*/
        .dmaChannel = 1U,                                 /* DMA通道*/
    };

    eio_uart_user_config_t eio_uart_TX_Config0 = {        /* 发送配置*/
        .driverType = EIO_DRIVER_TYPE_INTERRUPTS,         /* 采用中断模式*/
        .direction = EIO_UART_DIRECTION_TX,               /* 作为发送方*/
        .baudRate = 115200U,                              /* 波特率为115200*/
        .bitCount = 8U,                                   /* 长度为8*/
        .dataPin = 0U,                                    /* 使用EIO通道0， 即PD0*/
        .callback = NULL,                                 /* 回调*/
        .callbackParam = NULL,                            /* 回调参数*/
        .dmaChannel = 0U,                                 /* DMA通道*/
    };

    EIO_SetChannelFunc();                                 /* GPIO配置*/
    (void)EIO_DRV_DeinitDevice(0);                        /* EIO反初始化，会清除EIO所有配置*/
    (void)EIO_DRV_InitDevice(0, &uartEIODeviceState);     /* EIO模块初始化*/
    (void)EIO_UART_DRV_Init(0, &eio_uart_RX_Config0, &uartStateRX); /*接收初始化*/
    (void)EIO_UART_DRV_Init(0, &eio_uart_TX_Config0, &uartStateTX); /*发送初始化*/

    while (1)
    {

        EIO_UART_DRV_ReceiveData(&uartStateRX, g_uartRxBuffer, UART_BUFFER_SIZE);   /* 接收数据*/
        while (EIO_UART_DRV_GetStatus(&uartStateRX, NULL) != STATUS_SUCCESS);       /* 查询，直至接收到UART_BUFFER_SIZE的数据，才会退出循环*/
        printf("Rece: ");
        for (index = 0; index < UART_BUFFER_SIZE; index++)                          /* debug串口打印接收到的数据*/
        {
            printf("%d\r\t", g_uartRxBuffer[index]);
        }
        printf("\r\n");
        EIO_UART_DRV_SendData(&uartStateTX, g_uartRxBuffer, UART_BUFFER_SIZE);      /* 将接收到的数据发送*/
    }

}



