/*
 * hal_adapter.c
 *
 *  Created on: 2022Äê1ÔÂ18ÈÕ
 *      Author: QiXiang_CHENJIE
 */
#include "hal_adapter.h"
uint8_t __attribute__((section(".non_cacheable_data"))) RX_Buffer[UART_CH_MAX_CONFIG][BUFFER_SIZE];
Std_ReturnType UART_Query_Data(uint8 transChannel, uint8 recvChannel, const uint8 *txBuffer, uint32 sendLength, uint8 *rxBuffer, uint32 T_timeout)
{
    volatile Std_ReturnType R_Uart_Status;
    volatile Std_ReturnType T_Uart_Status;
    volatile Uart_StatusType Uart_ReceiveStatus = UART_STATUS_TIMEOUT;
    volatile Uart_StatusType Uart_TransmitStatus = UART_STATUS_TIMEOUT;
    uint32 T_bytesRemaining;
    uint32 timeout = T_timeout;
    uint32 retVal = E_NOT_OK;
    //    uint8 Rx_Buffer[MSG_LEN];

    /* Uart_AsyncReceive transmit data */
    //    IP_LPUART0->CTRL |= LPUART_CTRL_ILIE(0);
    if (txBuffer == NULL || rxBuffer == NULL)
    {
        return retVal;
    }

    Uart_SetBuffer(recvChannel, &RX_Buffer[recvChannel][0], DMA_SIZE, UART_RECEIVE);
    R_Uart_Status = Uart_AsyncReceive(recvChannel, rxBuffer, DMA_SIZE);
    if (E_OK != R_Uart_Status)
    {
        Uart_Abort(recvChannel, UART_RECEIVE);
        return E_NOT_OK;
    }

    /* Uart_AsyncSend transmit data */
    Uart_SetBuffer(transChannel, txBuffer, sendLength, UART_SEND);
    T_Uart_Status = Uart_AsyncSend(transChannel, txBuffer, sendLength);
    if (E_OK != T_Uart_Status)
    {
        Uart_Abort(transChannel, UART_SEND);
        return E_NOT_OK;
    }

    /* Check for no on-going transmission */
    do
    {
        Uart_TransmitStatus = Uart_GetStatus(transChannel, &T_bytesRemaining, UART_SEND);
        Uart_ReceiveStatus = Uart_GetStatus(recvChannel, &T_bytesRemaining, UART_RECEIVE);
    } while (((UART_STATUS_NO_ERROR != Uart_TransmitStatus || UART_STATUS_NO_ERROR != Uart_ReceiveStatus) && 0 < --timeout));

    if ((UART_STATUS_NO_ERROR != Uart_TransmitStatus))
    {
        Uart_Abort(transChannel, UART_SEND);
        retVal = E_NOT_OK;
    }
    else
    {
        retVal = E_OK;
    }

    if ((UART_STATUS_NO_ERROR != Uart_ReceiveStatus))
    {
        Uart_Abort(recvChannel, UART_RECEIVE);
        //    	IP_LPUART0->CTRL |= LPUART_CTRL_ILIE(1);
        retVal = E_NOT_OK;
    }
    else
    {
        retVal = E_OK;
    }
    return retVal;
}

Std_ReturnType UART_Send_Data(uint8 transChannel, const uint8 *txBuffer, uint32 sendLength, uint32 T_timeout)
{

    volatile Std_ReturnType T_Uart_Status;
    volatile Uart_StatusType Uart_TransmitStatus = UART_STATUS_TIMEOUT;
    uint32 T_bytesRemaining;
    uint32 timeout = T_timeout;
    uint32 retVal = E_NOT_OK;
    //    uint8 Rx_Buffer[MSG_LEN];

    if (txBuffer == NULL)
    {
        return retVal;
    }

    /* Uart_AsyncSend transmit data */
    Uart_SetBuffer(transChannel, txBuffer, sendLength, UART_SEND);
    T_Uart_Status = Uart_AsyncSend(transChannel, txBuffer, sendLength);
    if (E_OK != T_Uart_Status)
    {
        Uart_Abort(transChannel, UART_SEND);
        return E_NOT_OK;
    }

    /* Check for no on-going transmission */
    do
    {
        Uart_TransmitStatus = Uart_GetStatus(transChannel, &T_bytesRemaining, UART_SEND);
    } while ((UART_STATUS_NO_ERROR != Uart_TransmitStatus && 0 < --timeout));

    if ((UART_STATUS_NO_ERROR != Uart_TransmitStatus))
    {
        Uart_Abort(transChannel, UART_SEND);
        retVal = E_NOT_OK;
    }
    else
    {
        retVal = E_OK;
    }
    return retVal;
}

uint32 UART_Receive_Data(uint8 recvChannel, uint8 *rxBuffer, uint32 T_timeout)
{
    volatile Std_ReturnType R_Uart_Status;
    volatile Uart_StatusType Uart_ReceiveStatus = UART_STATUS_TIMEOUT;
    uint32 T_bytesRemaining;
    uint32 timeout = T_timeout;
    uint32 retVal = E_NOT_OK;
    //    uint8 Rx_Buffer[MSG_LEN];

    if (rxBuffer == NULL)
    {
        return retVal;
    }
    /* Uart_AsyncReceive transmit data */
    Uart_SetBuffer(recvChannel, &RX_Buffer[recvChannel][0], DMA_SIZE, UART_RECEIVE);
    R_Uart_Status = Uart_AsyncReceive(recvChannel, rxBuffer, DMA_SIZE);
    if (E_OK != R_Uart_Status)
    {
        Uart_Abort(recvChannel, UART_RECEIVE);
        return E_NOT_OK;
    }

    /* Check for no on-going transmission */
    do
    {
        Uart_ReceiveStatus = Uart_GetStatus(recvChannel, &T_bytesRemaining, UART_RECEIVE);
    } while ((UART_STATUS_NO_ERROR != Uart_ReceiveStatus && 0 < --timeout));

    if ((UART_STATUS_NO_ERROR != Uart_ReceiveStatus))
    {
        Uart_Abort(recvChannel, UART_RECEIVE);
        retVal = E_NOT_OK;
    }
    else
    {
        retVal = E_OK;
    }
    return retVal;
}
void UART_Callback(uint32 hwInstance, Lpuart_Uart_Ip_EventType event, void *userData)
{

    (void)userData;
    uint32_t temp;
    static uint32_t bufferIdx;
    /* Check the event type */
    if (event == LPUART_UART_IP_EVENT_RX_FULL)
    {
        /* The reception stops when receiving idle is detected or the buffer is full */
        if (bufferIdx <= (BUFFER_SIZE - DMA_SIZE))
        {
            /* Update the buffer index and the rx buffer */
            bufferIdx += DMA_SIZE;
            Uart_SetBuffer(hwInstance, &RX_Buffer[hwInstance][bufferIdx], DMA_SIZE, UART_RECEIVE);
            // Lpuart_Uart_Ip_SetRxBuffer(hwInstance, &RX_Buffer[bufferIdx], DMA_SIZE);
        }
    }
    if (event == LPUART_UART_IP_EVENT_ERROR)
    {
        /*Get the transfered data size. DMA Channel 1 is used for LPUART DMA receiving, please modify accordingly.*/
        temp = DMA_SIZE - (uint32_t)IP_DMA->TCD->CITER.ELINKNO;
        /*Add the remaining data size to the sum of the received size*/
        bufferIdx += temp;
        /*Abort the receiving after detecting IDLE receiving*/
        Lpuart_Uart_Ip_AbortReceivingData(hwInstance);
    }
}
