/* 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 eio_uart_polling_sample.c
*
* @brief This file contains the user EIO UART function..
*
*/

/* ===========================================  Includes  =========================================== */
#include <string.h>
#include "ac7803x.h"
#include "ac780x_eio_uart_drv.h"
#include "ac780x_gpio.h"
#include "eio_uart_polling_sample.h"

/* ============================================  Define  ============================================ */

#define UART_BUFFER_SIZE   16                 /*Data transmission length*/

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

/* ==========================================  Variables  =========================================== */
uint8_t g_uartRxBuffer[UART_BUFFER_SIZE];     /*receive data BUFFER */

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

/* ======================================  Functions define  ======================================== */
/*!
* @brief EIO_SetChannelFunc, EIO GPIO Init
*
* @param[in] none
* @return    none
*/
static void EIO_SetChannelFunc(void)
{
    /* GPIO config for EIO function */
    GPIO_SetFunc(GPIOB, GPIO_PIN12, GPIO_FUN5);  /*PB12:EIO_D0 */
    GPIO_SetFunc(GPIOB, GPIO_PIN11, GPIO_FUN5);  /*PB11:EIO_D1 */

    GPIO_SetPullup(GPIOB, GPIO_PIN12, ENABLE);   /*Pin configuration pull-up*/
    GPIO_SetPullup(GPIOB, GPIO_PIN11, ENABLE);   /*Pin configuration pull-up*/
}

/*!
* @brief EIO_UART_Test
*        EIO UART receive data of specified length,then send the received data
* @param[in] none
* @return none
*/
void EIO_UART_Test(void)
{
    eio_uart_state_t          uartStateTX;
    eio_uart_state_t          uartStateRX;
    eio_device_state_t        uartEIODeviceState;

    uartStateRX.eioCommon.instance = 0;
    uartStateTX.eioCommon.instance = 0;

    uint8_t index = 0;

    eio_uart_user_config_t eio_uart_RX_Config0 =          /*Receive Configuration*/
    {
        .driverType = EIO_DRIVER_TYPE_INTERRUPTS,         /*use interrupt type*/
        .direction = EIO_UART_DIRECTION_RX,               /*as received*/
        .baudRate = 115200U,                              /*baud rate is 115200*/
        .bitCount = 8U,                                   /*8 bit*/
        .dataPin = 1U,                                    /*eio pin to use as Rx, PB11*/
        .callback = NULL,                                 /*no callback*/
        .callbackParam = NULL,                            /*param[in]eter for the callback function*/
        .dmaChannel = DMA0_CHANNEL1,                      /*DMA channel number. Only used in DMA mode*/
    };

    eio_uart_user_config_t eio_uart_TX_Config0 =          /*Send Configuration*/
    {
        .driverType = EIO_DRIVER_TYPE_POLLING,            /*use polling type*/
        .direction = EIO_UART_DIRECTION_TX,               /*Driver direction: TX*/
        .baudRate = 115200U,                              /*baud rate is 115200*/
        .bitCount = 8U,                                   /*8 bit*/
        .dataPin = 0U,                                    /*eio pin to use as Tx, PB12*/
        .callback = NULL,                                 /*no callback*/
        .callbackParam = NULL,                            /*param[in]eter for the callback function*/
        .dmaChannel = DMA0_CHANNEL0,                      /*DMA channel number. Only used in DMA mode*/
    };

    EIO_SetChannelFunc();                                 /* GPIO configuration*/
    (void)EIO_DRV_DeinitDevice(0);                        /* EIO deinitialization, clearing all EIO configurations*/
    (void)EIO_DRV_InitDevice(0, &uartEIODeviceState);     /* EIO initialization*/
    (void)EIO_UART_DRV_Init(0, &eio_uart_RX_Config0, &uartStateRX); /*EIO UART receive initialization*/
    (void)EIO_UART_DRV_Init(0, &eio_uart_TX_Config0, &uartStateTX); /*EIO UART send initialization*/

    while (1)
    {

        EIO_UART_DRV_ReceiveData(&uartStateRX, g_uartRxBuffer, UART_BUFFER_SIZE);   /*Receive Data*/
        while (EIO_UART_DRV_GetStatus(&uartStateRX, NULL) != STATUS_SUCCESS);       /*wait until receive data of specified length*/
        printf("Rece: ");
        for (index = 0; index < UART_BUFFER_SIZE; index++)                          /*Debug serial port printing received data*/
        {
            printf("0x%x\r\t", g_uartRxBuffer[index]);
        }
        printf("\r\n");
        EIO_UART_DRV_SendDataBlocking(&uartStateTX, g_uartRxBuffer, UART_BUFFER_SIZE, 3000);      /*Send received data*/
    }
}

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




