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

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

#include "uart_rs485.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                /* Data transmission length */

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

/* ==========================================  Variables  =========================================== */
uint8_t g_rxBuf[UART_DATA_LEN];          /* UART receive array */
uint8_t g_txBuf[UART_DATA_LEN];          /* UART send array */

uint8_t g_rxLen  = 0;                    /* Serial port receive length */
uint8_t g_txLen  = 0;                    /* Serial port transmit length */
uint8_t g_txCnt  = 0;                    /* Serial port transmission count */

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

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

/*!
 * @brief UART Callback function
 *
 * @param[in] void *device: UART_Type pointer
 * @param[in] uint32_t wpara: UART lsr0 register
 * @param[in] uint32_t lpara: UART lsr1 register
 * *
 * @return none
 */
void UART_Callback(void *device, uint32_t wpara, uint32_t lpara)
{
    UART_Type *Uart_Device = (UART_Type *)device;

    if (wpara & UART_LSR0_DR_Msk)   /* Check if uart data ready */
    {
        g_rxBuf[g_rxLen++] = UART_ReceiveData(Uart_Device);

        if (g_rxLen > UART_DATA_LEN)
        {
            g_rxLen = 0;
        }
    }

    if (Uart_Device->IER & UART_IER_ETXE_Msk)   /* Check if Transmit Data Register is Empty Interrupt Enable */
    {
        UART_SendData(Uart_Device, g_rxBuf[g_txCnt++]);

        if (g_txCnt >= g_txLen)
        {
            g_txCnt = 0;
            UART_SetTXEInterrupt(Uart_Device, DISABLE);
        }
    }

    if (lpara & UART_LSR1_IDLE_Msk)   /* Check if uart idle interrupt Occurred */
    {
        g_txLen = g_rxLen;
        g_rxLen = 0;

        UART_SetTXEInterrupt(Uart_Device, ENABLE);
    }
}

/*!
 * @brief UART Init function
 *
 * @param[in] none
 *
 * @return none
 */
void Uart_RS485_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 */
    GPIO_SetFunc(GPIOD, GPIO_PIN5, GPIO_FUN4);    /* uart RTS */

    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 = DISABLE;              /* Disable uart fifo */
    uart_config.dmaEn      = UART_DMA_TXRX_NONE;   /* Disable uart 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   = UART_Callback;        /* uart callback function */

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

    UART_SetRS485CR(UART1, UART_RS485CR_RS485EN_Msk);    /* Enable rs485 mode */

    UART_SetRXNEInterrupt(UART1, ENABLE);                /* enable uart receive interrupt */

    /* 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);
}

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